@npora/request 0.1.2 → 0.2.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,12 +3,30 @@
3
3
  */
4
4
  type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
5
5
 
6
- type QueryValue = string | number | boolean | null | undefined;
7
- type QueryParams = Record<string, QueryValue | QueryValue[]>;
8
- type ResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'stream';
6
+ /**
7
+ * Per-request retry configuration used by the retry extension.
8
+ */
9
9
  interface RetryOptions {
10
10
  retries?: number;
11
+ /**
12
+ * HTTP methods that may be retried.
13
+ *
14
+ * @default GET, HEAD, OPTIONS, PUT and DELETE
15
+ */
16
+ methods?: readonly HttpMethod[];
11
17
  delay?: number | ((attempt: number, error: unknown) => number | Promise<number>);
18
+ /**
19
+ * Respect the server Retry-After response header.
20
+ *
21
+ * @default true
22
+ */
23
+ respectRetryAfter?: boolean;
24
+ /**
25
+ * Maximum retry delay in milliseconds.
26
+ *
27
+ * @default 60000
28
+ */
29
+ maxDelay?: number;
12
30
  shouldRetry?: (error: unknown, attempt: number) => boolean | Promise<boolean>;
13
31
  }
14
32
  interface CacheOptions {
@@ -24,8 +42,29 @@ interface LoggerOptions {
24
42
  enabled?: boolean;
25
43
  }
26
44
  type UploadData = FormData | Record<string, unknown>;
45
+ /**
46
+ * Upload progress information.
47
+ */
48
+ interface UploadProgress {
49
+ /**
50
+ * Number of bytes sent.
51
+ */
52
+ loaded: number;
53
+ /**
54
+ * Total request size when the browser provides it.
55
+ */
56
+ total?: number;
57
+ /**
58
+ * Progress ratio between 0 and 1 when total is available.
59
+ */
60
+ progress?: number;
61
+ }
27
62
  interface UploadOptions {
28
63
  data: UploadData;
64
+ /**
65
+ * Called while XMLHttpRequest uploads the request body.
66
+ */
67
+ onProgress?: (progress: UploadProgress) => void;
29
68
  }
30
69
  /**
31
70
  * Download progress information.
@@ -51,10 +90,35 @@ interface DownloadOptions {
51
90
  */
52
91
  onProgress?: (progress: DownloadProgress) => void;
53
92
  }
