diff --git a/.changeset/khaki-gorillas-sell.md b/.changeset/khaki-gorillas-sell.md new file mode 100644 index 00000000..146638b3 --- /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 c101ed38..778c3bb7 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 789de8c2..539946dd 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] &&