import * as tb from "../base"; import { ProtocolBase } from "./Protocol"; type ValueOf = Obj[keyof Obj]; type MethodMessage< Protocol extends ProtocolBase, MethodName extends keyof Protocol["methods"], > = { id: number; method: MethodName; args: tb.BicoderTargets; }; type Request = tb.Bicoder< ValueOf< { [MethodName in keyof Protocol["methods"]]: MethodMessage< Protocol, MethodName >; } > >; export function RequestBicoder( protocol: Protocol, ): Request { return tb.Union( ...Object.keys(protocol.methods).map((methodName) => tb.Object({ id: tb.size, method: tb.Exact(methodName), args: tb.Tuple(...protocol.methods[methodName].args), }) ), ) as unknown as Request; } export default Request;