From 3cf82d97ff1a03defc8f5e36b485fd86b47fcd98 Mon Sep 17 00:00:00 2001 From: homura Date: Fri, 2 Aug 2024 13:16:35 +0900 Subject: [PATCH] refactor(rpc): added null and boolean to batch rpc (#736) Co-authored-by: Chen Yu --- .changeset/khaki-gorillas-sell.md | 5 +++++ packages/rpc/src/index.ts | 4 ++-- packages/rpc/src/method.ts | 9 +++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 .changeset/khaki-gorillas-sell.md diff --git a/.changeset/khaki-gorillas-sell.md b/.changeset/khaki-gorillas-sell.md new file mode 100644 index 000000000..146638b38 --- /dev/null +++ b/.changeset/khaki-gorillas-sell.md @@ -0,0 +1,5 @@ +--- +"@ckb-lumos/rpc": minor +--- + +refactor: added null and boolean for batch client diff --git a/packages/rpc/src/index.ts b/packages/rpc/src/index.ts index c101ed38a..778c3bb7e 100644 --- a/packages/rpc/src/index.ts +++ b/packages/rpc/src/index.ts @@ -1,5 +1,5 @@ import { Base } from "./Base"; -import { Method } from "./method"; +import { JSONValue, Method } from "./method"; import { CKBComponents } from "./types/api"; import { formatter as paramsFormatter } from "./paramsFormatter"; import * as resultFormatter from "./resultFormatter"; @@ -85,7 +85,7 @@ export class CKBRPC extends Base { /* eslint-disable */ public createBatchRequest = < N extends keyof Base, - P extends (string | number | object)[], + P extends JSONValue[], R = any[] >( params: [method: N, ...rest: P][] = [] diff --git a/packages/rpc/src/method.ts b/packages/rpc/src/method.ts index 789de8c2f..539946dd9 100644 --- a/packages/rpc/src/method.ts +++ b/packages/rpc/src/method.ts @@ -5,6 +5,11 @@ import { DEFAULT_RPC_TIMEOUT } from "."; import AbortController from "abort-controller"; import fetch_ from "cross-fetch"; +type JSONPrimitive = string | number | boolean | null; +type JSONArray = JSONValue[]; +type JSONObject = { [key: string]: JSONValue }; +export type JSONValue = JSONPrimitive | JSONArray | JSONObject; + export class Method { #name: string; #config: RPCConfig; @@ -41,7 +46,7 @@ export class Method { } /* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types */ - public call = async (...params: (string | number | object)[]) => { + public call = async (...params: JSONValue[]) => { const payload = this.getPayload(...params); const controller = new AbortController(); const signal = controller.signal as AbortSignal; @@ -72,7 +77,7 @@ export class Method { return res; }; - public getPayload = (...params: (string | number | object)[]) => { + public getPayload = (...params: JSONValue[]) => { const data = params.map( (p, i) => (this.#options.paramsFormatters[i] &&