From 14ed63325317951d0b5d2b8a25a8898d9a3cb563 Mon Sep 17 00:00:00 2001 From: Tom Wang Date: Mon, 13 May 2024 13:45:48 +0800 Subject: [PATCH] fix: correct CKBRPC.createBatchRequest() types (#683) --- .changeset/wet-turtles-double.md | 5 +++++ packages/rpc/src/index.ts | 18 ++++++++---------- packages/rpc/src/method.ts | 4 +++- 3 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 .changeset/wet-turtles-double.md diff --git a/.changeset/wet-turtles-double.md b/.changeset/wet-turtles-double.md new file mode 100644 index 000000000..0afc13e11 --- /dev/null +++ b/.changeset/wet-turtles-double.md @@ -0,0 +1,5 @@ +--- +"@ckb-lumos/rpc": patch +--- + +fix: correct CKBRPC.createBatchRequest() types diff --git a/packages/rpc/src/index.ts b/packages/rpc/src/index.ts index 7e12a78dd..c101ed38a 100644 --- a/packages/rpc/src/index.ts +++ b/packages/rpc/src/index.ts @@ -15,6 +15,8 @@ import AbortController from "abort-controller"; export const ParamsFormatter = paramsFormatter; export const ResultFormatter = resultFormatter; +export const DEFAULT_RPC_TIMEOUT = 30000; + export class CKBRPC extends Base { #config: RPCConfig; #node: CKBComponents.Node = { @@ -40,7 +42,7 @@ export class CKBRPC extends Base { constructor(url: string, config: Partial = {}) { super(); this.setNode({ url }); - const { timeout = 30000, fetch = fetch_ } = config; + const { timeout = DEFAULT_RPC_TIMEOUT, fetch = fetch_ } = config; this.#config = { timeout, fetch }; Object.defineProperties(this, { @@ -86,14 +88,11 @@ export class CKBRPC extends Base { P extends (string | number | object)[], R = any[] >( - // TODO fix me - // params: [method: N, ...rest: P][] = [], - params: any = [] + params: [method: N, ...rest: P][] = [] ) => { const ctx = this; - // TODO fix me - const proxied: any = new Proxy([], { + const proxied: [method: N, ...rest: P][] = new Proxy([], { set(...p) { const methods = Object.keys(ctx); if (p[1] !== "length") { @@ -121,8 +120,7 @@ export class CKBRPC extends Base { }, exec: { async value() { - // TODO fix me - const payload = proxied.map(([f, ...p]: any, i: any) => { + const payload = proxied.map(([f, ...p], i) => { try { const method = new Method(ctx.node, { ...ctx.rpcProperties[f], @@ -165,8 +163,8 @@ export class CKBRPC extends Base { }, }, }); - // TODO fix me - params.forEach((p: any) => proxied.push(p)); + + params.forEach((p) => proxied.push(p)); return proxied as typeof proxied & { add: (n: N, ...p: P) => typeof proxied; diff --git a/packages/rpc/src/method.ts b/packages/rpc/src/method.ts index 247045466..789de8c2f 100644 --- a/packages/rpc/src/method.ts +++ b/packages/rpc/src/method.ts @@ -1,6 +1,7 @@ import { IdNotMatchException, ResponseException } from "./exceptions"; import { CKBComponents } from "./types/api"; import { RPCConfig } from "./types/common"; +import { DEFAULT_RPC_TIMEOUT } from "."; import AbortController from "abort-controller"; import fetch_ from "cross-fetch"; @@ -29,7 +30,7 @@ export class Method { this.#node = node; this.#options = options; this.#name = options.name; - const { timeout = 30000, fetch = fetch_ } = config; + const { timeout = DEFAULT_RPC_TIMEOUT, fetch = fetch_ } = config; this.#config = { timeout, fetch }; Object.defineProperty(this.call, "name", { @@ -78,6 +79,7 @@ export class Method { this.#options.paramsFormatters[i](p)) || p ); + /* eslint-disable @typescript-eslint/no-magic-numbers */ const id = Math.round(Math.random() * 10000); const payload = { id,