93
+
94
+ type QueryValue = string | number | boolean | null | undefined;
95
+ type QueryParams = Record<string, QueryValue | QueryValue[]>;
96
+ type ResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'stream';
97
+ /**
98
+ * Native Fetch options passed to the underlying adapter.
99
+ *
100
+ * Request fields managed by Npora Request are intentionally omitted so there
101
+ * is a single source of truth for method, headers, body and cancellation.
102
+ */
103
+ type FetchOptions = Omit<RequestInit, 'method' | 'headers' | 'body' | 'signal'>;
104
+ /**
105
+ * Plugin-owned request configuration.
106
+ *
107
+ * Plugins extend this interface through TypeScript module augmentation.
108
+ */
109
+ interface RequestExtensions {
110
+ auth?: AuthOptions;
111
+ cache?: CacheOptions;
112
+ download?: DownloadOptions;
113
+ logger?: LoggerOptions;
114
+ retry?: number | RetryOptions;
115
+ upload?: UploadOptions;
116
+ }
54
117
  interface RequestConfig {
55
118
  url: string;
56
119
  method?: HttpMethod;
57
120
  baseURL?: string;
121
+ fetchOptions?: FetchOptions;
58
122
  headers?: HeadersInit;
59
123
  query?: QueryParams;
60
124
  body?: BodyInit | Record<string, unknown> | null;
@@ -65,11 +129,30 @@ interface RequestConfig {
65
129
  signal?: AbortSignal;
66
130
  responseType?: ResponseType;
67
131
  validateStatus?: (status: number) => boolean;
132
+ extensions?: RequestExtensions;
133
+ /**
134
+ * @deprecated Use extensions.retry instead.
135
+ */
68
136
  retry?: number | RetryOptions;
137
+ /**
138
+ * @deprecated Use extensions.cache instead.
139
+ */
69
140
  cache?: CacheOptions;
141
+ /**
142
+ * @deprecated Use extensions.auth instead.
143
+ */
70
144
  auth?: AuthOptions;
145
+ /**
146
+ * @deprecated Use extensions.logger instead.
147
+ */
71
148
  logger?: LoggerOptions;
149
+ /**
150
+ * @deprecated Use extensions.upload instead.
151
+ */
72
152
  upload?: UploadOptions;
153
+ /**
154
+ * @deprecated Use extensions.download instead.
155
+ */
73
156
  download?: DownloadOptions;
74
157
  }
75
158
 
@@ -111,6 +194,14 @@ interface Adapter {
111
194
  * Send request.
112
195
  */
113
196
  request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
197
+ /**
198
+ * Optional first-attempt fast path for built-in adapters.
199
+ *
200
+ * Custom adapters do not need to implement this method.
201
+ *
202
+ * @internal
203
+ */
204
+ requestValidated?<T = unknown>(config: RequestConfig, validatedHeaders: Headers, preserveRaw?: boolean): Promise<NporaResponse<T>>;
114
205
  }
115
206
 
116
207
  /**
@@ -127,6 +218,8 @@ interface ClientOptions extends Partial<Omit<RequestConfig, 'url' | 'method'>> {
127
218
 
128
219
  declare class FetchAdapter implements Adapter {
129
220
  request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
221
+ requestValidated<T = unknown>(config: RequestConfig, validatedHeaders: Headers, preserveRaw?: boolean): Promise<NporaResponse<T>>;
222
+ private execute;
130
223
  }
131
224
 
132
225
  type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
@@ -141,13 +234,24 @@ declare class MockAdapter implements Adapter {
141
234
  }
142
235
 
143
236
  type Interceptor<T> = (value: T) => T | Promise<T>;
237
+ interface InterceptorOptions {
238
+ /**
239
+ * Higher priority interceptors run first.
240
+ *
241
+ * @default 0
242
+ */
243
+ priority?: number;
244
+ }
144
245
  declare class InterceptorManager<T> {
145
246
  private id;
146
247
  private readonly interceptors;
147
- use(interceptor: Interceptor<T>): number;
248
+ private orderedInterceptors;
249
+ get active(): boolean;
250
+ use(interceptor: Interceptor<T>, options?: InterceptorOptions): number;
148
251
  eject(id: number): void;
149
252
  clear(): void;
150
253
  run(value: T): Promise<T>;
254
+ private refreshOrder;
151
255
  }
152
256
 
