@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/README.md CHANGED
@@ -8,10 +8,10 @@ RequestManager is a JavaScript library designed to manage and regulate HTTP requ
8
8
  ## Key Features
9
9
 
10
10
  - **Universal Compatibility**: Dedicated helpers for fetch, axios, ajax-style clients (jQuery / Ext.Ajax), and XMLHttpRequest — plus a low-level `request()` escape hatch
11
- - **Automatic Cancellation**: When a request is repeated with the same identifier, the previous request is automatically cancelled. The ID comes from the cleaned URL, or from `options.requestKey`
11
+ - **Automatic Cancellation**: When a request is repeated with the same identifier, the previous request is automatically cancelled. The ID comes from the HTTP method + cleaned URL, or from `options.requestKey`
12
12
  - **Prioritizes Recent Requests**: Only the most recent request for a given ID is kept; older ones are aborted
13
13
  - **Simple API**: Prefer the helper that matches your HTTP client; wire cancel yourself only with `request()`
14
- - **Adapt to your requirements**: Shared options (`requestKey`, `noCancel`, `includeQuery`, ...) across helpers
14
+ - **Adapt to your requirements**: Shared options (`requestKey`, `noCancel`, `includeQuery`, `includeMethod`, ...) across helpers
15
15
  - **TypeScript Support**: Full TypeScript type definitions included
16
16
  - **Multiple Module Formats**: ESM, CommonJS, and UMD builds available
17
17
 
@@ -71,7 +71,7 @@ Pick the **dedicated helper** for your HTTP client. Use `request()` only when no
71
71
  | Client | Use this | Why |
72
72
  | --------------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------ |
73
73
  | `fetch` | **`fetch(url, options)`** | Creates the AbortSignal and passes it to `fetch` for you |
74
- | `axios` | **`axios(url, options, axiosInstance?)`** | Creates axios `CancelToken` and wires cancel for you |
74
+ | `axios` | **`axios(url, options, axiosInstance?)`** | Creates an `AbortSignal` and wires cancel for you (axios ≥ 0.22) |
75
75
  | jQuery `.ajax`, Ext.Ajax, similar | **`ajax(ajaxFunction, url, options)`** | Runs your ajax function, then wires abort for you (`req.abort`, `Ext.Ajax.abort(req)`, or `xhr.abort`) |
76
76
  | Raw `XMLHttpRequest` | **`xhr(url, options)`** | Owns open/send and abort lifecycle |
77
77
  | Custom / already-started Promise | **`request(url, promiseOrFn, options)`** | Escape hatch — **you** must pass `signal` / `cancelToken` / `addAbortListener` |
@@ -165,8 +165,9 @@ import RequestManager from '@enegalan/request-manager';
165
165
 
166
166
  const requestManager = new RequestManager();
167
167
 
168
- // By default, requests with the same URL (cleaned) will cancel previous ones
169
- // The URL is automatically cleaned (protocol and query params removed) to generate the request ID
168
+ // By default, requests with the same method + URL (cleaned) will cancel previous ones
169
+ // The URL is automatically cleaned (protocol and query params removed) and the HTTP
170
+ // method is prepended to generate the request ID (e.g. request_GET_/api/search)
170
171
  requestManager.fetch('/api/search?q=test').catch((error) => {
171
172
  console.log('First request cancelled:', error.message);
172
173
  });
@@ -384,13 +385,14 @@ Low-level entry point. Tracks the call by ID and cancels the previous one with t
384
385
  - `requestKey` (string|number|Function, optional): Key to identify duplicate requests. If provided, requests with the same key will share the same ID and cancel previous ones. If not provided, the cleaned URL is used as the key. Can be a string, number, or function that returns a key.
385
386
  - `noCancel` (boolean): If true, this request will not cancel previous requests with the same ID, allowing concurrent requests. Useful for lazy loading scenarios where multiple requests should execute in parallel.
386
387
  - `includeQuery` (boolean): If true, keeps the query string when generating the request ID from the URL.
388
+ - `includeMethod` (boolean): If true (default), the HTTP method is part of the URL-based request ID
387
389
 
388
390
  > [!TIP]
389
391
  > When `requestPromise` is a Function, you can pass custom properties in `options`. These will be accessible inside the callback via the `{ options }` parameter.
390
392
 
391
393
  **Returns:** Promise that resolves/rejects based on the most recent request
392
394
 
