Skip to content

Commit

Permalink
feat(rpc): make fetch customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Nov 27, 2023
1 parent 68e4f5b commit b04bf4b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-foxes-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/rpc": minor
---

make `fetch` customizable
23 changes: 13 additions & 10 deletions packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,10 +37,11 @@ export class CKBRPC extends Base {
return this.#resultFormatter;
}

constructor(url: string, config: RPCConfig = { timeout: 30000 }) {
constructor(url: string, config: Partial<RPCConfig> = {}) {
super();
this.setNode({ url });
this.#config = config;
const { timeout = 30000, fetch = fetch_ } = config;
this.#config = { timeout, fetch };

Object.defineProperties(this, {
addMethod: {
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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);

Expand Down
24 changes: 13 additions & 11 deletions packages/rpc/src/method.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,12 +24,13 @@ export class Method {
constructor(
node: CKBComponents.Node,
options: CKBComponents.Method,
config: RPCConfig = { timeout: 30000 }
config: Partial<RPCConfig> = {}
) {
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,
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions packages/rpc/src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// <reference lib="dom" />

export type RPCConfig = {
timeout: number;
fetch: typeof fetch;
};

0 comments on commit b04bf4b

Please sign in to comment.