@ahoo-wang/fetcher 0.9.0 → 0.9.2
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 +3 -2
- package/README.zh-CN.md +2 -2
- package/dist/fetchExchange.d.ts +7 -7
- package/dist/fetchInterceptor.d.ts +13 -13
- package/dist/fetchInterceptor.d.ts.map +1 -1
- package/dist/fetcher.d.ts +163 -56
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/fetcherError.d.ts +122 -0
- package/dist/fetcherError.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +336 -196
- package/dist/index.umd.js +1 -1
- package/dist/interceptor.d.ts +47 -49
- package/dist/interceptor.d.ts.map +1 -1
- package/dist/requestBodyInterceptor.d.ts +14 -19
- package/dist/requestBodyInterceptor.d.ts.map +1 -1
- package/dist/timeout.d.ts +6 -35
- package/dist/timeout.d.ts.map +1 -1
- package/dist/urlBuilder.d.ts +25 -19
- package/dist/urlBuilder.d.ts.map +1 -1
- package/dist/urlResolveInterceptor.d.ts +6 -12
- package/dist/urlResolveInterceptor.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,12 +8,13 @@
|
|
|
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 (
|
|
11
|
+
A modern, ultra-lightweight (2.3KiB) HTTP client with built-in path parameters, query parameters, and Axios-like API.
|
|
12
|
+
83%
|
|
12
13
|
smaller than Axios while providing the same powerful features.
|
|
13
14
|
|
|
14
15
|
## 🌟 Features
|
|
15
16
|
|
|
16
|
-
- **⚡ Ultra-Lightweight**: Only
|
|
17
|
+
- **⚡ Ultra-Lightweight**: Only 2.3KiB min+gzip - 83% smaller than Axios
|
|
17
18
|
- **🧭 Path & Query Parameters**: Built-in support for path (`{id}`) and query parameters
|
|
18
19
|
- **🔗 Interceptor System**: Request, response, and error interceptors for middleware patterns
|
|
19
20
|
- **⏱️ Timeout Control**: Configurable request timeouts with proper error handling
|
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
|
- **⏱️ 超时控制**:可配置的请求超时和适当的错误处理
|
package/dist/fetchExchange.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Fetcher } from './fetcher';
|
|
2
2
|
import { FetchRequest } from './fetchRequest';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Container for HTTP request/response data that flows through the interceptor chain.
|
|
5
5
|
*
|
|
6
6
|
* Represents the complete exchange object that flows through the interceptor chain.
|
|
7
7
|
* This object contains all the information about a request, response, and any errors
|
|
@@ -43,7 +43,7 @@ import { FetchRequest } from './fetchRequest';
|
|
|
43
43
|
*/
|
|
44
44
|
export declare class FetchExchange {
|
|
45
45
|
/**
|
|
46
|
-
* The Fetcher instance that initiated this exchange
|
|
46
|
+
* The Fetcher instance that initiated this exchange.
|
|
47
47
|
*/
|
|
48
48
|
fetcher: Fetcher;
|
|
49
49
|
/**
|
|
@@ -51,15 +51,15 @@ export declare class FetchExchange {
|
|
|
51
51
|
*/
|
|
52
52
|
request: FetchRequest;
|
|
53
53
|
/**
|
|
54
|
-
* The response object, undefined until the request completes successfully
|
|
54
|
+
* The response object, undefined until the request completes successfully.
|
|
55
55
|
*/
|
|
56
56
|
response: Response | undefined;
|
|
57
57
|
/**
|
|
58
|
-
* Any error that occurred during the request processing, undefined if no error occurred
|
|
58
|
+
* Any error that occurred during the request processing, undefined if no error occurred.
|
|
59
59
|
*/
|
|
60
60
|
error: Error | any | undefined;
|
|
61
61
|
/**
|
|
62
|
-
* Shared attributes for passing data between interceptors
|
|
62
|
+
* Shared attributes for passing data between interceptors.
|
|
63
63
|
*
|
|
64
64
|
* This property allows interceptors to share arbitrary data with each other.
|
|
65
65
|
* Interceptors can read from and write to this object to pass information
|
|
@@ -75,13 +75,13 @@ export declare class FetchExchange {
|
|
|
75
75
|
attributes: Record<string, any>;
|
|
76
76
|
constructor(fetcher: Fetcher, request: FetchRequest, response?: Response, error?: Error | any);
|
|
77
77
|
/**
|
|
78
|
-
* Checks if the exchange has an error
|
|
78
|
+
* Checks if the exchange has an error.
|
|
79
79
|
*
|
|
80
80
|
* @returns true if an error is present, false otherwise
|
|
81
81
|
*/
|
|
82
82
|
hasError(): boolean;
|
|
83
83
|
/**
|
|
84
|
-
* Checks if the exchange has a response
|
|
84
|
+
* Checks if the exchange has a response.
|
|
85
85
|
*
|
|
86
86
|
* @returns true if a response is present, false otherwise
|
|
87
87
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Interceptor } from './interceptor';
|
|
2
2
|
import { FetchExchange } from './fetchExchange';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Interceptor implementation responsible for executing actual HTTP requests.
|
|
5
5
|
*
|
|
6
6
|
* This is an interceptor implementation responsible for executing actual HTTP requests
|
|
7
7
|
* and handling timeout control. It is the last interceptor in the Fetcher request
|
|
@@ -15,17 +15,15 @@ import { FetchExchange } from './fetchExchange';
|
|
|
15
15
|
*/
|
|
16
16
|
export declare class FetchInterceptor implements Interceptor {
|
|
17
17
|
/**
|
|
18
|
-
* Interceptor name, used to identify and manage interceptor instances
|
|
18
|
+
* Interceptor name, used to identify and manage interceptor instances.
|
|
19
19
|
*
|
|
20
|
-
* @remarks
|
|
21
20
|
* Each interceptor must have a unique name for identification and manipulation
|
|
22
|
-
* within the interceptor manager
|
|
21
|
+
* within the interceptor manager.
|
|
23
22
|
*/
|
|
24
23
|
name: string;
|
|
25
24
|
/**
|
|
26
|
-
* Interceptor execution order, set to near maximum safe integer to ensure last execution
|
|
25
|
+
* Interceptor execution order, set to near maximum safe integer to ensure last execution.
|
|
27
26
|
*
|
|
28
|
-
* @remarks
|
|
29
27
|
* Since this is the interceptor that actually executes HTTP requests, it should
|
|
30
28
|
* execute after all other request interceptors, so its order is set to
|
|
31
29
|
* Number.MAX_SAFE_INTEGER - 100. This ensures that all request preprocessing is
|
|
@@ -34,7 +32,7 @@ export declare class FetchInterceptor implements Interceptor {
|
|
|
34
32
|
*/
|
|
35
33
|
order: number;
|
|
36
34
|
/**
|
|
37
|
-
* Intercept and process HTTP requests
|
|
35
|
+
* Intercept and process HTTP requests.
|
|
38
36
|
*
|
|
39
37
|
* Executes the actual HTTP request and applies timeout control. This is the final
|
|
40
38
|
* step in the request processing chain, responsible for calling the timeoutFetch
|
|
@@ -46,15 +44,17 @@ export declare class FetchInterceptor implements Interceptor {
|
|
|
46
44
|
*
|
|
47
45
|
* @example
|
|
48
46
|
* // Usually called internally by Fetcher
|
|
49
|
-
* const
|
|
50
|
-
*
|
|
51
|
-
*
|
|
47
|
+
* const fetcher = new Fetcher();
|
|
48
|
+
* const exchange = new FetchExchange(
|
|
49
|
+
* fetcher,
|
|
50
|
+
* {
|
|
51
|
+
* url: 'https://api.example.com/users',
|
|
52
52
|
* method: 'GET',
|
|
53
53
|
* timeout: 5000
|
|
54
54
|
* }
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* console.log(
|
|
55
|
+
* );
|
|
56
|
+
* await fetchInterceptor.intercept(exchange);
|
|
57
|
+
* console.log(exchange.response); // HTTP response object
|
|
58
58
|
*/
|
|
59
59
|
intercept(exchange: FetchExchange): Promise<void>;
|
|
60
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchInterceptor.d.ts","sourceRoot":"","sources":["../src/fetchInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;GAYG;AACH,qBAAa,gBAAiB,YAAW,WAAW;IAClD
|
|
1
|
+
{"version":3,"file":"fetchInterceptor.d.ts","sourceRoot":"","sources":["../src/fetchInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;GAYG;AACH,qBAAa,gBAAiB,YAAW,WAAW;IAClD;;;;;OAKG;IACH,IAAI,SAAsB;IAE1B;;;;;;;;OAQG;IACH,KAAK,SAAiC;IAEtC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa;CAGxC"}
|
package/dist/fetcher.d.ts
CHANGED
|
@@ -4,7 +4,10 @@ import { FetcherInterceptors } from './interceptor';
|
|
|
4
4
|
import { FetchExchange } from './fetchExchange';
|
|
5
5
|
import { BaseURLCapable, FetchRequest, FetchRequestInit, RequestHeaders, RequestHeadersCapable } from './fetchRequest';
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* Configuration options for the Fetcher client.
|
|
8
|
+
*
|
|
9
|
+
* Defines the customizable aspects of a Fetcher instance including base URL,
|
|
10
|
+
* default headers, timeout settings, and interceptors.
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```typescript
|
|
@@ -21,7 +24,11 @@ export interface FetcherOptions extends BaseURLCapable, RequestHeadersCapable, T
|
|
|
21
24
|
}
|
|
22
25
|
export declare const DEFAULT_OPTIONS: FetcherOptions;
|
|
23
26
|
/**
|
|
24
|
-
* HTTP client
|
|
27
|
+
* HTTP client with support for interceptors, URL building, and timeout control.
|
|
28
|
+
*
|
|
29
|
+
* The Fetcher class provides a flexible and extensible HTTP client implementation
|
|
30
|
+
* that follows the interceptor pattern. It supports URL parameter interpolation,
|
|
31
|
+
* request/response transformation, and timeout handling.
|
|
25
32
|
*
|
|
26
33
|
* @example
|
|
27
34
|
* ```typescript
|
|
@@ -41,108 +48,208 @@ export declare class Fetcher implements UrlBuilderCapable, RequestHeadersCapable
|
|
|
41
48
|
timeout?: number;
|
|
42
49
|
interceptors: FetcherInterceptors;
|
|
43
50
|
/**
|
|
44
|
-
*
|
|
51
|
+
* Initializes a new Fetcher instance with optional configuration.
|
|
52
|
+
*
|
|
53
|
+
* Creates a Fetcher with default settings that can be overridden through the options parameter.
|
|
54
|
+
* If no interceptors are provided, a default set of interceptors will be used.
|
|
45
55
|
*
|
|
46
|
-
* @param options - Fetcher
|
|
56
|
+
* @param options - Configuration options for the Fetcher instance
|
|
47
57
|
*/
|
|
48
58
|
constructor(options?: FetcherOptions);
|
|
49
59
|
/**
|
|
50
|
-
*
|
|
60
|
+
* Executes an HTTP request with the specified URL and options.
|
|
51
61
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
62
|
+
* This is the primary method for making HTTP requests. It processes the request
|
|
63
|
+
* through the interceptor chain and returns the resulting Response.
|
|
64
|
+
*
|
|
65
|
+
* @param url - The URL path for the request (relative to baseURL if set)
|
|
66
|
+
* @param request - Request configuration including headers, body, parameters, etc.
|
|
67
|
+
* @returns Promise that resolves to the HTTP response
|
|
68
|
+
* @throws FetchError if the request fails and no response is generated
|
|
55
69
|
*/
|
|
56
70
|
fetch(url: string, request?: FetchRequestInit): Promise<Response>;
|
|
57
71
|
/**
|
|
58
|
-
*
|
|
72
|
+
* Processes an HTTP request through the Fetcher's internal workflow.
|
|
59
73
|
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
74
|
+
* This method prepares the request by merging headers and timeout settings,
|
|
75
|
+
* creates a FetchExchange object, and passes it through the exchange method
|
|
76
|
+
* for interceptor processing.
|
|
62
77
|
*
|
|
63
|
-
* @
|
|
78
|
+
* @param request - Complete request configuration object
|
|
79
|
+
* @returns Promise that resolves to a FetchExchange containing request/response data
|
|
80
|
+
* @throws Error if an unhandled error occurs during request processing
|
|
64
81
|
*/
|
|
65
82
|
request(request: FetchRequest): Promise<FetchExchange>;
|
|
66
83
|
/**
|
|
67
|
-
*
|
|
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
|
|
68
117
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
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
|
|
73
122
|
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* // Create a fetcher with custom interceptors
|
|
126
|
+
* const fetcher = new Fetcher();
|
|
77
127
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
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
|
+
* ```
|
|
81
168
|
*/
|
|
82
169
|
exchange(fetchExchange: FetchExchange): Promise<FetchExchange>;
|
|
83
170
|
/**
|
|
84
|
-
*
|
|
171
|
+
* Internal helper method for making HTTP requests with a specific method.
|
|
85
172
|
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* @
|
|
173
|
+
* This private method is used by the public HTTP method methods (get, post, etc.)
|
|
174
|
+
* to execute requests with the appropriate HTTP verb.
|
|
175
|
+
*
|
|
176
|
+
* @param method - The HTTP method to use for the request
|
|
177
|
+
* @param url - The URL path for the request
|
|
178
|
+
* @param request - Additional request options
|
|
179
|
+
* @returns Promise that resolves to the HTTP response
|
|
90
180
|
*/
|
|
91
181
|
private methodFetch;
|
|
92
182
|
/**
|
|
93
|
-
*
|
|
183
|
+
* Makes a GET HTTP request.
|
|
184
|
+
*
|
|
185
|
+
* Convenience method for making GET requests. The request body is omitted
|
|
186
|
+
* as GET requests should not contain a body according to HTTP specification.
|
|
94
187
|
*
|
|
95
|
-
* @param url -
|
|
96
|
-
* @param request - Request options
|
|
97
|
-
* @returns Promise
|
|
188
|
+
* @param url - The URL path for the request
|
|
189
|
+
* @param request - Request options excluding method and body
|
|
190
|
+
* @returns Promise that resolves to the HTTP response
|
|
98
191
|
*/
|
|
99
192
|
get(url: string, request?: Omit<FetchRequestInit, 'method' | 'body'>): Promise<Response>;
|
|
100
193
|
/**
|
|
101
|
-
*
|
|
194
|
+
* Makes a POST HTTP request.
|
|
195
|
+
*
|
|
196
|
+
* Convenience method for making POST requests, commonly used for creating resources.
|
|
102
197
|
*
|
|
103
|
-
* @param url -
|
|
104
|
-
* @param request - Request options
|
|
105
|
-
* @returns Promise
|
|
198
|
+
* @param url - The URL path for the request
|
|
199
|
+
* @param request - Request options including body and other parameters
|
|
200
|
+
* @returns Promise that resolves to the HTTP response
|
|
106
201
|
*/
|
|
107
202
|
post(url: string, request?: Omit<FetchRequestInit, 'method'>): Promise<Response>;
|
|
108
203
|
/**
|
|
109
|
-
*
|
|
204
|
+
* Makes a PUT HTTP request.
|
|
110
205
|
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* @
|
|
206
|
+
* Convenience method for making PUT requests, commonly used for updating resources.
|
|
207
|
+
*
|
|
208
|
+
* @param url - The URL path for the request
|
|
209
|
+
* @param request - Request options including body and other parameters
|
|
210
|
+
* @returns Promise that resolves to the HTTP response
|
|
114
211
|
*/
|
|
115
212
|
put(url: string, request?: Omit<FetchRequestInit, 'method'>): Promise<Response>;
|
|
116
213
|
/**
|
|
117
|
-
*
|
|
214
|
+
* Makes a DELETE HTTP request.
|
|
215
|
+
*
|
|
216
|
+
* Convenience method for making DELETE requests, commonly used for deleting resources.
|
|
118
217
|
*
|
|
119
|
-
* @param url -
|
|
120
|
-
* @param request - Request options
|
|
121
|
-
* @returns Promise
|
|
218
|
+
* @param url - The URL path for the request
|
|
219
|
+
* @param request - Request options excluding method and body
|
|
220
|
+
* @returns Promise that resolves to the HTTP response
|
|
122
221
|
*/
|
|
123
222
|
delete(url: string, request?: Omit<FetchRequestInit, 'method'>): Promise<Response>;
|
|
124
223
|
/**
|
|
125
|
-
*
|
|
224
|
+
* Makes a PATCH HTTP request.
|
|
225
|
+
*
|
|
226
|
+
* Convenience method for making PATCH requests, commonly used for partial updates.
|
|
126
227
|
*
|
|
127
|
-
* @param url -
|
|
128
|
-
* @param request - Request options
|
|
129
|
-
* @returns Promise
|
|
228
|
+
* @param url - The URL path for the request
|
|
229
|
+
* @param request - Request options including body and other parameters
|
|
230
|
+
* @returns Promise that resolves to the HTTP response
|
|
130
231
|
*/
|
|
131
232
|
patch(url: string, request?: Omit<FetchRequestInit, 'method'>): Promise<Response>;
|
|
132
233
|
/**
|
|
133
|
-
*
|
|
234
|
+
* Makes a HEAD HTTP request.
|
|
134
235
|
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
236
|
+
* Convenience method for making HEAD requests, which retrieve headers only.
|
|
237
|
+
* The request body is omitted as HEAD requests should not contain a body.
|
|
238
|
+
*
|
|
239
|
+
* @param url - The URL path for the request
|
|
240
|
+
* @param request - Request options excluding method and body
|
|
241
|
+
* @returns Promise that resolves to the HTTP response
|
|
138
242
|
*/
|
|
139
243
|
head(url: string, request?: Omit<FetchRequestInit, 'method' | 'body'>): Promise<Response>;
|
|
140
244
|
/**
|
|
141
|
-
*
|
|
245
|
+
* Makes an OPTIONS HTTP request.
|
|
246
|
+
*
|
|
247
|
+
* Convenience method for making OPTIONS requests, commonly used for CORS preflight.
|
|
248
|
+
* The request body is omitted as OPTIONS requests typically don't contain a body.
|
|
142
249
|
*
|
|
143
|
-
* @param url -
|
|
144
|
-
* @param request - Request options
|
|
145
|
-
* @returns Promise
|
|
250
|
+
* @param url - The URL path for the request
|
|
251
|
+
* @param request - Request options excluding method and body
|
|
252
|
+
* @returns Promise that resolves to the HTTP response
|
|
146
253
|
*/
|
|
147
254
|
options(url: string, request?: Omit<FetchRequestInit, 'method' | 'body'>): Promise<Response>;
|
|
148
255
|
}
|
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,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EACL,cAAc,EAEd,YAAY,EACZ,gBAAgB,EAEhB,cAAc,EACd,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;
|
|
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,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EACL,cAAc,EAEd,YAAY,EACZ,gBAAgB,EAEhB,cAAc,EACd,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AAIxB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,qBAAqB,EACrB,cAAc;IAChB,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC;AAMD,eAAO,MAAM,eAAe,EAAE,cAG7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,OACX,YAAW,iBAAiB,EAAE,qBAAqB,EAAE,cAAc;IACnE,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,CAAC,EAAE,cAAc,CAAmB;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,mBAAmB,CAAC;IAElC;;;;;;;OAOG;gBACS,OAAO,GAAE,cAAgC;IAOrD;;;;;;;;;;OAUG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAU3E;;;;;;;;;;OAUG;IACG,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAa5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqFG;IACG,QAAQ,CAAC,aAAa,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAsBpE;;;;;;;;;;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,122 @@
|
|
|
1
|
+
import { FetchExchange } from './fetchExchange';
|
|
2
|
+
import { FetchRequest } from './fetchRequest';
|
|
3
|
+
/**
|
|
4
|
+
* Base error class for all Fetcher-related errors.
|
|
5
|
+
*
|
|
6
|
+
* This class extends the native Error class and provides a foundation for
|
|
7
|
+
* all custom errors thrown by the Fetcher library. It includes support for
|
|
8
|
+
* error chaining through the cause property.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* try {
|
|
13
|
+
* await fetcher.get('/api/users');
|
|
14
|
+
* } catch (error) {
|
|
15
|
+
* if (error instanceof FetcherError) {
|
|
16
|
+
* console.log('Fetcher error:', error.message);
|
|
17
|
+
* if (error.cause) {
|
|
18
|
+
* console.log('Caused by:', error.cause);
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class FetcherError extends Error {
|
|
25
|
+
readonly cause?: (Error | any) | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new FetcherError instance.
|
|
28
|
+
*
|
|
29
|
+
* @param errorMsg - Optional error message. If not provided, will use the cause's message or a default message.
|
|
30
|
+
* @param cause - Optional underlying error that caused this error.
|
|
31
|
+
*/
|
|
32
|
+
constructor(errorMsg?: string, cause?: (Error | any) | undefined);
|
|
33
|
+
}
|
|
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
|
+
/**
|
|
66
|
+
* Custom error class for Fetcher request failures.
|
|
67
|
+
*
|
|
68
|
+
* This error is thrown when a fetch request fails and no response is generated.
|
|
69
|
+
* It wraps the FetchExchange object to provide comprehensive information about the failed request.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* try {
|
|
74
|
+
* await fetcher.get('/api/users');
|
|
75
|
+
* } catch (error) {
|
|
76
|
+
* if (error instanceof FetchError) {
|
|
77
|
+
* console.log('Failed URL:', error.exchange.request.url);
|
|
78
|
+
* console.log('Request headers:', error.exchange.request.headers);
|
|
79
|
+
* console.log('Interceptor attributes:', error.exchange.attributes);
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare class FetchError extends FetcherError {
|
|
85
|
+
readonly exchange: FetchExchange;
|
|
86
|
+
/**
|
|
87
|
+
* Creates a new FetchError instance.
|
|
88
|
+
*
|
|
89
|
+
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
90
|
+
* @param errorMsg - Optional custom error message. If not provided, will use the exchange's error message.
|
|
91
|
+
*/
|
|
92
|
+
constructor(exchange: FetchExchange, errorMsg?: string);
|
|
93
|
+
}
|
|
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
|
+
//# 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;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;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;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,aAAc,SAAQ,YAAY;aAMjB,QAAQ,EAAE,aAAa;IALnD;;;;OAIG;gBACyB,QAAQ,EAAE,aAAa;CASpD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,UAAW,SAAQ,YAAY;aAQxB,QAAQ,EAAE,aAAa;IAPzC;;;;;OAKG;gBAEe,QAAQ,EAAE,aAAa,EACvC,QAAQ,CAAC,EAAE,MAAM;CAUpB;AAED;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,iBAAkB,SAAQ,YAAY;IACjD;;OAEG;IACH,OAAO,EAAE,YAAY,CAAC;IAEtB;;;;OAIG;gBACS,OAAO,EAAE,YAAY;CASlC"}
|
package/dist/index.d.ts
CHANGED
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,gBAAgB,CAAC;AAC/B,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"}
|