@morambacrypto/connect 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,12 @@
1
1
  import { AxiosInstance } from "axios";
2
2
  export declare class HttpClient {
3
3
  private client;
4
- constructor(baseURL: string, headers?: Record<string, string>);
4
+ private maxRetries;
5
+ private retryDelay;
6
+ constructor(baseURL: string, headers?: Record<string, string>, maxRetries?: number, // default: 4 retries
7
+ retryDelay?: number);
5
8
  get axiosInstance(): AxiosInstance;
9
+ private setupRetryInterceptor;
6
10
  post<T = any>(url: string, data: any): Promise<T>;
7
11
  get<T = any>(url: string): Promise<T>;
8
12
  }
@@ -1,15 +1,39 @@
1
1
  import axios from "axios";
2
2
  export class HttpClient {
3
- constructor(baseURL, headers = {}) {
3
+ constructor(baseURL, headers = {}, maxRetries = 4, // default: 4 retries
4
+ retryDelay = 300 // base delay: 300ms
5
+ ) {
4
6
  this.client = axios.create({
5
7
  baseURL,
6
8
  headers,
7
9
  timeout: 10000,
8
10
  });
11
+ this.maxRetries = maxRetries;
12
+ this.retryDelay = retryDelay;
13
+ this.setupRetryInterceptor();
9
14
  }
10
15
  get axiosInstance() {
11
16
  return this.client;
12
17
  }
18
+ setupRetryInterceptor() {
19
+ this.client.interceptors.response.use(response => response, async (error) => {
20
+ const config = error.config;
21
+ if (!config)
22
+ throw error;
23
+ // initialize retry counter
24
+ if (!config._retryCount)
25
+ config._retryCount = 0;
26
+ // stop retry if max reached
27
+ if (config._retryCount >= this.maxRetries) {
28
+ throw error;
29
+ }
30
+ config._retryCount++;
31
+ // exponential delay: delay * 2^(retryCount)
32
+ const delay = this.retryDelay * Math.pow(2, config._retryCount);
33
+ await new Promise(resolve => setTimeout(resolve, delay));
34
+ return this.client(config);
35
+ });
36
+ }
13
37
  post(url, data) {
14
38
  return this.client.post(url, data).then((res) => res.data);
15
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@morambacrypto/connect",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "This library use for interact with moramba-crypto project",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",