@npora/request 1.3.0 → 1.5.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
@@ -3,6 +3,26 @@
3
3
  */
4
4
  type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
5
5
 
6
+ type CircuitState = 'closed' | 'open' | 'half-open';
7
+ interface CircuitBreakerOptions {
8
+ /**
9
+ * Enable circuit-breaker protection for this request.
10
+ *
11
+ * @default true
12
+ */
13
+ enabled?: boolean;
14
+ /**
15
+ * Override the isolation key for this request.
16
+ */
17
+ key?: string;
18
+ }
19
+ interface CircuitBreakerStateChange {
20
+ key: string;
21
+ previousState: CircuitState;
22
+ state: CircuitState;
23
+ timestamp: number;
24
+ failures: number;
25
+ }
6
26
  /**
7
27
  * Per-request retry configuration used by the retry extension.
8
28
  */
@@ -200,6 +220,7 @@ type FetchOptions = Omit<RequestInit, 'method' | 'headers' | 'body' | 'signal'>;
200
220
  interface RequestExtensions {
201
221
  auth?: AuthOptions;
202
222
  cache?: CacheOptions;
223
+ circuitBreaker?: CircuitBreakerOptions;
203
224
  download?: DownloadOptions;
204
225
  logger?: LoggerOptions;
205
226
  retry?: number | RetryOptions;
@@ -290,14 +311,69 @@ declare class FetchAdapter implements Adapter {
290
311
  }
291
312
 
292
313
  type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
314
+ type MockURLMatcher = string | RegExp;
315
+ interface MockRequestMatcher {
316
+ url: MockURLMatcher;
317
+ query?: QueryParams;
318
+ headers?: HeadersInit;
319
+ }
320
+ interface MockReply<T = unknown> {
321
+ status: number;
322
+ data?: T;
323
+ headers?: HeadersInit;
324
+ statusText?: string;
325
+ delay?: number;
326
+ }
327
+ type MockReplyHandler<T = unknown> = (config: RequestConfig) => MockReply<T> | Promise<MockReply<T>>;
328
+ interface MockResponseOptions {
329
+ headers?: HeadersInit;
330
+ statusText?: string;
331
+ delay?: number;
332
+ }
333
+ interface MockRoute {
334
+ reply<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
335
+ reply<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
336
+ replyOnce<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
337
+ replyOnce<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
338
+ networkError(message?: string): MockAdapter;
339
+ networkErrorOnce(message?: string): MockAdapter;
340
+ timeout(message?: string): MockAdapter;
341
+ timeoutOnce(message?: string): MockAdapter;
342
+ }
293
343
  interface MockAdapterOptions {
294
344
  handlers?: Record<string, MockHandler>;
345
+ /**
346
+ * Default delay for mock handlers and routes.
347
+ *
348
+ * @default 0
349
+ */
350
+ delay?: number;
295
351
  }
296
352
  declare class MockAdapter implements Adapter {
297
353
  private readonly handlers;
354
+ private readonly rules;
355
+ private readonly requests;
356
+ private readonly delay;
298
357
  constructor(options?: MockAdapterOptions);
358
+ get history(): readonly RequestConfig[];
359
+ /**
360
+ * Register a legacy URL-only data handler.
361
+ */
299
362
  on<T = unknown>(url: string, handler: MockHandler<T>): this;
363
+ onMethod(method: HttpMethod, matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
364
+ onGet(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
365
+ onPost(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
366
+ onPut(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
367
+ onPatch(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
368
+ onDelete(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
369
+ onHead(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
370
+ onOptions(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
371
+ resetHistory(): this;
372
+ resetHandlers(): this;
373
+ reset(): this;
300
374
  request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
375
+ private registerReply;
376
+ private registerError;
301
377
  }
302
378
 
303
379
  type Interceptor<T> = (value: T) => T | Promise<T>;
@@ -440,7 +516,7 @@ declare function createClient(options?: ClientOptions): Client;
440
516
  /**
441
517
  * Public request error codes.
442
518
  */
443
- type RequestErrorCode = 'CONFIG_ERROR' | 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'ABORT_ERROR' | 'PARSER_ERROR';
519
+ type RequestErrorCode = 'CONFIG_ERROR' | 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'ABORT_ERROR' | 'PARSER_ERROR' | 'CIRCUIT_OPEN';
444
520
  interface RequestErrorOptions<T = unknown> {
445
521
  /**
446
522
  * Stable machine-readable error code.
@@ -599,6 +675,54 @@ interface CachePlugin extends Plugin {
599
675
  }
600
676
  declare function cachePlugin(options?: CachePluginOptions): CachePlugin;
601
677
 
678
+ interface CircuitBreakerPluginOptions {
679
+ /**
680
+ * Consecutive counted failures required to open a closed circuit.
681
+ *
682
+ * @default 5
683
+ */
684
+ failureThreshold?: number;
685
+ /**
686
+ * Successful probes required to close a half-open circuit.
687
+ *
688
+ * @default 1
689
+ */
690
+ successThreshold?: number;
691
+ /**
692
+ * Time in milliseconds before an open circuit permits probes.
693
+ *
694
+ * @default 30000
695
+ */
696
+ resetTimeout?: number;
697
+ /**
698
+ * Maximum concurrent probes admitted while a circuit is half-open.
699
+ *
700
+ * @default 1
701
+ */
702
+ halfOpenMaxRequests?: number;
703
+ /**
704
+ * Create the default isolation key for a request.
705
+ *
706
+ * @default the resolved request origin or "default"
707
+ */
708
+ createKey?: (config: RequestConfig) => string;
709
+ /**
710
+ * Decide whether a final request failure affects the circuit.
711
+ *
712
+ * @default network errors, timeouts, HTTP 429, and HTTP 5xx
713
+ */
714
+ shouldCountFailure?: (error: unknown, config: RequestConfig) => boolean | Promise<boolean>;
715
+ /**
716
+ * Observe circuit state changes. Observer failures are isolated.
717
+ */
718
+ onStateChange?: (event: Readonly<CircuitBreakerStateChange>) => void | Promise<void>;
719
+ }
720
+ interface CircuitBreakerPlugin extends Plugin {
721
+ getState(key: string): CircuitState;
722
+ reset(key?: string): void;
723
+ }
724
+ declare function circuitBreakerPlugin(options?: CircuitBreakerPluginOptions): CircuitBreakerPlugin;
725
+
602
726
  type DownloadTransport = 'auto' | 'fetch' | 'xhr';
603
727
  interface DownloadPluginOptions {
604
728
  /**
@@ -619,4 +743,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
619
743
 
620
744
  declare function uploadPlugin(): Plugin;
621
745
 
622
- 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 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 };
746
+ export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, type CircuitBreakerOptions, type CircuitBreakerPlugin, type CircuitBreakerPluginOptions, type CircuitBreakerStateChange, type CircuitState, 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, circuitBreakerPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,26 @@
3
3
  */
4
4
  type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
5
5
 
6
+ type CircuitState = 'closed' | 'open' | 'half-open';
7
+ interface CircuitBreakerOptions {
8
+ /**
9
+ * Enable circuit-breaker protection for this request.
10
+ *
11
+ * @default true
12
+ */
13
+ enabled?: boolean;
14
+ /**
15
+ * Override the isolation key for this request.
16
+ */
17
+ key?: string;
18
+ }
19
+ interface CircuitBreakerStateChange {
20
+ key: string;
21
+ previousState: CircuitState;
22
+ state: CircuitState;
23
+ timestamp: number;
24
+ failures: number;
25
+ }
6
26
  /**
7
27
  * Per-request retry configuration used by the retry extension.
8
28
  */
@@ -200,6 +220,7 @@ type FetchOptions = Omit<RequestInit, 'method' | 'headers' | 'body' | 'signal'>;
200
220
  interface RequestExtensions {
201
221
  auth?: AuthOptions;
202
222
  cache?: CacheOptions;
223
+ circuitBreaker?: CircuitBreakerOptions;
203
224
  download?: DownloadOptions;
204
225
  logger?: LoggerOptions;
205
226
  retry?: number | RetryOptions;
@@ -290,14 +311,69 @@ declare class FetchAdapter implements Adapter {
290
311
  }
291
312
 
292
313
  type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
314
+ type MockURLMatcher = string | RegExp;
315
+ interface MockRequestMatcher {
316
+ url: MockURLMatcher;
317
+ query?: QueryParams;
318
+ headers?: HeadersInit;
319
+ }
320
+ interface MockReply<T = unknown> {
321
+ status: number;
322
+ data?: T;
323
+ headers?: HeadersInit;
324
+ statusText?: string;
325
+ delay?: number;
326
+ }
327
+ type MockReplyHandler<T = unknown> = (config: RequestConfig) => MockReply<T> | Promise<MockReply<T>>;
328
+ interface MockResponseOptions {
329
+ headers?: HeadersInit;
330
+ statusText?: string;
331
+ delay?: number;
332
+ }
333
+ interface MockRoute {
334
+ reply<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
335
+ reply<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
336
+ replyOnce<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
337
+ replyOnce<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
338
+ networkError(message?: string): MockAdapter;
339
+ networkErrorOnce(message?: string): MockAdapter;
340
+ timeout(message?: string): MockAdapter;
341
+ timeoutOnce(message?: string): MockAdapter;
342
+ }
293
343
  interface MockAdapterOptions {
294
344
  handlers?: Record<string, MockHandler>;
345
+ /**
346
+ * Default delay for mock handlers and routes.
347
+ *
348
+ * @default 0
349
+ */
350
+ delay?: number;
295
351
  }
296
352
  declare class MockAdapter implements Adapter {
297
353
  private readonly handlers;
354
+ private readonly rules;
355
+ private readonly requests;
356
+ private readonly delay;
298
357
  constructor(options?: MockAdapterOptions);
358
+ get history(): readonly RequestConfig[];
359
+ /**
360
+ * Register a legacy URL-only data handler.
361
+ */
299
362
  on<T = unknown>(url: string, handler: MockHandler<T>): this;
363
+ onMethod(method: HttpMethod, matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
364
+ onGet(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
365
+ onPost(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
366
+ onPut(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
367
+ onPatch(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
368
+ onDelete(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
369
+ onHead(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
370
+ onOptions(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
371
+ resetHistory(): this;
372
+ resetHandlers(): this;
373
+ reset(): this;
300
374
  request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
375
+ private registerReply;
376
+ private registerError;
301
377
  }
302
378
 
303
379
  type Interceptor<T> = (value: T) => T | Promise<T>;
@@ -440,7 +516,7 @@ declare function createClient(options?: ClientOptions): Client;
440
516
  /**
441
517
  * Public request error codes.
442
518
  */
443
- type RequestErrorCode = 'CONFIG_ERROR' | 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'ABORT_ERROR' | 'PARSER_ERROR';
519
+ type RequestErrorCode = 'CONFIG_ERROR' | 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'ABORT_ERROR' | 'PARSER_ERROR' | 'CIRCUIT_OPEN';
444
520
  interface RequestErrorOptions<T = unknown> {
445
521
  /**
446
522
  * Stable machine-readable error code.
@@ -599,6 +675,54 @@ interface CachePlugin extends Plugin {
599
675
  }
600
676
  declare function cachePlugin(options?: CachePluginOptions): CachePlugin;
601
677
 
678
+ interface CircuitBreakerPluginOptions {
679
+ /**
680
+ * Consecutive counted failures required to open a closed circuit.
681
+ *
682
+ * @default 5
683
+ */
684
+ failureThreshold?: number;
685
+ /**
686
+ * Successful probes required to close a half-open circuit.
687
+ *
688
+ * @default 1
689
+ */
690
+ successThreshold?: number;
691
+ /**
692
+ * Time in milliseconds before an open circuit permits probes.
693
+ *
694
+ * @default 30000
695
+ */
696
+ resetTimeout?: number;
697
+ /**
698
+ * Maximum concurrent probes admitted while a circuit is half-open.
699
+ *
700
+ * @default 1
701
+ */
702
+ halfOpenMaxRequests?: number;
703
+ /**
704
+ * Create the default isolation key for a request.
705
+ *
706
+ * @default the resolved request origin or "default"
707
+ */
708
+ createKey?: (config: RequestConfig) => string;
709
+ /**
710
+ * Decide whether a final request failure affects the circuit.
711
+ *
712
+ * @default network errors, timeouts, HTTP 429, and HTTP 5xx
713
+ */
714
+ shouldCountFailure?: (error: unknown, config: RequestConfig) => boolean | Promise<boolean>;
715
+ /**
716
+ * Observe circuit state changes. Observer failures are isolated.
717
+ */
718
+ onStateChange?: (event: Readonly<CircuitBreakerStateChange>) => void | Promise<void>;
719
+ }
720
+ interface CircuitBreakerPlugin extends Plugin {
721
+ getState(key: string): CircuitState;
722
+ reset(key?: string): void;
723
+ }
724
+ declare function circuitBreakerPlugin(options?: CircuitBreakerPluginOptions): CircuitBreakerPlugin;
725
+
602
726
  type DownloadTransport = 'auto' | 'fetch' | 'xhr';
603
727
  interface DownloadPluginOptions {
604
728
  /**
@@ -619,4 +743,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
619
743
 
620
744
  declare function uploadPlugin(): Plugin;
621
745
 
622
- 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 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 };
746
+ export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, type CircuitBreakerOptions, type CircuitBreakerPlugin, type CircuitBreakerPluginOptions, type CircuitBreakerStateChange, type CircuitState, 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, circuitBreakerPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };