DynamicLink
DynamicLink lets you choose a link at runtime. Use it when different requests should be routed through different links.
Example
import { function createORPCClient<T extends AnyNestedClient>(link: ClientLink<InferClientContext<T>>, { path, ...options }?: NoInfer<ORPCClientOptions<T>>): TCreates a fully typed oRPC client from a link.
The returned client mirrors the shape of your router or contract,
so calling a procedure is as simple as calling a function.createORPCClient, class DynamicLink<TClientContext extends ClientContext>DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
based on the request path, input, and context.DynamicLink } from '@orpc/client'
interface ClientContext {
ClientContext.cache?: boolean | undefinedcache?: boolean
}
const const cacheLink: RPCLink<ClientContext>cacheLink = new new RPCLink<ClientContext>(options: RPCLinkOptions<ClientContext>): RPCLink<ClientContext>Client link that communicates with an RPC Handler over the Fetch API (HTTP).RPCLink({
FetchLinkTransportOptions<ClientContext>.origin?: Value<Promisable<`https://${string}` | `http://${string}` | ({} & string) | undefined>, [options: ClientOptions<ClientContext>, path: string[]]>The origin to prepend to all request URLs, useful for CORS requests.origin: 'https://cache.example.com',
})
const const noCacheLink: RPCLink<ClientContext>noCacheLink = new new RPCLink<ClientContext>(options: RPCLinkOptions<ClientContext>): RPCLink<ClientContext>Client link that communicates with an RPC Handler over the Fetch API (HTTP).RPCLink({
FetchLinkTransportOptions<ClientContext>.origin?: Value<Promisable<`https://${string}` | `http://${string}` | ({} & string) | undefined>, [options: ClientOptions<ClientContext>, path: string[]]>The origin to prepend to all request URLs, useful for CORS requests.origin: 'https://example.com',
})
const const link: DynamicLink<ClientContext>link = new new DynamicLink<ClientContext>(linkResolver: (options: ClientOptions<ClientContext>, path: string[], input: unknown) => Promisable<ClientLink<ClientContext>>): DynamicLink<ClientContext>DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
based on the request path, input, and context.DynamicLink<ClientContext>((options: ClientOptions<ClientContext>options, path: string[]path, input: unknowninput) => {
if (options: ClientOptions<ClientContext>options.ClientOptions<ClientContext>.context: ClientContextcontext?.ClientContext.cache?: boolean | undefinedcache) {
return const cacheLink: RPCLink<ClientContext>cacheLink
}
return const noCacheLink: RPCLink<ClientContext>noCacheLink
})
const const client: {
ping: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
pong: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
}
client: type RouterClient<TRouter extends AnyRouter, TClientContext extends ClientContext = object> = TRouter extends Procedure<any, any, infer $InputSchema extends AnySchema, infer $OutputSchema extends AnySchema, infer $ErrorMap extends ErrorMap, infer $ReturnedError extends AnyORPCError> ? ProcedureClient<TClientContext, $InputSchema, $OutputSchema, $ErrorMap, $ReturnedError> : { [K in keyof TRouter]: TRouter[K] extends Lazyable<infer U extends AnyRouter> ? RouterClient<...> : never; }The client type derived from a router, exposing every procedure as a callable
function while preserving the router's shape.RouterClient<typeof const router: {
ping: DecoratedProcedure<DefaultInitialContext & object, object, InitialInputSchema, Schema<string>, Record<never, never>, never>;
pong: DecoratedProcedure<DefaultInitialContext & object, object, InitialInputSchema, Schema<string>, Record<never, never>, never>;
}
router, ClientContext> = createORPCClient<{
ping: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
pong: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
}>(link: ClientLink<ClientContext>, { path, ...options }?: NoInfer<ORPCClientOptions<{
ping: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
pong: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
}>>): {
ping: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
pong: ProcedureClient<ClientContext, InitialInputSchema, Schema<string>, Record<never, never>, never>;
}
Creates a fully typed oRPC client from a link.
The returned client mirrors the shape of your router or contract,
so calling a procedure is as simple as calling a function.createORPCClient(const link: DynamicLink<ClientContext>link)