@ahoo-wang/fetcher 1.5.5 → 1.5.8
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/dist/fetchExchange.d.ts +19 -0
- package/dist/fetchExchange.d.ts.map +1 -1
- package/dist/fetcher.d.ts +1 -36
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.es.js +215 -244
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/fetchExchange.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Fetcher } from './fetcher';
|
|
|
2
2
|
import { FetchRequest, RequestHeaders } from './fetchRequest';
|
|
3
3
|
import { UrlParams } from './urlBuilder';
|
|
4
4
|
import { RequiredBy } from './types';
|
|
5
|
+
import { ResultExtractor } from './resultExtractor';
|
|
5
6
|
export interface AttributesCapable {
|
|
6
7
|
/**
|
|
7
8
|
* Shared attributes for passing data between interceptors.
|
|
@@ -28,6 +29,7 @@ export interface FetchExchangeInit extends AttributesCapable {
|
|
|
28
29
|
* The request configuration including url, method, headers, body, etc.
|
|
29
30
|
*/
|
|
30
31
|
request: FetchRequest;
|
|
32
|
+
resultExtractor?: ResultExtractor<any>;
|
|
31
33
|
/**
|
|
32
34
|
* The response object, undefined until the request completes successfully.
|
|
33
35
|
*/
|
|
@@ -87,6 +89,11 @@ export declare class FetchExchange implements RequiredBy<FetchExchangeInit, 'att
|
|
|
87
89
|
* The request configuration including url, method, headers, body, etc.
|
|
88
90
|
*/
|
|
89
91
|
request: FetchRequest;
|
|
92
|
+
/**
|
|
93
|
+
* The result extractor function used to transform the response into the desired format.
|
|
94
|
+
* Defaults to ResultExtractors.Exchange if not provided.
|
|
95
|
+
*/
|
|
96
|
+
resultExtractor: ResultExtractor<any>;
|
|
90
97
|
/**
|
|
91
98
|
* The response object, undefined until the request completes successfully.
|
|
92
99
|
*/
|
|
@@ -95,6 +102,11 @@ export declare class FetchExchange implements RequiredBy<FetchExchangeInit, 'att
|
|
|
95
102
|
* Any error that occurred during the request processing, undefined if no error occurred.
|
|
96
103
|
*/
|
|
97
104
|
error?: Error | any;
|
|
105
|
+
/**
|
|
106
|
+
* Cached result of the extracted result to avoid repeated computations.
|
|
107
|
+
* Undefined when not yet computed, null when computation failed.
|
|
108
|
+
*/
|
|
109
|
+
private cachedExtractedResult?;
|
|
98
110
|
/**
|
|
99
111
|
* Shared attributes for passing data between interceptors.
|
|
100
112
|
*
|
|
@@ -156,5 +168,12 @@ export declare class FetchExchange implements RequiredBy<FetchExchangeInit, 'att
|
|
|
156
168
|
* @returns The Response object for this exchange
|
|
157
169
|
*/
|
|
158
170
|
get requiredResponse(): Response;
|
|
171
|
+
/**
|
|
172
|
+
* Gets the extracted result by applying the result extractor to the exchange.
|
|
173
|
+
* The result is cached after the first computation.
|
|
174
|
+
*
|
|
175
|
+
* @returns The extracted result or null if extraction failed
|
|
176
|
+
*/
|
|
177
|
+
typedExtractedResult<R = any>(): R | Promise<R>;
|
|
159
178
|
}
|
|
160
179
|
//# sourceMappingURL=fetchExchange.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchExchange.d.ts","sourceRoot":"","sources":["../src/fetchExchange.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEnE,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"fetchExchange.d.ts","sourceRoot":"","sources":["../src/fetchExchange.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAA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AAQ,CAAC,SAAS,CAAC;IAgB7C;;;;OAIG;IACH,QAAQ,IAAI,OAAO;IAInB;;;;OAIG;IACH,WAAW,IAAI,OAAO;IAItB;;;;;;;;;OASG;IACH,IAAI,gBAAgB,IAAI,QAAQ,CAQ/B;IAED;;;;;OAKG;IACH,oBAAoB,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAOhD"}
|
package/dist/fetcher.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { UrlBuilder, UrlBuilderCapable } from './urlBuilder';
|
|
2
2
|
import { TimeoutCapable } from './timeout';
|
|
3
|
-
import { FetchExchange
|
|
3
|
+
import { FetchExchange } from './fetchExchange';
|
|
4
4
|
import { BaseURLCapable, FetchRequest, FetchRequestInit, RequestHeaders, RequestHeadersCapable } from './fetchRequest';
|
|
5
5
|
import { InterceptorManager } from './interceptorManager';
|
|
6
6
|
import { UrlTemplateStyle } from './urlTemplateResolver';
|
|
@@ -92,41 +92,6 @@ export declare class Fetcher implements UrlBuilderCapable, RequestHeadersCapable
|
|
|
92
92
|
* @throws Error if an unhandled error occurs during request processing
|
|
93
93
|
*/
|
|
94
94
|
request<R = FetchExchange>(request: FetchRequest, resultExtractor?: ResultExtractor<R>, attributes?: Record<string, any>): Promise<R>;
|
|
95
|
-
/**
|
|
96
|
-
* Executes an HTTP request exchange and processes the result using a result extractor.
|
|
97
|
-
*
|
|
98
|
-
* This method creates a FetchExchange object, runs it through the interceptor chain,
|
|
99
|
-
* and then applies a result extractor function to transform the exchange into the desired result type.
|
|
100
|
-
* It's the core method that powers all HTTP request functionality in the Fetcher.
|
|
101
|
-
*
|
|
102
|
-
* @template R - The type of result to return after processing the exchange
|
|
103
|
-
* @param exchangeInit - The initial configuration for the exchange, excluding fetcher, response, and error properties
|
|
104
|
-
* @param resultExtractor - A function that transforms the completed exchange into the desired result type.
|
|
105
|
-
* Defaults to returning the exchange object itself.
|
|
106
|
-
* @returns A Promise that resolves to the result of applying the resultExtractor to the completed exchange
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* ```typescript
|
|
110
|
-
* // Basic usage with default result extractor (returns the exchange)
|
|
111
|
-
* const exchange = await fetcher.exchange({
|
|
112
|
-
* request: { url: '/api/users', method: 'GET' }
|
|
113
|
-
* });
|
|
114
|
-
* console.log(exchange.response.status);
|
|
115
|
-
*
|
|
116
|
-
* // Using a custom result extractor to get the response object
|
|
117
|
-
* const response = await fetcher.exchange(
|
|
118
|
-
* { request: { url: '/api/users', method: 'GET' } },
|
|
119
|
-
* (exchange) => exchange.requiredResponse
|
|
120
|
-
* );
|
|
121
|
-
*
|
|
122
|
-
* // Using a built-in result extractor to get JSON data
|
|
123
|
-
* const users = await fetcher.exchange(
|
|
124
|
-
* { request: { url: '/api/users', method: 'GET' } },
|
|
125
|
-
* ResultExtractors.Json
|
|
126
|
-
* );
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
exchange<R>(exchangeInit: Omit<FetchExchangeInit, 'fetcher'>, resultExtractor?: ResultExtractor<R>): Promise<R>;
|
|
130
95
|
/**
|
|
131
96
|
* Internal helper method for making HTTP requests with a specific method.
|
|
132
97
|
*
|
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,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,EAAkB,KAAK,cAAc,EAAE,MA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