Skip to content

Commit

Permalink
Merge pull request #655 from refractedlabs/add-timeout-to-lcd-client
Browse files Browse the repository at this point in the history
feat: add timeout to the arguments of LcdClient constructor
  • Loading branch information
pyramation authored Sep 16, 2024
2 parents 420d972 + 8e7c4e9 commit f67914c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
11 changes: 10 additions & 1 deletion packages/lcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@
"clean": "rimraf ./types",
"prepare": "npm run build",
"lint": "eslint .",
"format": "eslint . --fix"
"format": "eslint . --fix",
"test": "jest"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"transform": {
"^.+\\.ts?$": "ts-jest"
},
"transformIgnorePatterns": [
"<rootDir>/node_modules/"
],
"testPathIgnorePatterns": [
"main/",
"types/"
Expand Down
11 changes: 11 additions & 0 deletions packages/lcd/src/rest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LCDClient } from "./rest";

it('createLcdClient', async () => {
let client = new LCDClient({ restEndpoint: "http://localhost:1317" });
expect(client.restEndpoint).toBe("http://localhost:1317/");
expect(client.timeout).toBe(10000);

client = new LCDClient({ restEndpoint: "http://localhost:1317/", timeout: 20000 });
expect(client.restEndpoint).toBe("http://localhost:1317/");
expect(client.timeout).toBe(20000);
});
12 changes: 7 additions & 5 deletions packages/lcd/src/rest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import axios from 'axios';
export class LCDClient {
restEndpoint: string;
timeout: number;
private instance: any;

constructor({ restEndpoint }) {
this.restEndpoint = restEndpoint.endsWith('/') ? restEndpoint : `${restEndpoint}/`;
constructor(config: { restEndpoint: string, timeout?: number }) {
this.restEndpoint = config.restEndpoint.endsWith('/') ? config.restEndpoint : `${config.restEndpoint}/`;
this.timeout = config.timeout ?? 10000;
this.instance = axios.create({
baseURL: this.restEndpoint,
timeout: 10000,
timeout: this.timeout,
headers: {}
});
this.get = this.get.bind(this);
Expand All @@ -18,7 +20,7 @@ export class LCDClient {
return new Promise<ResponseType>(async (resolve, reject) => {
try {
const response = await this.instance.get(endpoint, {
timeout: 10000,
timeout: this.timeout,
...opts
});
if (response && response.data) {
Expand All @@ -37,7 +39,7 @@ export class LCDClient {
return new Promise<ResponseType>(async (resolve, reject) => {
try {
const response = await this.instance.post(endpoint, body, {
timeout: 10000,
timeout: this.timeout,
...opts
});
if (response && response.data) {
Expand Down
6 changes: 4 additions & 2 deletions packages/lcd/types/rest.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export declare class LCDClient {
restEndpoint: string;
timeout: number;
private instance;
constructor({ restEndpoint }: {
restEndpoint: any;
constructor(config: {
restEndpoint: string;
timeout?: number;
});
get<ResponseType = unknown>(endpoint: any, opts?: {}): Promise<ResponseType>;
post<ResponseType = unknown>(endpoint: any, body?: {}, opts?: {}): Promise<ResponseType>;
Expand Down

0 comments on commit f67914c

Please sign in to comment.