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 7338f24
Show file tree
Hide file tree
Showing 5 changed files with 90 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"
}
}
9 changes: 9 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 @@ -59,6 +60,7 @@ const serviceConnector = ({ service, headerIntercepts, loggable, logs = true, ..
);
const customError = createError(error.response?.status ?? 500, errorMessage, { response: error.response });
customError.response = error.response;
customError.config = error.response.config;
return Promise.reject(customError);
}
);
Expand All @@ -76,6 +78,13 @@ const serviceConnector = ({ service, headerIntercepts, loggable, logs = true, ..
if (!res) return response;
return res.status(response.status).json(response.data);
};
instance.enableRetry = (options) => {
axiosRetry(instance, {
retryDelay: (retryCount) => retryCount * 1000,
...options
});
return instance;
};
return instance;
};

Expand Down
16 changes: 16 additions & 0 deletions packages/service-connector/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,19 @@ describe("service-connector resolver", () => {
expect(response).toStrictEqual(mockData.data);
});
});

describe("service-connector retry", () => {
test("successful request with retry", async () => {
let retries = 0;
await serviceConnector()
.enableRetry({
retryCondition: () => true,
onRetry: async () => {
retries++;
}
})
.get("https://google.com/123")
.catch(() => {});
expect(retries).toStrictEqual(3);
});
});
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 7338f24

Please sign in to comment.