@aligent/microservice-util-lib 1.2.0 → 1.2.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aligent/microservice-util-lib",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "A set of utility functions for Aligent Microservices",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -84,6 +84,17 @@ function getRetryDelayFn(config) {
84
84
  function shouldRetryOnStatus(status, retryOn) {
85
85
  return retryOn.includes(status);
86
86
  }
87
+ /**
88
+ * Checks the response status and throw error if it's not "ok".
89
+ *
90
+ * @param {Response} response - The HTTP response object.
91
+ * @throws {Error} When the response is not an "ok" response.
92
+ */
93
+ function throwErrorIfNotOkResponse(response) {
94
+ if (!response.ok) {
95
+ throw new Error(`${response.status}: ${response.statusText}`);
96
+ }
97
+ }
87
98
  /**
88
99
  * This middleware implements retry logic with support for:
89
100
  * - Configurable number of retry attempts
@@ -147,7 +158,8 @@ function retryMiddleware(config) {
147
158
  const context = { attempt: 1, request, response, error: null };
148
159
  // If retryOn is specified, only use that list
149
160
  if (config?.retryOn && config.retryOn.length > 0) {
150
- if (!shouldRetryOnStatus(response.status, config.retryOn)) {
161
+ if (!shouldRetryOnStatus(context.response.status, config.retryOn)) {
162
+ throwErrorIfNotOkResponse(context.response);
151
163
  return context.response;
152
164
  }
153
165
  return await performRetries(normalisedConfig, context);
@@ -155,6 +167,7 @@ function retryMiddleware(config) {
155
167
  // Otherwise, check if we should retry based on retry condition
156
168
  const shouldRetry = await normalisedConfig.retryCondition(context, normalisedConfig.idempotentOnly);
157
169
  if (!shouldRetry) {
170
+ throwErrorIfNotOkResponse(context.response);
158
171
  return context.response;
159
172
  }
160
173
  return await performRetries(normalisedConfig, context);
@@ -201,18 +214,18 @@ async function performRetries(config, context) {
201
214
  continue;
202
215
  }
203
216
  if (config.retryOn && !shouldRetryOnStatus(response?.status, config.retryOn)) {
217
+ throwErrorIfNotOkResponse(response);
204
218
  return response;
205
219
  }
206
220
  const shouldRetry = await config.retryCondition(context, config.idempotentOnly);
207
221
  if (!shouldRetry) {
222
+ throwErrorIfNotOkResponse(response);
208
223
  return response;
209
224
  }
210
225
  } while (attempt <= maxRetries);
211
226
  if (!response) {
212
227
  throw context.error;
213
228
  }
214
- if (!response.ok) {
215
- throw new Error(`${response.status}: ${response.statusText}`);
216
- }
229
+ throwErrorIfNotOkResponse(response);
217
230
  return response;
218
231
  }