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.

Revisions

  1. yusukebe revised this gist Oct 23, 2022. 4 changed files with 56 additions and 35 deletions.
    28 changes: 28 additions & 0 deletions app.ts
    Original 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
    12 changes: 12 additions & 0 deletions client.ts
    Original 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()
    16 changes: 16 additions & 0 deletions server.ts
    Original 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
    }
    }
    35 changes: 0 additions & 35 deletions trpc-like.ts
    Original file line number Diff line number Diff line change
    @@ -1,35 +0,0 @@
    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()
  2. yusukebe created this gist Oct 23, 2022.
    35 changes: 35 additions & 0 deletions trpc-like.ts
    Original 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()