Skip to content

Commit df024bb

Browse files
authored
feat(server): InferRouterInitialContexts & InferRouterCurrentContexts (#201)
1 parent 954e6e2 commit df024bb

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

apps/content/docs/router.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export default {
8282

8383
## Utilities
8484

85-
### Infer Router Input
85+
> **Note:** Every [procedure](/docs/procedure) is also a router, so you can apply these utilities to procedures as well.
86+
87+
### Infer Router Inputs
8688

8789
```ts twoslash
8890
import type { router } from './shared/planet'
@@ -94,9 +96,9 @@ export type Inputs = InferRouterInputs<typeof router>
9496
type FindPlanetInput = Inputs['planet']['find']
9597
```
9698
97-
This snippet automatically extracts the expected input types for each procedure in the router.
99+
Infers the expected input types for each procedure in the router.
98100
99-
### Infer Router Output
101+
### Infer Router Outputs
100102
101103
```ts twoslash
102104
import type { router } from './shared/planet'
@@ -108,4 +110,32 @@ export type Outputs = InferRouterOutputs<typeof router>
108110
type FindPlanetOutput = Outputs['planet']['find']
109111
```
110112
111-
Similarly, this utility infers the output types, ensuring that your application correctly handles the results from each procedure.
113+
Infers the expected output types for each procedure in the router.
114+
115+
### Infer Router Initial Contexts
116+
117+
```ts twoslash
118+
import type { router } from './shared/planet'
119+
// ---cut---
120+
import type { InferRouterInitialContexts } from '@orpc/server'
121+
122+
export type InitialContexts = InferRouterInitialContexts<typeof router>
123+
124+
type FindPlanetInitialContext = InitialContexts['planet']['find']
125+
```
126+
127+
Infers the [initial context](/docs/context#initial-context) types defined for each procedure.
128+
129+
### Infer Router Current Contexts
130+
131+
```ts twoslash
132+
import type { router } from './shared/planet'
133+
// ---cut---
134+
import type { InferRouterCurrentContexts } from '@orpc/server'
135+
136+
export type CurrentContexts = InferRouterCurrentContexts<typeof router>
137+
138+
type FindPlanetCurrentContext = CurrentContexts['planet']['find']
139+
```
140+
141+
Infers the [current context](/docs/context#combining-initial-and-execution-context) types, which combine the initial context with the execution context and pass it to the handler.

packages/server/src/router.test-d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CurrentContext, InitialContext } from '../tests/shared'
44
import type { Context } from './context'
55
import type { Lazy } from './lazy'
66
import type { Procedure } from './procedure'
7-
import type { AdaptedRouter, InferRouterInitialContext, InferRouterInputs, InferRouterOutputs, Router } from './router'
7+
import type { AdaptedRouter, InferRouterCurrentContexts, InferRouterInitialContext, InferRouterInitialContexts, InferRouterInputs, InferRouterOutputs, Router } from './router'
88
import { ping, pong, router } from '../tests/shared'
99

1010
describe('Router', () => {
@@ -23,6 +23,20 @@ it('InferRouterInitialContext', () => {
2323
expectTypeOf<InferRouterInitialContext<typeof pong>>().toEqualTypeOf<Context>()
2424
})
2525

26+
it('InferRouterInitialContexts', () => {
27+
expectTypeOf<InferRouterInitialContexts<typeof router>['ping']>().toEqualTypeOf<InitialContext>()
28+
expectTypeOf<InferRouterInitialContexts<typeof router>['nested']['ping']>().toEqualTypeOf<InitialContext>()
29+
expectTypeOf<InferRouterInitialContexts<typeof router>['pong']>().toEqualTypeOf<Context>()
30+
expectTypeOf<InferRouterInitialContexts<typeof router>['nested']['pong']>().toEqualTypeOf<Context>()
31+
})
32+
33+
it('InferRouterCurrentContexts', () => {
34+
expectTypeOf<InferRouterCurrentContexts<typeof router>['ping']>().toEqualTypeOf<CurrentContext>()
35+
expectTypeOf<InferRouterCurrentContexts<typeof router>['nested']['ping']>().toEqualTypeOf<CurrentContext>()
36+
expectTypeOf<InferRouterCurrentContexts<typeof router>['pong']>().toEqualTypeOf<Context>()
37+
expectTypeOf<InferRouterCurrentContexts<typeof router>['nested']['pong']>().toEqualTypeOf<Context>()
38+
})
39+
2640
it('InferRouterInputs', () => {
2741
type Inferred = InferRouterInputs<typeof router>
2842

packages/server/src/router.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ export type InferRouterInitialContext<T extends AnyRouter> = T extends Router<in
2828
? UInitialContext
2929
: never
3030

31+
export type InferRouterInitialContexts<T extends AnyRouter> =
32+
T extends Lazy<infer U extends AnyRouter> ? InferRouterInitialContexts<U>
33+
: T extends Procedure<infer UInitialContext, any, any, any, any, any, any>
34+
? UInitialContext
35+
: {
36+
[K in keyof T]: T[K] extends AnyRouter ? InferRouterInitialContexts<T[K]> : never
37+
}
38+
39+
export type InferRouterCurrentContexts<T extends AnyRouter> =
40+
T extends Lazy<infer U extends AnyRouter> ? InferRouterCurrentContexts<U>
41+
: T extends Procedure<any, infer UCurrentContext, any, any, any, any, any>
42+
? UCurrentContext
43+
: {
44+
[K in keyof T]: T[K] extends AnyRouter ? InferRouterCurrentContexts<T[K]> : never
45+
}
46+
3147
export type InferRouterInputs<T extends AnyRouter> =
3248
T extends Lazy<infer U extends AnyRouter> ? InferRouterInputs<U>
3349
: T extends Procedure<any, any, infer UInputSchema, any, any, any, any>

0 commit comments

Comments
 (0)