Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Last active October 24, 2022 01:49
Show Gist options
  • Select an option

  • Save yusukebe/37574fbc72c54843966732dc6755fba9 to your computer and use it in GitHub Desktop.

Select an option

Save yusukebe/37574fbc72c54843966732dc6755fba9 to your computer and use it in GitHub Desktop.
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
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()
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