-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathTRPC.ts
51 lines (43 loc) · 1.4 KB
/
TRPC.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { initTRPC } from '@trpc/server'
import type { Registry } from '../Registry.js'
import type * as trpcExpress from '@trpc/server/adapters/express'
import { EventEmitter, on } from 'events'
// created for each request
export const createTrpcContext = ({} /* req, res */ : trpcExpress.CreateExpressContextOptions) => ({}) // no context
type Context = Awaited<ReturnType<typeof createTrpcContext>>
/**
* Initialization of tRPC backend
* Should be done only once per backend!
*/
const t = initTRPC.context<Context>().create()
/**
* Export reusable router and procedure helpers
* that can be used throughout the router
*/
export const router = t.router
export const publicProcedure = t.procedure
export const protectedProcedure = t.procedure
/**
* Create the root TRPC router
* @param registry
* @returns
*/
export function createTrpcRouter(registry: Registry) {
return router({
// ...
userList: publicProcedure.query(async () => {
return [1, 2, 3]
}),
appInfo: registry.ui.update.createTrpcRouter(),
})
}
// Export type router type signature,
// NOT the router itself.
export type AppRouter = ReturnType<typeof createTrpcRouter>
export function toIterable<T extends Record<string, any[]>, TKey extends string & keyof T>(
ee: EventEmitter<T>,
key: TKey,
signal: AbortSignal | undefined
): NodeJS.AsyncIterator<T[TKey]> {
return on(ee as any, key, { signal }) as NodeJS.AsyncIterator<T[TKey]>
}