@alwatr/fetch 0.22.1 → 0.23.0
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/CHANGELOG.md +10 -0
- package/fetch.d.ts +3 -75
- package/fetch.d.ts.map +1 -1
- package/fetch.js +52 -42
- package/fetch.js.map +1 -1
- package/package.json +3 -3
- package/type.d.ts +77 -0
- package/type.d.ts.map +1 -0
- package/type.js +2 -0
- package/type.js.map +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.23.0](https://github.com/AliMD/alwatr/compare/v0.22.1...v0.23.0) (2022-11-23)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **fetch:** types ([4ce81a7](https://github.com/AliMD/alwatr/commit/4ce81a7b82d01ab938754bc5e483ccb074671e33))
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
- **fetch:** set token in options ([033a638](https://github.com/AliMD/alwatr/commit/033a63846f8c43b86c7d0a662e4bb6aa7cae3af5))
|
|
15
|
+
|
|
6
16
|
## [0.22.1](https://github.com/AliMD/alwatr/compare/v0.22.0...v0.22.1) (2022-11-21)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @alwatr/fetch
|
package/fetch.d.ts
CHANGED
|
@@ -1,75 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export interface FetchOptions extends RequestInit {
|
|
4
|
-
/**
|
|
5
|
-
* Request URL.
|
|
6
|
-
*/
|
|
7
|
-
url: string;
|
|
8
|
-
/**
|
|
9
|
-
* A string to set request's method.
|
|
10
|
-
*/
|
|
11
|
-
method: string;
|
|
12
|
-
/**
|
|
13
|
-
* A timeout for the fetch request.
|
|
14
|
-
* Set `0` for disable it.
|
|
15
|
-
*
|
|
16
|
-
* Use with cation, you will have memory leak issue in nodejs.
|
|
17
|
-
*
|
|
18
|
-
* @default 10_000 ms
|
|
19
|
-
*/
|
|
20
|
-
timeout: number;
|
|
21
|
-
/**
|
|
22
|
-
* If fetch response not acceptable or timed out, it will retry the request.
|
|
23
|
-
*
|
|
24
|
-
* @default 3
|
|
25
|
-
*/
|
|
26
|
-
retry: number;
|
|
27
|
-
/**
|
|
28
|
-
* Delay before each retries.
|
|
29
|
-
*
|
|
30
|
-
* @default 1_000 ms
|
|
31
|
-
*/
|
|
32
|
-
retryDelay: number;
|
|
33
|
-
/**
|
|
34
|
-
* Simple memory caching for remove duplicate/parallel requests.
|
|
35
|
-
*
|
|
36
|
-
* - `never`: Never use memory caching.
|
|
37
|
-
* - `always`: Always use memory caching and remove all duplicate requests.
|
|
38
|
-
* - `until_load`: Cache parallel requests until request completed (it will be removed after the promise resolved).
|
|
39
|
-
* - `auto`: If CacheStorage was supported use `until_load` strategy else use `always`.
|
|
40
|
-
*
|
|
41
|
-
* @default 'never'
|
|
42
|
-
*/
|
|
43
|
-
removeDuplicate: CacheDuplicate;
|
|
44
|
-
/**
|
|
45
|
-
* Strategies for caching.
|
|
46
|
-
*
|
|
47
|
-
* - `network_only`: Only network request without any cache.
|
|
48
|
-
* - `network_first`: Network first, falling back to cache.
|
|
49
|
-
* - `cache_only`: Cache only without any network request.
|
|
50
|
-
* - `cache_first`: Cache first, falling back to network.
|
|
51
|
-
* - `stale_while_revalidate`: Fastest strategy, Use cached first but always request network to update the cache.
|
|
52
|
-
*
|
|
53
|
-
* @default 'network_only'
|
|
54
|
-
*/
|
|
55
|
-
cacheStrategy: CacheStrategy;
|
|
56
|
-
/**
|
|
57
|
-
* Revalidate callback for `stale_while_revalidate` cache strategy.
|
|
58
|
-
*/
|
|
59
|
-
revalidateCallback?: (response: Response) => void;
|
|
60
|
-
/**
|
|
61
|
-
* Cache storage custom name.
|
|
62
|
-
*/
|
|
63
|
-
cacheStorageName?: string;
|
|
64
|
-
/**
|
|
65
|
-
* Body as JS Object.
|
|
66
|
-
*/
|
|
67
|
-
bodyJson?: Record<string | number, unknown>;
|
|
68
|
-
/**
|
|
69
|
-
* URL Query Parameters as JS Object.
|
|
70
|
-
*/
|
|
71
|
-
queryParameters?: Record<string, string | number | boolean>;
|
|
72
|
-
}
|
|
1
|
+
import type { FetchOptions, CacheDuplicate, CacheStrategy } from './type';
|
|
2
|
+
export { FetchOptions, CacheDuplicate, CacheStrategy };
|
|
73
3
|
/**
|
|
74
4
|
* It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,
|
|
75
5
|
* remove duplicates, etc.
|
|
@@ -87,7 +17,5 @@ export interface FetchOptions extends RequestInit {
|
|
|
87
17
|
* });
|
|
88
18
|
* ```
|
|
89
19
|
*/
|
|
90
|
-
export declare function fetch(_options:
|
|
91
|
-
url: string;
|
|
92
|
-
}): Promise<Response>;
|
|
20
|
+
export declare function fetch(_options: FetchOptions): Promise<Response>;
|
|
93
21
|
//# sourceMappingURL=fetch.d.ts.map
|
package/fetch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,YAAY,EAAE,cAAc,EAAE,aAAa,EAAC,MAAM,QAAQ,CAAC;AAExE,OAAO,EAAC,YAAY,EAAE,cAAc,EAAE,aAAa,EAAC,CAAC;AAcrD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI/D"}
|
package/fetch.js
CHANGED
|
@@ -66,34 +66,13 @@ function _processOptions(options) {
|
|
|
66
66
|
'Content-Type': 'application/json',
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
async function _handleRemoveDuplicate(options) {
|
|
75
|
-
if (options.removeDuplicate === 'never')
|
|
76
|
-
return _handleRetryPattern(options);
|
|
77
|
-
logger.logMethod('_handleRemoveDuplicate');
|
|
78
|
-
const cacheKey = `[${options.method}] ${options.url}`;
|
|
79
|
-
const firstRequest = duplicateRequestStorage[cacheKey] == null;
|
|
80
|
-
// We must cache fetch promise without await for handle other parallel requests.
|
|
81
|
-
duplicateRequestStorage[cacheKey] ?? (duplicateRequestStorage[cacheKey] = _handleRetryPattern(options));
|
|
82
|
-
try {
|
|
83
|
-
// For all requests need to await for clone responses.
|
|
84
|
-
const response = await duplicateRequestStorage[cacheKey];
|
|
85
|
-
if (firstRequest === true) {
|
|
86
|
-
if (response.ok !== true || options.removeDuplicate === 'until_load') {
|
|
87
|
-
delete duplicateRequestStorage[cacheKey];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return response.clone();
|
|
91
|
-
}
|
|
92
|
-
catch (err) {
|
|
93
|
-
// clean cache on any error.
|
|
94
|
-
delete duplicateRequestStorage[cacheKey];
|
|
95
|
-
throw err;
|
|
69
|
+
if (options.token != null) {
|
|
70
|
+
options.headers = {
|
|
71
|
+
...options.headers,
|
|
72
|
+
Authorization: `Bearer ${options.token}`,
|
|
73
|
+
};
|
|
96
74
|
}
|
|
75
|
+
return options;
|
|
97
76
|
}
|
|
98
77
|
/**
|
|
99
78
|
* Handle Cache Strategy over `_handleRemoveDuplicate`.
|
|
@@ -112,8 +91,10 @@ async function _handleCacheStrategy(options) {
|
|
|
112
91
|
switch (options.cacheStrategy) {
|
|
113
92
|
case 'cache_first': {
|
|
114
93
|
const cachedResponse = await cacheStorage.match(request);
|
|
115
|
-
if (cachedResponse != null)
|
|
94
|
+
if (cachedResponse != null) {
|
|
116
95
|
return cachedResponse;
|
|
96
|
+
}
|
|
97
|
+
// else
|
|
117
98
|
const response = await _handleRemoveDuplicate(options);
|
|
118
99
|
if (response.ok) {
|
|
119
100
|
cacheStorage.put(request, response.clone());
|
|
@@ -122,9 +103,11 @@ async function _handleCacheStrategy(options) {
|
|
|
122
103
|
}
|
|
123
104
|
case 'cache_only': {
|
|
124
105
|
const cachedResponse = await cacheStorage.match(request);
|
|
125
|
-
if (cachedResponse
|
|
126
|
-
|
|
127
|
-
|
|
106
|
+
if (cachedResponse != null) {
|
|
107
|
+
return cachedResponse;
|
|
108
|
+
}
|
|
109
|
+
// else
|
|
110
|
+
throw new Error('fetch_cache_not_found');
|
|
128
111
|
}
|
|
129
112
|
case 'network_first': {
|
|
130
113
|
try {
|
|
@@ -136,29 +119,57 @@ async function _handleCacheStrategy(options) {
|
|
|
136
119
|
}
|
|
137
120
|
catch (err) {
|
|
138
121
|
const cachedResponse = await cacheStorage.match(request);
|
|
139
|
-
if (cachedResponse
|
|
140
|
-
|
|
141
|
-
|
|
122
|
+
if (cachedResponse != null) {
|
|
123
|
+
return cachedResponse;
|
|
124
|
+
}
|
|
125
|
+
// else
|
|
126
|
+
throw err;
|
|
142
127
|
}
|
|
143
128
|
}
|
|
144
129
|
case 'stale_while_revalidate': {
|
|
145
130
|
const cachedResponse = await cacheStorage.match(request);
|
|
146
|
-
const fetchedResponsePromise = _handleRemoveDuplicate(options)
|
|
147
|
-
fetchedResponsePromise.then((networkResponse) => {
|
|
131
|
+
const fetchedResponsePromise = _handleRemoveDuplicate(options).then((networkResponse) => {
|
|
148
132
|
if (networkResponse.ok) {
|
|
149
133
|
cacheStorage.put(request, networkResponse.clone());
|
|
150
134
|
if (cachedResponse != null && typeof options.revalidateCallback === 'function') {
|
|
151
135
|
options.revalidateCallback(networkResponse);
|
|
152
136
|
}
|
|
153
137
|
}
|
|
138
|
+
return networkResponse;
|
|
154
139
|
});
|
|
155
|
-
return cachedResponse
|
|
140
|
+
return cachedResponse ?? fetchedResponsePromise;
|
|
156
141
|
}
|
|
157
142
|
default: {
|
|
158
143
|
return _handleRemoveDuplicate(options);
|
|
159
144
|
}
|
|
160
145
|
}
|
|
161
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Handle Remove Duplicates over `_handleRetryPattern`.
|
|
149
|
+
*/
|
|
150
|
+
async function _handleRemoveDuplicate(options) {
|
|
151
|
+
if (options.removeDuplicate === 'never')
|
|
152
|
+
return _handleRetryPattern(options);
|
|
153
|
+
logger.logMethod('_handleRemoveDuplicate');
|
|
154
|
+
const cacheKey = options.method + ' ' + options.url;
|
|
155
|
+
// We must cache fetch promise without await for handle other parallel requests.
|
|
156
|
+
duplicateRequestStorage[cacheKey] ?? (duplicateRequestStorage[cacheKey] = _handleRetryPattern(options));
|
|
157
|
+
try {
|
|
158
|
+
// For all requests need to await for clone responses.
|
|
159
|
+
const response = await duplicateRequestStorage[cacheKey];
|
|
160
|
+
if (duplicateRequestStorage[cacheKey] != null) {
|
|
161
|
+
if (response.ok !== true || options.removeDuplicate === 'until_load') {
|
|
162
|
+
delete duplicateRequestStorage[cacheKey];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return response.clone();
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
// clean cache on any error.
|
|
169
|
+
delete duplicateRequestStorage[cacheKey];
|
|
170
|
+
throw err;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
162
173
|
/**
|
|
163
174
|
* Handle retry pattern over `_handleTimeout`.
|
|
164
175
|
*/
|
|
@@ -170,12 +181,11 @@ async function _handleRetryPattern(options) {
|
|
|
170
181
|
const externalAbortSignal = options.signal;
|
|
171
182
|
try {
|
|
172
183
|
const response = await _handleTimeout(options);
|
|
173
|
-
if (response.status
|
|
174
|
-
logger.incident('fetch', 'fetch_server_error', 'fetch server error ' + response.status);
|
|
175
|
-
throw new Error('fetch_server_error');
|
|
176
|
-
}
|
|
177
|
-
else
|
|
184
|
+
if (response.status < 500) {
|
|
178
185
|
return response;
|
|
186
|
+
}
|
|
187
|
+
// else
|
|
188
|
+
throw new Error('fetch_server_error');
|
|
179
189
|
}
|
|
180
190
|
catch (err) {
|
|
181
191
|
logger.accident('fetch', err?.name ?? 'fetch_failed', 'fetch failed and retry', { err });
|
package/fetch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAMlE,MAAM,MAAM,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;AAE5C,oBAAoB,CAAC,IAAI,CAAC;IACxB,IAAI,EAAE,eAAe;IACrB,OAAO,EAAE,oBAAoB;CAC9B,CAAC,CAAC;AAEH,IAAI,kBAAyB,CAAC;AAC9B,MAAM,cAAc,GAAG,QAAQ,IAAI,UAAU,CAAC;AAE9C,MAAM,uBAAuB,GAAsC,EAAE,CAAC;AAEtE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,KAAK,CAAC,QAAsB;IAC1C,MAAM,OAAO,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC1C,MAAM,CAAC,aAAa,CAAC,OAAO,EAAE,EAAC,OAAO,EAAC,CAAC,CAAC;IACzC,OAAO,oBAAoB,CAAC,OAAO,CAAC,CAAC;AACvC,CAAC;AAED;;GAEG;AACH,SAAS,eAAe,CAAC,OAAqB;IAC5C,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IAC/E,OAAO,CAAC,MAAM,KAAd,OAAO,CAAC,MAAM,GAAK,IAAI,EAAC;IAExB,OAAO,CAA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retry pattern, timeout, helper methods and enhanced types written in tiny TypeScript, ES module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"url": "https://github.com/AliMD/alwatr/issues"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@alwatr/logger": "^0.
|
|
37
|
+
"@alwatr/logger": "^0.23.0",
|
|
38
38
|
"tslib": "^2.4.1"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "dc058c8579094a527af9e12f4fe63615e7add42f"
|
|
41
41
|
}
|
package/type.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export type CacheStrategy = 'network_only' | 'network_first' | 'cache_only' | 'cache_first' | 'stale_while_revalidate';
|
|
2
|
+
export type CacheDuplicate = 'never' | 'always' | 'until_load' | 'auto';
|
|
3
|
+
export interface FetchOptions extends RequestInit {
|
|
4
|
+
/**
|
|
5
|
+
* Request URL.
|
|
6
|
+
*/
|
|
7
|
+
url: string;
|
|
8
|
+
/**
|
|
9
|
+
* A string to set request's method.
|
|
10
|
+
*/
|
|
11
|
+
method?: string;
|
|
12
|
+
/**
|
|
13
|
+
* A timeout for the fetch request.
|
|
14
|
+
* Set `0` for disable it.
|
|
15
|
+
*
|
|
16
|
+
* Use with cation, you will have memory leak issue in nodejs.
|
|
17
|
+
*
|
|
18
|
+
* @default 10_000 ms
|
|
19
|
+
*/
|
|
20
|
+
timeout?: number;
|
|
21
|
+
/**
|
|
22
|
+
* If fetch response not acceptable or timed out, it will retry the request.
|
|
23
|
+
*
|
|
24
|
+
* @default 3
|
|
25
|
+
*/
|
|
26
|
+
retry?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Delay before each retries.
|
|
29
|
+
*
|
|
30
|
+
* @default 1_000 ms
|
|
31
|
+
*/
|
|
32
|
+
retryDelay?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Simple memory caching for remove duplicate/parallel requests.
|
|
35
|
+
*
|
|
36
|
+
* - `never`: Never use memory caching.
|
|
37
|
+
* - `always`: Always use memory caching and remove all duplicate requests.
|
|
38
|
+
* - `until_load`: Cache parallel requests until request completed (it will be removed after the promise resolved).
|
|
39
|
+
* - `auto`: If CacheStorage was supported use `until_load` strategy else use `always`.
|
|
40
|
+
*
|
|
41
|
+
* @default 'never'
|
|
42
|
+
*/
|
|
43
|
+
removeDuplicate?: CacheDuplicate;
|
|
44
|
+
/**
|
|
45
|
+
* Strategies for caching.
|
|
46
|
+
*
|
|
47
|
+
* - `network_only`: Only network request without any cache.
|
|
48
|
+
* - `network_first`: Network first, falling back to cache.
|
|
49
|
+
* - `cache_only`: Cache only without any network request.
|
|
50
|
+
* - `cache_first`: Cache first, falling back to network.
|
|
51
|
+
* - `stale_while_revalidate`: Fastest strategy, Use cached first but always request network to update the cache.
|
|
52
|
+
*
|
|
53
|
+
* @default 'network_only'
|
|
54
|
+
*/
|
|
55
|
+
cacheStrategy?: CacheStrategy;
|
|
56
|
+
/**
|
|
57
|
+
* Revalidate callback for `stale_while_revalidate` cache strategy.
|
|
58
|
+
*/
|
|
59
|
+
revalidateCallback?: (response: Response) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Cache storage custom name.
|
|
62
|
+
*/
|
|
63
|
+
cacheStorageName?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Body as JS Object.
|
|
66
|
+
*/
|
|
67
|
+
bodyJson?: Record<string | number, unknown>;
|
|
68
|
+
/**
|
|
69
|
+
* URL Query Parameters as JS Object.
|
|
70
|
+
*/
|
|
71
|
+
queryParameters?: Record<string, string | number | boolean>;
|
|
72
|
+
/**
|
|
73
|
+
* Add token to Authentication bearer header.
|
|
74
|
+
*/
|
|
75
|
+
token?: string;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=type.d.ts.map
|
package/type.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,aAAa,GAAG,cAAc,GAAG,eAAe,GAAG,YAAY,GAAG,aAAa,GAAG,wBAAwB,CAAC;AACvH,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,QAAQ,GAAG,YAAY,GAAG,MAAM,CAAC;AAExE,MAAM,WAAW,YAAa,SAAQ,WAAW;IAC/C;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,cAAc,CAAC;IAEjC;;;;;;;;;;OAUG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;IAE9B;;OAEG;IACH,kBAAkB,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IAElD;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC;IAE5C;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;IAE5D;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
|
package/type.js
ADDED
package/type.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":""}
|