@aligent/microservice-util-lib 1.2.0 → 1.2.2

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.2",
4
4
  "description": "A set of utility functions for Aligent Microservices",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -64,27 +64,18 @@ function combineUrlAndPathParams(url, pathParams) {
64
64
  *
65
65
  * @param {string} baseURL - The base URL.
66
66
  * @param {string} url - The URL to process.
67
- * @returns {{ baseUri: string, searchParams: URLSearchParams | null }} The processed URL and its search parameters.
67
+ * @returns The processed URL.
68
68
  */
69
- function handleOAuthUrl(baseURL, url) {
69
+ function getOAuthUrl(baseURL, url) {
70
70
  const oauthUrl = new URL(!baseURL || isAbsoluteURL(url) ? url : combineURLs(baseURL, url));
71
- let searchParams = null;
72
- // Query parameters are hashed as part of params rather than as part of the URL
73
- if (oauthUrl.search) {
74
- searchParams = new URLSearchParams(oauthUrl.search);
75
- oauthUrl.search = '';
76
- }
77
- // Do not include hash in signature
78
- oauthUrl.hash = '';
71
+ oauthUrl.search = ''; // Query parameters are hashed as part of params rather than as part of the URL
72
+ oauthUrl.hash = ''; // Do not include hash in signature
79
73
  // Remove port if it is the default for that protocol
80
74
  if ((oauthUrl.protocol === 'https:' && oauthUrl.port === '443') ||
81
75
  (oauthUrl.protocol === 'http:' && oauthUrl.port === '80')) {
82
76
  oauthUrl.port = '';
83
77
  }
84
- return {
85
- baseUri: oauthUrl.toString(),
86
- searchParams,
87
- };
78
+ return oauthUrl.toString();
88
79
  }
89
80
  /**
90
81
  * Adds a parameter to the list of parameters to sign.
@@ -174,10 +165,6 @@ async function generateOauthParams(request, options, params, config) {
174
165
  if (params.query) {
175
166
  addParamsToSign(paramsToSign, params.query);
176
167
  }
177
- const { baseUri, searchParams } = handleOAuthUrl(options.baseUrl, url);
178
- if (searchParams) {
179
- addParamsToSign(paramsToSign, searchParams);
180
- }
181
168
  const body = await request.text();
182
169
  // If user submit a form, then include form parameters in the
183
170
  // signature as parameters rather than the body hash
@@ -195,7 +182,8 @@ async function generateOauthParams(request, options, params, config) {
195
182
  addParamToSign(paramsToSign, 'oauth_body_hash', bodyHash);
196
183
  }
197
184
  }
198
- oauthParams.oauth_signature = (0, oauth_sign_1.sign)(algorithm, method, baseUri, paramsToSign, consumerSecret, tokenSecret);
185
+ const oauthUrl = getOAuthUrl(options.baseUrl, url);
186
+ oauthParams.oauth_signature = (0, oauth_sign_1.sign)(algorithm, method, oauthUrl, paramsToSign, consumerSecret, tokenSecret);
199
187
  // realm should not be included in the signature calculation
200
188
  // but is optional in the OAuth 1.0 Authorization header
201
189
  // so we need to add it after signing the request
@@ -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
  }