@ahoo-wang/fetcher 0.9.2 → 0.9.5
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/README.md +31 -20
- package/README.zh-CN.md +28 -18
- package/dist/fetchInterceptor.d.ts +27 -8
- package/dist/fetchInterceptor.d.ts.map +1 -1
- package/dist/fetcher.d.ts +7 -94
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/fetcherError.d.ts +0 -60
- package/dist/fetcherError.d.ts.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +345 -267
- package/dist/index.umd.js +1 -1
- package/dist/interceptor.d.ts +106 -94
- package/dist/interceptor.d.ts.map +1 -1
- package/dist/interceptorManager.d.ts +200 -0
- package/dist/interceptorManager.d.ts.map +1 -0
- package/dist/requestBodyInterceptor.d.ts +24 -6
- package/dist/requestBodyInterceptor.d.ts.map +1 -1
- package/dist/timeout.d.ts +29 -0
- package/dist/timeout.d.ts.map +1 -1
- package/dist/urlBuilder.d.ts +1 -1
- package/dist/urlBuilder.d.ts.map +1 -1
- package/dist/urlResolveInterceptor.d.ts +24 -9
- package/dist/urlResolveInterceptor.d.ts.map +1 -1
- package/dist/validateStatusInterceptor.d.ts +112 -0
- package/dist/validateStatusInterceptor.d.ts.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
[](https://deepwiki.com/Ahoo-Wang/fetcher)
|
|
10
10
|
|
|
11
11
|
A modern, ultra-lightweight (2.3KiB) HTTP client with built-in path parameters, query parameters, and Axios-like API.
|
|
12
|
-
83%
|
|
13
|
-
smaller than Axios while providing the same powerful features.
|
|
12
|
+
83% smaller than Axios while providing the same powerful features.
|
|
14
13
|
|
|
15
14
|
## 🌟 Features
|
|
16
15
|
|
|
@@ -124,9 +123,11 @@ responses, and errors at different stages of the HTTP request lifecycle.
|
|
|
124
123
|
|
|
125
124
|
Fetcher comes with several built-in interceptors that are automatically registered:
|
|
126
125
|
|
|
127
|
-
1. **UrlResolveInterceptor**: Resolves URLs with path and query parameters (order: Number.MIN_SAFE_INTEGER +
|
|
128
|
-
2. **RequestBodyInterceptor**: Converts object bodies to JSON strings (order: Number.MIN_SAFE_INTEGER +
|
|
129
|
-
3. **FetchInterceptor**: Executes the actual HTTP request (order: Number.MAX_SAFE_INTEGER -
|
|
126
|
+
1. **UrlResolveInterceptor**: Resolves URLs with path and query parameters (order: Number.MIN_SAFE_INTEGER + 1000)
|
|
127
|
+
2. **RequestBodyInterceptor**: Converts object bodies to JSON strings (order: Number.MIN_SAFE_INTEGER + 2000)
|
|
128
|
+
3. **FetchInterceptor**: Executes the actual HTTP request (order: Number.MAX_SAFE_INTEGER - 1000)
|
|
129
|
+
4. **ValidateStatusInterceptor**: Validates HTTP status codes and throws errors for invalid statuses (response
|
|
130
|
+
interceptor, order: Number.MAX_SAFE_INTEGER - 1000)
|
|
130
131
|
|
|
131
132
|
### Using Interceptors
|
|
132
133
|
|
|
@@ -247,6 +248,13 @@ new Fetcher(options ? : FetcherOptions);
|
|
|
247
248
|
- `headers`: Default request headers
|
|
248
249
|
- `interceptors`: Interceptor collection for request, response, and error handling
|
|
249
250
|
|
|
251
|
+
#### Properties
|
|
252
|
+
|
|
253
|
+
- `urlBuilder`: URL builder instance for constructing URLs
|
|
254
|
+
- `headers`: Default request headers
|
|
255
|
+
- `timeout`: Default request timeout
|
|
256
|
+
- `interceptors`: Interceptor collection for request, response, and error handling
|
|
257
|
+
|
|
250
258
|
#### Methods
|
|
251
259
|
|
|
252
260
|
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` - Generic HTTP request method
|
|
@@ -257,13 +265,8 @@ new Fetcher(options ? : FetcherOptions);
|
|
|
257
265
|
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH request
|
|
258
266
|
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD request
|
|
259
267
|
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS request
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
- `urlBuilder`: URL builder instance for constructing URLs
|
|
264
|
-
- `headers`: Default request headers
|
|
265
|
-
- `timeout`: Default request timeout
|
|
266
|
-
- `interceptors`: Interceptor collection for request, response, and error handling
|
|
268
|
+
- `request(request: FetchRequest): Promise<FetchExchange>` - Process an HTTP request through the Fetcher's internal
|
|
269
|
+
workflow
|
|
267
270
|
|
|
268
271
|
### FetcherRequest Interface
|
|
269
272
|
|
|
@@ -274,8 +277,7 @@ Configuration options for HTTP requests.
|
|
|
274
277
|
- `method`: HTTP method (GET, POST, PUT, DELETE, etc.)
|
|
275
278
|
- `headers`: Request headers
|
|
276
279
|
- `body`: Request body (can be object, string, Blob, etc.)
|
|
277
|
-
- `
|
|
278
|
-
- `query`: Query parameters for URL query string
|
|
280
|
+
- `urlParams`: URL parameters including path parameters for URL templating and query parameters for URL query string
|
|
279
281
|
- `timeout`: Request timeout in milliseconds
|
|
280
282
|
|
|
281
283
|
### Response Extension
|
|
@@ -333,9 +335,13 @@ Interceptor interface that defines the basic structure of interceptors.
|
|
|
333
335
|
|
|
334
336
|
- `intercept(exchange: FetchExchange): void | Promise<void>` - Intercept and process data
|
|
335
337
|
|
|
336
|
-
####
|
|
338
|
+
#### InterceptorRegistry Class
|
|
339
|
+
|
|
340
|
+
Registry for managing multiple interceptors of the same type.
|
|
337
341
|
|
|
338
|
-
|
|
342
|
+
**Properties:**
|
|
343
|
+
|
|
344
|
+
- `interceptors: Interceptor[]` - Get all interceptors in the registry
|
|
339
345
|
|
|
340
346
|
**Methods:**
|
|
341
347
|
|
|
@@ -344,15 +350,20 @@ Interceptor manager for managing multiple interceptors of the same type.
|
|
|
344
350
|
- `clear(): void` - Clear all interceptors
|
|
345
351
|
- `intercept(exchange: FetchExchange): Promise<void>` - Execute all interceptors in sequence
|
|
346
352
|
|
|
347
|
-
####
|
|
353
|
+
#### InterceptorManager Class
|
|
348
354
|
|
|
349
355
|
Fetcher interceptor collection, including request, response, and error interceptor managers.
|
|
350
356
|
|
|
351
357
|
**Properties:**
|
|
352
358
|
|
|
353
|
-
- `request:
|
|
354
|
-
- `response:
|
|
355
|
-
- `error:
|
|
359
|
+
- `request: InterceptorRegistry` - Request interceptor manager
|
|
360
|
+
- `response: InterceptorRegistry` - Response interceptor manager
|
|
361
|
+
- `error: InterceptorRegistry` - Error interceptor manager
|
|
362
|
+
|
|
363
|
+
**Methods:**
|
|
364
|
+
|
|
365
|
+
- `exchange(fetchExchange: FetchExchange): Promise<FetchExchange>` - Process a FetchExchange through the interceptor
|
|
366
|
+
pipeline, executing request, response, and error interceptors in sequence
|
|
356
367
|
|
|
357
368
|
## 🤝 Contributing
|
|
358
369
|
|
package/README.zh-CN.md
CHANGED
|
@@ -121,9 +121,11 @@ Fetcher 中的拦截器系统遵循中间件模式,允许您在 HTTP 请求生
|
|
|
121
121
|
|
|
122
122
|
Fetcher 自带几个内置拦截器,它们会自动注册:
|
|
123
123
|
|
|
124
|
-
1. **UrlResolveInterceptor**:解析带路径和查询参数的 URL(顺序:Number.MIN_SAFE_INTEGER +
|
|
125
|
-
2. **RequestBodyInterceptor**:将对象体转换为 JSON 字符串(顺序:Number.MIN_SAFE_INTEGER +
|
|
126
|
-
3. **FetchInterceptor**:执行实际的 HTTP 请求(顺序:Number.MAX_SAFE_INTEGER -
|
|
124
|
+
1. **UrlResolveInterceptor**:解析带路径和查询参数的 URL(顺序:Number.MIN_SAFE_INTEGER + 1000)
|
|
125
|
+
2. **RequestBodyInterceptor**:将对象体转换为 JSON 字符串(顺序:Number.MIN_SAFE_INTEGER + 2000)
|
|
126
|
+
3. **FetchInterceptor**:执行实际的 HTTP 请求(顺序:Number.MAX_SAFE_INTEGER - 1000)
|
|
127
|
+
4. **ValidateStatusInterceptor**:验证 HTTP 状态码并在状态码无效时抛出错误(响应拦截器,顺序:Number.MAX_SAFE_INTEGER -
|
|
128
|
+
1000)
|
|
127
129
|
|
|
128
130
|
### 使用拦截器
|
|
129
131
|
|
|
@@ -244,6 +246,13 @@ new Fetcher(options ? : FetcherOptions);
|
|
|
244
246
|
- `headers`:默认请求头部
|
|
245
247
|
- `interceptors`:用于请求、响应和错误处理的拦截器集合
|
|
246
248
|
|
|
249
|
+
#### 属性
|
|
250
|
+
|
|
251
|
+
- `urlBuilder`:用于构建 URL 的 URL 构建器实例
|
|
252
|
+
- `headers`:默认请求头部
|
|
253
|
+
- `timeout`:默认请求超时时间
|
|
254
|
+
- `interceptors`:用于请求、响应和错误处理的拦截器集合
|
|
255
|
+
|
|
247
256
|
#### 方法
|
|
248
257
|
|
|
249
258
|
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` - 通用 HTTP 请求方法
|
|
@@ -254,13 +263,7 @@ new Fetcher(options ? : FetcherOptions);
|
|
|
254
263
|
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH 请求
|
|
255
264
|
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD 请求
|
|
256
265
|
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS 请求
|
|
257
|
-
|
|
258
|
-
#### 属性
|
|
259
|
-
|
|
260
|
-
- `urlBuilder`:用于构建 URL 的 URL 构建器实例
|
|
261
|
-
- `headers`:默认请求头部
|
|
262
|
-
- `timeout`:默认请求超时时间
|
|
263
|
-
- `interceptors`:用于请求、响应和错误处理的拦截器集合
|
|
266
|
+
- `request(request: FetchRequest): Promise<FetchExchange>` - 通过 Fetcher 的内部工作流处理 HTTP 请求
|
|
264
267
|
|
|
265
268
|
### FetcherRequest 接口
|
|
266
269
|
|
|
@@ -271,8 +274,7 @@ HTTP 请求的配置选项。
|
|
|
271
274
|
- `method`:HTTP 方法(GET、POST、PUT、DELETE 等)
|
|
272
275
|
- `headers`:请求头部
|
|
273
276
|
- `body`:请求体(可以是对象、字符串、Blob 等)
|
|
274
|
-
- `
|
|
275
|
-
- `query`:用于 URL 查询字符串的查询参数
|
|
277
|
+
- `urlParams`:URL 参数,包括用于 URL 模板的路径参数和用于 URL 查询字符串的查询参数
|
|
276
278
|
- `timeout`:请求超时时间(毫秒)
|
|
277
279
|
|
|
278
280
|
### 响应扩展
|
|
@@ -330,9 +332,13 @@ string, options ? : FetcherOptions
|
|
|
330
332
|
|
|
331
333
|
- `intercept(exchange: FetchExchange): void | Promise<void>` - 拦截并处理数据
|
|
332
334
|
|
|
333
|
-
####
|
|
335
|
+
#### InterceptorRegistry 类
|
|
336
|
+
|
|
337
|
+
用于管理同一类型多个拦截器的拦截器注册表。
|
|
334
338
|
|
|
335
|
-
|
|
339
|
+
**属性:**
|
|
340
|
+
|
|
341
|
+
- `interceptors: Interceptor[]` - 获取注册表中的所有拦截器
|
|
336
342
|
|
|
337
343
|
**方法:**
|
|
338
344
|
|
|
@@ -341,15 +347,19 @@ string, options ? : FetcherOptions
|
|
|
341
347
|
- `clear(): void` - 清除所有拦截器
|
|
342
348
|
- `intercept(exchange: FetchExchange): Promise<void>` - 顺序执行所有拦截器
|
|
343
349
|
|
|
344
|
-
####
|
|
350
|
+
#### InterceptorManager 类
|
|
345
351
|
|
|
346
352
|
Fetcher 拦截器集合,包括请求、响应和错误拦截器管理器。
|
|
347
353
|
|
|
348
354
|
**属性:**
|
|
349
355
|
|
|
350
|
-
- `request:
|
|
351
|
-
- `response:
|
|
352
|
-
- `error:
|
|
356
|
+
- `request: InterceptorRegistry` - 请求拦截器管理器
|
|
357
|
+
- `response: InterceptorRegistry` - 响应拦截器管理器
|
|
358
|
+
- `error: InterceptorRegistry` - 错误拦截器管理器
|
|
359
|
+
|
|
360
|
+
**方法:**
|
|
361
|
+
|
|
362
|
+
- `exchange(fetchExchange: FetchExchange): Promise<FetchExchange>` - 通过拦截器管道处理 FetchExchange
|
|
353
363
|
|
|
354
364
|
## 🤝 贡献
|
|
355
365
|
|
|
@@ -1,36 +1,55 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RequestInterceptor } from './interceptor';
|
|
2
2
|
import { FetchExchange } from './fetchExchange';
|
|
3
|
+
/**
|
|
4
|
+
* The name of the FetchInterceptor.
|
|
5
|
+
*/
|
|
6
|
+
export declare const FETCH_INTERCEPTOR_NAME = "FetchInterceptor";
|
|
7
|
+
/**
|
|
8
|
+
* The order of the FetchInterceptor.
|
|
9
|
+
* Set to Number.MAX_SAFE_INTEGER - 1000 to ensure it runs latest among request interceptors.
|
|
10
|
+
*/
|
|
11
|
+
export declare const FETCH_INTERCEPTOR_ORDER: number;
|
|
3
12
|
/**
|
|
4
13
|
* Interceptor implementation responsible for executing actual HTTP requests.
|
|
5
14
|
*
|
|
6
15
|
* This is an interceptor implementation responsible for executing actual HTTP requests
|
|
7
|
-
* and handling timeout control. It is the
|
|
16
|
+
* and handling timeout control. It is the latest interceptor in the Fetcher request
|
|
8
17
|
* processing chain, ensuring that the actual network request is executed after all
|
|
9
18
|
* previous interceptors have completed processing.
|
|
10
19
|
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* This interceptor runs at the very end of the request interceptor chain to ensure
|
|
22
|
+
* that the actual HTTP request is executed after all other request processing is complete.
|
|
23
|
+
* The order is set to FETCH_INTERCEPTOR_ORDER to ensure it executes after all other
|
|
24
|
+
* request interceptors, completing the request processing pipeline before the network
|
|
25
|
+
* request is made. This positioning ensures that all request preprocessing is
|
|
26
|
+
* completed before the actual network request is made.
|
|
27
|
+
*
|
|
11
28
|
* @example
|
|
12
29
|
* // Usually not created manually as Fetcher uses it automatically
|
|
13
30
|
* const fetcher = new Fetcher();
|
|
14
31
|
* // FetchInterceptor is automatically registered in fetcher.interceptors.request
|
|
15
32
|
*/
|
|
16
|
-
export declare class FetchInterceptor implements
|
|
33
|
+
export declare class FetchInterceptor implements RequestInterceptor {
|
|
17
34
|
/**
|
|
18
35
|
* Interceptor name, used to identify and manage interceptor instances.
|
|
19
36
|
*
|
|
20
37
|
* Each interceptor must have a unique name for identification and manipulation
|
|
21
38
|
* within the interceptor manager.
|
|
22
39
|
*/
|
|
23
|
-
name
|
|
40
|
+
readonly name = "FetchInterceptor";
|
|
24
41
|
/**
|
|
25
|
-
* Interceptor execution order, set to near maximum safe integer to ensure
|
|
42
|
+
* Interceptor execution order, set to near maximum safe integer to ensure latest execution.
|
|
26
43
|
*
|
|
27
44
|
* Since this is the interceptor that actually executes HTTP requests, it should
|
|
28
45
|
* execute after all other request interceptors, so its order is set to
|
|
29
|
-
*
|
|
46
|
+
* FETCH_INTERCEPTOR_ORDER. This ensures that all request preprocessing is
|
|
30
47
|
* completed before the actual network request is made, while still allowing
|
|
31
|
-
* other interceptors to run after it if needed.
|
|
48
|
+
* other interceptors to run after it if needed. The positioning at the end
|
|
49
|
+
* of the request chain ensures that all transformations and validations are
|
|
50
|
+
* completed before the network request is executed.
|
|
32
51
|
*/
|
|
33
|
-
order: number;
|
|
52
|
+
readonly order: number;
|
|
34
53
|
/**
|
|
35
54
|
* Intercept and process HTTP requests.
|
|
36
55
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchInterceptor.d.ts","sourceRoot":"","sources":["../src/fetchInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"fetchInterceptor.d.ts","sourceRoot":"","sources":["../src/fetchInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;GAEG;AACH,eAAO,MAAM,sBAAsB,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,QAAiC,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,gBAAiB,YAAW,kBAAkB;IACzD;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,sBAA0B;IAEvC;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,KAAK,SAA2B;IAEzC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa;CAGxC"}
|
package/dist/fetcher.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { UrlBuilder, UrlBuilderCapable } from './urlBuilder';
|
|
2
2
|
import { TimeoutCapable } from './timeout';
|
|
3
|
-
import { FetcherInterceptors } from './interceptor';
|
|
4
3
|
import { FetchExchange } from './fetchExchange';
|
|
5
4
|
import { BaseURLCapable, FetchRequest, FetchRequestInit, RequestHeaders, RequestHeadersCapable } from './fetchRequest';
|
|
5
|
+
import { InterceptorManager } from './interceptorManager';
|
|
6
6
|
/**
|
|
7
7
|
* Configuration options for the Fetcher client.
|
|
8
8
|
*
|
|
@@ -15,12 +15,12 @@ import { BaseURLCapable, FetchRequest, FetchRequestInit, RequestHeaders, Request
|
|
|
15
15
|
* baseURL: 'https://api.example.com',
|
|
16
16
|
* headers: { 'Content-Type': 'application/json' },
|
|
17
17
|
* timeout: 5000,
|
|
18
|
-
* interceptors: new
|
|
18
|
+
* interceptors: new InterceptorManager()
|
|
19
19
|
* };
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
22
|
export interface FetcherOptions extends BaseURLCapable, RequestHeadersCapable, TimeoutCapable {
|
|
23
|
-
interceptors?:
|
|
23
|
+
interceptors?: InterceptorManager;
|
|
24
24
|
}
|
|
25
25
|
export declare const DEFAULT_OPTIONS: FetcherOptions;
|
|
26
26
|
/**
|
|
@@ -43,10 +43,10 @@ export declare const DEFAULT_OPTIONS: FetcherOptions;
|
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
45
|
export declare class Fetcher implements UrlBuilderCapable, RequestHeadersCapable, TimeoutCapable {
|
|
46
|
-
urlBuilder: UrlBuilder;
|
|
47
|
-
headers?: RequestHeaders;
|
|
48
|
-
timeout?: number;
|
|
49
|
-
interceptors:
|
|
46
|
+
readonly urlBuilder: UrlBuilder;
|
|
47
|
+
readonly headers?: RequestHeaders;
|
|
48
|
+
readonly timeout?: number;
|
|
49
|
+
readonly interceptors: InterceptorManager;
|
|
50
50
|
/**
|
|
51
51
|
* Initializes a new Fetcher instance with optional configuration.
|
|
52
52
|
*
|
|
@@ -80,93 +80,6 @@ export declare class Fetcher implements UrlBuilderCapable, RequestHeadersCapable
|
|
|
80
80
|
* @throws Error if an unhandled error occurs during request processing
|
|
81
81
|
*/
|
|
82
82
|
request(request: FetchRequest): Promise<FetchExchange>;
|
|
83
|
-
/**
|
|
84
|
-
* Processes a FetchExchange through the interceptor pipeline.
|
|
85
|
-
*
|
|
86
|
-
* This method is the core of the Fetcher's interceptor system. It executes the three
|
|
87
|
-
* phases of interceptors in sequence:
|
|
88
|
-
* 1. Request interceptors - Process the request before sending
|
|
89
|
-
* 2. Response interceptors - Process the response after receiving
|
|
90
|
-
* 3. Error interceptors - Handle any errors that occurred during the process
|
|
91
|
-
*
|
|
92
|
-
* The interceptor pipeline follows the Chain of Responsibility pattern, where each
|
|
93
|
-
* interceptor can modify the exchange object and decide whether to continue or
|
|
94
|
-
* terminate the chain.
|
|
95
|
-
*
|
|
96
|
-
* @param fetchExchange - The exchange object containing request, response, and error information
|
|
97
|
-
* @returns Promise that resolves to the processed FetchExchange
|
|
98
|
-
* @throws ExchangeError if an unhandled error occurs during processing
|
|
99
|
-
*
|
|
100
|
-
* @remarks
|
|
101
|
-
* The method handles three distinct phases:
|
|
102
|
-
*
|
|
103
|
-
* 1. Request Phase: Executes request interceptors which can modify headers, URL, body, etc.
|
|
104
|
-
* Built-in interceptors handle URL resolution, body serialization, and actual HTTP execution.
|
|
105
|
-
*
|
|
106
|
-
* 2. Response Phase: Executes response interceptors which can transform or validate responses.
|
|
107
|
-
* These interceptors only run if the request phase completed without throwing.
|
|
108
|
-
*
|
|
109
|
-
* 3. Error Phase: Executes error interceptors when any phase throws an error. Error interceptors
|
|
110
|
-
* can handle errors by clearing the error property. If error interceptors clear the error,
|
|
111
|
-
* the exchange is returned successfully.
|
|
112
|
-
*
|
|
113
|
-
* Error Handling:
|
|
114
|
-
* - If any interceptor throws an error, the error phase is triggered
|
|
115
|
-
* - Error interceptors can "fix" errors by clearing the error property on the exchange
|
|
116
|
-
* - If errors remain after error interceptors run, they are wrapped in ExchangeError
|
|
117
|
-
*
|
|
118
|
-
* Order of Execution:
|
|
119
|
-
* 1. Request interceptors (sorted by order property, ascending)
|
|
120
|
-
* 2. Response interceptors (sorted by order property, ascending) - only if no error in request phase
|
|
121
|
-
* 3. Error interceptors (sorted by order property, ascending) - only if an error occurred
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```typescript
|
|
125
|
-
* // Create a fetcher with custom interceptors
|
|
126
|
-
* const fetcher = new Fetcher();
|
|
127
|
-
*
|
|
128
|
-
* // Add a request interceptor
|
|
129
|
-
* fetcher.interceptors.request.use({
|
|
130
|
-
* name: 'AuthInterceptor',
|
|
131
|
-
* order: 100,
|
|
132
|
-
* async intercept(exchange: FetchExchange) {
|
|
133
|
-
* exchange.request.headers = {
|
|
134
|
-
* ...exchange.request.headers,
|
|
135
|
-
* 'Authorization': 'Bearer ' + getToken()
|
|
136
|
-
* };
|
|
137
|
-
* }
|
|
138
|
-
* });
|
|
139
|
-
*
|
|
140
|
-
* // Add a response interceptor
|
|
141
|
-
* fetcher.interceptors.response.use({
|
|
142
|
-
* name: 'ResponseLogger',
|
|
143
|
-
* order: 100,
|
|
144
|
-
* async intercept(exchange: FetchExchange) {
|
|
145
|
-
* console.log(`Response status: ${exchange.response?.status}`);
|
|
146
|
-
* }
|
|
147
|
-
* });
|
|
148
|
-
*
|
|
149
|
-
* // Add an error interceptor
|
|
150
|
-
* fetcher.interceptors.error.use({
|
|
151
|
-
* name: 'ErrorLogger',
|
|
152
|
-
* order: 100,
|
|
153
|
-
* async intercept(exchange: FetchExchange) {
|
|
154
|
-
* console.error(`Request to ${exchange.request.url} failed:`, exchange.error);
|
|
155
|
-
* // Clear the error to indicate it's been handled
|
|
156
|
-
* exchange.error = undefined;
|
|
157
|
-
* }
|
|
158
|
-
* });
|
|
159
|
-
*
|
|
160
|
-
* // Create and process an exchange
|
|
161
|
-
* const request: FetchRequest = {
|
|
162
|
-
* url: '/api/users',
|
|
163
|
-
* method: HttpMethod.GET
|
|
164
|
-
* };
|
|
165
|
-
* const exchange = new FetchExchange(fetcher, request);
|
|
166
|
-
* const result = await fetcher.exchange(exchange);
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
exchange(fetchExchange: FetchExchange): Promise<FetchExchange>;
|
|
170
83
|
/**
|
|
171
84
|
* Internal helper method for making HTTP requests with a specific method.
|
|
172
85
|
*
|
package/dist/fetcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAkB,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAkB,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EACL,cAAc,EAEd,YAAY,EACZ,gBAAgB,EAEhB,cAAc,EACd,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,qBAAqB,EACrB,cAAc;IAChB,YAAY,CAAC,EAAE,kBAAkB,CAAC;CACnC;AAMD,eAAO,MAAM,eAAe,EAAE,cAG7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,OACX,YAAW,iBAAiB,EAAE,qBAAqB,EAAE,cAAc;IAEnE,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,QAAQ,CAAC,OAAO,CAAC,EAAE,cAAc,CAAmB;IACpD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,YAAY,EAAE,kBAAkB,CAAC;IAE1C;;;;;;;OAOG;gBACS,OAAO,GAAE,cAAgC;IAOrD;;;;;;;;;;OAUG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAa3E;;;;;;;;;;OAUG;IACG,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAa5D;;;;;;;;;;OAUG;YACW,WAAW;IAWzB;;;;;;;;;OASG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,MAAM,CACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,KAAK,CACT,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;;OASG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;;OASG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;CAGrB"}
|
package/dist/fetcherError.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { FetchExchange } from './fetchExchange';
|
|
2
|
-
import { FetchRequest } from './fetchRequest';
|
|
3
2
|
/**
|
|
4
3
|
* Base error class for all Fetcher-related errors.
|
|
5
4
|
*
|
|
@@ -31,37 +30,6 @@ export declare class FetcherError extends Error {
|
|
|
31
30
|
*/
|
|
32
31
|
constructor(errorMsg?: string, cause?: (Error | any) | undefined);
|
|
33
32
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Custom error class for FetchExchange related errors.
|
|
36
|
-
*
|
|
37
|
-
* This error is thrown when there are issues with the HTTP exchange process,
|
|
38
|
-
* such as when a request fails and no response is generated. It provides
|
|
39
|
-
* comprehensive information about the failed request through the exchange object.
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```typescript
|
|
43
|
-
* try {
|
|
44
|
-
* await fetcher.get('/api/users');
|
|
45
|
-
* } catch (error) {
|
|
46
|
-
* if (error instanceof ExchangeError) {
|
|
47
|
-
* console.log('Request URL:', error.exchange.request.url);
|
|
48
|
-
* console.log('Request method:', error.exchange.request.method);
|
|
49
|
-
* if (error.exchange.error) {
|
|
50
|
-
* console.log('Underlying error:', error.exchange.error);
|
|
51
|
-
* }
|
|
52
|
-
* }
|
|
53
|
-
* }
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export declare class ExchangeError extends FetcherError {
|
|
57
|
-
readonly exchange: FetchExchange;
|
|
58
|
-
/**
|
|
59
|
-
* Creates a new ExchangeError instance.
|
|
60
|
-
*
|
|
61
|
-
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
62
|
-
*/
|
|
63
|
-
constructor(exchange: FetchExchange);
|
|
64
|
-
}
|
|
65
33
|
/**
|
|
66
34
|
* Custom error class for Fetcher request failures.
|
|
67
35
|
*
|
|
@@ -91,32 +59,4 @@ export declare class FetchError extends FetcherError {
|
|
|
91
59
|
*/
|
|
92
60
|
constructor(exchange: FetchExchange, errorMsg?: string);
|
|
93
61
|
}
|
|
94
|
-
/**
|
|
95
|
-
* Exception class thrown when an HTTP request times out.
|
|
96
|
-
*
|
|
97
|
-
* This error is thrown by the timeoutFetch function when a request exceeds its timeout limit.
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* try {
|
|
102
|
-
* const response = await timeoutFetch('https://api.example.com/users', {}, 1000);
|
|
103
|
-
* } catch (error) {
|
|
104
|
-
* if (error instanceof FetchTimeoutError) {
|
|
105
|
-
* console.log(`Request timed out after ${error.timeout}ms`);
|
|
106
|
-
* }
|
|
107
|
-
* }
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
export declare class FetchTimeoutError extends FetcherError {
|
|
111
|
-
/**
|
|
112
|
-
* The request options that timed out.
|
|
113
|
-
*/
|
|
114
|
-
request: FetchRequest;
|
|
115
|
-
/**
|
|
116
|
-
* Creates a new FetchTimeoutError instance.
|
|
117
|
-
*
|
|
118
|
-
* @param request - The request options that timed out
|
|
119
|
-
*/
|
|
120
|
-
constructor(request: FetchRequest);
|
|
121
|
-
}
|
|
122
62
|
//# sourceMappingURL=fetcherError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcherError.d.ts","sourceRoot":"","sources":["../src/fetcherError.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"fetcherError.d.ts","sourceRoot":"","sources":["../src/fetcherError.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,YAAa,SAAQ,KAAK;aASnB,KAAK,CAAC,GAAE,KAAK,GAAG,GAAG;IARrC;;;;;OAKG;gBAED,QAAQ,CAAC,EAAE,MAAM,EACD,KAAK,CAAC,GAAE,KAAK,GAAG,GAAG,aAAA;CAetC;AAGD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,UAAW,SAAQ,YAAY;aAQxB,QAAQ,EAAE,aAAa;IAPzC;;;;;OAKG;gBAEe,QAAQ,EAAE,aAAa,EACvC,QAAQ,CAAC,EAAE,MAAM;CAUpB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export * from './fetcherError';
|
|
2
1
|
export * from './fetcher';
|
|
2
|
+
export * from './fetcherError';
|
|
3
3
|
export * from './fetcherRegistrar';
|
|
4
4
|
export * from './fetchExchange';
|
|
5
5
|
export * from './fetchInterceptor';
|
|
6
6
|
export * from './fetchRequest';
|
|
7
7
|
export * from './interceptor';
|
|
8
|
+
export * from './interceptorManager';
|
|
8
9
|
export * from './mergeRequest';
|
|
9
10
|
export * from './namedFetcher';
|
|
10
11
|
export * from './orderedCapable';
|
|
@@ -15,4 +16,5 @@ export * from './urlBuilder';
|
|
|
15
16
|
export * from './urlResolveInterceptor';
|
|
16
17
|
export * from './urls';
|
|
17
18
|
export * from './utils';
|
|
19
|
+
export * from './validateStatusInterceptor';
|
|
18
20
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,yBAAyB,CAAC;AACxC,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,6BAA6B,CAAC"}
|