@enegalan/request-manager 1.0.10 → 1.1.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/main.js CHANGED
@@ -96,15 +96,14 @@ class RequestManager {
96
96
  * @returns {Promise} A Promise that resolves/rejects based on the most recent request
97
97
  * @example
98
98
  * // Request with Promise
99
- * requestManager.request('/api/users', axios.get('/api/users', { cancelToken: axios.CancelToken.source().token }));
99
+ * requestManager.request('/api/users', axios.get('/api/users'));
100
100
  * @example
101
101
  * // Request with Function
102
102
  * requestManager.request('/api/users', ({ options }) => fetch('/api/users', { signal: options.signal, ...options }));
103
103
  * @example
104
104
  * // Request with Promise and custom cancellation grouping with requestKey
105
105
  * const options = {
106
- * requestKey: 'get-users',
107
- * cancelToken: axios.CancelToken.source().cancel
106
+ * requestKey: 'get-users'
108
107
  * }
109
108
  * requestManager.request('/api/users', axios.get('/api/users', options), options);
110
109
  */
@@ -165,12 +164,12 @@ class RequestManager {
165
164
  */
166
165
  axios(url, options = {}, axiosInstance = null) {
167
166
  const axiosLib = axiosInstance || axios;
168
- const cancelToken = axiosLib.CancelToken.source();
169
- const requestId = this.getRequestId(url, options);
170
- return this.#_request(requestId, axiosLib({ url, cancelToken: cancelToken.token, ...options }), {
171
- cancelToken: cancelToken,
172
- ...options,
173
- });
167
+ this.#_checkAxiosVersion(axiosLib);
168
+ return this.#_request(
169
+ this.getRequestId(url, options),
170
+ ({ options: requestOptions }) => axiosLib({ url, ...requestOptions }),
171
+ options
172
+ );
174
173
  }
175
174
 
176
175
  /**
@@ -239,14 +238,13 @@ class RequestManager {
239
238
  let response = xhr.response;
240
239
  if (
241
240
  options.responseType === 'json' ||
242
- (xhr.getResponseHeader('Content-Type') &&
243
- xhr.getResponseHeader('Content-Type').includes('application/json'))
241
+ ((!options.responseType || options.responseType === 'text') &&
242
+ xhr.getResponseHeader('Content-Type')?.includes('application/json') &&
243
+ typeof response === 'string')
244
244
  ) {
245
245
  try {
246
- response = JSON.parse(xhr.responseText);
247
- } catch {
248
- response = xhr.responseText;
249
- }
246
+ response = JSON.parse(response);
247
+ } catch {}
250
248
  }
251
249
  resolve({
252
250
  data: response,
@@ -338,7 +336,11 @@ class RequestManager {
338
336
  if (cleanedUrl.includes('://')) cleanedUrl = cleanedUrl.split('://')[1];
339
337
  if (cleanedUrl.includes('#')) cleanedUrl = cleanedUrl.split('#')[0];
340
338
  if (!options.includeQuery && cleanedUrl.includes('?')) cleanedUrl = cleanedUrl.split('?')[0];
341
- return `${prefix}${cleanedUrl}`;
339
+
340
+ const methodPrefix =
341
+ options.includeMethod === false ? '' : `${(options.method || options.type || 'GET').toUpperCase()}_`;
342
+
343
+ return `${prefix}${methodPrefix}${cleanedUrl}`;
342
344
  }
343
345
 
344
346
  /**
@@ -456,6 +458,24 @@ class RequestManager {
456
458
  return requestOptions;
457
459
  }
458
460
 
461
+ /**
462
+ * Warns when the provided axios instance predates 0.22.0, the first version
463
+ * supporting AbortSignal cancellation. Older instances silently ignore
464
+ * options.signal, so duplicate requests would not be cancelled.
465
+ * @param {object} axiosLib - The axios instance about to be used
466
+ * @private
467
+ */
468
+ #_checkAxiosVersion(axiosLib) {
469
+ const version = typeof axiosLib?.VERSION === 'string' ? axiosLib.VERSION : null;
470
+ if (!version) return;
471
+ const [major, minor] = version.split('.').map(Number);
472
+ if (major === 0 && minor < 22) {
473
+ console.warn(
474
+ `[request-manager] axios >= 0.22.0 is required: axios ${version} ignores the AbortSignal used for automatic cancellation. Please upgrade axios.`
475
+ );
476
+ }
477
+ }
478
+
459
479
  /**
460
480
  * Deletes a request from the active requests map and rejects the wrapper promise
461
481
  * @param {string} requestId - The unique identifier of the request
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enegalan/request-manager",
3
- "version": "1.0.10",
3
+ "version": "1.1.1",
4
4
  "description": "JavaScript library for managing and regulating HTTP requests efficiently",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -41,7 +41,9 @@
41
41
  "test:coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
42
42
  },
43
43
  "devDependencies": {
44
+ "@babel/preset-env": "^7.29.7",
44
45
  "@eslint/js": "^10.0.1",
46
+ "@rollup/plugin-babel": "^6.1.0",
45
47
  "@rollup/plugin-terser": "^0.4.4",
46
48
  "eslint": "^10.8.1",
47
49
  "eslint-config-prettier": "^10.1.8",