@metamask/kernel-rpc-methods
Version:
Utilities for implementing Ocap Kernel JSON-RPC methods
37 lines • 1.9 kB
text/typescript
import type { Json, JsonRpcParams } from "@metamask/utils";
import type { Handler } from "./types.mjs";
type ExtractHooks<Handlers> = Handlers extends Handler<string, any, any, infer Hooks> ? Hooks : never;
type ExtractMethods<Handlers> = Handlers extends Handler<infer Methods, any, any, any> ? Methods : never;
type HandlerRecord<Handlers extends Handler<string, JsonRpcParams, Json | void, Record<string, unknown>>> = Record<Handlers['method'], Handlers>;
/**
* A registry for RPC method handlers that provides type-safe registration and execution.
*/
export declare class RpcService<Handlers extends HandlerRecord<Handler<string, any, any, any>>> {
#private;
/**
* Create a new HandlerRegistry with the specified method handlers.
*
* @param handlers - A record mapping method names to their handler implementations.
* @param hooks - The hooks to pass to the method implementation.
*/
constructor(handlers: Handlers, hooks: ExtractHooks<Handlers[keyof Handlers]>);
/**
* Assert that a method is registered in this registry.
*
* @param method - The method name to check.
* @throws If the method is not registered.
*/
assertHasMethod(method: string): asserts method is ExtractMethods<Handlers[keyof Handlers]>;
/**
* Execute a method with the provided parameters. Only the hooks specified in the
* handler's `hooks` array will be passed to the implementation.
*
* @param method - The method name to execute.
* @param params - The parameters to pass to the method implementation.
* @returns The result of the method execution.
* @throws If the parameters are invalid.
*/
execute<Method extends ExtractMethods<Handlers[keyof Handlers]>>(method: Method, params: unknown): Promise<ReturnType<Handlers[Method]['implementation']>>;
}
export {};
//# sourceMappingURL=RpcService.d.mts.map