@ahoo-wang/fetcher 0.8.0 → 0.8.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 +27 -223
- package/README.zh-CN.md +132 -34
- package/dist/fetchExchange.d.ts +163 -0
- package/dist/fetchExchange.d.ts.map +1 -0
- package/dist/fetchInterceptor.d.ts +2 -1
- package/dist/fetchInterceptor.d.ts.map +1 -1
- package/dist/fetcher.d.ts +22 -86
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +127 -89
- package/dist/index.umd.js +1 -1
- package/dist/interceptor.d.ts +6 -79
- package/dist/interceptor.d.ts.map +1 -1
- package/dist/mergeRequest.d.ts +1 -1
- package/dist/mergeRequest.d.ts.map +1 -1
- package/dist/requestBodyInterceptor.d.ts +7 -4
- package/dist/requestBodyInterceptor.d.ts.map +1 -1
- package/dist/urlBuilder.d.ts +3 -0
- package/dist/urlBuilder.d.ts.map +1 -1
- package/dist/urlResolveInterceptor.d.ts +39 -0
- package/dist/urlResolveInterceptor.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/interceptor.d.ts
CHANGED
|
@@ -1,82 +1,6 @@
|
|
|
1
|
-
import { Fetcher, FetcherRequest } from './fetcher';
|
|
2
1
|
import { NamedCapable } from './types';
|
|
3
2
|
import { OrderedCapable } from './orderedCapable';
|
|
4
|
-
|
|
5
|
-
* FetchExchange Interface
|
|
6
|
-
*
|
|
7
|
-
* Represents the complete exchange object that flows through the interceptor chain.
|
|
8
|
-
* This object contains all the information about a request, response, and any errors
|
|
9
|
-
* that occur during the HTTP request lifecycle. It also provides a mechanism for
|
|
10
|
-
* sharing data between interceptors through the attributes property.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* // In a request interceptor
|
|
15
|
-
* const requestInterceptor: Interceptor = {
|
|
16
|
-
* name: 'RequestInterceptor',
|
|
17
|
-
* order: 0,
|
|
18
|
-
* async intercept(exchange: FetchExchange): Promise<FetchExchange> {
|
|
19
|
-
* // Add custom data to share with other interceptors
|
|
20
|
-
* exchange.attributes = exchange.attributes || {};
|
|
21
|
-
* exchange.attributes.startTime = Date.now();
|
|
22
|
-
* exchange.attributes.customHeader = 'my-value';
|
|
23
|
-
* return exchange;
|
|
24
|
-
* }
|
|
25
|
-
* };
|
|
26
|
-
*
|
|
27
|
-
* // In a response interceptor
|
|
28
|
-
* const responseInterceptor: Interceptor = {
|
|
29
|
-
* name: 'ResponseInterceptor',
|
|
30
|
-
* order: 0,
|
|
31
|
-
* async intercept(exchange: FetchExchange): Promise<FetchExchange> {
|
|
32
|
-
* // Access data shared by previous interceptors
|
|
33
|
-
* if (exchange.attributes && exchange.attributes.startTime) {
|
|
34
|
-
* const startTime = exchange.attributes.startTime;
|
|
35
|
-
* const duration = Date.now() - startTime;
|
|
36
|
-
* console.log(`Request took ${duration}ms`);
|
|
37
|
-
* }
|
|
38
|
-
* return exchange;
|
|
39
|
-
* }
|
|
40
|
-
* };
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export interface FetchExchange {
|
|
44
|
-
/**
|
|
45
|
-
* The Fetcher instance that initiated this exchange
|
|
46
|
-
*/
|
|
47
|
-
fetcher: Fetcher;
|
|
48
|
-
/**
|
|
49
|
-
* The URL for this request
|
|
50
|
-
*/
|
|
51
|
-
url: string;
|
|
52
|
-
/**
|
|
53
|
-
* The request configuration including method, headers, body, etc.
|
|
54
|
-
*/
|
|
55
|
-
request: FetcherRequest;
|
|
56
|
-
/**
|
|
57
|
-
* The response object, undefined until the request completes successfully
|
|
58
|
-
*/
|
|
59
|
-
response: Response | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* Any error that occurred during the request processing, undefined if no error occurred
|
|
62
|
-
*/
|
|
63
|
-
error: Error | any | undefined;
|
|
64
|
-
/**
|
|
65
|
-
* Shared attributes for passing data between interceptors
|
|
66
|
-
*
|
|
67
|
-
* This property allows interceptors to share arbitrary data with each other.
|
|
68
|
-
* Interceptors can read from and write to this object to pass information
|
|
69
|
-
* along the interceptor chain.
|
|
70
|
-
*
|
|
71
|
-
* @remarks
|
|
72
|
-
* - This property is optional and may be undefined initially
|
|
73
|
-
* - Interceptors should initialize this property if they need to use it
|
|
74
|
-
* - Use string keys to avoid conflicts between different interceptors
|
|
75
|
-
* - Consider namespacing your keys (e.g., 'mylib.retryCount' instead of 'retryCount')
|
|
76
|
-
* - Be mindful of memory usage when storing large objects
|
|
77
|
-
*/
|
|
78
|
-
attributes?: Record<string, any>;
|
|
79
|
-
}
|
|
3
|
+
import { FetchExchange } from './fetchExchange';
|
|
80
4
|
/**
|
|
81
5
|
* Interceptor Interface
|
|
82
6
|
*
|
|
@@ -257,11 +181,14 @@ export declare class FetcherInterceptors {
|
|
|
257
181
|
* Request Interceptor Manager
|
|
258
182
|
*
|
|
259
183
|
* Responsible for managing all request-phase interceptors, executed before HTTP requests are sent.
|
|
260
|
-
* Contains
|
|
184
|
+
* Contains three built-in interceptors by default: UrlResolveInterceptor, RequestBodyInterceptor, and FetchInterceptor.
|
|
261
185
|
*
|
|
262
186
|
* @remarks
|
|
263
187
|
* Request interceptors are executed in ascending order of their order values, with smaller values having higher priority.
|
|
264
|
-
*
|
|
188
|
+
* The default interceptors are:
|
|
189
|
+
* 1. UrlResolveInterceptor (order: Number.MIN_SAFE_INTEGER) - Resolves the final URL
|
|
190
|
+
* 2. RequestBodyInterceptor (order: 0) - Converts object bodies to JSON
|
|
191
|
+
* 3. FetchInterceptor (order: Number.MAX_SAFE_INTEGER) - Executes the actual HTTP request
|
|
265
192
|
*/
|
|
266
193
|
request: InterceptorManager;
|
|
267
194
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"interceptor.d.ts","s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D"}
|
package/dist/mergeRequest.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mergeRequest.d.ts","sourceRoot":"","sources":["../src/mergeRequest.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,cAAc,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"mergeRequest.d.ts","sourceRoot":"","sources":["../src/mergeRequest.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,cAAc,EACrB,MAAM,EAAE,cAAc,GACrB,cAAc,CA6ChB"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Interceptor } from './interceptor';
|
|
2
|
+
import { FetchExchange } from './fetchExchange';
|
|
2
3
|
/**
|
|
3
4
|
* RequestBodyInterceptor Class
|
|
4
5
|
*
|
|
@@ -17,11 +18,13 @@ export declare class RequestBodyInterceptor implements Interceptor {
|
|
|
17
18
|
*/
|
|
18
19
|
name: string;
|
|
19
20
|
/**
|
|
20
|
-
* Interceptor execution order, set to
|
|
21
|
+
* Interceptor execution order, set to run after UrlResolveInterceptor but before FetchInterceptor
|
|
21
22
|
*
|
|
22
23
|
* @remarks
|
|
23
|
-
* This interceptor should run
|
|
24
|
-
* request
|
|
24
|
+
* This interceptor should run after URL resolution (UrlResolveInterceptor) but before
|
|
25
|
+
* the actual HTTP request is made (FetchInterceptor). The order is set to
|
|
26
|
+
* Number.MIN_SAFE_INTEGER + 100 to ensure it executes in the correct position
|
|
27
|
+
* in the interceptor chain.
|
|
25
28
|
*/
|
|
26
29
|
order: number;
|
|
27
30
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestBodyInterceptor.d.ts","sourceRoot":"","sources":["../src/requestBodyInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"requestBodyInterceptor.d.ts","sourceRoot":"","sources":["../src/requestBodyInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;GAWG;AACH,qBAAa,sBAAuB,YAAW,WAAW;IACxD;;OAEG;IACH,IAAI,SAA4B;IAEhC;;;;;;;;OAQG;IACH,KAAK,SAAiC;IAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa;CA2ClD"}
|
package/dist/urlBuilder.d.ts
CHANGED
package/dist/urlBuilder.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;;;;;;;;;;;GAYG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC/C;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;gBACS,OAAO,EAAE,MAAM;IAI3B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CACH,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,MAAM;IAYT;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM;CAWhE"}
|
|
1
|
+
{"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;;;;;;;;;;;GAYG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC/C;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;gBACS,OAAO,EAAE,MAAM;IAI3B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CACH,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,MAAM;IAYT;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM;CAWhE;AAED,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,UAAU,CAAC;CACxB"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Interceptor } from './interceptor';
|
|
2
|
+
import { FetchExchange } from './fetchExchange';
|
|
3
|
+
/**
|
|
4
|
+
* URL Resolution Interceptor
|
|
5
|
+
*
|
|
6
|
+
* This interceptor is responsible for resolving the final URL for a request by combining
|
|
7
|
+
* the base URL, path parameters, and query parameters. It should be executed early in
|
|
8
|
+
* the interceptor chain to ensure the URL is properly resolved before other interceptors
|
|
9
|
+
* process the request.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* This interceptor has the lowest possible order (Number.MIN_SAFE_INTEGER) to ensure
|
|
13
|
+
* it runs first in the request interceptor chain, before any other request processing.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // With baseURL: 'https://api.example.com'
|
|
17
|
+
* // Request URL: '/users/{id}'
|
|
18
|
+
* // Path params: { id: 123 }
|
|
19
|
+
* // Query params: { filter: 'active' }
|
|
20
|
+
* // Final URL: 'https://api.example.com/users/123?filter=active'
|
|
21
|
+
*/
|
|
22
|
+
export declare class UrlResolveInterceptor implements Interceptor {
|
|
23
|
+
/**
|
|
24
|
+
* The name of this interceptor
|
|
25
|
+
*/
|
|
26
|
+
name: string;
|
|
27
|
+
/**
|
|
28
|
+
* The order of this interceptor (executed first)
|
|
29
|
+
*/
|
|
30
|
+
order: number;
|
|
31
|
+
/**
|
|
32
|
+
* Resolves the final URL by combining the base URL, path parameters, and query parameters
|
|
33
|
+
*
|
|
34
|
+
* @param exchange - The fetch exchange containing the request information
|
|
35
|
+
* @returns The modified exchange with the resolved URL
|
|
36
|
+
*/
|
|
37
|
+
intercept(exchange: FetchExchange): FetchExchange;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=urlResolveInterceptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlResolveInterceptor.d.ts","sourceRoot":"","sources":["../src/urlResolveInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,qBAAsB,YAAW,WAAW;IACvD;;OAEG;IACH,IAAI,SAA2B;IAE/B;;OAEG;IACH,KAAK,SAA2B;IAEhC;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa;CAQlD"}
|