From b04bf4b9526fc40495e9588fbf3f42b7b4936ddf Mon Sep 17 00:00:00 2001 From: homura Date: Mon, 27 Nov 2023 13:29:00 +0800 Subject: [PATCH] feat(rpc): make fetch customizable --- .changeset/new-foxes-heal.md | 5 +++++ packages/rpc/src/index.ts | 23 +++++++++++++---------- packages/rpc/src/method.ts | 24 +++++++++++++----------- packages/rpc/src/types/common.ts | 3 +++ 4 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 .changeset/new-foxes-heal.md diff --git a/.changeset/new-foxes-heal.md b/.changeset/new-foxes-heal.md new file mode 100644 index 000000000..3c102ca39 --- /dev/null +++ b/.changeset/new-foxes-heal.md @@ -0,0 +1,5 @@ +--- +"@ckb-lumos/rpc": minor +--- + +make `fetch` customizable diff --git a/packages/rpc/src/index.ts b/packages/rpc/src/index.ts index cad961650..9405c55a9 100644 --- a/packages/rpc/src/index.ts +++ b/packages/rpc/src/index.ts @@ -9,7 +9,7 @@ import { PayloadInBatchException, } from "./exceptions"; import { RPCConfig } from "./types/common"; -import fetch from "cross-fetch"; +import fetch_ from "cross-fetch"; import { AbortController as CrossAbortController } from "abort-controller"; export const ParamsFormatter = paramsFormatter; @@ -37,10 +37,11 @@ export class CKBRPC extends Base { return this.#resultFormatter; } - constructor(url: string, config: RPCConfig = { timeout: 30000 }) { + constructor(url: string, config: Partial = {}) { super(); this.setNode({ url }); - this.#config = config; + const { timeout = 30000, fetch = fetch_ } = config; + this.#config = { timeout, fetch }; Object.defineProperties(this, { addMethod: { @@ -59,7 +60,7 @@ export class CKBRPC extends Base { }); Object.keys(this.rpcProperties).forEach((name) => { - this.addMethod({ name, ...this.rpcProperties[name] }, config); + this.addMethod({ name, ...this.rpcProperties[name] }, this.#config); }); } @@ -141,12 +142,14 @@ export class CKBRPC extends Base { ctx.#config.timeout ); - const batchRes = await fetch(ctx.#node.url, { - method: "POST", - headers: { "content-type": "application/json" }, - body: JSON.stringify(payload), - signal: signal, - }).then((res) => res.json()); + const batchRes = await ctx.#config + .fetch(ctx.#node.url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(payload), + signal: signal, + }) + .then((res) => res.json()); clearTimeout(timeout); diff --git a/packages/rpc/src/method.ts b/packages/rpc/src/method.ts index 947fcdebe..e671eff62 100644 --- a/packages/rpc/src/method.ts +++ b/packages/rpc/src/method.ts @@ -1,8 +1,8 @@ import { IdNotMatchException, ResponseException } from "./exceptions"; import { CKBComponents } from "./types/api"; import { RPCConfig } from "./types/common"; -import fetch from "cross-fetch"; import { AbortController as CrossAbortController } from "abort-controller"; +import fetch_ from "cross-fetch"; export class Method { #name: string; @@ -24,12 +24,13 @@ export class Method { constructor( node: CKBComponents.Node, options: CKBComponents.Method, - config: RPCConfig = { timeout: 30000 } + config: Partial = {} ) { this.#node = node; this.#options = options; this.#name = options.name; - this.#config = config; + const { timeout = 30000, fetch = fetch_ } = config; + this.#config = { timeout, fetch }; Object.defineProperty(this.call, "name", { value: options.name, @@ -46,14 +47,15 @@ export class Method { const timeout = setTimeout(() => controller.abort(), this.#config.timeout); - const res = await fetch(this.#node.url, { - method: "POST", - headers: { - "content-type": "application/json", - }, - body: JSON.stringify(payload), - signal, - }) + const res = await this.#config + .fetch(this.#node.url, { + method: "POST", + headers: { + "content-type": "application/json", + }, + body: JSON.stringify(payload), + signal, + }) .then((res) => res.json()) .then((res) => { if (res.id !== payload.id) { diff --git a/packages/rpc/src/types/common.ts b/packages/rpc/src/types/common.ts index 030febe75..6d31656bb 100644 --- a/packages/rpc/src/types/common.ts +++ b/packages/rpc/src/types/common.ts @@ -1,3 +1,6 @@ +/// + export type RPCConfig = { timeout: number; + fetch: typeof fetch; };