Skip to main content

method

Creates a typed RPC method definition. Supports optional Zod input validation.

Signatures

With input validation:

rpc.method<I, O>(def: {
description?: string;
timeout?: number;
input: ZodType<I>;
handler: (params: I) => Promise<O>;
}): RpcMethodDef<I, O>

Without input:

rpc.method<O>(def: {
description?: string;
timeout?: number;
handler: () => Promise<O>;
}): RpcMethodDef<unknown, O>

Parameters

ParameterTypeRequiredDescription
descriptionstringNoHuman-readable description (exposed in the RPC registry)
timeoutnumberNoTimeout in milliseconds (default: 30000)
inputZodType<I>NoZod schema for input validation
handler(params: I) => Promise<O> or () => Promise<O>YesAsync function that processes the request. For the no-input overload, the handler takes no arguments.

Returns

RpcMethodDef<I, O>

Example

import { rpc } from "@hmcs/sdk/rpc";
import { z } from "zod";

// With input validation
const speak = rpc.method({
description: "Speak a message",
timeout: 10000,
input: z.object({ text: z.string() }),
handler: async ({ text }) => {
return { spoken: true };
},
});

// Without input
const status = rpc.method({
description: "Get current status",
handler: async () => {
return { ready: true };
},
});