@enegalan/request-manager 1.0.0
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/LICENSE +21 -0
- package/README.md +608 -0
- package/dist/request-manager.cjs.js +670 -0
- package/dist/request-manager.cjs.js.map +1 -0
- package/dist/request-manager.cjs.min.js +18 -0
- package/dist/request-manager.cjs.min.js.map +1 -0
- package/dist/request-manager.esm.js +665 -0
- package/dist/request-manager.esm.js.map +1 -0
- package/dist/request-manager.esm.min.js +17 -0
- package/dist/request-manager.esm.min.js.map +1 -0
- package/dist/request-manager.js +670 -0
- package/dist/request-manager.js.map +1 -0
- package/dist/request-manager.min.js +17 -0
- package/dist/request-manager.min.js.map +1 -0
- package/dist/request-manager.umd.js +673 -0
- package/dist/request-manager.umd.js.map +1 -0
- package/dist/request-manager.umd.min.js +17 -0
- package/dist/request-manager.umd.min.js.map +1 -0
- package/index.d.ts +471 -0
- package/index.js +2 -0
- package/main.js +662 -0
- package/package.json +72 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RequestManager - TypeScript Type Definitions
|
|
3
|
+
* A library for managing and regulating HTTP requests efficiently.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A function that generates a request key dynamically
|
|
8
|
+
*/
|
|
9
|
+
export type RequestKeyFunction = () => string | number | null | undefined;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Request key type - can be a string, number, or a function that returns a dynamic key
|
|
13
|
+
*/
|
|
14
|
+
export type RequestKey = string | number | RequestKeyFunction;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Cancel token type (axios compatibility) - can be a function or an object with a cancel method
|
|
18
|
+
*/
|
|
19
|
+
export type CancelToken = (() => void) | { cancel: () => void };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Manager options passed to the RequestManager constructor
|
|
23
|
+
*/
|
|
24
|
+
export interface ManagerOptions {
|
|
25
|
+
/**
|
|
26
|
+
* If true, cancellation errors will include messages globally for all requests
|
|
27
|
+
*/
|
|
28
|
+
verbose?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Base request options shared by all request methods
|
|
33
|
+
*/
|
|
34
|
+
export interface BaseRequestOptions {
|
|
35
|
+
/**
|
|
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.
|
|
39
|
+
*/
|
|
40
|
+
requestKey?: RequestKey;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* AbortController instance (created automatically if not provided)
|
|
44
|
+
*/
|
|
45
|
+
abortController?: AbortController;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Cancel token (axios compatibility)
|
|
49
|
+
*/
|
|
50
|
+
cancelToken?: CancelToken;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* If true, this request will not cancel previous requests with the same ID,
|
|
54
|
+
* allowing concurrent requests.
|
|
55
|
+
*/
|
|
56
|
+
noCancel?: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Options for the request() method
|
|
61
|
+
*/
|
|
62
|
+
export interface RequestOptions extends BaseRequestOptions, Omit<RequestInit, 'signal'> {}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Options for the fetch() method
|
|
66
|
+
*/
|
|
67
|
+
export interface FetchOptions extends BaseRequestOptions, Omit<RequestInit, 'signal'> {}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Options for the axios() method
|
|
71
|
+
*/
|
|
72
|
+
export interface AxiosRequestOptions extends BaseRequestOptions {
|
|
73
|
+
/**
|
|
74
|
+
* HTTP method
|
|
75
|
+
*/
|
|
76
|
+
method?: string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Request headers
|
|
80
|
+
*/
|
|
81
|
+
headers?: Record<string, string>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* URL parameters
|
|
85
|
+
*/
|
|
86
|
+
params?: Record<string, any>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Request body data
|
|
90
|
+
*/
|
|
91
|
+
data?: any;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Base URL for the request
|
|
95
|
+
*/
|
|
96
|
+
baseURL?: string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Request timeout in milliseconds
|
|
100
|
+
*/
|
|
101
|
+
timeout?: number;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Whether to send credentials with the request
|
|
105
|
+
*/
|
|
106
|
+
withCredentials?: boolean;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Response type
|
|
110
|
+
*/
|
|
111
|
+
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream';
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Additional axios options
|
|
115
|
+
*/
|
|
116
|
+
[key: string]: any;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Options for the xhr() method
|
|
121
|
+
*/
|
|
122
|
+
export interface XhrOptions extends BaseRequestOptions {
|
|
123
|
+
/**
|
|
124
|
+
* HTTP method (GET, POST, PUT, DELETE, etc.). Defaults to 'GET'.
|
|
125
|
+
*/
|
|
126
|
+
method?: string;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Headers object to set on the request
|
|
130
|
+
*/
|
|
131
|
+
headers?: Record<string, string>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Request body
|
|
135
|
+
*/
|
|
136
|
+
body?: string | FormData | Blob | ArrayBuffer | null;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Response type ('text', 'json', 'blob', 'arraybuffer', 'document'). Defaults to 'text'.
|
|
140
|
+
*/
|
|
141
|
+
responseType?: XMLHttpRequestResponseType;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Whether to send credentials with the request
|
|
145
|
+
*/
|
|
146
|
+
withCredentials?: boolean;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Request timeout in milliseconds
|
|
150
|
+
*/
|
|
151
|
+
timeout?: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Response from the xhr() method
|
|
156
|
+
*/
|
|
157
|
+
export interface XhrResponse<T = any> {
|
|
158
|
+
/**
|
|
159
|
+
* The response data (automatically parsed as JSON if Content-Type is application/json)
|
|
160
|
+
*/
|
|
161
|
+
data: T;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* HTTP status code
|
|
165
|
+
*/
|
|
166
|
+
status: number;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* HTTP status text
|
|
170
|
+
*/
|
|
171
|
+
statusText: string;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Response headers string
|
|
175
|
+
*/
|
|
176
|
+
headers: string;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The XMLHttpRequest instance
|
|
180
|
+
*/
|
|
181
|
+
xhr: XMLHttpRequest;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Options passed to the request function callback
|
|
186
|
+
*/
|
|
187
|
+
export interface RequestFunctionOptions {
|
|
188
|
+
/**
|
|
189
|
+
* The prepared fetch options including the abort signal
|
|
190
|
+
*/
|
|
191
|
+
options: RequestInit & { signal: AbortSignal };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A function that receives options and returns a Promise
|
|
196
|
+
*/
|
|
197
|
+
export type RequestFunction<T = any> = (params: RequestFunctionOptions) => Promise<T>;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Ajax method function type
|
|
201
|
+
*/
|
|
202
|
+
export type AjaxMethod<T = any> = (params: { url: string; signal?: AbortSignal } & Record<string, any>) => Promise<T> & { abort?: () => void };
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Axios instance interface (minimal definition for compatibility)
|
|
206
|
+
*/
|
|
207
|
+
export interface AxiosInstance {
|
|
208
|
+
get<T = any>(url: string, config?: any): Promise<T>;
|
|
209
|
+
post<T = any>(url: string, data?: any, config?: any): Promise<T>;
|
|
210
|
+
put<T = any>(url: string, data?: any, config?: any): Promise<T>;
|
|
211
|
+
delete<T = any>(url: string, config?: any): Promise<T>;
|
|
212
|
+
patch<T = any>(url: string, data?: any, config?: any): Promise<T>;
|
|
213
|
+
request<T = any>(config: any): Promise<T>;
|
|
214
|
+
head<T = any>(url: string, config?: any): Promise<T>;
|
|
215
|
+
options<T = any>(url: string, config?: any): Promise<T>;
|
|
216
|
+
postForm<T = any>(url: string, data?: any, config?: any): Promise<T>;
|
|
217
|
+
putForm<T = any>(url: string, data?: any, config?: any): Promise<T>;
|
|
218
|
+
patchForm<T = any>(url: string, data?: any, config?: any): Promise<T>;
|
|
219
|
+
CancelToken: {
|
|
220
|
+
source(): {
|
|
221
|
+
token: any;
|
|
222
|
+
cancel: (message?: string) => void;
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Axios static interface (for global axios)
|
|
229
|
+
*/
|
|
230
|
+
export interface AxiosStatic extends AxiosInstance {
|
|
231
|
+
create(config?: any): AxiosInstance;
|
|
232
|
+
isCancel(value: any): boolean;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* RequestManager - A library for managing and regulating HTTP requests efficiently.
|
|
237
|
+
*
|
|
238
|
+
* This library allows you to manage HTTP requests from any library (ajax, Ext.Ajax, axios, fetch, etc.)
|
|
239
|
+
* by accepting Promises as parameters. When a request is repeated with the same identifier,
|
|
240
|
+
* the previous request is automatically cancelled and the new one is executed.
|
|
241
|
+
*/
|
|
242
|
+
declare class RequestManager {
|
|
243
|
+
/**
|
|
244
|
+
* Map to store active requests by their unique identifier
|
|
245
|
+
*/
|
|
246
|
+
activeRequests: Map<string, any>;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Verbose mode: if true, cancellation errors will include messages
|
|
250
|
+
*/
|
|
251
|
+
verbose: boolean;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Manager options that were passed to the constructor
|
|
255
|
+
*/
|
|
256
|
+
managerOptions: ManagerOptions;
|
|
257
|
+
|
|
258
|
+
/**
|
|
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
|
|
265
|
+
*/
|
|
266
|
+
abortController: AbortController | null;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Creates a new RequestManager instance
|
|
270
|
+
* @param managerOptions - Configuration options for the manager
|
|
271
|
+
*/
|
|
272
|
+
constructor(managerOptions?: ManagerOptions);
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Sets the manager options
|
|
276
|
+
* @param options - The manager options to set
|
|
277
|
+
*/
|
|
278
|
+
setOptions(options: ManagerOptions): void;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Gets the manager options
|
|
282
|
+
* @returns The manager options
|
|
283
|
+
*/
|
|
284
|
+
getOptions(): ManagerOptions;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Creates an AbortController and returns its signal.
|
|
288
|
+
* The AbortController is stored internally and will be used by the next request() call.
|
|
289
|
+
* @returns The signal from a new AbortController
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* const signal = requestManager.getSignal();
|
|
293
|
+
* requestManager.request('/api/users', fetch('/api/users', { signal }));
|
|
294
|
+
*/
|
|
295
|
+
getSignal(): AbortSignal;
|
|
296
|
+
|
|
297
|
+
/**
|
|
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
|
|
301
|
+
*/
|
|
302
|
+
getAbortController(): AbortController;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Executes an HTTP request, cancelling any previous request with the same identifier.
|
|
306
|
+
*
|
|
307
|
+
* @param url - The URL to request
|
|
308
|
+
* @param requestPromise - The request promise, function that returns a promise, or URL string
|
|
309
|
+
* @param options - Optional configuration
|
|
310
|
+
* @returns A Promise that resolves/rejects based on the most recent request
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* // Request with Promise
|
|
314
|
+
* requestManager.request('/api/users', axios.get('/api/users'));
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* // Request with Function
|
|
318
|
+
* requestManager.request('/api/users', ({ options }) => fetch('/api/users', options));
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* // Request with noCancel for concurrent requests
|
|
322
|
+
* requestManager.request('/api/lazy', fetch('/api/lazy'), { noCancel: true });
|
|
323
|
+
*/
|
|
324
|
+
request<T = Response>(
|
|
325
|
+
url: string,
|
|
326
|
+
requestPromise: Promise<T> | RequestFunction<T> | string,
|
|
327
|
+
options?: RequestOptions
|
|
328
|
+
): Promise<T>;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Executes an HTTP request using fetch, cancelling any previous request with the same identifier.
|
|
332
|
+
*
|
|
333
|
+
* @param url - The URL to fetch
|
|
334
|
+
* @param options - Optional configuration (fetch options + RequestManager options)
|
|
335
|
+
* @returns A Promise that resolves/rejects based on the most recent request
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* // Simple GET request
|
|
339
|
+
* requestManager.fetch('/api/users');
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* // POST request with options
|
|
343
|
+
* requestManager.fetch('/api/users', {
|
|
344
|
+
* method: 'POST',
|
|
345
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
346
|
+
* body: JSON.stringify({ name: 'John' })
|
|
347
|
+
* });
|
|
348
|
+
*/
|
|
349
|
+
fetch(url: string, options?: FetchOptions): Promise<Response>;
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Executes an HTTP request using axios, cancelling any previous request with the same identifier.
|
|
353
|
+
*
|
|
354
|
+
* @param url - The URL to request
|
|
355
|
+
* @param options - Optional configuration (axios options + RequestManager options)
|
|
356
|
+
* @param axiosInstance - Optional axios instance to use. If not provided, uses global axios.
|
|
357
|
+
* @returns A Promise that resolves/rejects based on the most recent request
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* // Simple GET request (uses global axios)
|
|
361
|
+
* requestManager.axios('/api/users');
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* // With custom axios instance
|
|
365
|
+
* const myAxios = axios.create({ baseURL: 'https://api.example.com' });
|
|
366
|
+
* requestManager.axios('/users', {}, myAxios);
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* // POST request with options
|
|
370
|
+
* requestManager.axios('/api/users', {
|
|
371
|
+
* method: 'POST',
|
|
372
|
+
* data: { name: 'John' }
|
|
373
|
+
* });
|
|
374
|
+
*/
|
|
375
|
+
axios<T = any>(
|
|
376
|
+
url: string,
|
|
377
|
+
options?: AxiosRequestOptions,
|
|
378
|
+
axiosInstance?: AxiosInstance | AxiosStatic | null
|
|
379
|
+
): Promise<T>;
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* 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
|
|
385
|
+
* @param url - The URL to request
|
|
386
|
+
* @param options - Optional configuration
|
|
387
|
+
* @returns A Promise that resolves/rejects based on the most recent request
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* // Using with jQuery.ajax
|
|
391
|
+
* requestManager.ajax(
|
|
392
|
+
* ({ url, ...options }) => $.ajax({ url, ...options }),
|
|
393
|
+
* '/api/users',
|
|
394
|
+
* { method: 'GET' }
|
|
395
|
+
* );
|
|
396
|
+
*/
|
|
397
|
+
ajax<T = any>(
|
|
398
|
+
ajaxMethod: AjaxMethod<T>,
|
|
399
|
+
url: string,
|
|
400
|
+
options?: BaseRequestOptions & Record<string, any>
|
|
401
|
+
): Promise<T>;
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Executes an HTTP request using XMLHttpRequest, cancelling any previous request with the same identifier.
|
|
405
|
+
*
|
|
406
|
+
* @param url - The URL to request
|
|
407
|
+
* @param options - Optional configuration
|
|
408
|
+
* @returns A Promise that resolves/rejects based on the most recent request
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* // Simple GET request
|
|
412
|
+
* requestManager.xhr('/api/users');
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* // POST request with options
|
|
416
|
+
* requestManager.xhr('/api/users', {
|
|
417
|
+
* method: 'POST',
|
|
418
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
419
|
+
* body: JSON.stringify({ name: 'John' })
|
|
420
|
+
* });
|
|
421
|
+
*/
|
|
422
|
+
xhr<T = any>(url: string, options?: XhrOptions): Promise<XhrResponse<T>>;
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Cancels a specific request by its identifier.
|
|
426
|
+
*
|
|
427
|
+
* @param requestId - The unique identifier of the request to cancel
|
|
428
|
+
* @returns True if the request was found and cancelled, false otherwise
|
|
429
|
+
*/
|
|
430
|
+
cancel(requestId: string): boolean;
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Link abort signal with HTTP client abort method.
|
|
434
|
+
* Useful for custom HTTP clients that only support the abort method to cancel requests.
|
|
435
|
+
*
|
|
436
|
+
* @param abortMethod - The abort method to call when the signal is aborted
|
|
437
|
+
* @param signal - The signal to listen to
|
|
438
|
+
*/
|
|
439
|
+
addAbortListener(abortMethod: (() => void) | undefined, signal: AbortSignal | undefined): void;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Cancels all active requests.
|
|
443
|
+
*
|
|
444
|
+
* @returns The number of requests that were cancelled
|
|
445
|
+
*/
|
|
446
|
+
cancelAll(): number;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Checks if a request with the given identifier is currently active.
|
|
450
|
+
*
|
|
451
|
+
* @param requestId - The unique identifier to check
|
|
452
|
+
* @returns True if the request is active, false otherwise
|
|
453
|
+
*/
|
|
454
|
+
isActive(requestId: string): boolean;
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Gets the number of active requests.
|
|
458
|
+
*
|
|
459
|
+
* @returns The number of currently active requests
|
|
460
|
+
*/
|
|
461
|
+
getActiveCount(): number;
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Clears all active requests without cancelling them.
|
|
465
|
+
* Use with caution - this will not cancel the underlying HTTP requests.
|
|
466
|
+
*/
|
|
467
|
+
clear(): void;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export default RequestManager;
|
|
471
|
+
export { RequestManager };
|
package/index.js
ADDED