Skip to content

Commit f59a986

Browse files
dinwwwhclaude
andauthored
fix(server): serve the implementation when a normal router is implemented as a contract (#2378)
`withHiddenRouterContract` now strips every procedure in the contract down to a plain `ProcedureContract` (without `handler` and `orderedMiddlewares`), so handlers always resolve the implementation by path instead of serving the contract's procedures, and middlewares applied through the implementer run over HTTP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VVaw7RynEorPGuUfvJGZXH
1 parent 7f91725 commit f59a986

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎packages/server/src/router-hidden.test.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { oc } from '@orpc/contract'
1+
import { oc, ProcedureContract } from '@orpc/contract'
2+
import { z } from 'zod'
23
import { os } from './builder'
4+
import { Procedure } from './procedure'
35
import { getHiddenRouterContract, withHiddenRouterContract } from './router-hidden'
46

57
const contract = {
@@ -15,5 +17,30 @@ it('withHiddenRouterContract & getHiddenRouterContract', () => {
1517

1618
expect(applied).not.toBe(router)
1719
expect(applied).toEqual(router)
18-
expect(getHiddenRouterContract(applied)).toEqual(contract)
20+
expect(getHiddenRouterContract(applied)).toBe(contract)
21+
expect(getHiddenRouterContract(withHiddenRouterContract(router.ping, contract.ping))).toBe(contract.ping)
22+
})
23+
24+
describe('withHiddenRouterContract with a normal router as the contract', () => {
25+
const real = {
26+
ping: os.$config({ disableInputValidation: true }).input(z.string()).handler(() => 'REAL'),
27+
nested: { pong: os.handler(() => 'REAL') },
28+
contractFirst: { ping: contract.ping },
29+
invalid: undefined as any,
30+
}
31+
32+
it('strips procedures down to plain contracts', () => {
33+
const hidden = getHiddenRouterContract(withHiddenRouterContract({}, real)) as any
34+
35+
for (const [procedure, original] of [[hidden.ping, real.ping], [hidden.nested.pong, real.nested.pong]] as const) {
36+
expect(procedure).toBeInstanceOf(ProcedureContract)
37+
expect(procedure).not.toBeInstanceOf(Procedure)
38+
const { handler, orderedMiddlewares, ...contractDef } = original['~orpc']
39+
expect(procedure['~orpc']).toEqual(contractDef)
40+
}
41+
42+
// routers without procedures are kept as is
43+
expect(hidden.contractFirst).toBe(real.contractFirst)
44+
expect(hidden.invalid).toBeUndefined()
45+
})
1946
})

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
11
import type { RouterContract } from '@orpc/contract'
22
import type { Lazyable } from './lazy'
33
import type { AnyRouter } from './router'
4+
import { ProcedureContract } from '@orpc/contract'
5+
import { isTypescriptObject, omit } from '@orpc/shared'
6+
import { Procedure } from './procedure'
47

58
const HIDDEN_ROUTER_CONTRACT_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_HIDDEN_ROUTER_CONTRACT')
69

10+
/**
11+
* A normal router can be used as a contract, but its procedures carry a handler and middlewares.
12+
* Handlers serve the hidden contract and treat any procedure as the implementation, so the
13+
* contract's procedures would run instead of the actual implementation.
14+
*
15+
* Rebuilds the contract with every procedure turned into a plain {@link ProcedureContract}
16+
* (without `handler` and `orderedMiddlewares`), copying only the routers that contain one,
17+
* so contract-first contracts are returned as is.
18+
*/
19+
function toPlainRouterContract<T>(contract: T): T {
20+
if (!isTypescriptObject(contract)) {
21+
return contract
22+
}
23+
24+
if (contract instanceof ProcedureContract) {
25+
if (!(contract instanceof Procedure)) {
26+
return contract
27+
}
28+
29+
// handler & orderedMiddlewares are what make a definition a procedure (see Procedure[Symbol.hasInstance])
30+
return new ProcedureContract(omit(contract['~orpc'], ['handler', 'orderedMiddlewares'])) as T
31+
}
32+
33+
let copied: Record<string, unknown> | undefined
34+
35+
for (const key of Object.keys(contract)) {
36+
const child = (contract as Record<string, unknown>)[key]
37+
const plainChild = toPlainRouterContract(child)
38+
39+
if (plainChild !== child) {
40+
copied ??= { ...contract }
41+
copied[key] = plainChild
42+
}
43+
}
44+
45+
return (copied ?? contract) as T
46+
}
47+
748
export function withHiddenRouterContract<T extends Lazyable<AnyRouter>>(router: T, contract: RouterContract): T {
49+
const plainContract = toPlainRouterContract(contract)
50+
851
return new Proxy(router, {
952
get(target, key) {
1053
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
11-
return contract
54+
return plainContract
1255
}
1356

1457
return Reflect.get(target, key)

0 commit comments

Comments
 (0)