Last active
October 24, 2022 01:49
-
-
Save yusukebe/37574fbc72c54843966732dc6755fba9 to your computer and use it in GitHub Desktop.
Revisions
-
yusukebe revised this gist
Oct 23, 2022 . 4 changed files with 56 additions and 35 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ 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 } } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,35 +0,0 @@ -
yusukebe created this gist
Oct 23, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ 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]: { post: () => H } } const newServer: Server<typeof s> = new Server(s) return newServer } build() { return this.prev } } const server = new Server({}) const app = server .add('/post', { title: 'Hello', body: 'Have a nice day!' }) .add('/author', { name: 'Superman', age: 20 }) .build() type AppType = typeof app class Client<T> { constructor() {} endpoints(): T { return {} as T } } const client = new Client<AppType>() const endpoints = client.endpoints() const author = endpoints['/author'].post()