Skip to content

Commit 0c48eda

Browse files
authored
fix(server): check context in .lazy of router implementer with middlewares (#2456)
## Problem `RouterImplementerWithMiddlewares.lazy` constrained the loaded router with `ContractedRouter<TContract, any>`. `.router` uses `ContractedRouter<TContract, MergedContext<TInitialContext, TInjectedContext>>`. Because of the `any`, a lazy router whose procedures need a context that doesn't match the implementer's context passed type-checking and then crashed at runtime: ```ts const impl = implement(c).$context<{ db: string }>().use(({ next }) => next()) const wrongProc = implement(c).$context<{ db: { query(): string } }>().p.handler(({ context }) => context.db.query()) impl.router({ p: wrongProc }) // rejected const lz = impl.lazy(async () => ({ default: { p: wrongProc } })) // accepted before this fix createRouterClient({ lz }, { context: { db: 'str' } }).lz.p() // type-checks, crashes at runtime ``` ## Fix `.lazy` now uses the same constraint as `.router`. The no-middleware `SharedRouterImplementer.lazy` stays as it is. Its sibling `.router` also uses `any`, and both return `T` unchanged, so the context is still checked later, where the router is used. Changing only `.lazy` there would make the two methods disagree. ## Tests - Added a `context is conflict` case to `implementer-router.test-d.ts`. It has two `@ts-expect-error` lines: one where the context conflicts with `TInitialContext` and one where it conflicts with `TInjectedContext`. Both directives are reported as unused without the fix. - `pnpm check`, `pnpm test` and a root `tsc` all pass.
1 parent 1a94a77 commit 0c48eda

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,67 @@ describe('RouterImplementerWithMiddlewares', () => {
980980
})
981981
})
982982

983+
it('context is conflict', () => {
984+
const router = {
985+
ping: {} as Procedure<
986+
{ auth: boolean; extra: boolean },
987+
object,
988+
typeof schema1,
989+
typeof schema2,
990+
typeof errorMap
991+
>,
992+
nested: {
993+
pong: {} as Procedure<{ auth: boolean }, object, typeof schema2, typeof schema1, object>,
994+
},
995+
use: {} as Procedure<{ auth: boolean }, object, typeof schema3, typeof schema3, object>,
996+
middleware: {} as Procedure<
997+
{ auth: boolean },
998+
object,
999+
typeof schema1,
1000+
typeof schema1,
1001+
object
1002+
>,
1003+
router: {
1004+
router: {} as Procedure<
1005+
{ auth: boolean },
1006+
object,
1007+
typeof schema2,
1008+
typeof schema2,
1009+
object
1010+
>,
1011+
},
1012+
lazy: {
1013+
lazy: {} as Procedure<{ auth: boolean }, object, typeof schema2, typeof schema2, object>,
1014+
},
1015+
}
1016+
1017+
implementer.router(router)
1018+
1019+
implementer.router({
1020+
...router,
1021+
// @ts-expect-error - context is conflict
1022+
ping: {} as Procedure<
1023+
{ auth: 'invalid' },
1024+
object,
1025+
typeof schema1,
1026+
typeof schema2,
1027+
typeof errorMap
1028+
>,
1029+
})
1030+
1031+
implementer.router({
1032+
...router,
1033+
// @ts-expect-error - context is conflict
1034+
ping: {} as Procedure<
1035+
{ extra: 'invalid' },
1036+
object,
1037+
typeof schema1,
1038+
typeof schema2,
1039+
typeof errorMap
1040+
>,
1041+
})
1042+
})
1043+
9831044
it('deep access', () => {
9841045
const router = {
9851046
pong: {} as Procedure<
@@ -1148,6 +1209,75 @@ describe('RouterImplementerWithMiddlewares', () => {
11481209
)
11491210
})
11501211

1212+
it('context is conflict', () => {
1213+
const router = {
1214+
ping: {} as Procedure<
1215+
{ auth: boolean; extra: boolean },
1216+
object,
1217+
typeof schema1,
1218+
typeof schema2,
1219+
typeof errorMap
1220+
>,
1221+
nested: {
1222+
pong: {} as Procedure<{ auth: boolean }, object, typeof schema2, typeof schema1, object>,
1223+
},
1224+
use: {} as Procedure<{ auth: boolean }, object, typeof schema3, typeof schema3, object>,
1225+
middleware: {} as Procedure<
1226+
{ auth: boolean },
1227+
object,
1228+
typeof schema1,
1229+
typeof schema1,
1230+
object
1231+
>,
1232+
router: {
1233+
router: {} as Procedure<
1234+
{ auth: boolean },
1235+
object,
1236+
typeof schema2,
1237+
typeof schema2,
1238+
object
1239+
>,
1240+
},
1241+
lazy: {
1242+
lazy: {} as Procedure<{ auth: boolean }, object, typeof schema2, typeof schema2, object>,
1243+
},
1244+
}
1245+
1246+
implementer.lazy(() => Promise.resolve({ default: router }))
1247+
1248+
implementer.lazy(() =>
1249+
// @ts-expect-error - context is conflict
1250+
Promise.resolve({
1251+
default: {
1252+
...router,
1253+
ping: {} as Procedure<
1254+
{ auth: 'invalid' },
1255+
object,
1256+
typeof schema1,
1257+
typeof schema2,
1258+
typeof errorMap
1259+
>,
1260+
},
1261+
}),
1262+
)
1263+
1264+
implementer.lazy(() =>
1265+
// @ts-expect-error - context is conflict
1266+
Promise.resolve({
1267+
default: {
1268+
...router,
1269+
ping: {} as Procedure<
1270+
{ extra: 'invalid' },
1271+
object,
1272+
typeof schema1,
1273+
typeof schema2,
1274+
typeof errorMap
1275+
>,
1276+
},
1277+
}),
1278+
)
1279+
})
1280+
11511281
it('deep access', () => {
11521282
const router = {
11531283
pong: {} as Procedure<

‎packages/server/src/implementer-router.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export interface SharedRouterImplementerWithMiddlewares<
179179
router: T,
180180
): AugmentedRouterWithMiddlewares<T, TInitialContext, TInjectedContext, object>
181181

182-
lazy<T extends ContractedRouter<TContract, any>>(
182+
lazy<T extends ContractedRouter<TContract, MergedContext<TInitialContext, TInjectedContext>>>(
183183
loader: () => Promise<{ default: T }>,
184184
): Lazy<AugmentedRouterWithMiddlewares<T, TInitialContext, TInjectedContext, object>>
185185
}

0 commit comments

Comments
 (0)