Created
April 4, 2022 18:39
-
-
Save mattiamanzati/106d04d108c44e0e8749a58403fa9b28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @tsplus type Codec | |
*/ | |
export interface Codec<A> { | |
decode: (value: unknown) => E.Either<string, A>; | |
encode: (value: A) => unknown; | |
} | |
/** | |
* @tsplus implicit | |
*/ | |
export const implicitStrig: Codec<string> = { | |
decode: (_) => E.right("" + _), | |
encode: (_) => _, | |
}; | |
/** | |
* @tsplus derive Codec<_, _> 10 | |
*/ | |
export declare function deriveStruct<A>( | |
...rules: A extends Record<string, any> | |
? [ | |
{ | |
[K in keyof A]: Codec<A[K]> | |
} | |
] | |
: never | |
): Codec<A>; | |
/** | |
* @tsplus derive Codec<_, _> 10 | |
*/ | |
export declare function deriveLiteralString<A extends string>( | |
...[value]: [value: A] | |
): Codec<A> | |
// esempio | |
interface GetUserRequest { | |
_tag: "GetUser" | |
username: string | |
} | |
interface GetUserResponse { | |
name: string | |
surname: string | |
} | |
type Message<Req, Res> = { | |
request: Codec<Req> | |
response: Codec<Res> | |
} | |
const codecUser = Derive<Message<GetUserRequest, GetUserResponse>>("explain"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment