Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: available resources #95

Merged
merged 3 commits into from Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/connection-manager.ts
Expand Up @@ -17,19 +17,17 @@ export class ConnectionManager {
public disconnect(name?: string): void {
name = name || this.getDefaultConnection();

// @ts-ignore
// tslint:disable-next-line: no-dynamic-delete
delete this.connections[name];
}

public connection(name?: string): Connection {
name = name || this.getDefaultConnection();

// @ts-ignore
if (!this.connections[name]) {
throw new Error(`Connection [${name}] not configured.`);
}

// @ts-ignore
return this.connections[name];
}

Expand Down
20 changes: 8 additions & 12 deletions src/connection.ts
@@ -1,8 +1,8 @@
import ky from "ky-universal";
import isUrl from "is-url-superb";
import ky, { Options as kyOptions } from "ky-universal";
import { RequestError } from "./errors";
import { IResponse } from "./interfaces";
import { Resources } from "./resources";
import { AvailableResource, AvailableResourcesName, Resources } from "./resources";

export class Connection {
private opts: Record<string, any>;
Expand All @@ -13,11 +13,9 @@ export class Connection {
}
}

public api<T = any>(name: string): T {
name = name.charAt(0).toUpperCase() + name.slice(1);

// @ts-ignore
return new Resources[name](this);
public api<T extends AvailableResourcesName>(name: T) {
const selectedResourceClass = Resources[name];
return new selectedResourceClass(this) as AvailableResource<T>;
}

public withOptions(opts: Record<string, any>): this {
Expand All @@ -34,11 +32,10 @@ export class Connection {
return this.sendRequest("post", url, opts);
}

private async sendRequest<T>(method: string, url: string, opts?: Record<string, any>): Promise<IResponse<T>> {
private async sendRequest<T>(method: kyOptions["method"], url: string, opts?: Record<string, any>): Promise<IResponse<T>> {
opts = { ...this.opts, ...(opts || {}) };

if (opts.body && typeof opts !== "string") {
// @ts-ignore
opts.body = JSON.stringify(opts.body);
}

Expand All @@ -52,12 +49,11 @@ export class Connection {
}

try {
// @ts-ignore
const response = await ky[method](`${this.host}/${url}`, opts);
const response = await ky(`${this.host}/${url}`, { ...opts, method });

return {
body: await response.json(),
headers: response.headers,
headers: response.headers as any as IResponse<T>["headers"],
status: response.status,
};
} catch (error) {
Expand Down
3 changes: 1 addition & 2 deletions src/errors.ts
@@ -1,6 +1,5 @@
export class RequestError extends Error {
// @ts-ignore
public constructor(error) {
public constructor(error: any) {
super(error.message);

Object.defineProperty(this, "message", {
Expand Down
18 changes: 9 additions & 9 deletions src/resources/bridgechains.ts
Expand Up @@ -2,15 +2,15 @@ import { IResponse } from "../interfaces";
import { Resource } from "./resource";

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

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

public async search<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
return this.sendPost("bridgechains/search", payload);
}
public async search<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
return this.sendPost("bridgechains/search", payload);
}
}
24 changes: 12 additions & 12 deletions src/resources/businesses.ts
Expand Up @@ -2,19 +2,19 @@ import { IResponse } from "../interfaces";
import { Resource } from "./resource";

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

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

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

public async search<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
return this.sendPost("businesses/search", payload);
}
public async search<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
return this.sendPost("businesses/search", payload);
}
}
3 changes: 3 additions & 0 deletions src/resources/index.ts
Expand Up @@ -24,3 +24,6 @@ export const Resources = {
Votes,
Wallets,
};

export type AvailableResourcesName = keyof typeof Resources;
export type AvailableResource<T extends AvailableResourcesName> = InstanceType<typeof Resources[T]>;
24 changes: 12 additions & 12 deletions src/resources/locks.ts
Expand Up @@ -2,19 +2,19 @@ import { IResponse } from "../interfaces";
import { Resource } from "./resource";

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

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

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

public async unlocked<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
return this.sendPost("locks/unlocked", payload);
}
public async unlocked<T = any>(payload?: Record<string, any>): Promise<IResponse<T>> {
return this.sendPost("locks/unlocked", payload);
}
}
4 changes: 1 addition & 3 deletions src/resources/node.ts
Expand Up @@ -19,8 +19,6 @@ export class Node extends Resource {
}

public async fees<T = any>(days: number): Promise<IResponse<T>> {
return this.sendGet("node/fees", {
days,
});
return this.sendGet("node/fees", { days });
}
}
6 changes: 4 additions & 2 deletions tslint.json
Expand Up @@ -2,6 +2,8 @@
"extends": "@typeskrift/tslint",
"rules": {
"typedef": false,
"no-unsafe-any": false
"no-unsafe-any": false,
"no-parameter-reassignment": false,
"interface-name": false
}
}
}