@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.
@@ -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 two built-in interceptors by default: RequestBodyInterceptor and FetchInterceptor.
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
- * FetchInterceptor's order is set to Number.MAX_SAFE_INTEGER to ensure it executes last.
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,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,cAAc,EAAY,MAAM,kBAAkB,CAAC;AAI5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,qBAAa,mBAAmB;IAC9B;;;;;;;;;;;;OAYG;IACH,OAAO,EAAE,kBAAkB,CAIxB;IAEH;;;;;;;;OAQG;IACH,QAAQ,EAAE,kBAAkB,CAA4B;IAExD;;;;;;;;;OASG;IACH,KAAK,EAAE,kBAAkB,CAA4B;CACtD"}
@@ -1,4 +1,4 @@
1
- import { FetcherRequest } from './fetcher';
1
+ import { FetcherRequest } from './fetchExchange';
2
2
  /**
3
3
  * Merges two FetcherRequest objects into one.
4
4
  *
@@ -1 +1 @@
1
- {"version":3,"file":"mergeRequest.d.ts","sourceRoot":"","sources":["../src/mergeRequest.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,cAAc,EACrB,MAAM,EAAE,cAAc,GACrB,cAAc,CA6ChB"}
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 { FetchExchange, Interceptor } from './interceptor';
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 minimum safe integer to ensure early execution
21
+ * Interceptor execution order, set to run after UrlResolveInterceptor but before FetchInterceptor
21
22
  *
22
23
  * @remarks
23
- * This interceptor should run before other request interceptors to ensure
24
- * request bodies are properly formatted early in the processing chain.
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,aAAa,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG3D;;;;;;;;;;;GAWG;AACH,qBAAa,sBAAuB,YAAW,WAAW;IACxD;;OAEG;IACH,IAAI,SAA4B;IAEhC;;;;;;OAMG;IACH,KAAK,SAA2B;IAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa;CA2ClD"}
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"}
@@ -60,4 +60,7 @@ export declare class UrlBuilder implements BaseURLCapable {
60
60
  */
61
61
  interpolateUrl(url: string, path?: Record<string, any>): string;
62
62
  }
63
+ export interface UrlBuilderCapable {
64
+ urlBuilder: UrlBuilder;
65
+ }
63
66
  //# sourceMappingURL=urlBuilder.d.ts.map
@@ -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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "Ultra-lightweight (1.9kB) HTTP client with built-in path parameters and Axios-like API",
5
5
  "keywords": [
6
6
  "fetch",