/** * @zh 协议定义模块 * @en Protocol Definition Module */ import type { ApiDef, MsgDef, ProtocolDef } from './types' /** * @zh 创建 API 定义 * @en Create API definition * * @example * ```typescript * const join = rpc.api<{ name: string }, { id: string }>() * ``` */ function api(): ApiDef { return { _type: 'api' } as ApiDef } /** * @zh 创建消息定义 * @en Create message definition * * @example * ```typescript * const chat = rpc.msg<{ from: string; text: string }>() * ``` */ function msg(): MsgDef { return { _type: 'msg' } as MsgDef } /** * @zh 定义协议 * @en Define protocol * * @example * ```typescript * export const protocol = rpc.define({ * api: { * join: rpc.api<{ name: string }, { id: string }>(), * leave: rpc.api(), * }, * msg: { * chat: rpc.msg<{ from: string; text: string }>(), * }, * }) * ``` */ function define(protocol: T): T { return protocol } /** * @zh RPC 协议定义工具 * @en RPC protocol definition utilities */ export const rpc = { define, api, msg, } as const