Last active
October 24, 2022 01:49
-
-
Save yusukebe/37574fbc72c54843966732dc6755fba9 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
| import { Server } from './server' | |
| type Post = { | |
| id: number | |
| title: string | |
| body: string | |
| author: Author | |
| } | |
| type Author = { | |
| name: string | |
| age: number | |
| } | |
| const post: Post = { | |
| id: 123, | |
| title: 'Hello!', | |
| body: 'Have a nice day!', | |
| author: { | |
| name: 'Superman', | |
| age: 20, | |
| }, | |
| } | |
| const server = new Server({}) | |
| const app = server.add('/post', post).build() | |
| export type AppType = typeof app |
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
| import { AppType } from './app' | |
| class Client<T> { | |
| constructor() {} | |
| endpoints(): T { | |
| return {} as T | |
| } | |
| } | |
| const client = new Client<AppType>() | |
| const endpoints = client.endpoints() | |
| const post = endpoints['/post'].query() |
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
| export class Server<S extends Record<string, unknown> = Record<string, unknown>> { | |
| constructor(private prev: S) {} | |
| add<T, P extends string, H>(path: P, handler: H) { | |
| const length = Object.keys(this.prev).length | |
| const s = (length ? { ...this.prev, [path]: {} as T } : { [path]: {} as T }) as S & { | |
| [key in P]: { query: () => H } | |
| } | |
| const newServer: Server<typeof s> = new Server(s) | |
| return newServer | |
| } | |
| build() { | |
| return this.prev | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment