Skip to content

Commit 7e2dd83

Browse files
alexbarnsleyfaustbrian
authored andcommitted
feat: allow passing options (#49)
1 parent 336dc78 commit 7e2dd83

12 files changed

Lines changed: 162 additions & 41 deletions

File tree

__tests__/mocks/node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ export const mockNode = (host: string) => {
1515

1616
nock(host)
1717
.get("/node/fees")
18+
.query({ days: 30 })
1819
.reply(200, { data: [] });
1920
};

__tests__/resources/node.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { configureMocks } from "../mocks";
33

44
const resource: Node = configureMocks<Node>(Node);
55

6-
describe("API - 2.0 - Resources - Loader", () => {
6+
describe("API - 2.0 - Resources - Node", () => {
77
it("should call \"status\" method", async () => {
88
const response = await resource.status();
99

src/connection.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { IResponse } from "./interfaces";
55
import { Resources } from "./resources";
66

77
export class Connection {
8+
private opts: Record<string, any>;
9+
810
public constructor(private readonly host: string) {
911
if (!isUrl(host)) {
1012
throw new Error(`${host} is not a valid URL.`);
@@ -18,6 +20,12 @@ export class Connection {
1820
return new Resources[name](this);
1921
}
2022

23+
public withOptions(opts: Record<string, any>): this {
24+
this.opts = opts;
25+
26+
return this;
27+
}
28+
2129
public async get<T = any>(url: string, opts?: Record<string, any>): Promise<IResponse<T>> {
2230
return this.sendRequest("get", url, opts);
2331
}
@@ -27,10 +35,7 @@ export class Connection {
2735
}
2836

2937
private async sendRequest<T>(method: string, url: string, opts?: Record<string, any>): Promise<IResponse<T>> {
30-
if (!opts) {
31-
// @ts-ignore
32-
opts = {};
33-
}
38+
opts = { ...this.opts, ...(opts || {}) };
3439

3540
if (opts.body && typeof opts !== "string") {
3641
// @ts-ignore

src/resources/blocks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { Resource } from "./resource";
33

44
export class Blocks extends Resource {
55
public async all<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
6-
return this.connection.get("blocks", query);
6+
return this.sendGet("blocks", query);
77
}
88

99
public async get<T = any>(id: string): Promise<IResponse<T>> {
10-
return this.connection.get(`blocks/${id}`);
10+
return this.sendGet(`blocks/${id}`);
1111
}
1212

1313
public async transactions<T = any>(id: string, query?: Record<string, any>): Promise<IResponse<T>> {
14-
return this.connection.get(`blocks/${id}/transactions`, query);
14+
return this.sendGet(`blocks/${id}/transactions`, query);
1515
}
1616

1717
public async search<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
18-
return this.connection.post("blocks/search", payload);
18+
return this.sendPost("blocks/search", payload);
1919
}
2020
}

src/resources/delegates.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { Resource } from "./resource";
33

44
export class Delegates extends Resource {
55
public async all<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
6-
return this.connection.get("delegates", query);
6+
return this.sendGet("delegates", query);
77
}
88

99
public async get<T = any>(id: string): Promise<IResponse<T>> {
10-
return this.connection.get(`delegates/${id}`);
10+
return this.sendGet(`delegates/${id}`);
1111
}
1212

1313
public async blocks<T = any>(id: string, query?: Record<string, any>): Promise<IResponse<T>> {
14-
return this.connection.get(`delegates/${id}/blocks`, query);
14+
return this.sendGet(`delegates/${id}/blocks`, query);
1515
}
1616

1717
public async voters<T = any>(id: string, query?: Record<string, any>): Promise<IResponse<T>> {
18-
return this.connection.get(`delegates/${id}/voters`, query);
18+
return this.sendGet(`delegates/${id}/voters`, query);
1919
}
2020
}

src/resources/node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import { Resource } from "./resource";
33

44
export class Node extends Resource {
55
public async status<T = any>(): Promise<IResponse<T>> {
6-
return this.connection.get("node/status");
6+
return this.sendGet("node/status");
77
}
88

99
public async syncing<T = any>(): Promise<IResponse<T>> {
10-
return this.connection.get("node/syncing");
10+
return this.sendGet("node/syncing");
1111
}
1212

1313
public async configuration<T = any>(): Promise<IResponse<T>> {
14-
return this.connection.get("node/configuration");
14+
return this.sendGet("node/configuration");
1515
}
1616

1717
public async fees<T = any>(days: number): Promise<IResponse<T>> {
18-
return this.connection.get("node/fees", {
18+
return this.sendGet("node/fees", {
1919
days,
2020
});
2121
}

src/resources/peers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { Resource } from "./resource";
33

44
export class Peers extends Resource {
55
public async all<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
6-
return this.connection.get("peers", query);
6+
return this.sendGet("peers", query);
77
}
88

99
public async get<T = any>(ip: string): Promise<IResponse<T>> {
10-
return this.connection.get(`peers/${ip}`);
10+
return this.sendGet(`peers/${ip}`);
1111
}
1212
}

src/resources/resource.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
import { Connection } from "../connection";
2+
import { IResponse } from "../interfaces";
23

34
export class Resource {
5+
private opts: Record<string, any> = {};
6+
47
public constructor(protected readonly connection: Connection) {}
8+
9+
public withOptions(opts: Record<string, any>): this {
10+
this.opts = opts;
11+
12+
return this;
13+
}
14+
15+
public resetOptions(): this {
16+
this.opts = {};
17+
18+
return this;
19+
}
20+
21+
public async sendGet<T = any>(url: string, query?: Record<string, any>): Promise<IResponse<T>> {
22+
const response = await this.connection.get(url, { ...this.opts, ...{ query } });
23+
24+
this.resetOptions();
25+
26+
return response;
27+
}
28+
29+
public async sendPost<T = any>(url: string, body?: Record<string, any>): Promise<IResponse<T>> {
30+
const response = await this.connection.post(url, { ...this.opts, ...{ body } });
31+
32+
this.resetOptions();
33+
34+
return response;
35+
}
536
}

src/resources/transactions.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ import { Resource } from "./resource";
33

44
export class Transactions extends Resource {
55
public async all<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
6-
return this.connection.get("transactions", query);
6+
return this.sendGet("transactions", query);
77
}
88

99
public async create<T = any>(payload: object[]): Promise<IResponse<T>> {
10-
return this.connection.post("transactions", payload);
10+
return this.sendPost("transactions", payload);
1111
}
1212

1313
public async get<T = any>(id: string): Promise<IResponse<T>> {
14-
return this.connection.get(`transactions/${id}`);
14+
return this.sendGet(`transactions/${id}`);
1515
}
1616

1717
public async allUnconfirmed<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
18-
return this.connection.get("transactions/unconfirmed", query);
18+
return this.sendGet("transactions/unconfirmed", query);
1919
}
2020

2121
public async getUnconfirmed<T = any>(id: string): Promise<IResponse<T>> {
22-
return this.connection.get(`transactions/unconfirmed/${id}`);
22+
return this.sendGet(`transactions/unconfirmed/${id}`);
2323
}
2424

2525
public async search<T = any>(payload: Record<string, any>): Promise<IResponse<T>> {
26-
return this.connection.post("transactions/search", payload);
26+
return this.sendPost("transactions/search", payload);
2727
}
2828

2929
public async types<T = any>(): Promise<IResponse<T>> {
30-
return this.connection.get("transactions/types");
30+
return this.sendGet("transactions/types");
3131
}
3232

3333
public async fees<T = any>(): Promise<IResponse<T>> {
34-
return this.connection.get("transactions/fees");
34+
return this.sendGet("transactions/fees");
3535
}
3636
}

src/resources/votes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { Resource } from "./resource";
33

44
export class Votes extends Resource {
55
public async all<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
6-
return this.connection.get("votes", query);
6+
return this.sendGet("votes", query);
77
}
88

99
public async get<T = any>(id: string): Promise<IResponse<T>> {
10-
return this.connection.get(`votes/${id}`);
10+
return this.sendGet(`votes/${id}`);
1111
}
1212
}

0 commit comments

Comments
 (0)