Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export async function safe<TOutput, TError = ThrowableError>(promise: ClientProm
export function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T> {
return {
...options,
context: options?.context ?? {} as T, // Context only optional if all fields are optional
context: options.context ?? {} as T, // Context only optional if all fields are optional
}
}
2 changes: 1 addition & 1 deletion packages/server/src/adapters/standard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export type FriendlyStandardHandleOptions<T extends Context> =
export function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T> {
return {
...options,
context: options?.context ?? {} as T, // Context only optional if all fields are optional
context: options.context ?? {} as T, // Context only optional if all fields are optional
}
}
1 change: 1 addition & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './implementer'
export * from './implementer-procedure'
export * from './implementer-variants'
export * from './lazy'
export * from './link-utils'
export * from './middleware'
export * from './middleware-decorated'
export * from './middleware-utils'
Expand Down
10 changes: 10 additions & 0 deletions packages/server/src/link-utils.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RPCLink } from '@orpc/client/fetch'
import { router } from '../tests/shared'
import { inferRPCMethodFromRouter } from './link-utils'

it('inferRPCMethodFromContractRouter', () => {
const link = new RPCLink({
url: 'http://localhost:3000/rpc',
method: inferRPCMethodFromRouter(router),
})
})
24 changes: 24 additions & 0 deletions packages/server/src/link-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { os } from './builder'
import { inferRPCMethodFromRouter } from './link-utils'

it('inferRPCMethodFromRouter', async () => {
const method = inferRPCMethodFromRouter({
head: os.route({ method: 'HEAD' }).handler(() => {}),
get: os.route({ method: 'GET' }).handler(() => { }),
post: os.route({}).handler(() => { }),
nested: os.lazy(() => Promise.resolve({
default: {
get: os.lazy(() => Promise.resolve({ default: os.route({ method: 'GET' }).handler(() => { }) })),
delete: os.route({ method: 'DELETE' }).handler(() => { }),
},
})),
})

expect(await method({}, ['head'])).toBe('GET')
expect(await method({}, ['get'])).toBe('GET')
expect(await method({}, ['post'])).toBe('POST')
expect(await method({}, ['nested', 'get'])).toBe('GET')
expect(await method({}, ['nested', 'delete'])).toBe('DELETE')

await expect(method({}, ['nested', 'not-exist'])).rejects.toThrow(/No valid procedure found at path/)
})
26 changes: 26 additions & 0 deletions packages/server/src/link-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { HTTPMethod } from '@orpc/client'
import type { AnyRouter } from './router'
import { fallbackContractConfig } from '@orpc/contract'
import { unlazy } from './lazy'
import { isProcedure } from './procedure'
import { getRouter } from './router-utils'

/**
* Help RPCLink automatically send requests using the specified HTTP method in the router.
*/
export function inferRPCMethodFromRouter(router: AnyRouter): (options: unknown, path: readonly string[]) => Promise<Exclude<HTTPMethod, 'HEAD'>> {
return async (_, path) => {
const { default: procedure } = await unlazy(getRouter(router, path))

if (!isProcedure(procedure)) {
throw new Error(
`[inferRPCMethodFromRouter] No valid procedure found at path "${path.join('.')}". `
+ `This may happen when the router is not properly configured.`,
)
}

const method = fallbackContractConfig('defaultMethod', procedure['~orpc'].route.method)

return method === 'HEAD' ? 'GET' : method
}
}