Skip to content
Open
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
47 changes: 47 additions & 0 deletions packages/server/src/router-client.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { AnyNestedClient } from '@orpc/client'
import type { Schema } from '@orpc/contract'
import * as z from 'zod'

import { os } from './builder'
import type { Lazyable } from './lazy'
import type { ProcedureClient } from './procedure-client'
import type { RouterClient } from './router-client'
import { createRouterClient } from './router-client'

const errorMap = {
BASE: {
Expand Down Expand Up @@ -58,6 +61,44 @@ describe('RouterClient', () => {
>()
})

it('unwraps lazy at any depth', () => {
const lazyRouter = {
ping: os.lazy(() => Promise.resolve({ default: router.ping })),
nested: os.lazy(() => Promise.resolve({ default: { lazy: router.lazy } })),
}
const client = {} as RouterClient<typeof lazyRouter>

expectTypeOf(client.ping).toEqualTypeOf<
ProcedureClient<object, typeof schema1, typeof schema2, object>
>()

expectTypeOf(client.nested.lazy.peng).toEqualTypeOf<
ProcedureClient<object, typeof schema1, Schema<string>, object>
>()
})

it('supports a key that is either lazy or not', () => {
type LazyableRouter = {
ping: Lazyable<typeof router.ping>
nested: Lazyable<typeof router.nested>
}
const client = {} as RouterClient<LazyableRouter>

expectTypeOf(client.ping).toEqualTypeOf<
ProcedureClient<object, typeof schema1, typeof schema2, object>
>()

expectTypeOf(client.nested.pong).toEqualTypeOf<
ProcedureClient<object, typeof schema3, typeof schema2, typeof errorMap>
>()
})

it('maps an optional key to never', () => {
type OptionalRouter = { ping?: typeof router.ping }

expectTypeOf<RouterClient<OptionalRouter>>().toEqualTypeOf<{ ping?: never }>()
})

it('support single procedure', () => {
type P = RouterClient<(typeof router)['ping'], { cache?: boolean }>

Expand All @@ -66,3 +107,9 @@ describe('RouterClient', () => {
>()
})
})

describe('createRouterClient', () => {
it('accepts an untyped router', () => {
const _client: AnyNestedClient = createRouterClient({} as any, {})
})
})
19 changes: 14 additions & 5 deletions packages/server/src/router-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { resolveMaybeOptionalOptions, toArray } from '@orpc/shared'

import type { Lazyable } from './lazy'
import { Lazy } from './lazy'
import type { ProcedureDefinition } from './procedure'
import { Procedure } from './procedure'
import type { ProcedureClient, ProcedureClientOptions } from './procedure-client'
import { createProcedureClient } from './procedure-client'
Expand All @@ -19,12 +20,20 @@ import { getRouter } from './router-utils'
*
* @see {@link https://orpc.dev/docs/client/client-side#creating-a-client | Client-Side Clients - Creating a Client}
*/
export type RouterClient<TRouter extends AnyRouter, TClientContext extends ClientContext = object> =
TRouter extends Procedure<any, any, infer $InputSchema, infer $OutputSchema, infer $ErrorMap>
? ProcedureClient<TClientContext, $InputSchema, $OutputSchema, $ErrorMap>
export type RouterClient<
TRouter extends AnyRouter,
TClientContext extends ClientContext = object,
> = RouterClientInner<TRouter, TClientContext>

type RouterClientInner<T, TClientContext extends ClientContext> = T extends {
'~orpc': ProcedureDefinition<any, any, infer $InputSchema, infer $OutputSchema, infer $ErrorMap>
}
? ProcedureClient<TClientContext, $InputSchema, $OutputSchema, $ErrorMap>
: T extends Lazy<infer U extends AnyRouter>
? RouterClientInner<U, TClientContext>
: {
[K in keyof TRouter]: TRouter[K] extends Lazyable<infer U extends AnyRouter>
? RouterClient<U, TClientContext>
[K in keyof T]: T[K] extends Lazyable<AnyRouter>
? RouterClientInner<T[K], TClientContext>
: never
}

Expand Down
Loading