@npora/request 1.2.0 → 1.4.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/dist/index.d.cts CHANGED
@@ -71,6 +71,12 @@ interface CacheOptions {
71
71
  enabled?: boolean;
72
72
  ttl?: number;
73
73
  key?: string;
74
+ /**
75
+ * Share one network operation between concurrent equivalent requests.
76
+ *
77
+ * @default true
78
+ */
79
+ dedupe?: boolean;
74
80
  }
75
81
  interface AuthOptions {
76
82
  token?: string | (() => string | Promise<string>);
@@ -284,14 +290,69 @@ declare class FetchAdapter implements Adapter {
284
290
  }
285
291
 
286
292
  type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
293
+ type MockURLMatcher = string | RegExp;
294
+ interface MockRequestMatcher {
295
+ url: MockURLMatcher;
296
+ query?: QueryParams;
297
+ headers?: HeadersInit;
298
+ }
299
+ interface MockReply<T = unknown> {
300
+ status: number;
301
+ data?: T;
302
+ headers?: HeadersInit;
303
+ statusText?: string;
304
+ delay?: number;
305
+ }
306
+ type MockReplyHandler<T = unknown> = (config: RequestConfig) => MockReply<T> | Promise<MockReply<T>>;
307
+ interface MockResponseOptions {
308
+ headers?: HeadersInit;
309
+ statusText?: string;
310
+ delay?: number;
311
+ }
312
+ interface MockRoute {
313
+ reply<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
314
+ reply<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
315
+ replyOnce<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
316
+ replyOnce<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
317
+ networkError(message?: string): MockAdapter;
318
+ networkErrorOnce(message?: string): MockAdapter;
319
+ timeout(message?: string): MockAdapter;
320
+ timeoutOnce(message?: string): MockAdapter;
321
+ }
287
322
  interface MockAdapterOptions {
288
323
  handlers?: Record<string, MockHandler>;
324
+ /**
325
+ * Default delay for mock handlers and routes.
326
+ *
327
+ * @default 0
328
+ */
329
+ delay?: number;
289
330
  }
290
331
  declare class MockAdapter implements Adapter {
291
332
  private readonly handlers;
333
+ private readonly rules;
334
+ private readonly requests;
335
+ private readonly delay;
292
336
  constructor(options?: MockAdapterOptions);
337
+ get history(): readonly RequestConfig[];
338
+ /**
339
+ * Register a legacy URL-only data handler.
340
+ */
293
341
  on<T = unknown>(url: string, handler: MockHandler<T>): this;
342
+ onMethod(method: HttpMethod, matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
343
+ onGet(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
344
+ onPost(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
345
+ onPut(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
346
+ onPatch(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
347
+ onDelete(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
348
+ onHead(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
349
+ onOptions(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
350
+ resetHistory(): this;
351
+ resetHandlers(): this;
352
+ reset(): this;
294
353
  request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
354
+ private registerReply;
355
+ private registerError;
295
356
  }
296
357
 
297
358
  type Interceptor<T> = (value: T) => T | Promise<T>;
@@ -349,6 +410,12 @@ interface PluginHookManager {
349
410
  onRequest(hook: RequestHook, options?: HookOptions): HookDisposer;
350
411
  onResponse(hook: RequestHook, options?: HookOptions): HookDisposer;
351
412
  onError(hook: RequestHook, options?: HookOptions): HookDisposer;
413
+ /**
414
+ * Observe the final request outcome after retries and interceptors finish.
415
+ *
416
+ * Settled hook failures are isolated from the request result.
417
+ */
418
+ onSettled(hook: RequestHook, options?: HookOptions): HookDisposer;
352
419
  onRetry(hook: RetryHook, options?: HookOptions): HookDisposer;
353
420
  }
354
421
  interface PluginContext {
@@ -484,7 +551,7 @@ declare class PluginError extends Error {
484
551
  constructor(message: string, options: PluginErrorOptions);
485
552
  }
486
553
 
487
- type MaybePromise<T> = T | Promise<T>;
554
+ type MaybePromise$1<T> = T | Promise<T>;
488
555
  /**
489
556
  * Token storage used by the authentication extension.
490
557
  *
@@ -492,9 +559,9 @@ type MaybePromise<T> = T | Promise<T>;
492
559
  * The request library does not access localStorage or sessionStorage directly.
493
560
  */
494
561
  interface AuthTokenStorage {
495
- get(): MaybePromise<string | null | undefined>;
496
- set(token: string): MaybePromise<void>;
497
- remove(): MaybePromise<void>;
562
+ get(): MaybePromise$1<string | null | undefined>;
563
+ set(token: string): MaybePromise$1<void>;
564
+ remove(): MaybePromise$1<void>;
498
565
  }
499
566
  interface AuthPluginOptions {
500
567
  /**
@@ -504,7 +571,7 @@ interface AuthPluginOptions {
504
571
  * the request-level token has the highest priority,
505
572
  * followed by this token option, then storage.
506
573
  */
507
- token?: string | (() => MaybePromise<string>);
574
+ token?: string | (() => MaybePromise$1<string>);
508
575
  /**
509
576
  * Authorization scheme.
510
577
  *
@@ -521,19 +588,41 @@ interface AuthPluginOptions {
521
588
  * Returning a token stores it through storage when configured.
522
589
  * Returning void allows the callback to update external state itself.
523
590
  */
524
- refreshToken?: () => MaybePromise<string | void>;
591
+ refreshToken?: () => MaybePromise$1<string | void>;
525
592
  /**
526
593
  * Determine whether an error should trigger token refresh.
527
594
  *
528
595
  * @default HTTP status 401
529
596
  */
530
- shouldRefresh?: (error: unknown) => MaybePromise<boolean>;
597
+ shouldRefresh?: (error: unknown) => MaybePromise$1<boolean>;
531
598
  }
532
599
  /**
533
600
  * Authentication plugin.
534
601
  */
535
602
  declare function authPlugin(options?: AuthPluginOptions): Plugin;
536
603
 
604
+ type MaybePromise<T> = T | Promise<T>;
605
+ interface CacheEntry {
606
+ data: unknown;
607
+ expiresAt: number;
608
+ status: number;
609
+ statusText: string;
610
+ headers: Array<[string, string]>;
611
+ raw?: Response;
612
+ }
613
+ interface CacheStore {
614
+ get(key: string): MaybePromise<CacheEntry | undefined>;
615
+ set(key: string, entry: CacheEntry): MaybePromise<void>;
616
+ delete(key: string): MaybePromise<void>;
617
+ clear(): MaybePromise<void>;
618
+ }
619
+ declare class MemoryCacheStore implements CacheStore {
620
+ private readonly entries;
621
+ get(key: string): CacheEntry | undefined;
622
+ set(key: string, entry: CacheEntry): void;
623
+ delete(key: string): void;
624
+ clear(): void;
625
+ }
537
626
  interface CachePluginOptions {
538
627
  /**
539
628
  * HTTP methods that may be cached.
@@ -547,9 +636,21 @@ interface CachePluginOptions {
547
636
  * @default authorization, cookie, accept and accept-language
548
637
  */
549
638
  varyHeaders?: readonly string[];
639
+ /**
640
+ * Cache storage shared by this plugin instance.
641
+ *
642
+ * @default an isolated MemoryCacheStore
643
+ */
644
+ store?: CacheStore;
645
+ /**
646
+ * Share one network operation between concurrent equivalent requests.
647
+ *
648
+ * @default true
649
+ */
650
+ dedupe?: boolean;
550
651
  }
551
652
  interface CachePlugin extends Plugin {
552
- clear(): void;
653
+ clear(): MaybePromise<void>;
553
654
  }
554
655
  declare function cachePlugin(options?: CachePluginOptions): CachePlugin;
555
656
 
@@ -573,4 +674,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
573
674
 
574
675
  declare function uploadPlugin(): Plugin;
575
676
 
576
- export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
677
+ export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
package/dist/index.d.ts CHANGED
@@ -71,6 +71,12 @@ interface CacheOptions {
71
71
  enabled?: boolean;
72
72
  ttl?: number;
73
73
  key?: string;
74
+ /**
75
+ * Share one network operation between concurrent equivalent requests.
76
+ *
77
+ * @default true
78
+ */
79
+ dedupe?: boolean;
74
80
  }
75
81
  interface AuthOptions {
76
82
  token?: string | (() => string | Promise<string>);
@@ -284,14 +290,69 @@ declare class FetchAdapter implements Adapter {
284
290
  }
285
291
 
286
292
  type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
293
+ type MockURLMatcher = string | RegExp;
294
+ interface MockRequestMatcher {
295
+ url: MockURLMatcher;
296
+ query?: QueryParams;
297
+ headers?: HeadersInit;
298
+ }
299
+ interface MockReply<T = unknown> {
300
+ status: number;
301
+ data?: T;
302
+ headers?: HeadersInit;
303
+ statusText?: string;
304
+ delay?: number;
305
+ }
306
+ type MockReplyHandler<T = unknown> = (config: RequestConfig) => MockReply<T> | Promise<MockReply<T>>;
307
+ interface MockResponseOptions {
308
+ headers?: HeadersInit;
309
+ statusText?: string;
310
+ delay?: number;
311
+ }
312
+ interface MockRoute {
313
+ reply<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
314
+ reply<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
315
+ replyOnce<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
316
+ replyOnce<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
317
+ networkError(message?: string): MockAdapter;
318
+ networkErrorOnce(message?: string): MockAdapter;
319
+ timeout(message?: string): MockAdapter;
320
+ timeoutOnce(message?: string): MockAdapter;
321
+ }
287
322
  interface MockAdapterOptions {
288
323
  handlers?: Record<string, MockHandler>;
324
+ /**
325
+ * Default delay for mock handlers and routes.
326
+ *
327
+ * @default 0
328
+ */
329
+ delay?: number;
289
330
  }
290
331
  declare class MockAdapter implements Adapter {
291
332
  private readonly handlers;
333
+ private readonly rules;
334
+ private readonly requests;
335
+ private readonly delay;
292
336
  constructor(options?: MockAdapterOptions);
337
+ get history(): readonly RequestConfig[];
338
+ /**
339
+ * Register a legacy URL-only data handler.
340
+ */
293
341
  on<T = unknown>(url: string, handler: MockHandler<T>): this;
342
+ onMethod(method: HttpMethod, matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
343
+ onGet(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
344
+ onPost(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
345
+ onPut(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
346
+ onPatch(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
347
+ onDelete(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
348
+ onHead(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
349
+ onOptions(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
350
+ resetHistory(): this;
351
+ resetHandlers(): this;
352
+ reset(): this;
294
353
  request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
354
+ private registerReply;
355
+ private registerError;
295
356
  }
296
357
 
297
358
  type Interceptor<T> = (value: T) => T | Promise<T>;
@@ -349,6 +410,12 @@ interface PluginHookManager {
349
410
  onRequest(hook: RequestHook, options?: HookOptions): HookDisposer;
350
411
  onResponse(hook: RequestHook, options?: HookOptions): HookDisposer;
351
412
  onError(hook: RequestHook, options?: HookOptions): HookDisposer;
413
+ /**
414
+ * Observe the final request outcome after retries and interceptors finish.
415
+ *
416
+ * Settled hook failures are isolated from the request result.
417
+ */
418
+ onSettled(hook: RequestHook, options?: HookOptions): HookDisposer;
352
419
  onRetry(hook: RetryHook, options?: HookOptions): HookDisposer;
353
420
  }
354
421
  interface PluginContext {
@@ -484,7 +551,7 @@ declare class PluginError extends Error {
484
551
  constructor(message: string, options: PluginErrorOptions);
485
552
  }
486
553
 
487
- type MaybePromise<T> = T | Promise<T>;
554
+ type MaybePromise$1<T> = T | Promise<T>;
488
555
  /**
489
556
  * Token storage used by the authentication extension.
490
557
  *
@@ -492,9 +559,9 @@ type MaybePromise<T> = T | Promise<T>;
492
559
  * The request library does not access localStorage or sessionStorage directly.
493
560
  */
494
561
  interface AuthTokenStorage {
495
- get(): MaybePromise<string | null | undefined>;
496
- set(token: string): MaybePromise<void>;
497
- remove(): MaybePromise<void>;
562
+ get(): MaybePromise$1<string | null | undefined>;
563
+ set(token: string): MaybePromise$1<void>;
564
+ remove(): MaybePromise$1<void>;
498
565
  }
499
566
  interface AuthPluginOptions {
500
567
  /**
@@ -504,7 +571,7 @@ interface AuthPluginOptions {
504
571
  * the request-level token has the highest priority,
505
572
  * followed by this token option, then storage.
506
573
  */
507
- token?: string | (() => MaybePromise<string>);
574
+ token?: string | (() => MaybePromise$1<string>);
508
575
  /**
509
576
  * Authorization scheme.
510
577
  *
@@ -521,19 +588,41 @@ interface AuthPluginOptions {
521
588
  * Returning a token stores it through storage when configured.
522
589
  * Returning void allows the callback to update external state itself.
523
590
  */
524
- refreshToken?: () => MaybePromise<string | void>;
591
+ refreshToken?: () => MaybePromise$1<string | void>;
525
592
  /**
526
593
  * Determine whether an error should trigger token refresh.
527
594
  *
528
595
  * @default HTTP status 401
529
596
  */
530
- shouldRefresh?: (error: unknown) => MaybePromise<boolean>;
597
+ shouldRefresh?: (error: unknown) => MaybePromise$1<boolean>;
531
598
  }
532
599
  /**
533
600
  * Authentication plugin.
534
601
  */
535
602
  declare function authPlugin(options?: AuthPluginOptions): Plugin;
536
603
 
604
+ type MaybePromise<T> = T | Promise<T>;
605
+ interface CacheEntry {
606
+ data: unknown;
607
+ expiresAt: number;
608
+ status: number;
609
+ statusText: string;
610
+ headers: Array<[string, string]>;
611
+ raw?: Response;
612
+ }
613
+ interface CacheStore {
614
+ get(key: string): MaybePromise<CacheEntry | undefined>;
615
+ set(key: string, entry: CacheEntry): MaybePromise<void>;
616
+ delete(key: string): MaybePromise<void>;
617
+ clear(): MaybePromise<void>;
618
+ }
619
+ declare class MemoryCacheStore implements CacheStore {
620
+ private readonly entries;
621
+ get(key: string): CacheEntry | undefined;
622
+ set(key: string, entry: CacheEntry): void;
623
+ delete(key: string): void;
624
+ clear(): void;
625
+ }
537
626
  interface CachePluginOptions {
538
627
  /**
539
628
  * HTTP methods that may be cached.
@@ -547,9 +636,21 @@ interface CachePluginOptions {
547
636
  * @default authorization, cookie, accept and accept-language
548
637
  */
549
638
  varyHeaders?: readonly string[];
639
+ /**
640
+ * Cache storage shared by this plugin instance.
641
+ *
642
+ * @default an isolated MemoryCacheStore
643
+ */
644
+ store?: CacheStore;
645
+ /**
646
+ * Share one network operation between concurrent equivalent requests.
647
+ *
648
+ * @default true
649
+ */
650
+ dedupe?: boolean;
550
651
  }
551
652
  interface CachePlugin extends Plugin {
552
- clear(): void;
653
+ clear(): MaybePromise<void>;
553
654
  }
554
655
  declare function cachePlugin(options?: CachePluginOptions): CachePlugin;
555
656
 
@@ -573,4 +674,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
573
674
 
574
675
  declare function uploadPlugin(): Plugin;
575
676
 
576
- export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
677
+ export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };