Skip to content
rest-rpc
Esc
↑↓navigate↵open⌘Jpreview
On this page

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.
@remarksoptions can be used to provide values HTTP request would normally provide. Rejects like the fetch client: with the `RouteError` that would be sent, marked as defined when the route defines its code. Other errors thrown by the handler reject unchanged.@see{@link https://rest-rpc.vercel.app/docs/client/local-calls}
call
, const route: Omit<ServerRouteBuilder<RootBuilderState, DefaultContext, DefaultContext>, "meta">
Entry point for defining routes with handlers.
@remarksResponses are inferred from the handler unless they are defined before `.handler()`. Routes are runtime-independent: the same route works with the Fetch and Node HTTP handlers.@see{@link https://rest-rpc.vercel.app/docs/quickstart#define-routes}
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.
@remarksResponses are inferred from the handler unless they are defined before `.handler()`. Routes are runtime-independent: the same route works with the Fetch and Node HTTP handlers.@see{@link https://rest-rpc.vercel.app/docs/quickstart#define-routes}
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.
@see{@link https://rest-rpc.vercel.app/docs/route-builder}
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.
@see{@link https://rest-rpc.vercel.app/docs/route-builder#request}
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.
@see{@link https://rest-rpc.vercel.app/docs/route-builder#responses}
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.
@remarksoptions can be used to provide values HTTP request would normally provide. Rejects like the fetch client: with the `RouteError` that would be sent, marked as defined when the route defines its code. Other errors thrown by the handler reject unchanged.@see{@link https://rest-rpc.vercel.app/docs/client/local-calls}
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;
};

Was this page helpful?