UNPKG

picorpc

Version:

A tiny RPC library and spec, inspired by JSON-RPC 2.0 and tRPC.

28 lines (27 loc) 866 B
/* IMPORT */ import { VERSION } from '../constants.js'; import Request from '../objects/request.js'; import { isFunction, isString } from '../utils.js'; /* MAIN */ const createAbstractClient = (options) => { const { context, handler } = options; let client = Object.seal(Object.freeze({})); //TSC let id = 0n; return new Proxy(client, { get: (_, method) => { if (!isString(method)) throw new Error('Invalid method'); return (...params) => { return new Request(handler, { version: VERSION, id: String(id += 1n), method, params, context: isFunction(context) ? context() : undefined }); }; } }); }; /* EXPORT */ export default createAbstractClient;