From 7338f248b677ecdf9a60b77739eefcb82a14e5a4 Mon Sep 17 00:00:00 2001 From: Akalanka Perera Date: Fri, 10 May 2024 03:28:00 +0000 Subject: [PATCH] Feat(service-connector): added support for retrying --- packages/service-connector/package.json | 75 ++++++++++--------- packages/service-connector/src/index.js | 9 +++ packages/service-connector/test/index.test.js | 16 ++++ packages/service-connector/types/index.d.ts | 8 ++ pnpm-lock.yaml | 19 +++++ 5 files changed, 90 insertions(+), 37 deletions(-) diff --git a/packages/service-connector/package.json b/packages/service-connector/package.json index bc47d43..c9b3f5e 100644 --- a/packages/service-connector/package.json +++ b/packages/service-connector/package.json @@ -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" + } +} diff --git a/packages/service-connector/src/index.js b/packages/service-connector/src/index.js index b7277a7..42ddd4f 100644 --- a/packages/service-connector/src/index.js +++ b/packages/service-connector/src/index.js @@ -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"; @@ -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); } ); @@ -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; }; diff --git a/packages/service-connector/test/index.test.js b/packages/service-connector/test/index.test.js index b94f0d0..6f0d31f 100644 --- a/packages/service-connector/test/index.test.js +++ b/packages/service-connector/test/index.test.js @@ -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); + }); +}); diff --git a/packages/service-connector/types/index.d.ts b/packages/service-connector/types/index.d.ts index 536681d..a72e0eb 100644 --- a/packages/service-connector/types/index.d.ts +++ b/packages/service-connector/types/index.d.ts @@ -1,4 +1,5 @@ import { InternalAxiosRequestConfig, AxiosResponse, CreateAxiosDefaults, AxiosInstance } from "axios"; +import { IAxiosRetryConfig } from "axios-retry"; declare module "axios" { interface AxiosInstance { @@ -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; } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 38281c1..0d2a805 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -150,6 +150,9 @@ importers: axios: specifier: 1.6.0 version: 1.6.0 + axios-retry: + specifier: 4.1.0 + version: 4.1.0(axios@1.6.0) chalk: specifier: 4.1.2 version: 4.1.2 @@ -3681,6 +3684,16 @@ packages: engines: { node: ">= 0.4" } dev: true + /axios-retry@4.1.0(axios@1.6.0): + resolution: + { integrity: sha512-svdth4H00yhlsjBbjfLQ/sMLkXqeLxhiFC1nE1JtkN/CIssGxqk0UwTEdrVjwA2gr3yJkAulwvDSIm4z4HyPvg== } + peerDependencies: + axios: 0.x || 1.x + dependencies: + axios: 1.6.0 + is-retry-allowed: 2.2.0 + dev: false + /axios@1.6.0: resolution: { integrity: sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg== } @@ -6116,6 +6129,12 @@ packages: is-unc-path: 1.0.0 dev: false + /is-retry-allowed@2.2.0: + resolution: + { integrity: sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg== } + engines: { node: ">=10" } + dev: false + /is-shared-array-buffer@1.0.2: resolution: { integrity: sha512-sqN2UDu1/0y6uvXyStCOzyhAjCSlHceFoMKJW8W9EU9cvic/QdsZ0kEU93HEy3IUEFZIiH/3w+AH/UQbPHNdhA== }