@enegalan/request-manager 1.0.2 → 1.0.10

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/index.d.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  export type RequestKeyFunction = () => string | number | null | undefined;
10
10
 
11
11
  /**
12
- * Request key type - can be a string, number, or a function that returns a dynamic key
12
+ * Request key type - can be a plain value, or a function that returns a dynamic key
13
13
  */
14
14
  export type RequestKey = string | number | RequestKeyFunction;
15
15
 
@@ -19,55 +19,97 @@ export type RequestKey = string | number | RequestKeyFunction;
19
19
  export type CancelToken = (() => void) | { cancel: () => void };
20
20
 
21
21
  /**
22
- * Manager options passed to the RequestManager constructor
22
+ * Active request data interface stored by the RequestManager
23
23
  */
24
- export interface ManagerOptions {
24
+ export interface ActiveRequest<T = any> {
25
25
  /**
26
- * If true, cancellation errors will include messages globally for all requests
26
+ * The original request: a promise or a request object (XHR, Ext.Ajax, etc.)
27
+ */
28
+ promise: any;
29
+
30
+ /**
31
+ * AbortController instance used to abort the request
32
+ */
33
+ abortController: AbortController;
34
+
35
+ /**
36
+ * Cancel token (axios compatibility)
37
+ */
38
+ cancelToken: CancelToken | null;
39
+
40
+ /**
41
+ * Function to resolve the wrapper promise
42
+ */
43
+ resolveWrapper: (value: T | PromiseLike<T>) => void;
44
+
45
+ /**
46
+ * Function to reject the wrapper promise
47
+ */
48
+ rejectWrapper: (reason?: any) => void;
49
+
50
+ /**
51
+ * Whether the request has been cancelled
52
+ */
53
+ isCancelled: boolean;
54
+ }
55
+
56
+ /**
57
+ * RequestManager global options interface
58
+ */
59
+ export interface Options {
60
+ /**
61
+ * If true, cancellation rejects the wrapper promise with a message that includes the request identifier.
62
+ * If false, cancellation is silent: the wrapper promise does not settle and nothing is logged.
63
+ * @default false
27
64
  */
28
65
  verbose?: boolean;
29
66
  }
30
67
 
31
68
  /**
32
- * Base request options shared by all request methods
69
+ * Base request options interface shared by all request methods
33
70
  */
34
71
  export interface BaseRequestOptions {
35
72
  /**
36
- * Key to identify duplicate requests.
37
- * If provided, requests with the same key will cancel previous ones.
38
- * Can be a string, number, or function that returns a key.
73
+ * @type {RequestKey}
39
74
  */
40
75
  requestKey?: RequestKey;
41
76
 
42
77
  /**
43
- * AbortController instance (created automatically if not provided)
78
+ * @type {AbortController}
44
79
  */
45
80
  abortController?: AbortController;
46
81
 
47
82
  /**
48
- * Cancel token (axios compatibility)
83
+ * @type {CancelToken}
49
84
  */
50
85
  cancelToken?: CancelToken;
51
86
 
52
87
  /**
53
- * If true, this request will not cancel previous requests with the same ID,
88
+ * If true, this request will not cancel previous requests with the same identifier,
54
89
  * allowing concurrent requests.
90
+ * @default false
55
91
  */
56
92
  noCancel?: boolean;
93
+
94
+ /**
95
+ * If true, the query string is kept when generating the request identifier from the URL.
96
+ * @default false
97
+ */
98
+ includeQuery?: boolean;
57
99
  }
58
100
 
59
101
  /**
60
- * Options for the request() method
102
+ * Options interface for the request() method
61
103
  */
62
104
  export interface RequestOptions extends BaseRequestOptions, Omit<RequestInit, 'signal'> {}
63
105
 
64
106
  /**
65
- * Options for the fetch() method
107
+ * Options interface for the fetch() method
66
108
  */
67
109
  export interface FetchOptions extends BaseRequestOptions, Omit<RequestInit, 'signal'> {}
68
110
 
69
111
  /**
70
- * Options for the axios() method
112
+ * Options interface for the axios() method
71
113
  */
72
114
  export interface AxiosRequestOptions extends BaseRequestOptions {
73
115
  /**
@@ -117,11 +159,11 @@ export interface AxiosRequestOptions extends BaseRequestOptions {
117
159
  }
118
160
 
119
161
  /**
120
- * Options for the xhr() method
162
+ * Options interface for the xhr() method
121
163
  */
122
164
  export interface XhrOptions extends BaseRequestOptions {
123
165
  /**
124
- * HTTP method (GET, POST, PUT, DELETE, etc.). Defaults to 'GET'.
166
+ * HTTP method
125
167
  */
126
168
  method?: string;
127
169
 
@@ -136,7 +178,8 @@ export interface XhrOptions extends BaseRequestOptions {
136
178
  body?: string | FormData | Blob | ArrayBuffer | null;
137
179
 
138
180
  /**
139
- * Response type ('text', 'json', 'blob', 'arraybuffer', 'document'). Defaults to 'text'.
181
+ * Response type
182
+ * @default 'text'
140
183
  */
141
184
  responseType?: XMLHttpRequestResponseType;
142
185
 
@@ -152,7 +195,7 @@ export interface XhrOptions extends BaseRequestOptions {
152
195
  }
153
196
 
154
197
  /**
155
- * Response from the xhr() method
198
+ * Response interface from the xhr() method
156
199
  */
157
200
  export interface XhrResponse<T = any> {
158
201
  /**
@@ -182,7 +225,7 @@ export interface XhrResponse<T = any> {
182
225
  }
183
226
 
184
227
  /**
185
- * Options passed to the request function callback
228
+ * Options interface passed to the request function callback
186
229
  */
187
230
  export interface RequestFunctionOptions {
188
231
  /**
@@ -192,17 +235,19 @@ export interface RequestFunctionOptions {
192
235
  }
193
236
 
194
237
  /**
195
- * A function that receives options and returns a Promise
238
+ * Function type that receives options and returns a Promise
196
239
  */
197
240
  export type RequestFunction<T = any> = (params: RequestFunctionOptions) => Promise<T>;
198
241
 
199
242
  /**
200
- * Ajax method function type
243
+ * Ajax function type
201
244
  */
202
- export type AjaxMethod<T = any> = (params: { url: string; signal?: AbortSignal } & Record<string, any>) => Promise<T> & { abort?: () => void };
245
+ export type AjaxFunction<T = any> = (
246
+ params: { url: string; signal?: AbortSignal } & Record<string, any>
247
+ ) => Promise<T> & { abort?: () => void };
203
248
 
204
249
  /**
205
- * Axios instance interface (minimal definition for compatibility)
250
+ * Axios instance interface
206
251
  */
207
252
  export interface AxiosInstance {
208
253
  get<T = any>(url: string, config?: any): Promise<T>;
@@ -225,7 +270,7 @@ export interface AxiosInstance {
225
270
  }
226
271
 
227
272
  /**
228
- * Axios static interface (for global axios)
273
+ * Axios static interface
229
274
  */
230
275
  export interface AxiosStatic extends AxiosInstance {
231
276
  create(config?: any): AxiosInstance;
@@ -243,51 +288,43 @@ declare class RequestManager {
243
288
  /**
244
289
  * Map to store active requests by their unique identifier
245
290
  */
246
- activeRequests: Map<string, any>;
247
-
248
- /**
249
- * Verbose mode: if true, cancellation errors will include messages
250
- */
251
- verbose: boolean;
291
+ activeRequests: Map<string, ActiveRequest>;
252
292
 
253
293
  /**
254
294
  * Manager options that were passed to the constructor
255
295
  */
256
- managerOptions: ManagerOptions;
296
+ options: Options;
257
297
 
258
298
  /**
259
- * Options for the current request (flushed after each request)
260
- */
261
- options: Record<string, any>;
262
-
263
- /**
264
- * AbortController instance for the current request
299
+ * One-shot AbortController handoff from getAbortController()/getSignal().
300
+ * Consumed by the next request that does not pass options.abortController.
265
301
  */
266
302
  abortController: AbortController | null;
267
303
 
268
304
  /**
269
305
  * Creates a new RequestManager instance
270
- * @param managerOptions - Configuration options for the manager
306
+ * @param options - Configuration options for the manager
271
307
  */
272
- constructor(managerOptions?: ManagerOptions);
308
+ constructor(options?: Options);
273
309
 
274
310
  /**
275
311
  * Sets the manager options
276
312
  * @param options - The manager options to set
277
313
  */
278
- setOptions(options: ManagerOptions): void;
314
+ setOptions(options: Options): void;
279
315
 
280
316
  /**
281
317
  * Gets the manager options
282
318
  * @returns The manager options
283
319
  */
284
- getOptions(): ManagerOptions;
320
+ getOptions(): Options;
285
321
 
286
322
  /**
287
- * Creates an AbortController and returns its signal.
288
- * The AbortController is stored internally and will be used by the next request() call.
323
+ * Creates a new AbortController and returns its signal for the next request()
324
+ * (one getSignal one request). Do not use for parallel requests; use fetch(),
325
+ * axios(), or request(url, ({ options }) => ...) instead — they create their own signal.
289
326
  * @returns The signal from a new AbortController
290
- *
327
+ *
291
328
  * @example
292
329
  * const signal = requestManager.getSignal();
293
330
  * requestManager.request('/api/users', fetch('/api/users', { signal }));
@@ -295,28 +332,28 @@ declare class RequestManager {
295
332
  getSignal(): AbortSignal;
296
333
 
297
334
  /**
298
- * Gets the current AbortController instance.
299
- * Creates a new AbortController if none exists or if the current one is aborted.
300
- * @returns The current AbortController instance
335
+ * Creates a new AbortController for the next request handoff.
336
+ * Always returns a fresh controller (never reuses one from another in-flight request).
337
+ * @returns A new AbortController instance
301
338
  */
302
339
  getAbortController(): AbortController;
303
340
 
304
341
  /**
305
342
  * Executes an HTTP request, cancelling any previous request with the same identifier.
306
- *
343
+ *
307
344
  * @param url - The URL to request
308
345
  * @param requestPromise - The request promise, function that returns a promise, or URL string
309
346
  * @param options - Optional configuration
310
347
  * @returns A Promise that resolves/rejects based on the most recent request
311
- *
348
+ *
312
349
  * @example
313
350
  * // Request with Promise
314
351
  * requestManager.request('/api/users', axios.get('/api/users'));
315
- *
352
+ *
316
353
  * @example
317
354
  * // Request with Function
318
355
  * requestManager.request('/api/users', ({ options }) => fetch('/api/users', options));
319
- *
356
+ *
320
357
  * @example
321
358
  * // Request with noCancel for concurrent requests
322
359
  * requestManager.request('/api/lazy', fetch('/api/lazy'), { noCancel: true });
@@ -329,15 +366,15 @@ declare class RequestManager {
329
366
 
330
367
  /**
331
368
  * Executes an HTTP request using fetch, cancelling any previous request with the same identifier.
332
- *
369
+ *
333
370
  * @param url - The URL to fetch
334
371
  * @param options - Optional configuration (fetch options + RequestManager options)
335
372
  * @returns A Promise that resolves/rejects based on the most recent request
336
- *
373
+ *
337
374
  * @example
338
375
  * // Simple GET request
339
376
  * requestManager.fetch('/api/users');
340
- *
377
+ *
341
378
  * @example
342
379
  * // POST request with options
343
380
  * requestManager.fetch('/api/users', {
@@ -350,21 +387,21 @@ declare class RequestManager {
350
387
 
351
388
  /**
352
389
  * Executes an HTTP request using axios, cancelling any previous request with the same identifier.
353
- *
390
+ *
354
391
  * @param url - The URL to request
355
392
  * @param options - Optional configuration (axios options + RequestManager options)
356
393
  * @param axiosInstance - Optional axios instance to use. If not provided, uses global axios.
357
394
  * @returns A Promise that resolves/rejects based on the most recent request
358
- *
395
+ *
359
396
  * @example
360
397
  * // Simple GET request (uses global axios)
361
398
  * requestManager.axios('/api/users');
362
- *
399
+ *
363
400
  * @example
364
401
  * // With custom axios instance
365
402
  * const myAxios = axios.create({ baseURL: 'https://api.example.com' });
366
403
  * requestManager.axios('/users', {}, myAxios);
367
- *
404
+ *
368
405
  * @example
369
406
  * // POST request with options
370
407
  * requestManager.axios('/api/users', {
@@ -380,12 +417,12 @@ declare class RequestManager {
380
417
 
381
418
  /**
382
419
  * Executes an HTTP request using a custom ajax method, cancelling any previous request with the same identifier.
383
- *
384
- * @param ajaxMethod - A function that receives { url, ...options } and returns a Promise
420
+ *
421
+ * @param ajaxFunction - A function that receives { url, ...options } and returns a Promise
385
422
  * @param url - The URL to request
386
423
  * @param options - Optional configuration
387
424
  * @returns A Promise that resolves/rejects based on the most recent request
388
- *
425
+ *
389
426
  * @example
390
427
  * // Using with jQuery.ajax
391
428
  * requestManager.ajax(
@@ -395,22 +432,22 @@ declare class RequestManager {
395
432
  * );
396
433
  */
397
434
  ajax<T = any>(
398
- ajaxMethod: AjaxMethod<T>,
435
+ ajaxFunction: AjaxFunction<T>,
399
436
  url: string,
400
437
  options?: BaseRequestOptions & Record<string, any>
401
438
  ): Promise<T>;
402
439
 
403
440
  /**
404
441
  * Executes an HTTP request using XMLHttpRequest, cancelling any previous request with the same identifier.
405
- *
442
+ *
406
443
  * @param url - The URL to request
407
444
  * @param options - Optional configuration
408
445
  * @returns A Promise that resolves/rejects based on the most recent request
409
- *
446
+ *
410
447
  * @example
411
448
  * // Simple GET request
412
449
  * requestManager.xhr('/api/users');
413
- *
450
+ *
414
451
  * @example
415
452
  * // POST request with options
416
453
  * requestManager.xhr('/api/users', {
@@ -421,9 +458,21 @@ declare class RequestManager {
421
458
  */
422
459
  xhr<T = any>(url: string, options?: XhrOptions): Promise<XhrResponse<T>>;
423
460
 
461
+ /**
462
+ * Returns the request ID that RequestManager would assign for a URL and options.
463
+ *
464
+ * Note: with noCancel => true each call generates a new unique ID, so the value
465
+ * returned here will not match an already in-flight noCancel request.
466
+ *
467
+ * @param url - The URL used when starting the request
468
+ * @param options - Same options used for the request
469
+ * @returns The request identifier
470
+ */
471
+ getRequestId(url: string, options?: Pick<BaseRequestOptions, 'requestKey' | 'includeQuery' | 'noCancel'>): string;
472
+
424
473
  /**
425
474
  * Cancels a specific request by its identifier.
426
- *
475
+ *
427
476
  * @param requestId - The unique identifier of the request to cancel
428
477
  * @returns True if the request was found and cancelled, false otherwise
429
478
  */
@@ -432,7 +481,7 @@ declare class RequestManager {
432
481
  /**
433
482
  * Link abort signal with HTTP client abort method.
434
483
  * Useful for custom HTTP clients that only support the abort method to cancel requests.
435
- *
484
+ *
436
485
  * @param abortMethod - The abort method to call when the signal is aborted
437
486
  * @param signal - The signal to listen to
438
487
  */
@@ -440,14 +489,14 @@ declare class RequestManager {
440
489
 
441
490
  /**
442
491
  * Cancels all active requests.
443
- *
492
+ *
444
493
  * @returns The number of requests that were cancelled
445
494
  */
446
495
  cancelAll(): number;
447
496
 
448
497
  /**
449
498
  * Checks if a request with the given identifier is currently active.
450
- *
499
+ *
451
500
  * @param requestId - The unique identifier to check
452
501
  * @returns True if the request is active, false otherwise
453
502
  */
@@ -455,7 +504,7 @@ declare class RequestManager {
455
504
 
456
505
  /**
457
506
  * Gets the number of active requests.
458
- *
507
+ *
459
508
  * @returns The number of currently active requests
460
509
  */
461
510
  getActiveCount(): number;