153
257
  declare class RequestContext<T = unknown> {
@@ -163,39 +267,159 @@ interface RetryDecision {
163
267
  retry: boolean;
164
268
  delay?: number;
165
269
  }
270
+ interface HookOptions {
271
+ /**
272
+ * Higher priority hooks run first.
273
+ *
274
+ * @default 0
275
+ */
276
+ priority?: number;
277
+ }
278
+ type HookDisposer = () => void;
166
279
  type RequestHook = (context: RequestContext<unknown>) => void | Promise<void>;
167
280
  type RetryHook = (context: RequestContext<unknown>, attempt: number) => RetryDecision | undefined | Promise<RetryDecision | undefined>;
168
- /**
169
- * Internal plugin lifecycle manager.
170
- *
171
- * It coordinates plugin hooks without exposing Client internals.
172
- */
173
- declare class PluginHooks {
174
- private readonly requestHooks;
175
- private readonly responseHooks;
176
- private readonly errorHooks;
177
- private readonly retryHooks;
178
- onRequest(hook: RequestHook): void;
179
- onResponse(hook: RequestHook): void;
180
- onError(hook: RequestHook): void;
181
- onRetry(hook: RetryHook): void;
182
- runRequest(context: RequestContext<unknown>): Promise<void>;
183
- runResponse(context: RequestContext<unknown>): Promise<void>;
184
- runError(context: RequestContext<unknown>): Promise<void>;
185
- resolveRetry(context: RequestContext<unknown>, attempt: number): Promise<RetryDecision>;
186
- }
187
281
 
282
+ interface PluginInterceptorManager<T> {
283
+ use(interceptor: Interceptor<T>, options?: InterceptorOptions): number;
284
+ eject(id: number): void;
285
+ }
286
+ interface PluginHookManager {
287
+ onRequest(hook: RequestHook, options?: HookOptions): HookDisposer;
288
+ onResponse(hook: RequestHook, options?: HookOptions): HookDisposer;
289
+ onError(hook: RequestHook, options?: HookOptions): HookDisposer;
290
+ onRetry(hook: RetryHook, options?: HookOptions): HookDisposer;
291
+ }
188
292
  interface PluginContext {
189
293
  interceptors: {
190
- request: InterceptorManager<RequestConfig>;
191
- response: InterceptorManager<NporaResponse>;
192
- error: InterceptorManager<unknown>;
294
+ request: PluginInterceptorManager<RequestConfig>;
295
+ response: PluginInterceptorManager<NporaResponse>;
296
+ error: PluginInterceptorManager<unknown>;
193
297
  };
194
- hooks: Pick<PluginHooks, 'onRequest' | 'onResponse' | 'onError' | 'onRetry'>;
298
+ hooks: PluginHookManager;
195
299
  }
300
+ type PluginCleanup = () => void;
196
301
  interface Plugin {
197
302
  name: string;
198
- install: (context: PluginContext) => void;
303
+ /**
304
+ * Default priority for interceptors and hooks registered by this plugin.
305
+ * Higher priority registrations run first.
306
+ *
307
+ * @default 0
308
+ */
309
+ priority?: number;
310
+ /**
311
+ * Plugins that must already be installed.
312
+ */
313
+ requires?: readonly string[];
314
+ /**
315
+ * Plugins that cannot be installed on the same client.
316
+ */
317
+ conflicts?: readonly string[];
318
+ install: (context: PluginContext) => void | PluginCleanup;
319
+ }
320
+
321
+ declare class Client {
322
+ private readonly defaults;
323
+ private readonly adapter;
324
+ private readonly installedPlugins;
325
+ private readonly hooks;
326
+ readonly interceptors: {
327
+ request: InterceptorManager<RequestConfig>;
328
+ response: InterceptorManager<NporaResponse<unknown>>;
329
+ error: InterceptorManager<unknown>;
330
+ };
331
+ private readonly pipeline;
332
+ constructor(options?: ClientOptions);
333
+ /**
334
+ * Create an isolated client that inherits this client's defaults.
335
+ */
336
+ extend(options?: ClientOptions): Client;
337
+ use(plugin: Plugin): this;
338
+ unuse(pluginName: string): this;
339
+ hasPlugin(pluginName: string): boolean;
340
+ request<T = unknown>(config: RequestConfig): Promise<T>;
341
+ requestResponse<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
342
+ get<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
343
+ getResponse<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<NporaResponse<T>>;
344
+ post<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
345
+ postResponse<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<NporaResponse<T>>;
346
+ put<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
347
+ putResponse<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<NporaResponse<T>>;
348
+ patch<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
349
+ patchResponse<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<NporaResponse<T>>;
350
+ delete<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
351
+ deleteResponse<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<NporaResponse<T>>;
352
+ head(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<void>;
353
+ headResponse(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<NporaResponse<void>>;
354
+ options<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
355
+ optionsResponse<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<NporaResponse<T>>;
356
+ private createPipeline;
357
+ private validatePlugin;
358
+ private findDependent;
359
+ }
360
+
361
+ /**
362
+ * Create an HTTP client instance.
363
+ */
364
+ declare function createClient(options?: ClientOptions): Client;
365
+
366
+ /**
367
+ * Public request error codes.
368
+ */
369
+ type RequestErrorCode = 'CONFIG_ERROR' | 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'ABORT_ERROR' | 'PARSER_ERROR';
370
+ interface RequestErrorOptions<T = unknown> {
371
+ /**
372
+ * Stable machine-readable error code.
373
+ */
374
+ code: RequestErrorCode;
375
+ /**
376
+ * HTTP response status when available.
377
+ */
378
+ status?: number;
379
+ /**
380
+ * Parsed response body when the server returned a response.
381
+ */
382
+ data?: T;
383
+ /**
384
+ * Complete response metadata when the server returned a response.
385
+ */
386
+ response?: NporaResponse<T>;
387
+ /**
388
+ * Effective request configuration.
389
+ */
390
+ config?: RequestConfig;
391
+ /**
392
+ * Original error.
393
+ */
394
+ cause?: unknown;
395
+ }
396
+ /**
397
+ * Unified public error for all request failures.
398
+ */
399
+ declare class RequestError<T = unknown> extends Error {
400
+ readonly code: RequestErrorCode;
401
+ readonly status?: number;
402
+ readonly data?: T;
403
+ readonly response?: NporaResponse<T>;
404
+ readonly config?: RequestConfig;
405
+ readonly cause?: unknown;
406
+ constructor(message: string, options: RequestErrorOptions<T>);
407
+ }
408
+
409
+ type PluginErrorCode = 'MISSING_DEPENDENCY' | 'PLUGIN_CONFLICT' | 'DEPENDENCY_IN_USE';
410
+ interface PluginErrorOptions {
411
+ code: PluginErrorCode;
412
+ plugin: string;
413
+ relatedPlugin: string;
414
+ }
415
+ /**
416
+ * Stable error thrown for invalid plugin lifecycle operations.
417
+ */
418
+ declare class PluginError extends Error {
419
+ readonly code: PluginErrorCode;
420
+ readonly plugin: string;
421
+ readonly relatedPlugin: string;
422
+ constructor(message: string, options: PluginErrorOptions);
199
423
  }
200
424
 
201
425
  type MaybePromise<T> = T | Promise<T>;
@@ -248,10 +472,39 @@ interface AuthPluginOptions {
248
472
  */
249
473
  declare function authPlugin(options?: AuthPluginOptions): Plugin;
250
474
 
251
- declare function cachePlugin(): Plugin;
475
+ interface CachePluginOptions {
476
+ /**
477
+ * HTTP methods that may be cached.
478
+ *
479
+ * @default GET and HEAD
480
+ */
481
+ methods?: readonly HttpMethod[];
482
+ /**
483
+ * Request headers included in the default cache key.
484
+ *
485
+ * @default authorization, cookie, accept and accept-language
486
+ */
487
+ varyHeaders?: readonly string[];
488
+ }
489
+ interface CachePlugin extends Plugin {
490
+ clear(): void;
491
+ }
492
+ declare function cachePlugin(options?: CachePluginOptions): CachePlugin;
252
493
  declare function clearCache(): void;
253
494
 
254
- declare function downloadPlugin(): Plugin;
495
+ type DownloadTransport = 'auto' | 'fetch' | 'xhr';
496
+ interface DownloadPluginOptions {
497
+ /**
498
+ * Download progress transport.
499
+ *
500
+ * `auto` prefers Fetch response streams and falls back to XMLHttpRequest
501
+ * when streaming responses are unavailable.
502
+ *
503
+ * @default auto
504
+ */
505
+ transport?: DownloadTransport;
506
+ }
507
+ declare function downloadPlugin(options?: DownloadPluginOptions): Plugin;
255
508
 
256
509
  declare function loggerPlugin(defaultOptions?: LoggerOptions): Plugin;
257
510
 
@@ -259,58 +512,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
259
512
 
260
513
  declare function uploadPlugin(): Plugin;
261
514
 
262
- declare class Client {
263
- private readonly defaults;
264
- private readonly installedPlugins;
265
- private readonly hooks;
266
- readonly interceptors: {
267
- request: InterceptorManager<RequestConfig>;
268
- response: InterceptorManager<NporaResponse<unknown>>;
269
- error: InterceptorManager<unknown>;
270
- };
271
- private readonly pipeline;
272
- constructor(options?: ClientOptions);
273
- use(plugin: Plugin): this;
274
- request<T = unknown>(config: RequestConfig): Promise<T>;
275
- get<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
276
- post<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
277
- put<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
278
- patch<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
279
- delete<T = unknown>(url: string, config?: Omit<RequestConfig, 'url' | 'method'>): Promise<T>;
280
- private createPipeline;
281
- }
282
-
283
- /**
284
- * Create an HTTP client instance.
285
- */
286
- declare function createClient(options?: ClientOptions): Client;
287
-
288
- /**
289
- * Public request error codes.
290
- */
291
- type RequestErrorCode = 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'ABORT_ERROR' | 'PARSER_ERROR';
292
- interface RequestErrorOptions {
293
- /**
294
- * Stable machine-readable error code.
295
- */
296
- code: RequestErrorCode;
297
- /**
298
- * HTTP response status when available.
299
- */
300
- status?: number;
301
- /**
302
- * Original error.
303
- */
304
- cause?: unknown;
305
- }
306
- /**
307
- * Unified public error for all request failures.
308
- */
309
- declare class RequestError extends Error {
310
- readonly code: RequestErrorCode;
311
- readonly status?: number;
312
- readonly cause?: unknown;
313
- constructor(message: string, options: RequestErrorOptions);
314
- }
315
-
316
- export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, Client, type ClientOptions, type DownloadOptions, type DownloadProgress, FetchAdapter, type HttpMethod, type Interceptor, InterceptorManager, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginContext, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type ResponseType, type RetryOptions, type UploadData, type UploadOptions, authPlugin, cachePlugin, clearCache, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
515
+ 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, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, 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 ResponseType, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, clearCache, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };