@npora/request 0.1.1
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 +174 -0
- package/dist/index.cjs +999 -0
- package/dist/index.d.cts +316 -0
- package/dist/index.d.ts +316 -0
- package/dist/index.js +960 -0
- package/package.json +68 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP request method.
|
|
3
|
+
*/
|
|
4
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
5
|
+
|
|
6
|
+
type QueryValue = string | number | boolean | null | undefined;
|
|
7
|
+
type QueryParams = Record<string, QueryValue | QueryValue[]>;
|
|
8
|
+
type ResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'stream';
|
|
9
|
+
interface RetryOptions {
|
|
10
|
+
retries?: number;
|
|
11
|
+
delay?: number | ((attempt: number, error: unknown) => number | Promise<number>);
|
|
12
|
+
shouldRetry?: (error: unknown, attempt: number) => boolean | Promise<boolean>;
|
|
13
|
+
}
|
|
14
|
+
interface CacheOptions {
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
ttl?: number;
|
|
17
|
+
key?: string;
|
|
18
|
+
}
|
|
19
|
+
interface AuthOptions {
|
|
20
|
+
token?: string | (() => string | Promise<string>);
|
|
21
|
+
scheme?: string;
|
|
22
|
+
}
|
|
23
|
+
interface LoggerOptions {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
}
|
|
26
|
+
type UploadData = FormData | Record<string, unknown>;
|
|
27
|
+
interface UploadOptions {
|
|
28
|
+
data: UploadData;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Download progress information.
|
|
32
|
+
*/
|
|
33
|
+
interface DownloadProgress {
|
|
34
|
+
/**
|
|
35
|
+
* Number of bytes received.
|
|
36
|
+
*/
|
|
37
|
+
loaded: number;
|
|
38
|
+
/**
|
|
39
|
+
* Total response size when Content-Length is available.
|
|
40
|
+
*/
|
|
41
|
+
total?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Progress ratio between 0 and 1 when total is available.
|
|
44
|
+
*/
|
|
45
|
+
progress?: number;
|
|
46
|
+
}
|
|
47
|
+
interface DownloadOptions {
|
|
48
|
+
filename?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Called while the response stream is being consumed.
|
|
51
|
+
*/
|
|
52
|
+
onProgress?: (progress: DownloadProgress) => void;
|
|
53
|
+
}
|
|
54
|
+
interface RequestConfig {
|
|
55
|
+
url: string;
|
|
56
|
+
method?: HttpMethod;
|
|
57
|
+
baseURL?: string;
|
|
58
|
+
headers?: HeadersInit;
|
|
59
|
+
query?: QueryParams;
|
|
60
|
+
body?: BodyInit | Record<string, unknown> | null;
|
|
61
|
+
json?: Record<string, unknown> | unknown[];
|
|
62
|
+
form?: URLSearchParams | Record<string, QueryValue | QueryValue[]>;
|
|
63
|
+
formData?: FormData | Record<string, unknown>;
|
|
64
|
+
timeout?: number;
|
|
65
|
+
signal?: AbortSignal;
|
|
66
|
+
responseType?: ResponseType;
|
|
67
|
+
validateStatus?: (status: number) => boolean;
|
|
68
|
+
retry?: number | RetryOptions;
|
|
69
|
+
cache?: CacheOptions;
|
|
70
|
+
auth?: AuthOptions;
|
|
71
|
+
logger?: LoggerOptions;
|
|
72
|
+
upload?: UploadOptions;
|
|
73
|
+
download?: DownloadOptions;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Npora response.
|
|
78
|
+
*/
|
|
79
|
+
interface NporaResponse<T = unknown> {
|
|
80
|
+
/**
|
|
81
|
+
* Parsed response data.
|
|
82
|
+
*/
|
|
83
|
+
data: T;
|
|
84
|
+
/**
|
|
85
|
+
* HTTP status code.
|
|
86
|
+
*/
|
|
87
|
+
status: number;
|
|
88
|
+
/**
|
|
89
|
+
* HTTP status text.
|
|
90
|
+
*/
|
|
91
|
+
statusText: string;
|
|
92
|
+
/**
|
|
93
|
+
* Response headers.
|
|
94
|
+
*/
|
|
95
|
+
headers: Headers;
|
|
96
|
+
/**
|
|
97
|
+
* Request config.
|
|
98
|
+
*/
|
|
99
|
+
config: RequestConfig;
|
|
100
|
+
/**
|
|
101
|
+
* Raw Fetch Response.
|
|
102
|
+
*/
|
|
103
|
+
raw: Response;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Request adapter.
|
|
108
|
+
*/
|
|
109
|
+
interface Adapter {
|
|
110
|
+
/**
|
|
111
|
+
* Send request.
|
|
112
|
+
*/
|
|
113
|
+
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Client creation options.
|
|
118
|
+
*/
|
|
119
|
+
interface ClientOptions extends Partial<Omit<RequestConfig, 'url' | 'method'>> {
|
|
120
|
+
/**
|
|
121
|
+
* Request adapter.
|
|
122
|
+
*
|
|
123
|
+
* @default FetchAdapter
|
|
124
|
+
*/
|
|
125
|
+
adapter?: Adapter;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
declare class FetchAdapter implements Adapter {
|
|
129
|
+
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
|
|
133
|
+
interface MockAdapterOptions {
|
|
134
|
+
handlers?: Record<string, MockHandler>;
|
|
135
|
+
}
|
|
136
|
+
declare class MockAdapter implements Adapter {
|
|
137
|
+
private readonly handlers;
|
|
138
|
+
constructor(options?: MockAdapterOptions);
|
|
139
|
+
on<T = unknown>(url: string, handler: MockHandler<T>): this;
|
|
140
|
+
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
type Interceptor<T> = (value: T) => T | Promise<T>;
|
|
144
|
+
declare class InterceptorManager<T> {
|
|
145
|
+
private id;
|
|
146
|
+
private readonly interceptors;
|
|
147
|
+
use(interceptor: Interceptor<T>): number;
|
|
148
|
+
eject(id: number): void;
|
|
149
|
+
clear(): void;
|
|
150
|
+
run(value: T): Promise<T>;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
declare class RequestContext<T = unknown> {
|
|
154
|
+
config: RequestConfig;
|
|
155
|
+
response?: NporaResponse<T>;
|
|
156
|
+
error?: unknown;
|
|
157
|
+
readonly startTime: number;
|
|
158
|
+
endTime?: number;
|
|
159
|
+
constructor(config: RequestConfig);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface RetryDecision {
|
|
163
|
+
retry: boolean;
|
|
164
|
+
delay?: number;
|
|
165
|
+
}
|
|
166
|
+
type RequestHook = (context: RequestContext<unknown>) => void | Promise<void>;
|
|
167
|
+
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
|
+
|
|
188
|
+
interface PluginContext {
|
|
189
|
+
interceptors: {
|
|
190
|
+
request: InterceptorManager<RequestConfig>;
|
|
191
|
+
response: InterceptorManager<NporaResponse>;
|
|
192
|
+
error: InterceptorManager<unknown>;
|
|
193
|
+
};
|
|
194
|
+
hooks: Pick<PluginHooks, 'onRequest' | 'onResponse' | 'onError' | 'onRetry'>;
|
|
195
|
+
}
|
|
196
|
+
interface Plugin {
|
|
197
|
+
name: string;
|
|
198
|
+
install: (context: PluginContext) => void;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
202
|
+
/**
|
|
203
|
+
* Token storage used by the authentication extension.
|
|
204
|
+
*
|
|
205
|
+
* Storage implementation is supplied by the application.
|
|
206
|
+
* The request library does not access localStorage or sessionStorage directly.
|
|
207
|
+
*/
|
|
208
|
+
interface AuthTokenStorage {
|
|
209
|
+
get(): MaybePromise<string | null | undefined>;
|
|
210
|
+
set(token: string): MaybePromise<void>;
|
|
211
|
+
remove(): MaybePromise<void>;
|
|
212
|
+
}
|
|
213
|
+
interface AuthPluginOptions {
|
|
214
|
+
/**
|
|
215
|
+
* Access token or access-token provider.
|
|
216
|
+
*
|
|
217
|
+
* When both token and storage are provided,
|
|
218
|
+
* the request-level token has the highest priority,
|
|
219
|
+
* followed by this token option, then storage.
|
|
220
|
+
*/
|
|
221
|
+
token?: string | (() => MaybePromise<string>);
|
|
222
|
+
/**
|
|
223
|
+
* Authorization scheme.
|
|
224
|
+
*
|
|
225
|
+
* @default 'Bearer'
|
|
226
|
+
*/
|
|
227
|
+
scheme?: string;
|
|
228
|
+
/**
|
|
229
|
+
* Optional access-token storage.
|
|
230
|
+
*/
|
|
231
|
+
storage?: AuthTokenStorage;
|
|
232
|
+
/**
|
|
233
|
+
* Refresh the access token.
|
|
234
|
+
*
|
|
235
|
+
* Returning a token stores it through storage when configured.
|
|
236
|
+
* Returning void allows the callback to update external state itself.
|
|
237
|
+
*/
|
|
238
|
+
refreshToken?: () => MaybePromise<string | void>;
|
|
239
|
+
/**
|
|
240
|
+
* Determine whether an error should trigger token refresh.
|
|
241
|
+
*
|
|
242
|
+
* @default HTTP status 401
|
|
243
|
+
*/
|
|
244
|
+
shouldRefresh?: (error: unknown) => MaybePromise<boolean>;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Authentication plugin.
|
|
248
|
+
*/
|
|
249
|
+
declare function authPlugin(options?: AuthPluginOptions): Plugin;
|
|
250
|
+
|
|
251
|
+
declare function cachePlugin(): Plugin;
|
|
252
|
+
declare function clearCache(): void;
|
|
253
|
+
|
|
254
|
+
declare function downloadPlugin(): Plugin;
|
|
255
|
+
|
|
256
|
+
declare function loggerPlugin(defaultOptions?: LoggerOptions): Plugin;
|
|
257
|
+
|
|
258
|
+
declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
259
|
+
|
|
260
|
+
declare function uploadPlugin(): Plugin;
|
|
261
|
+
|
|
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 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP request method.
|
|
3
|
+
*/
|
|
4
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
5
|
+
|
|
6
|
+
type QueryValue = string | number | boolean | null | undefined;
|
|
7
|
+
type QueryParams = Record<string, QueryValue | QueryValue[]>;
|
|
8
|
+
type ResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'stream';
|
|
9
|
+
interface RetryOptions {
|
|
10
|
+
retries?: number;
|
|
11
|
+
delay?: number | ((attempt: number, error: unknown) => number | Promise<number>);
|
|
12
|
+
shouldRetry?: (error: unknown, attempt: number) => boolean | Promise<boolean>;
|
|
13
|
+
}
|
|
14
|
+
interface CacheOptions {
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
ttl?: number;
|
|
17
|
+
key?: string;
|
|
18
|
+
}
|
|
19
|
+
interface AuthOptions {
|
|
20
|
+
token?: string | (() => string | Promise<string>);
|
|
21
|
+
scheme?: string;
|
|
22
|
+
}
|
|
23
|
+
interface LoggerOptions {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
}
|
|
26
|
+
type UploadData = FormData | Record<string, unknown>;
|
|
27
|
+
interface UploadOptions {
|
|
28
|
+
data: UploadData;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Download progress information.
|
|
32
|
+
*/
|
|
33
|
+
interface DownloadProgress {
|
|
34
|
+
/**
|
|
35
|
+
* Number of bytes received.
|
|
36
|
+
*/
|
|
37
|
+
loaded: number;
|
|
38
|
+
/**
|
|
39
|
+
* Total response size when Content-Length is available.
|
|
40
|
+
*/
|
|
41
|
+
total?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Progress ratio between 0 and 1 when total is available.
|
|
44
|
+
*/
|
|
45
|
+
progress?: number;
|
|
46
|
+
}
|
|
47
|
+
interface DownloadOptions {
|
|
48
|
+
filename?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Called while the response stream is being consumed.
|
|
51
|
+
*/
|
|
52
|
+
onProgress?: (progress: DownloadProgress) => void;
|
|
53
|
+
}
|
|
54
|
+
interface RequestConfig {
|
|
55
|
+
url: string;
|
|
56
|
+
method?: HttpMethod;
|
|
57
|
+
baseURL?: string;
|
|
58
|
+
headers?: HeadersInit;
|
|
59
|
+
query?: QueryParams;
|
|
60
|
+
body?: BodyInit | Record<string, unknown> | null;
|
|
61
|
+
json?: Record<string, unknown> | unknown[];
|
|
62
|
+
form?: URLSearchParams | Record<string, QueryValue | QueryValue[]>;
|
|
63
|
+
formData?: FormData | Record<string, unknown>;
|
|
64
|
+
timeout?: number;
|
|
65
|
+
signal?: AbortSignal;
|
|
66
|
+
responseType?: ResponseType;
|
|
67
|
+
validateStatus?: (status: number) => boolean;
|
|
68
|
+
retry?: number | RetryOptions;
|
|
69
|
+
cache?: CacheOptions;
|
|
70
|
+
auth?: AuthOptions;
|
|
71
|
+
logger?: LoggerOptions;
|
|
72
|
+
upload?: UploadOptions;
|
|
73
|
+
download?: DownloadOptions;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Npora response.
|
|
78
|
+
*/
|
|
79
|
+
interface NporaResponse<T = unknown> {
|
|
80
|
+
/**
|
|
81
|
+
* Parsed response data.
|
|
82
|
+
*/
|
|
83
|
+
data: T;
|
|
84
|
+
/**
|
|
85
|
+
* HTTP status code.
|
|
86
|
+
*/
|
|
87
|
+
status: number;
|
|
88
|
+
/**
|
|
89
|
+
* HTTP status text.
|
|
90
|
+
*/
|
|
91
|
+
statusText: string;
|
|
92
|
+
/**
|
|
93
|
+
* Response headers.
|
|
94
|
+
*/
|
|
95
|
+
headers: Headers;
|
|
96
|
+
/**
|
|
97
|
+
* Request config.
|
|
98
|
+
*/
|
|
99
|
+
config: RequestConfig;
|
|
100
|
+
/**
|
|
101
|
+
* Raw Fetch Response.
|
|
102
|
+
*/
|
|
103
|
+
raw: Response;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Request adapter.
|
|
108
|
+
*/
|
|
109
|
+
interface Adapter {
|
|
110
|
+
/**
|
|
111
|
+
* Send request.
|
|
112
|
+
*/
|
|
113
|
+
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Client creation options.
|
|
118
|
+
*/
|
|
119
|
+
interface ClientOptions extends Partial<Omit<RequestConfig, 'url' | 'method'>> {
|
|
120
|
+
/**
|
|
121
|
+
* Request adapter.
|
|
122
|
+
*
|
|
123
|
+
* @default FetchAdapter
|
|
124
|
+
*/
|
|
125
|
+
adapter?: Adapter;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
declare class FetchAdapter implements Adapter {
|
|
129
|
+
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
|
|
133
|
+
interface MockAdapterOptions {
|
|
134
|
+
handlers?: Record<string, MockHandler>;
|
|
135
|
+
}
|
|
136
|
+
declare class MockAdapter implements Adapter {
|
|
137
|
+
private readonly handlers;
|
|
138
|
+
constructor(options?: MockAdapterOptions);
|
|
139
|
+
on<T = unknown>(url: string, handler: MockHandler<T>): this;
|
|
140
|
+
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
type Interceptor<T> = (value: T) => T | Promise<T>;
|
|
144
|
+
declare class InterceptorManager<T> {
|
|
145
|
+
private id;
|
|
146
|
+
private readonly interceptors;
|
|
147
|
+
use(interceptor: Interceptor<T>): number;
|
|
148
|
+
eject(id: number): void;
|
|
149
|
+
clear(): void;
|
|
150
|
+
run(value: T): Promise<T>;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
declare class RequestContext<T = unknown> {
|
|
154
|
+
config: RequestConfig;
|
|
155
|
+
response?: NporaResponse<T>;
|
|
156
|
+
error?: unknown;
|
|
157
|
+
readonly startTime: number;
|
|
158
|
+
endTime?: number;
|
|
159
|
+
constructor(config: RequestConfig);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface RetryDecision {
|
|
163
|
+
retry: boolean;
|
|
164
|
+
delay?: number;
|
|
165
|
+
}
|
|
166
|
+
type RequestHook = (context: RequestContext<unknown>) => void | Promise<void>;
|
|
167
|
+
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
|
+
|
|
188
|
+
interface PluginContext {
|
|
189
|
+
interceptors: {
|
|
190
|
+
request: InterceptorManager<RequestConfig>;
|
|
191
|
+
response: InterceptorManager<NporaResponse>;
|
|
192
|
+
error: InterceptorManager<unknown>;
|
|
193
|
+
};
|
|
194
|
+
hooks: Pick<PluginHooks, 'onRequest' | 'onResponse' | 'onError' | 'onRetry'>;
|
|
195
|
+
}
|
|
196
|
+
interface Plugin {
|
|
197
|
+
name: string;
|
|
198
|
+
install: (context: PluginContext) => void;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
202
|
+
/**
|
|
203
|
+
* Token storage used by the authentication extension.
|
|
204
|
+
*
|
|
205
|
+
* Storage implementation is supplied by the application.
|
|
206
|
+
* The request library does not access localStorage or sessionStorage directly.
|
|
207
|
+
*/
|
|
208
|
+
interface AuthTokenStorage {
|
|
209
|
+
get(): MaybePromise<string | null | undefined>;
|
|
210
|
+
set(token: string): MaybePromise<void>;
|
|
211
|
+
remove(): MaybePromise<void>;
|
|
212
|
+
}
|
|
213
|
+
interface AuthPluginOptions {
|
|
214
|
+
/**
|
|
215
|
+
* Access token or access-token provider.
|
|
216
|
+
*
|
|
217
|
+
* When both token and storage are provided,
|
|
218
|
+
* the request-level token has the highest priority,
|
|
219
|
+
* followed by this token option, then storage.
|
|
220
|
+
*/
|
|
221
|
+
token?: string | (() => MaybePromise<string>);
|
|
222
|
+
/**
|
|
223
|
+
* Authorization scheme.
|
|
224
|
+
*
|
|
225
|
+
* @default 'Bearer'
|
|
226
|
+
*/
|
|
227
|
+
scheme?: string;
|
|
228
|
+
/**
|
|
229
|
+
* Optional access-token storage.
|
|
230
|
+
*/
|
|
231
|
+
storage?: AuthTokenStorage;
|
|
232
|
+
/**
|
|
233
|
+
* Refresh the access token.
|
|
234
|
+
*
|
|
235
|
+
* Returning a token stores it through storage when configured.
|
|
236
|
+
* Returning void allows the callback to update external state itself.
|
|
237
|
+
*/
|
|
238
|
+
refreshToken?: () => MaybePromise<string | void>;
|
|
239
|
+
/**
|
|
240
|
+
* Determine whether an error should trigger token refresh.
|
|
241
|
+
*
|
|
242
|
+
* @default HTTP status 401
|
|
243
|
+
*/
|
|
244
|
+
shouldRefresh?: (error: unknown) => MaybePromise<boolean>;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Authentication plugin.
|
|
248
|
+
*/
|
|
249
|
+
declare function authPlugin(options?: AuthPluginOptions): Plugin;
|
|
250
|
+
|
|
251
|
+
declare function cachePlugin(): Plugin;
|
|
252
|
+
declare function clearCache(): void;
|
|
253
|
+
|
|
254
|
+
declare function downloadPlugin(): Plugin;
|
|
255
|
+
|
|
256
|
+
declare function loggerPlugin(defaultOptions?: LoggerOptions): Plugin;
|
|
257
|
+
|
|
258
|
+
declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
259
|
+
|
|
260
|
+
declare function uploadPlugin(): Plugin;
|
|
261
|
+
|
|
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 };
|