@ahoo-wang/fetcher 0.9.1 → 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 +33 -21
- package/README.zh-CN.md +30 -20
- package/dist/fetchInterceptor.d.ts +27 -8
- package/dist/fetchInterceptor.d.ts.map +1 -1
- package/dist/fetcher.d.ts +8 -25
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/fetcherError.d.ts +62 -0
- package/dist/fetcherError.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +419 -233
- 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 +28 -27
- 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
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
9
9
|
[](https://deepwiki.com/Ahoo-Wang/fetcher)
|
|
10
10
|
|
|
11
|
-
A modern, ultra-lightweight (
|
|
12
|
-
smaller than Axios while providing the same powerful features.
|
|
11
|
+
A modern, ultra-lightweight (2.3KiB) HTTP client with built-in path parameters, query parameters, and Axios-like API.
|
|
12
|
+
83% smaller than Axios while providing the same powerful features.
|
|
13
13
|
|
|
14
14
|
## 🌟 Features
|
|
15
15
|
|
|
16
|
-
- **⚡ Ultra-Lightweight**: Only
|
|
16
|
+
- **⚡ Ultra-Lightweight**: Only 2.3KiB min+gzip - 83% smaller than Axios
|
|
17
17
|
- **🧭 Path & Query Parameters**: Built-in support for path (`{id}`) and query parameters
|
|
18
18
|
- **🔗 Interceptor System**: Request, response, and error interceptors for middleware patterns
|
|
19
19
|
- **⏱️ Timeout Control**: Configurable request timeouts with proper error handling
|
|
@@ -123,9 +123,11 @@ responses, and errors at different stages of the HTTP request lifecycle.
|
|
|
123
123
|
|
|
124
124
|
Fetcher comes with several built-in interceptors that are automatically registered:
|
|
125
125
|
|
|
126
|
-
1. **UrlResolveInterceptor**: Resolves URLs with path and query parameters (order: Number.MIN_SAFE_INTEGER +
|
|
127
|
-
2. **RequestBodyInterceptor**: Converts object bodies to JSON strings (order: Number.MIN_SAFE_INTEGER +
|
|
128
|
-
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)
|
|
129
131
|
|
|
130
132
|
### Using Interceptors
|
|
131
133
|
|
|
@@ -246,6 +248,13 @@ new Fetcher(options ? : FetcherOptions);
|
|
|
246
248
|
- `headers`: Default request headers
|
|
247
249
|
- `interceptors`: Interceptor collection for request, response, and error handling
|
|
248
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
|
+
|
|
249
258
|
#### Methods
|
|
250
259
|
|
|
251
260
|
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` - Generic HTTP request method
|
|
@@ -256,13 +265,8 @@ new Fetcher(options ? : FetcherOptions);
|
|
|
256
265
|
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH request
|
|
257
266
|
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD request
|
|
258
267
|
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS request
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
- `urlBuilder`: URL builder instance for constructing URLs
|
|
263
|
-
- `headers`: Default request headers
|
|
264
|
-
- `timeout`: Default request timeout
|
|
265
|
-
- `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
|
|
266
270
|
|
|
267
271
|
### FetcherRequest Interface
|
|
268
272
|
|
|
@@ -273,8 +277,7 @@ Configuration options for HTTP requests.
|
|
|
273
277
|
- `method`: HTTP method (GET, POST, PUT, DELETE, etc.)
|
|
274
278
|
- `headers`: Request headers
|
|
275
279
|
- `body`: Request body (can be object, string, Blob, etc.)
|
|
276
|
-
- `
|
|
277
|
-
- `query`: Query parameters for URL query string
|
|
280
|
+
- `urlParams`: URL parameters including path parameters for URL templating and query parameters for URL query string
|
|
278
281
|
- `timeout`: Request timeout in milliseconds
|
|
279
282
|
|
|
280
283
|
### Response Extension
|
|
@@ -332,9 +335,13 @@ Interceptor interface that defines the basic structure of interceptors.
|
|
|
332
335
|
|
|
333
336
|
- `intercept(exchange: FetchExchange): void | Promise<void>` - Intercept and process data
|
|
334
337
|
|
|
335
|
-
####
|
|
338
|
+
#### InterceptorRegistry Class
|
|
339
|
+
|
|
340
|
+
Registry for managing multiple interceptors of the same type.
|
|
336
341
|
|
|
337
|
-
|
|
342
|
+
**Properties:**
|
|
343
|
+
|
|
344
|
+
- `interceptors: Interceptor[]` - Get all interceptors in the registry
|
|
338
345
|
|
|
339
346
|
**Methods:**
|
|
340
347
|
|
|
@@ -343,15 +350,20 @@ Interceptor manager for managing multiple interceptors of the same type.
|
|
|
343
350
|
- `clear(): void` - Clear all interceptors
|
|
344
351
|
- `intercept(exchange: FetchExchange): Promise<void>` - Execute all interceptors in sequence
|
|
345
352
|
|
|
346
|
-
####
|
|
353
|
+
#### InterceptorManager Class
|
|
347
354
|
|
|
348
355
|
Fetcher interceptor collection, including request, response, and error interceptor managers.
|
|
349
356
|
|
|
350
357
|
**Properties:**
|
|
351
358
|
|
|
352
|
-
- `request:
|
|
353
|
-
- `response:
|
|
354
|
-
- `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
|
|
355
367
|
|
|
356
368
|
## 🤝 Contributing
|
|
357
369
|
|
package/README.zh-CN.md
CHANGED
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
9
9
|
[](https://deepwiki.com/Ahoo-Wang/fetcher)
|
|
10
10
|
|
|
11
|
-
一个现代、超轻量级(
|
|
11
|
+
一个现代、超轻量级(2.3kB)的 HTTP 客户端,内置路径参数、查询参数和类似 Axios 的 API。比 Axios 小 83%,同时提供相同的强大功能。
|
|
12
12
|
|
|
13
13
|
## 🌟 特性
|
|
14
14
|
|
|
15
|
-
- **⚡ 超轻量级**:仅
|
|
15
|
+
- **⚡ 超轻量级**:仅 2.3KiB min+gzip - 比 Axios 小 83%
|
|
16
16
|
- **🧭 路径和查询参数**:内置支持路径(`{id}`)和查询参数
|
|
17
17
|
- **🔗 拦截器系统**:请求、响应和错误拦截器的中间件模式
|
|
18
18
|
- **⏱️ 超时控制**:可配置的请求超时和适当的错误处理
|
|
@@ -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
|
*
|
|
@@ -65,7 +65,7 @@ export declare class Fetcher implements UrlBuilderCapable, RequestHeadersCapable
|
|
|
65
65
|
* @param url - The URL path for the request (relative to baseURL if set)
|
|
66
66
|
* @param request - Request configuration including headers, body, parameters, etc.
|
|
67
67
|
* @returns Promise that resolves to the HTTP response
|
|
68
|
-
* @throws
|
|
68
|
+
* @throws FetchError if the request fails and no response is generated
|
|
69
69
|
*/
|
|
70
70
|
fetch(url: string, request?: FetchRequestInit): Promise<Response>;
|
|
71
71
|
/**
|
|
@@ -80,23 +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 chain.
|
|
85
|
-
*
|
|
86
|
-
* Orchestrates the complete request lifecycle by applying interceptors in sequence:
|
|
87
|
-
* 1. Request interceptors - Modify the outgoing request
|
|
88
|
-
* 2. Response interceptors - Process the incoming response
|
|
89
|
-
*
|
|
90
|
-
* Error handling follows this flow:
|
|
91
|
-
* - If an error occurs, error interceptors are invoked
|
|
92
|
-
* - If an error interceptor produces a response, it's returned
|
|
93
|
-
* - Otherwise, the original error is re-thrown
|
|
94
|
-
*
|
|
95
|
-
* @param fetchExchange - The exchange object containing request and response data
|
|
96
|
-
* @returns Promise resolving to the processed exchange
|
|
97
|
-
* @throws Error if an unhandled error occurs during processing
|
|
98
|
-
*/
|
|
99
|
-
exchange(fetchExchange: FetchExchange): Promise<FetchExchange>;
|
|
100
83
|
/**
|
|
101
84
|
* Internal helper method for making HTTP requests with a specific method.
|
|
102
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"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { FetchExchange } from './fetchExchange';
|
|
2
|
+
/**
|
|
3
|
+
* Base error class for all Fetcher-related errors.
|
|
4
|
+
*
|
|
5
|
+
* This class extends the native Error class and provides a foundation for
|
|
6
|
+
* all custom errors thrown by the Fetcher library. It includes support for
|
|
7
|
+
* error chaining through the cause property.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* try {
|
|
12
|
+
* await fetcher.get('/api/users');
|
|
13
|
+
* } catch (error) {
|
|
14
|
+
* if (error instanceof FetcherError) {
|
|
15
|
+
* console.log('Fetcher error:', error.message);
|
|
16
|
+
* if (error.cause) {
|
|
17
|
+
* console.log('Caused by:', error.cause);
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class FetcherError extends Error {
|
|
24
|
+
readonly cause?: (Error | any) | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new FetcherError instance.
|
|
27
|
+
*
|
|
28
|
+
* @param errorMsg - Optional error message. If not provided, will use the cause's message or a default message.
|
|
29
|
+
* @param cause - Optional underlying error that caused this error.
|
|
30
|
+
*/
|
|
31
|
+
constructor(errorMsg?: string, cause?: (Error | any) | undefined);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Custom error class for Fetcher request failures.
|
|
35
|
+
*
|
|
36
|
+
* This error is thrown when a fetch request fails and no response is generated.
|
|
37
|
+
* It wraps the FetchExchange object to provide comprehensive information about the failed request.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* try {
|
|
42
|
+
* await fetcher.get('/api/users');
|
|
43
|
+
* } catch (error) {
|
|
44
|
+
* if (error instanceof FetchError) {
|
|
45
|
+
* console.log('Failed URL:', error.exchange.request.url);
|
|
46
|
+
* console.log('Request headers:', error.exchange.request.headers);
|
|
47
|
+
* console.log('Interceptor attributes:', error.exchange.attributes);
|
|
48
|
+
* }
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare class FetchError extends FetcherError {
|
|
53
|
+
readonly exchange: FetchExchange;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new FetchError instance.
|
|
56
|
+
*
|
|
57
|
+
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
58
|
+
* @param errorMsg - Optional custom error message. If not provided, will use the exchange's error message.
|
|
59
|
+
*/
|
|
60
|
+
constructor(exchange: FetchExchange, errorMsg?: string);
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=fetcherError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,9 +1,11 @@
|
|
|
1
1
|
export * from './fetcher';
|
|
2
|
+
export * from './fetcherError';
|
|
2
3
|
export * from './fetcherRegistrar';
|
|
3
4
|
export * from './fetchExchange';
|
|
4
5
|
export * from './fetchInterceptor';
|
|
5
6
|
export * from './fetchRequest';
|
|
6
7
|
export * from './interceptor';
|
|
8
|
+
export * from './interceptorManager';
|
|
7
9
|
export * from './mergeRequest';
|
|
8
10
|
export * from './namedFetcher';
|
|
9
11
|
export * from './orderedCapable';
|
|
@@ -14,4 +16,5 @@ export * from './urlBuilder';
|
|
|
14
16
|
export * from './urlResolveInterceptor';
|
|
15
17
|
export * from './urls';
|
|
16
18
|
export * from './utils';
|
|
19
|
+
export * from './validateStatusInterceptor';
|
|
17
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,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,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"}
|
|
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"}
|