Skip to content

Commit

Permalink
feat: allow passing options (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored and faustbrian committed Jul 6, 2019
1 parent 336dc78 commit 7e2dd83
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 41 deletions.
1 change: 1 addition & 0 deletions __tests__/mocks/node.ts
Expand Up @@ -15,5 +15,6 @@ export const mockNode = (host: string) => {

nock(host)
.get("/node/fees")
.query({ days: 30 })
.reply(200, { data: [] });
};
2 changes: 1 addition & 1 deletion __tests__/resources/node.test.ts
Expand Up @@ -3,7 +3,7 @@ import { configureMocks } from "../mocks";

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

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

Expand Down
13 changes: 9 additions & 4 deletions src/connection.ts
Expand Up @@ -5,6 +5,8 @@ import { IResponse } from "./interfaces";
import { Resources } from "./resources";

export class Connection {
private opts: Record<string, any>;

public constructor(private readonly host: string) {
if (!isUrl(host)) {
throw new Error(`${host} is not a valid URL.`);
Expand All @@ -18,6 +20,12 @@ export class Connection {
return new Resources[name](this);
}

public withOptions(opts: Record<string, any>): this {
this.opts = opts;

return this;
}

public async get<T = any>(url: string, opts?: Record<string, any>): Promise<IResponse<T>> {
return this.sendRequest("get", url, opts);
}
Expand All @@ -27,10 +35,7 @@ export class Connection {
}

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

if (opts.body && typeof opts !== "string") {
// @ts-ignore
Expand Down
8 changes: 4 additions & 4 deletions src/resources/blocks.ts
Expand Up @@ -3,18 +3,18 @@ import { Resource } from "./resource";

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

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

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

public async search<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
return this.connection.post("blocks/search", payload);
return this.sendPost("blocks/search", payload);
}
}
8 changes: 4 additions & 4 deletions src/resources/delegates.ts
Expand Up @@ -3,18 +3,18 @@ import { Resource } from "./resource";

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

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

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

public async voters<T = any>(id: string, query?: Record<string, any>): Promise<IResponse<T>> {
return this.connection.get(`delegates/${id}/voters`, query);
return this.sendGet(`delegates/${id}/voters`, query);
}
}
8 changes: 4 additions & 4 deletions src/resources/node.ts
Expand Up @@ -3,19 +3,19 @@ import { Resource } from "./resource";

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

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

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

public async fees<T = any>(days: number): Promise<IResponse<T>> {
return this.connection.get("node/fees", {
return this.sendGet("node/fees", {
days,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/resources/peers.ts
Expand Up @@ -3,10 +3,10 @@ import { Resource } from "./resource";

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

public async get<T = any>(ip: string): Promise<IResponse<T>> {
return this.connection.get(`peers/${ip}`);
return this.sendGet(`peers/${ip}`);
}
}
31 changes: 31 additions & 0 deletions src/resources/resource.ts
@@ -1,5 +1,36 @@
import { Connection } from "../connection";
import { IResponse } from "../interfaces";

export class Resource {
private opts: Record<string, any> = {};

public constructor(protected readonly connection: Connection) {}

public withOptions(opts: Record<string, any>): this {
this.opts = opts;

return this;
}

public resetOptions(): this {
this.opts = {};

return this;
}

public async sendGet<T = any>(url: string, query?: Record<string, any>): Promise<IResponse<T>> {
const response = await this.connection.get(url, { ...this.opts, ...{ query } });

this.resetOptions();

return response;
}

public async sendPost<T = any>(url: string, body?: Record<string, any>): Promise<IResponse<T>> {
const response = await this.connection.post(url, { ...this.opts, ...{ body } });

this.resetOptions();

return response;
}
}
16 changes: 8 additions & 8 deletions src/resources/transactions.ts
Expand Up @@ -3,34 +3,34 @@ import { Resource } from "./resource";

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

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

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

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

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

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

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

public async fees<T = any>(): Promise<IResponse<T>> {
return this.connection.get("transactions/fees");
return this.sendGet("transactions/fees");
}
}
4 changes: 2 additions & 2 deletions src/resources/votes.ts
Expand Up @@ -3,10 +3,10 @@ import { Resource } from "./resource";

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

public async get<T = any>(id: string): Promise<IResponse<T>> {
return this.connection.get(`votes/${id}`);
return this.sendGet(`votes/${id}`);
}
}
16 changes: 8 additions & 8 deletions src/resources/wallets.ts
Expand Up @@ -3,34 +3,34 @@ import { Resource } from "./resource";

export class Wallets extends Resource {
public async all<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
return this.connection.get("wallets", query);
return this.sendGet("wallets", query);
}

public async top<T = any>(query?: Record<string, any>): Promise<IResponse<T>> {
return this.connection.get("wallets/top", query);
return this.sendGet("wallets/top", query);
}

public async get<T = any>(id: string): Promise<IResponse<T>> {
return this.connection.get(`wallets/${id}`);
return this.sendGet(`wallets/${id}`);
}

public async transactions<T = any>(id: string, query?: Record<string, any>): Promise<IResponse<T>> {
return this.connection.get(`wallets/${id}/transactions`, query);
return this.sendGet(`wallets/${id}/transactions`, query);
}

public async transactionsSent<T = any>(id: string, query?: Record<string, any>): Promise<IResponse<T>> {
return this.connection.get(`wallets/${id}/transactions/sent`, query);
return this.sendGet(`wallets/${id}/transactions/sent`, query);
}

public async transactionsReceived<T = any>(id: string, query?: Record<string, any>): Promise<IResponse<T>> {
return this.connection.get(`wallets/${id}/transactions/received`, query);
return this.sendGet(`wallets/${id}/transactions/received`, query);
}

public async votes<T = any>(id: string): Promise<IResponse<T>> {
return this.connection.get(`wallets/${id}/votes`);
return this.sendGet(`wallets/${id}/votes`);
}

public async search<T = any>(payload: Record<string, any>): Promise<IResponse<T>> {
return this.connection.post("wallets/search", payload);
return this.sendPost("wallets/search", payload);
}
}

0 comments on commit 7e2dd83

Please sign in to comment.