Local Calls
Run a route in the same process with call(), without HTTP.
call() runs one route without HTTP, such as in a test of a single route:
import { function call<TRoute extends ImplementedRoute>(route: TRoute, ...[request, options]: RequestArgs<InferClientRequest<TRoute, Record<never, string>>, OptionsArgs<InitialContextOf<TRoute["~restrpc"]>, Merge<Pick<FetchOptions, "signal"> & ContextOption<InitialContextOf<TRoute["~restrpc"]>>>>>): CallResult<TRoute>Calls a route handler locally, without HTTP serialization.call, const route: Omit<ServerRouteBuilder<RootBuilderState, DefaultContext, DefaultContext>, "meta">Entry point for defining routes with handlers.route } from "@rest-rpc/server";
import * as import zz from "zod";
const const get: HandledRoute<{
readonly method: "GET";
readonly path: "/posts/:id";
request: {
params: readonly [z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>];
};
responses: {};
readonly metaShape?: DefaultMeta | undefined;
}, DefaultContext, DefaultContext, {
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
}>
get = const route: Omit<ServerRouteBuilder<RootBuilderState, DefaultContext, DefaultContext>, "meta">Entry point for defining routes with handlers.route
.get<"/posts/:id">(path?: "/posts/:id" | undefined): Omit<ServerRouteBuilder<WithHttpMethod<RootBuilderState, "GET", "/posts/:id">, DefaultContext, DefaultContext>, "static" | OmittedMethods<WithHttpMethod<RootBuilderState, "GET", TPath>>>Defines a GET route.get("/posts/:id")
.params<z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>>(schema: z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>): Omit<ServerRouteBuilder<WithRequest<WithHttpMethod<RootBuilderState, "GET", "/posts/:id">, "params", z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>>, DefaultContext, DefaultContext>, "static" | OmittedMethods<WithRequest<WithHttpMethod<RootBuilderState, "GET", "/posts/:id">, "params", TSchema>>>
Defines path parameters.params(import zz.function object<{
id: z.ZodCoercedNumber<unknown>;
}>(shape?: {
id: z.ZodCoercedNumber<unknown>;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>
object({ id: z.ZodCoercedNumber<unknown>id: import zz.import coercecoerce.function number<unknown>(params?: string | z.core.$ZodNumberParams): z.ZodCoercedNumber<unknown>number() }))
.handler<{
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
}>(handler: HandlerFor<{
readonly method: "GET";
readonly path: "/posts/:id";
request: {
params: readonly [z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>];
};
responses: {};
readonly metaShape?: DefaultMeta | undefined;
}, DefaultContext, {
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
}>): HandledRoute<{
readonly method: "GET";
readonly path: "/posts/:id";
request: {
params: readonly [z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>];
};
responses: {};
readonly metaShape?: DefaultMeta | undefined;
}, DefaultContext, DefaultContext, {
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
}>
Attaches the server handler that completes the route.handler(({ params: {
id: number;
}
params }) => ({
id: numberid: params: {
id: number;
}
params.id: numberid,
title: "Hello"title: "Hello",
body: "First post"body: "First post",
}));
const const response: {
status: 200;
body: {
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
};
headers: Record<string, string>;
cookies: readonly string[];
}
response = await call<HandledRoute<{
readonly method: "GET";
readonly path: "/posts/:id";
request: {
params: readonly [z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>];
};
responses: {};
readonly metaShape?: DefaultMeta | undefined;
}, DefaultContext, DefaultContext, {
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
}>>(route: HandledRoute<{
readonly method: "GET";
readonly path: "/posts/:id";
request: {
params: readonly [z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>];
};
responses: {};
readonly metaShape?: DefaultMeta | undefined;
}, DefaultContext, DefaultContext, {
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
}>, request: {
...;
}, options?: {
...;
} | undefined): ClientPromise<...>
Calls a route handler locally, without HTTP serialization.call(const get: HandledRoute<{
readonly method: "GET";
readonly path: "/posts/:id";
request: {
params: readonly [z.ZodObject<{
id: z.ZodCoercedNumber<unknown>;
}, z.core.$strip>];
};
responses: {};
readonly metaShape?: DefaultMeta | undefined;
}, DefaultContext, DefaultContext, {
readonly id: number;
readonly title: "Hello";
readonly body: "First post";
}>
get, { params: {
id: unknown;
}
params: { id: unknownid: 1 } });
- It takes the same request and returns the same result as a client call.
- The request and response are validated, and middleware runs, as over HTTP.
- Nothing is encoded or decoded, so the result is the handler’s validated output, not what JSON would make of it.
- It rejects like a client call, so
safe()works with it.
type CallOptions = {
/** The initial context the route declares. Required when it declares any. */
context?: object;
/** Passed to the handler. Never aborts by default. */
signal?: AbortSignal;
};