@alwatr/fetch 2.2.1 → 3.1.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/fetch.js DELETED
@@ -1,293 +0,0 @@
1
- import { createLogger, definePackage, NODE_MODE, globalScope } from '@alwatr/logger';
2
- import { getClientId, delay } from '@alwatr/util';
3
- const logger = createLogger('alwatr/fetch');
4
- definePackage('fetch', '2.x');
5
- let alwatrCacheStorage;
6
- const cacheSupported = 'caches' in globalScope;
7
- const duplicateRequestStorage = {};
8
- /**
9
- * Fetch from alwatr services and return standard response.
10
- */
11
- export async function serviceRequest(options) {
12
- logger.logMethodArgs?.('serviceRequest', { url: options.url });
13
- if (!NODE_MODE) {
14
- options.headers ?? (options.headers = {});
15
- if (!options.headers['client-id']) {
16
- options.headers['client-id'] = getClientId();
17
- }
18
- }
19
- let response;
20
- try {
21
- response = await fetch(options);
22
- }
23
- catch (err) {
24
- const errMessage = err.message;
25
- if (errMessage !== 'fetch_cache_not_found') {
26
- logger.error('serviceRequest', err.message || 'fetch_failed', err, options);
27
- }
28
- throw err;
29
- }
30
- let responseText;
31
- try {
32
- responseText = await response.text();
33
- }
34
- catch (err) {
35
- logger.error('serviceRequest', 'invalid_response', err, {
36
- response,
37
- });
38
- throw err;
39
- }
40
- let responseJson;
41
- try {
42
- responseJson = JSON.parse(responseText);
43
- }
44
- catch (err) {
45
- logger.error('serviceRequest', 'invalid_json', err, { responseText });
46
- throw err;
47
- }
48
- if (responseJson.ok !== true) {
49
- if (typeof responseJson.errorCode === 'string') {
50
- logger.accident('serviceRequest', responseJson.errorCode, { responseJson });
51
- throw new Error(responseJson.errorCode);
52
- }
53
- else {
54
- logger.error('serviceRequest', 'fetch_nok', { responseJson });
55
- throw new Error('fetch_nok');
56
- }
57
- }
58
- // TODO: generate fetch signals hook (for easier handle loading and show error toast)
59
- return responseJson;
60
- }
61
- /**
62
- * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,
63
- * remove duplicates, etc.
64
- *
65
- * Example:
66
- *
67
- * ```ts
68
- * const response = await fetch({
69
- * url: '/api/products',
70
- * queryParameters: {limit: 10},
71
- * timeout: 10_000,
72
- * retry: 3,
73
- * cacheStrategy: 'stale_while_revalidate',
74
- * cacheDuplicate: 'auto',
75
- * });
76
- * ```
77
- */
78
- export function fetch(options) {
79
- options = _processOptions(options);
80
- logger.logMethodArgs?.('fetch', { options });
81
- return _handleCacheStrategy(options);
82
- }
83
- /**
84
- * Process fetch options and set defaults, etc.
85
- */
86
- function _processOptions(options) {
87
- options.method ?? (options.method = 'GET');
88
- options.window ?? (options.window = null);
89
- options.timeout ?? (options.timeout = 10000);
90
- options.retry ?? (options.retry = 3);
91
- options.retryDelay ?? (options.retryDelay = 1000);
92
- options.cacheStrategy ?? (options.cacheStrategy = 'network_only');
93
- options.removeDuplicate ?? (options.removeDuplicate = 'never');
94
- options.headers ?? (options.headers = {});
95
- if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {
96
- logger.incident?.('fetch', 'fetch_cache_strategy_unsupported', {
97
- cacheSupported,
98
- });
99
- options.cacheStrategy = 'network_only';
100
- }
101
- if (options.removeDuplicate === 'auto') {
102
- options.removeDuplicate = cacheSupported ? 'until_load' : 'always';
103
- }
104
- if (options.url.lastIndexOf('?') === -1 && options.queryParameters != null) {
105
- const queryParameters = options.queryParameters;
106
- // prettier-ignore
107
- const queryArray = Object
108
- .keys(queryParameters)
109
- .map((key) => `${key}=${String(queryParameters[key])}`);
110
- if (queryArray.length > 0) {
111
- options.url += '?' + queryArray.join('&');
112
- }
113
- }
114
- if (options.bodyJson != null) {
115
- options.body = JSON.stringify(options.bodyJson);
116
- options.headers['Content-Type'] = 'application/json';
117
- }
118
- if (options.token != null) {
119
- options.headers.Authorization = `Bearer ${options.token}`;
120
- }
121
- if (options.userAuth != null) {
122
- options.headers.Authorization = `Bearer ${options.userAuth.id}/${options.userAuth.token}`;
123
- }
124
- return options;
125
- }
126
- /**
127
- * Handle Cache Strategy over `_handleRemoveDuplicate`.
128
- */
129
- async function _handleCacheStrategy(options) {
130
- if (options.cacheStrategy === 'network_only') {
131
- return _handleRemoveDuplicate(options);
132
- }
133
- // else handle cache strategies!
134
- logger.logMethod?.('_handleCacheStrategy');
135
- if (alwatrCacheStorage == null && options.cacheStorageName == null) {
136
- alwatrCacheStorage = await caches.open('alwatr_fetch_cache');
137
- }
138
- const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : alwatrCacheStorage;
139
- const request = new Request(options.url, options);
140
- switch (options.cacheStrategy) {
141
- case 'cache_first': {
142
- const cachedResponse = await cacheStorage.match(request);
143
- if (cachedResponse != null) {
144
- return cachedResponse;
145
- }
146
- // else
147
- const response = await _handleRemoveDuplicate(options);
148
- if (response.ok) {
149
- cacheStorage.put(request, response.clone());
150
- }
151
- return response;
152
- }
153
- case 'cache_only': {
154
- const cachedResponse = await cacheStorage.match(request);
155
- if (cachedResponse == null) {
156
- logger.accident('_handleCacheStrategy', 'fetch_cache_not_found', { url: request.url });
157
- throw new Error('fetch_cache_not_found');
158
- }
159
- // else
160
- return cachedResponse;
161
- }
162
- case 'network_first': {
163
- try {
164
- const networkResponse = await _handleRemoveDuplicate(options);
165
- if (networkResponse.ok) {
166
- cacheStorage.put(request, networkResponse.clone());
167
- }
168
- return networkResponse;
169
- }
170
- catch (err) {
171
- const cachedResponse = await cacheStorage.match(request);
172
- if (cachedResponse != null) {
173
- return cachedResponse;
174
- }
175
- // else
176
- throw err;
177
- }
178
- }
179
- case 'update_cache': {
180
- const networkResponse = await _handleRemoveDuplicate(options);
181
- if (networkResponse.ok) {
182
- cacheStorage.put(request, networkResponse.clone());
183
- }
184
- return networkResponse;
185
- }
186
- case 'stale_while_revalidate': {
187
- const cachedResponse = await cacheStorage.match(request);
188
- const fetchedResponsePromise = _handleRemoveDuplicate(options).then((networkResponse) => {
189
- if (networkResponse.ok) {
190
- cacheStorage.put(request, networkResponse.clone());
191
- if (typeof options.revalidateCallback === 'function') {
192
- setTimeout(options.revalidateCallback, 0, networkResponse.clone());
193
- }
194
- }
195
- return networkResponse;
196
- });
197
- return cachedResponse ?? fetchedResponsePromise;
198
- }
199
- default: {
200
- return _handleRemoveDuplicate(options);
201
- }
202
- }
203
- }
204
- /**
205
- * Handle Remove Duplicates over `_handleRetryPattern`.
206
- */
207
- async function _handleRemoveDuplicate(options) {
208
- if (options.removeDuplicate === 'never')
209
- return _handleRetryPattern(options);
210
- logger.logMethod?.('_handleRemoveDuplicate');
211
- const cacheKey = options.method + ' ' + options.url;
212
- // We must cache fetch promise without await for handle other parallel requests.
213
- duplicateRequestStorage[cacheKey] ?? (duplicateRequestStorage[cacheKey] = _handleRetryPattern(options));
214
- try {
215
- // For all requests need to await for clone responses.
216
- const response = await duplicateRequestStorage[cacheKey];
217
- if (duplicateRequestStorage[cacheKey] != null) {
218
- if (response.ok !== true || options.removeDuplicate === 'until_load') {
219
- delete duplicateRequestStorage[cacheKey];
220
- }
221
- }
222
- return response.clone();
223
- }
224
- catch (err) {
225
- // clean cache on any error.
226
- delete duplicateRequestStorage[cacheKey];
227
- throw err;
228
- }
229
- }
230
- /**
231
- * Handle retry pattern over `_handleTimeout`.
232
- */
233
- async function _handleRetryPattern(options) {
234
- if (!(options.retry > 1))
235
- return _handleTimeout(options);
236
- logger.logMethod?.('_handleRetryPattern');
237
- options.retry--;
238
- const externalAbortSignal = options.signal;
239
- try {
240
- const response = await _handleTimeout(options);
241
- if (response.status < 500) {
242
- return response;
243
- }
244
- // else
245
- throw new Error('fetch_server_error');
246
- }
247
- catch (err) {
248
- logger.accident('fetch', 'fetch_failed_retry', err);
249
- if (globalScope.navigator?.onLine === false) {
250
- throw new Error('offline');
251
- }
252
- await delay(options.retryDelay);
253
- options.signal = externalAbortSignal;
254
- return _handleRetryPattern(options);
255
- }
256
- }
257
- /**
258
- * It's a wrapper around the browser's `fetch` with timeout.
259
- */
260
- function _handleTimeout(options) {
261
- if (options.timeout === 0) {
262
- return globalScope.fetch(options.url, options);
263
- }
264
- // else
265
- logger.logMethod?.('_handleTimeout');
266
- return new Promise((resolved, reject) => {
267
- const abortController = typeof globalScope.AbortController === 'function' ? new AbortController() : null;
268
- const externalAbortSignal = options.signal;
269
- options.signal = abortController?.signal;
270
- if (abortController !== null && externalAbortSignal != null) {
271
- // Respect external abort signal
272
- externalAbortSignal.addEventListener('abort', () => abortController.abort(), { once: true });
273
- }
274
- const timeoutId = setTimeout(() => {
275
- reject(new Error('fetch_timeout'));
276
- abortController?.abort('fetch_timeout');
277
- }, options.timeout);
278
- // abortController.signal.addEventListener('abort', () => {
279
- // logger.incident('fetch', 'fetch_abort_signal', {
280
- // reason: abortController.signal.reason,
281
- // });
282
- // });
283
- globalScope
284
- .fetch(options.url, options)
285
- .then((response) => resolved(response))
286
- .catch((reason) => reject(reason))
287
- .finally(() => {
288
- delete options.signal; // try to avoid memory leak in nodejs!
289
- clearTimeout(timeoutId);
290
- });
291
- });
292
- }
293
- //# sourceMappingURL=fetch.js.map
package/fetch.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetch.js","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,WAAW,EAAC,MAAM,gBAAgB,CAAC;AACnF,OAAO,EAAC,WAAW,EAAE,KAAK,EAAC,MAAM,cAAc,CAAC;AAOhD,MAAM,MAAM,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;AAE5C,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAE9B,IAAI,kBAAyB,CAAC;AAC9B,MAAM,cAAc,GAAG,QAAQ,IAAI,WAAW,CAAC;AAE/C,MAAM,uBAAuB,GAAsC,EAAE,CAAC;AAEtE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAqB;IAErB,MAAM,CAAC,aAAa,EAAE,CAAC,gBAAgB,EAAE,EAAC,GAAG,EAAE,OAAO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;YACnC,eAAe,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC;QAC1C,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QAEpB,2DAA2D;QAC3D,qDAAqD;QACrD,6CAA6C;QAC7C,QAAQ;QACR,MAAM;QAEN,WAAW;aACR,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC;aAC3B,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;aACtC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;aACjC,OAAO,CAAC,GAAG,EAAE;YACZ,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,sCAAsC;YAC7D,YAAY,CAAC,SAAS,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {createLogger, definePackage, NODE_MODE, globalScope} from '@alwatr/logger';\nimport {getClientId, delay} from '@alwatr/util';\n\nimport type {FetchOptions} from './type.js';\nimport type {AlwatrServiceResponse} from '@alwatr/type';\n\nexport type * from './type.js';\n\nconst logger = createLogger('alwatr/fetch');\n\ndefinePackage('fetch', '2.x');\n\nlet alwatrCacheStorage: Cache;\nconst cacheSupported = 'caches' in globalScope;\n\nconst duplicateRequestStorage: Record<string, Promise<Response>> = {};\n\n/**\n * Fetch from alwatr services and return standard response.\n */\nexport async function serviceRequest<T extends AlwatrServiceResponse = AlwatrServiceResponse>(\n options: FetchOptions,\n): Promise<T> {\n logger.logMethodArgs?.('serviceRequest', {url: options.url});\n\n if (!NODE_MODE) {\n options.headers ??= {};\n if (!options.headers['client-id']) {\n options.headers['client-id'] = getClientId();\n }\n }\n\n let response: Response;\n try {\n response = await fetch(options);\n }\n catch (err) {\n const errMessage = (err as Error).message;\n if (errMessage !== 'fetch_cache_not_found') {\n logger.error('serviceRequest', (err as Error).message || 'fetch_failed', err, options);\n }\n throw err;\n }\n\n let responseText: string;\n try {\n responseText = await response.text();\n }\n catch (err) {\n logger.error('serviceRequest', 'invalid_response', err, {\n response,\n });\n throw err;\n }\n\n let responseJson: T;\n try {\n responseJson = JSON.parse(responseText);\n }\n catch (err) {\n logger.error('serviceRequest', 'invalid_json', err, {responseText});\n throw err;\n }\n\n if (responseJson.ok !== true) {\n if (typeof responseJson.errorCode === 'string') {\n logger.accident('serviceRequest', responseJson.errorCode, {responseJson});\n throw new Error(responseJson.errorCode);\n }\n else {\n logger.error('serviceRequest', 'fetch_nok', {responseJson});\n throw new Error('fetch_nok');\n }\n }\n\n // TODO: generate fetch signals hook (for easier handle loading and show error toast)\n\n return responseJson;\n}\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * Example:\n *\n * ```ts\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 10_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport function fetch(options: FetchOptions): Promise<Response> {\n options = _processOptions(options);\n logger.logMethodArgs?.('fetch', {options});\n return _handleCacheStrategy(options as Required<FetchOptions>);\n}\n\n/**\n * Process fetch options and set defaults, etc.\n */\nfunction _processOptions(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 10_000;\n options.retry ??= 3;\n options.retryDelay ??= 1_000;\n options.cacheStrategy ??= 'network_only';\n options.removeDuplicate ??= 'never';\n options.headers ??= {};\n\n if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {\n logger.incident?.('fetch', 'fetch_cache_strategy_unsupported', {\n cacheSupported,\n });\n options.cacheStrategy = 'network_only';\n }\n\n if (options.removeDuplicate === 'auto') {\n options.removeDuplicate = cacheSupported ? 'until_load' : 'always';\n }\n\n if (options.url.lastIndexOf('?') === -1 && options.queryParameters != null) {\n const queryParameters = options.queryParameters;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParameters)\n .map((key) => `${key}=${String(queryParameters[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson != null) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['Content-Type'] = 'application/json';\n }\n\n if (options.token != null) {\n options.headers.Authorization = `Bearer ${options.token}`;\n }\n\n if (options.userAuth != null) {\n options.headers.Authorization = `Bearer ${options.userAuth.id}/${options.userAuth.token}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `_handleRemoveDuplicate`.\n */\nasync function _handleCacheStrategy(options: Required<FetchOptions>): Promise<Response> {\n if (options.cacheStrategy === 'network_only') {\n return _handleRemoveDuplicate(options);\n }\n // else handle cache strategies!\n logger.logMethod?.('_handleCacheStrategy');\n\n if (alwatrCacheStorage == null && options.cacheStorageName == null) {\n alwatrCacheStorage = await caches.open('alwatr_fetch_cache');\n }\n\n const cacheStorage =\n options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : alwatrCacheStorage;\n\n const request = new Request(options.url, options);\n\n switch (options.cacheStrategy) {\n case 'cache_first': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n const response = await _handleRemoveDuplicate(options);\n if (response.ok) {\n cacheStorage.put(request, response.clone());\n }\n return response;\n }\n\n case 'cache_only': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse == null) {\n logger.accident('_handleCacheStrategy', 'fetch_cache_not_found', {url: request.url});\n throw new Error('fetch_cache_not_found');\n }\n // else\n return cachedResponse;\n }\n\n case 'network_first': {\n try {\n const networkResponse = await _handleRemoveDuplicate(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n catch (err) {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n throw err;\n }\n }\n\n case 'update_cache': {\n const networkResponse = await _handleRemoveDuplicate(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n\n case 'stale_while_revalidate': {\n const cachedResponse = await cacheStorage.match(request);\n const fetchedResponsePromise = _handleRemoveDuplicate(options).then((networkResponse) => {\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n if (typeof options.revalidateCallback === 'function') {\n setTimeout(options.revalidateCallback, 0, networkResponse.clone());\n }\n }\n return networkResponse;\n });\n\n return cachedResponse ?? fetchedResponsePromise;\n }\n\n default: {\n return _handleRemoveDuplicate(options);\n }\n }\n}\n\n/**\n * Handle Remove Duplicates over `_handleRetryPattern`.\n */\nasync function _handleRemoveDuplicate(options: Required<FetchOptions>): Promise<Response> {\n if (options.removeDuplicate === 'never') return _handleRetryPattern(options);\n\n logger.logMethod?.('_handleRemoveDuplicate');\n\n const cacheKey = options.method + ' ' + options.url;\n\n // We must cache fetch promise without await for handle other parallel requests.\n duplicateRequestStorage[cacheKey] ??= _handleRetryPattern(options);\n\n try {\n // For all requests need to await for clone responses.\n const response = await duplicateRequestStorage[cacheKey];\n\n if (duplicateRequestStorage[cacheKey] != null) {\n if (response.ok !== true || options.removeDuplicate === 'until_load') {\n delete duplicateRequestStorage[cacheKey];\n }\n }\n\n return response.clone();\n }\n catch (err) {\n // clean cache on any error.\n delete duplicateRequestStorage[cacheKey];\n throw err;\n }\n}\n\n/**\n * Handle retry pattern over `_handleTimeout`.\n */\nasync function _handleRetryPattern(options: Required<FetchOptions>): Promise<Response> {\n if (!(options.retry > 1)) return _handleTimeout(options);\n\n logger.logMethod?.('_handleRetryPattern');\n options.retry--;\n\n const externalAbortSignal = options.signal;\n\n try {\n const response = await _handleTimeout(options);\n\n if (response.status < 500) {\n return response;\n }\n // else\n throw new Error('fetch_server_error');\n }\n catch (err) {\n logger.accident('fetch', 'fetch_failed_retry', err);\n\n if (globalScope.navigator?.onLine === false) {\n throw new Error('offline');\n }\n\n await delay(options.retryDelay);\n\n options.signal = externalAbortSignal;\n return _handleRetryPattern(options);\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` with timeout.\n */\nfunction _handleTimeout(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalScope.fetch(options.url, options);\n }\n // else\n logger.logMethod?.('_handleTimeout');\n return new Promise((resolved, reject) => {\n const abortController = typeof globalScope.AbortController === 'function' ? new AbortController() : null;\n const externalAbortSignal = options.signal;\n options.signal = abortController?.signal;\n\n if (abortController !== null && externalAbortSignal != null) {\n // Respect external abort signal\n externalAbortSignal.addEventListener('abort', () => abortController.abort(), {once: true});\n }\n\n const timeoutId = setTimeout(() => {\n reject(new Error('fetch_timeout'));\n abortController?.abort('fetch_timeout');\n }, options.timeout);\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalScope\n .fetch(options.url, options)\n .then((response) => resolved(response))\n .catch((reason) => reject(reason))\n .finally(() => {\n delete options.signal; // try to avoid memory leak in nodejs!\n clearTimeout(timeoutId);\n });\n });\n}\n"]}
package/type.d.ts DELETED
@@ -1,90 +0,0 @@
1
- import type { Methods, QueryParameters, StringifyableRecord, UserAuth } from '@alwatr/type';
2
- export type CacheStrategy = 'network_only' | 'network_first' | 'cache_only' | 'cache_first' | 'update_cache' | 'stale_while_revalidate';
3
- export type CacheDuplicate = 'never' | 'always' | 'until_load' | 'auto';
4
- export interface FetchOptions extends RequestInit {
5
- /**
6
- * Request URL.
7
- */
8
- url: string;
9
- /**
10
- * A string to set request's method.
11
- *
12
- * @default 'GET'
13
- */
14
- method?: Methods;
15
- /**
16
- * A Headers object to set request's headers.
17
- */
18
- headers?: Record<string, string>;
19
- /**
20
- * A timeout for the fetch request.
21
- * Set `0` for disable it.
22
- *
23
- * Use with cation, you will have memory leak issue in nodejs.
24
- *
25
- * @default 10_000 ms
26
- */
27
- timeout?: number;
28
- /**
29
- * If fetch response not acceptable or timed out, it will retry the request.
30
- *
31
- * @default 3
32
- */
33
- retry?: number;
34
- /**
35
- * Delay before each retries.
36
- *
37
- * @default 1_000 ms
38
- */
39
- retryDelay?: number;
40
- /**
41
- * Simple memory caching for remove duplicate/parallel requests.
42
- *
43
- * - `never`: Never use memory caching.
44
- * - `always`: Always use memory caching and remove all duplicate requests.
45
- * - `until_load`: Cache parallel requests until request completed (it will be removed after the promise resolved).
46
- * - `auto`: If CacheStorage was supported use `until_load` strategy else use `always`.
47
- *
48
- * @default 'never'
49
- */
50
- removeDuplicate?: CacheDuplicate;
51
- /**
52
- * Strategies for caching.
53
- *
54
- * - `network_only`: Only network request without any cache.
55
- * - `network_first`: Network first, falling back to cache.
56
- * - `cache_only`: Cache only without any network request.
57
- * - `cache_first`: Cache first, falling back to network.
58
- * - `update_cache`: Like `network_only` but with update cache.
59
- * - `stale_while_revalidate`: Fastest strategy, Use cached first but always request network to update the cache.
60
- *
61
- * @default 'network_only'
62
- */
63
- cacheStrategy?: CacheStrategy;
64
- /**
65
- * Revalidate callback for `stale_while_revalidate` cache strategy.
66
- */
67
- revalidateCallback?: (response: Response) => void | Promise<void>;
68
- /**
69
- * Cache storage custom name.
70
- */
71
- cacheStorageName?: string;
72
- /**
73
- * Body as JS Object.
74
- */
75
- bodyJson?: StringifyableRecord;
76
- /**
77
- * URL Query Parameters as JS Object.
78
- */
79
- queryParameters?: QueryParameters;
80
- /**
81
- * Add token to Authentication bearer header.
82
- */
83
- token?: string;
84
- /**
85
- * Add user id and token to Authentication bearer header.
86
- */
87
- userAuth?: UserAuth;
88
- }
89
- export type StringifyableFetchOptions = Pick<FetchOptions, 'url' | 'bodyJson' | 'cache' | 'cacheStorageName' | 'cacheStrategy' | 'headers' | 'credentials' | 'keepalive' | 'method' | 'mode' | 'queryParameters' | 'removeDuplicate' | 'timeout' | 'token' | 'retry' | 'retryDelay' | 'referrer' | 'referrerPolicy' | 'userAuth'>;
90
- //# sourceMappingURL=type.d.ts.map
package/type.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AAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,YAAY,EACV,KAAK,GACL,UAAU,GACV,OAAO,GACP,kBAAkB,GAClB,eAAe,GACf,SAAS,GACT,aAAa,GACb,WAAW,GACX,QAAQ,GACR,MAAM,GACN,iBAAiB,GACjB,iBAAiB,GACjB,SAAS,GACT,OAAO,GACP,OAAO,GACP,YAAY,GACZ,UAAU,GACV,gBAAgB,GAChB,UAAU,CACb,CAAC"}
package/type.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=type.js.map
package/type.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"","sourcesContent":["import type {Methods, QueryParameters, StringifyableRecord, UserAuth} from '@alwatr/type';\n\nexport type CacheStrategy =\n | 'network_only'\n | 'network_first'\n | 'cache_only'\n | 'cache_first'\n | 'update_cache'\n | 'stale_while_revalidate';\n\nexport type CacheDuplicate = 'never' | 'always' | 'until_load' | 'auto';\n\nexport interface FetchOptions extends RequestInit {\n /**\n * Request URL.\n */\n url: string;\n\n /**\n * A string to set request's method.\n *\n * @default 'GET'\n */\n method?: Methods;\n\n /**\n * A Headers object to set request's headers.\n */\n headers?: Record<string, string>;\n\n /**\n * A timeout for the fetch request.\n * Set `0` for disable it.\n *\n * Use with cation, you will have memory leak issue in nodejs.\n *\n * @default 10_000 ms\n */\n timeout?: number;\n\n /**\n * If fetch response not acceptable or timed out, it will retry the request.\n *\n * @default 3\n */\n retry?: number;\n\n /**\n * Delay before each retries.\n *\n * @default 1_000 ms\n */\n retryDelay?: number;\n\n /**\n * Simple memory caching for remove duplicate/parallel requests.\n *\n * - `never`: Never use memory caching.\n * - `always`: Always use memory caching and remove all duplicate requests.\n * - `until_load`: Cache parallel requests until request completed (it will be removed after the promise resolved).\n * - `auto`: If CacheStorage was supported use `until_load` strategy else use `always`.\n *\n * @default 'never'\n */\n removeDuplicate?: CacheDuplicate;\n\n /**\n * Strategies for caching.\n *\n * - `network_only`: Only network request without any cache.\n * - `network_first`: Network first, falling back to cache.\n * - `cache_only`: Cache only without any network request.\n * - `cache_first`: Cache first, falling back to network.\n * - `update_cache`: Like `network_only` but with update cache.\n * - `stale_while_revalidate`: Fastest strategy, Use cached first but always request network to update the cache.\n *\n * @default 'network_only'\n */\n cacheStrategy?: CacheStrategy;\n\n /**\n * Revalidate callback for `stale_while_revalidate` cache strategy.\n */\n revalidateCallback?: (response: Response) => void | Promise<void>;\n\n /**\n * Cache storage custom name.\n */\n cacheStorageName?: string;\n\n /**\n * Body as JS Object.\n */\n bodyJson?: StringifyableRecord;\n\n /**\n * URL Query Parameters as JS Object.\n */\n queryParameters?: QueryParameters;\n\n /**\n * Add token to Authentication bearer header.\n */\n token?: string;\n\n /**\n * Add user id and token to Authentication bearer header.\n */\n userAuth?: UserAuth;\n}\n\nexport type StringifyableFetchOptions = Pick<\n FetchOptions,\n | 'url'\n | 'bodyJson'\n | 'cache'\n | 'cacheStorageName'\n | 'cacheStrategy'\n | 'headers'\n | 'credentials'\n | 'keepalive'\n | 'method'\n | 'mode'\n | 'queryParameters'\n | 'removeDuplicate'\n | 'timeout'\n | 'token'\n | 'retry'\n | 'retryDelay'\n | 'referrer'\n | 'referrerPolicy'\n | 'userAuth'\n>;\n"]}