Add base app
This commit is contained in:
16
components/common/Link.tsx
Normal file
16
components/common/Link.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
import { FC } from 'react'
|
||||
import NextLink from 'next/link'
|
||||
|
||||
interface LinkProps {
|
||||
page: string
|
||||
className: string
|
||||
}
|
||||
const Link: FC<LinkProps> = (props) => (
|
||||
<NextLink href={props.page}>
|
||||
<a className={props.className + ' underline hover:text-gray-800'}>
|
||||
{props.children}
|
||||
</a>
|
||||
</NextLink>
|
||||
)
|
||||
|
||||
export default Link
|
1
components/common/result/index.ts
Normal file
1
components/common/result/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * as result from './result'
|
60
components/common/result/result.ts
Normal file
60
components/common/result/result.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { result } from '.'
|
||||
|
||||
type Ok<T> = {
|
||||
type: 'ok'
|
||||
val: T
|
||||
}
|
||||
export const ok = <T>(val: T): Ok<T> => ({ type: 'ok', val })
|
||||
type Err<T> = {
|
||||
type: 'err'
|
||||
err: T
|
||||
}
|
||||
export const err = <T>(err: T): Err<T> => ({ type: 'err', err })
|
||||
|
||||
export type Result<TOk, TErr> = Ok<TOk> | Err<TErr>
|
||||
|
||||
export const isOk = <TOk, TErr>(r: result.Result<TOk, TErr>): boolean => {
|
||||
switch (r.type) {
|
||||
case 'ok':
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export const getValue = <TOk, TErr>(r: result.Result<TOk, TErr>): TOk => {
|
||||
if (r.type === 'ok') {
|
||||
return r.val
|
||||
}
|
||||
|
||||
throw new Error(`couldn't get value as type is error: ${r.err}`)
|
||||
}
|
||||
|
||||
export const tap = <TOk, TErr>(
|
||||
r: result.Result<TOk, TErr>,
|
||||
okFunc: (okRes: TOk) => void,
|
||||
errFunc: (errRes: TErr) => void = (err) => {
|
||||
console.log("default tap catch of error, note tap doesn't break chain")
|
||||
}
|
||||
): result.Result<TOk, TErr> => {
|
||||
switch (r.type) {
|
||||
case 'ok':
|
||||
okFunc(r.val)
|
||||
break
|
||||
case 'err':
|
||||
errFunc(r.err)
|
||||
break
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
export const getError = <TOk, TErr = string>(
|
||||
r: result.Result<TOk, TErr>
|
||||
): TErr => {
|
||||
if (r.type === 'err') {
|
||||
return r.err
|
||||
}
|
||||
|
||||
throw new Error(`couldn't get value as type is ok: ${r.val}`)
|
||||
}
|
Reference in New Issue
Block a user