393
- **Note:** The request ID is automatically generated from the cleaned URL (protocol and hash removed; query params removed unless `includeQuery` is true) unless `requestKey` is specified. When `noCancel` is true, a unique ID is generated for each request to prevent cancellation. When `requestPromise` is a Function, it receives `{ options }` where `options` contains the `signal` (AbortSignal) and any other fetch options.
395
+ **Note:** The request ID is automatically generated from the cleaned URL (protocol and hash removed; query params removed unless `includeQuery` is true; HTTP method included unless `includeMethod` is false) unless `requestKey` is specified. When `noCancel` is true, a unique ID is generated for each request to prevent cancellation. When `requestPromise` is a Function, it receives `{ options }` where `options` contains the `signal` (AbortSignal) and any other fetch options.
394
396
 
395
397
  ### `fetch(url, options)`
396
398
 
@@ -405,6 +407,8 @@ Executes an HTTP request using fetch, cancelling any previous request with the s
405
407
  - `cancelToken` (Function|Object): Cancel token or cancel function for other libraries
406
408
  - `noCancel` (boolean): If true, this request will not cancel previous requests with the same ID, allowing concurrent requests
407
409
  - `includeQuery` (boolean): If true, keeps the query string in the URL-based request ID
410
+ - `includeMethod` (boolean): If true (default), the HTTP method is part of the URL-based request ID
411
+
408
412
  - Any other properties are passed as fetch options (method, headers, body, etc.)
409
413
 
410
414
  **Returns:** Promise that resolves/rejects based on the most recent request
@@ -422,12 +426,14 @@ Executes an HTTP request using axios, cancelling any previous request with the s
422
426
  - `requestKey` (string|number|Function, optional): Key to identify duplicate requests. If provided, requests with the same key will cancel previous ones. Can be a string, number, or function that returns a key.
423
427
  - `noCancel` (boolean): If true, this request will not cancel previous requests with the same ID, allowing concurrent requests
424
428
  - `includeQuery` (boolean): If true, keeps the query string in the URL-based request ID
429
+ - `includeMethod` (boolean): If true (default), the HTTP method is part of the URL-based request ID
430
+
425
431
  - Any other properties are passed as axios options (method, headers, params, data, etc.)
426
432
  - `axiosInstance` (Object, optional): Custom axios instance to use. If not provided, uses the global `axios` object.
427
433
 
428
434
  **Returns:** Promise that resolves/rejects based on the most recent request
429
435
 
430
- **Note:** This method automatically creates a CancelToken for axios cancellation. The request ID is automatically generated from the cleaned URL unless `requestKey` is specified. When `noCancel` is true, a unique ID is generated for each request.
436
+ **Note:** This method automatically creates an AbortController and passes its `signal` in the axios config, so **cancellation requires axios >= 0.22.0** (the first version supporting `AbortSignal`). Older versions silently ignore the signal; a console warning is emitted when one is detected. The request ID is automatically generated from the cleaned URL unless `requestKey` is specified. When `noCancel` is true, a unique ID is generated for each request.
431
437
 
432
438
  **Example:**
433
439
 
@@ -482,6 +488,8 @@ Calls `ajaxFunction({ url, ...options })`, then auto-wires cancel by inspecting
482
488
  - `verbose` (boolean): If true, cancellation rejects with a message that includes the request id
483
489
  - `noCancel` (boolean): If true, this request will not cancel previous requests with the same ID, allowing concurrent requests
484
490
  - `includeQuery` (boolean): If true, keeps the query string in the URL-based request ID
491
+ - `includeMethod` (boolean): If true (default), the HTTP method is part of the URL-based request ID
492
+
485
493
  - Any other properties are passed to the ajax method function
486
494
 
487
495
  **Returns:** Promise that resolves/rejects based on the most recent request
@@ -522,6 +530,7 @@ Executes an HTTP request using XMLHttpRequest, cancelling any previous request w
522
530
  - `verbose` (boolean): If true, cancellation rejects with a message that includes the request id
523
531
  - `noCancel` (boolean): If true, this request will not cancel previous requests with the same ID, allowing concurrent requests
524
532
  - `includeQuery` (boolean): If true, keeps the query string in the URL-based request ID
533
+ - `includeMethod` (boolean): If true (default), the HTTP method is part of the URL-based request ID
525
534
 
526
535
  **Returns:** Promise that resolves/rejects based on the most recent request. The resolved value is an object with:
527
536
 
@@ -565,6 +574,7 @@ Returns the request ID that RequestManager assigns for a URL and options.
565
574
  - `options` (Object, optional): Same options used for the request
566
575
  - `requestKey` (string|number|Function, optional): Key override
567
576
  - `includeQuery` (boolean, optional): Keep query string in the URL-based ID
577
+ - `includeMethod` (boolean, optional): If false, the HTTP method is not part of the URL-based ID (default true)
568
578
  - `noCancel` (boolean, optional): If true, returns a **new** unique ID (will not match an already in-flight `noCancel` request)
569
579
 
570
580
  **Returns:** `string` — the request identifier