Skip to content

Commit

Permalink
Feat(service-connector): added support for retrying
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 authored May 10, 2024
1 parent 1547432 commit 91cc514
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 37 deletions.
75 changes: 38 additions & 37 deletions packages/service-connector/package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
{
"name": "@sliit-foss/service-connector",
"version": "2.2.1",
"description": "A package to isolate filters and sorts from a given request's query parameters",
"main": "dist/index.js",
"types": "types/index.d.ts",
"scripts": {
"build": "node ../../scripts/esbuild.config.js",
"build:watch": "bash ../../scripts/esbuild.watch.sh",
"bump-version": "bash ../../scripts/bump-version.sh --name=@sliit-foss/service-connector",
"lint": "bash ../../scripts/lint.sh",
"release": "bash ../../scripts/release.sh",
"test": "bash ../../scripts/test/test.sh"
},
"dependencies": {
"@sliit-foss/module-logger": "1.3.1",
"axios": "1.6.0",
"chalk": "4.1.2",
"express-http-context": "1.2.4",
"http-errors": "2.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sliit-foss/npm-catalogue.git"
},
"homepage": "https://github.com/sliit-foss/npm-catalogue/blob/main/packages/service-connector/readme.md",
"keywords": [
"service-connector",
"microservice-connector",
"axios-decorator"
],
"author": "SLIIT FOSS",
"license": "MIT",
"bugs": {
"url": "https://github.com/sliit-foss/npm-catalogue/issues"
}
}
{
"name": "@sliit-foss/service-connector",
"version": "2.2.1",
"description": "A package to isolate filters and sorts from a given request's query parameters",
"main": "dist/index.js",
"types": "types/index.d.ts",
"scripts": {
"build": "node ../../scripts/esbuild.config.js",
"build:watch": "bash ../../scripts/esbuild.watch.sh",
"bump-version": "bash ../../scripts/bump-version.sh --name=@sliit-foss/service-connector",
"lint": "bash ../../scripts/lint.sh",
"release": "bash ../../scripts/release.sh",
"test": "bash ../../scripts/test/test.sh"
},
"dependencies": {
"@sliit-foss/module-logger": "1.3.1",
"axios": "1.6.0",
"axios-retry": "4.1.0",
"chalk": "4.1.2",
"express-http-context": "1.2.4",
"http-errors": "2.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sliit-foss/npm-catalogue.git"
},
"homepage": "https://github.com/sliit-foss/npm-catalogue/blob/main/packages/service-connector/readme.md",
"keywords": [
"service-connector",
"microservice-connector",
"axios-decorator"
],
"author": "SLIIT FOSS",
"license": "MIT",
"bugs": {
"url": "https://github.com/sliit-foss/npm-catalogue/issues"
}
}
10 changes: 10 additions & 0 deletions packages/service-connector/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import axiosRetry from "axios-retry";
import chalk from "chalk";
import context from "express-http-context";
import createError from "http-errors";
Expand Down Expand Up @@ -76,6 +77,15 @@ const serviceConnector = ({ service, headerIntercepts, loggable, logs = true, ..
if (!res) return response;
return res.status(response.status).json(response.data);
};
instance.enableRetry = (options) => {
axiosRetry(instance, {
retries: 3,
retryCondition: (err) => err.code === "ECONNABORTED" || err.response?.status >= 500,
retryDelay: (retryCount) => retryCount * 1000,
...options
});
return instance;
};
return instance;
};

Expand Down
8 changes: 8 additions & 0 deletions packages/service-connector/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InternalAxiosRequestConfig, AxiosResponse, CreateAxiosDefaults, AxiosInstance } from "axios";
import { IAxiosRetryConfig } from "axios-retry";

declare module "axios" {
interface AxiosInstance {
Expand All @@ -20,6 +21,13 @@ declare module "axios" {
* const data = await instance.get('https://example.com/users').then(resolve);
*/
resolve: (response: AxiosResponse) => any;
/**
* @description Enables request retrying. Uses `axios-retry` under the hood
* @param config The axios retry config
* @example
* instance.enableRetry()
*/
enableRetry: (config?: IAxiosRetryConfig) => AxiosInstance;
}
}

Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 91cc514

Please sign in to comment.