メインコンテンツまでスキップ

method

型付きの RPC メソッド定義を作成します。オプションで Zod による入力バリデーションをサポートします。

シグネチャ

入力バリデーションあり:

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

入力なし:

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

パラメータ

パラメータ必須説明
descriptionstringいいえ人間が読める説明(RPC レジストリに公開されます)
timeoutnumberいいえタイムアウト(ミリ秒単位、デフォルト: 30000)
inputZodType<I>いいえ入力バリデーション用の Zod スキーマ
handler(params: I) => Promise<O> または () => Promise<O>はいリクエストを処理する非同期関数。入力なしのオーバーロードでは、ハンドラーは引数を取りません。

戻り値

RpcMethodDef<I, O>

使用例

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

// 入力バリデーションあり
const speak = rpc.method({
description: "Speak a message",
timeout: 10000,
input: z.object({ text: z.string() }),
handler: async ({ text }) => {
return { spoken: true };
},
});

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