@alwatr/fetch 4.1.6 → 4.2.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 +41 -27
- package/dist/core.d.ts.map +1 -1
- package/dist/main.cjs +9 -7
- package/dist/main.cjs.map +3 -3
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +11 -7
- package/dist/main.mjs.map +4 -4
- package/dist/type.d.ts +4 -7
- package/dist/type.d.ts.map +1 -1
- package/package.json +11 -10
- package/src/main.test.js +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,20 @@
|
|
|
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
|
+
## [4.2.0](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@4.1.7...@alwatr/fetch@4.2.0) (2024-10-28)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **fetch:** use @alwatr/http-primer for types and http codes ([6fe993a](https://github.com/Alwatr/nanolib/commit/6fe993ac0f395a4c0c6ad3b2caa48a2986cc850f)) by @AliMD
|
|
11
|
+
|
|
12
|
+
### Code Refactoring
|
|
13
|
+
|
|
14
|
+
* **fetch:** update HTTP headers content-type to use MimeTypes constant ([c3862fc](https://github.com/Alwatr/nanolib/commit/c3862fc6a643da97dacbd15bcf5d3351caaaf269)) by @AliMD
|
|
15
|
+
|
|
16
|
+
## [4.1.7](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@4.1.6...@alwatr/fetch@4.1.7) (2024-10-25)
|
|
17
|
+
|
|
18
|
+
**Note:** Version bump only for package @alwatr/fetch
|
|
19
|
+
|
|
6
20
|
## [4.1.6](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@4.1.5...@alwatr/fetch@4.1.6) (2024-10-12)
|
|
7
21
|
|
|
8
22
|
**Note:** Version bump only for package @alwatr/fetch
|
|
@@ -11,14 +25,14 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
11
25
|
|
|
12
26
|
### Code Refactoring
|
|
13
27
|
|
|
14
|
-
|
|
15
|
-
|
|
28
|
+
- prevent side-effects ([01e00e1](https://github.com/Alwatr/nanolib/commit/01e00e191385cc92b28677df0c01a085916ae677)) by @mohammadhonarvar
|
|
29
|
+
- use new `global-this` package ([42510b9](https://github.com/Alwatr/nanolib/commit/42510b9ae0e385206a902db093d188949f1cb84e)) by @mohammadhonarvar
|
|
16
30
|
|
|
17
31
|
## [4.1.4](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@4.1.3...@alwatr/fetch@4.1.4) (2024-10-11)
|
|
18
32
|
|
|
19
33
|
### Miscellaneous Chores
|
|
20
34
|
|
|
21
|
-
|
|
35
|
+
- include LICENSE and LEGAL files to publish ([09f366f](https://github.com/Alwatr/nanolib/commit/09f366f680bfa9fb26acb2cd1ccbc68c5a9e9ad8)) by @AliMD
|
|
22
36
|
|
|
23
37
|
## [4.1.3](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@4.1.2...@alwatr/fetch@4.1.3) (2024-10-11)
|
|
24
38
|
|
|
@@ -28,7 +42,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
28
42
|
|
|
29
43
|
### Dependencies update
|
|
30
44
|
|
|
31
|
-
|
|
45
|
+
- bump the development-dependencies group with 10 updates ([fa4aaf0](https://github.com/Alwatr/nanolib/commit/fa4aaf04c907ecae06aa14000ce35216170c15ad)) by @dependabot[bot]
|
|
32
46
|
|
|
33
47
|
## [4.1.1](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@4.1.0...@alwatr/fetch@4.1.1) (2024-10-08)
|
|
34
48
|
|
|
@@ -38,30 +52,30 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
38
52
|
|
|
39
53
|
### Features
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
- use `package-tracer` ([cc3c5f9](https://github.com/Alwatr/nanolib/commit/cc3c5f9c1a3d03f0d81b46835665f16a0426fd0d)) by @mohammadhonarvar
|
|
42
56
|
|
|
43
57
|
### Bug Fixes
|
|
44
58
|
|
|
45
|
-
|
|
46
|
-
|
|
59
|
+
- all dependeny topology ([1c17f34](https://github.com/Alwatr/nanolib/commit/1c17f349adf3e98e2a80ab2da4f0f81028dc9c5f)) by @mohammadhonarvar
|
|
60
|
+
- **fetch:** remove unused import from fetch core module ([28ec726](https://github.com/Alwatr/nanolib/commit/28ec7269322f90dba02fbb33e4e622db42169368)) by @AliMD
|
|
47
61
|
|
|
48
62
|
### Code Refactoring
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
- **fetch:** update fetch package to use @alwatr/parse-duration for timeout and retryDelay durations ([1108c54](https://github.com/Alwatr/nanolib/commit/1108c547e43f2c65f46d65b58dd19cee9abd2fd7)) by @AliMD
|
|
65
|
+
- **fetch:** Update logger import and initialization ([1f0451c](https://github.com/Alwatr/nanolib/commit/1f0451c9fec81b875736135778cdd4150556ba97)) by @AliMD
|
|
66
|
+
- **fetch:** use new DictionaryReq type ([a8149cf](https://github.com/Alwatr/nanolib/commit/a8149cff114da7c7ce9a335c837ae794904fa3ca)) by @AliMD
|
|
67
|
+
- update Dictionary type definitions ([c94cbc4](https://github.com/Alwatr/nanolib/commit/c94cbc4523864e2cc47828ccf5508b68945ac2b8)) by @AliMD
|
|
68
|
+
- use new type-helper global types and remove all import types ([08b5d08](https://github.com/Alwatr/nanolib/commit/08b5d08c03c7c315382337239de0426462f384b8)) by @AliMD
|
|
69
|
+
- **wait:** rename package to delay ([cf8c45c](https://github.com/Alwatr/nanolib/commit/cf8c45cf3f5b61fdd4b1b1c7f744c4eb3e230016)) by @AliMD
|
|
56
70
|
|
|
57
71
|
### Miscellaneous Chores
|
|
58
72
|
|
|
59
|
-
|
|
60
|
-
|
|
73
|
+
- **fetch:** change the license to AGPL-3.0 ([edf9069](https://github.com/Alwatr/nanolib/commit/edf9069608bd276b85c9ac937e33ad225c5921a9)) by @ArmanAsadian
|
|
74
|
+
- Update build and lint scripts ([392d0b7](https://github.com/Alwatr/nanolib/commit/392d0b71f446bce336b0256119a80f07aff794ba)) by @AliMD
|
|
61
75
|
|
|
62
76
|
### Dependencies update
|
|
63
77
|
|
|
64
|
-
|
|
78
|
+
- bump @types/node ([3d80fed](https://github.com/Alwatr/nanolib/commit/3d80fedaf720af792feb060c2f81c737ebb84e11)) by @dependabot[bot]
|
|
65
79
|
|
|
66
80
|
## [4.0.1](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@4.0.0...@alwatr/fetch@4.0.1) (2024-09-21)
|
|
67
81
|
|
|
@@ -71,23 +85,23 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
71
85
|
|
|
72
86
|
### ⚠ BREAKING CHANGES
|
|
73
87
|
|
|
74
|
-
|
|
88
|
+
- **fetch:** queryParametters renamed to queryParams
|
|
75
89
|
|
|
76
90
|
### Code Refactoring
|
|
77
91
|
|
|
78
|
-
|
|
79
|
-
|
|
92
|
+
- **fetch:** handle fetchJson error responses properly ([ae8fe24](https://github.com/Alwatr/nanolib/commit/ae8fe244aca17f235c4347ff1fd10070a410340c)) by @AliMD
|
|
93
|
+
- **fetch:** update query parameters handling ([939b3d5](https://github.com/Alwatr/nanolib/commit/939b3d52998ec7b3f5c32ff5438b649148109ede)) by @AliMD
|
|
80
94
|
|
|
81
95
|
### Dependencies update
|
|
82
96
|
|
|
83
|
-
|
|
84
|
-
|
|
97
|
+
- bump the development-dependencies group across 1 directory with 10 updates ([9ed98ff](https://github.com/Alwatr/nanolib/commit/9ed98ffd0668d5a36e255c82edab3af53bffda8f)) by @dependabot[bot]
|
|
98
|
+
- update ([c36ed50](https://github.com/Alwatr/nanolib/commit/c36ed50f68da2f5608ccd96119963a16cfacb4ce)) by @AliMD
|
|
85
99
|
|
|
86
100
|
## [3.1.8](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.7...@alwatr/fetch@3.1.8) (2024-08-31)
|
|
87
101
|
|
|
88
102
|
### Miscellaneous Chores
|
|
89
103
|
|
|
90
|
-
|
|
104
|
+
- Update package.json exports for [@alwatr](https://github.com/alwatr) packages ([dacb362](https://github.com/Alwatr/nanolib/commit/dacb362b145e3c51b4aba00ff643687a3fac11d2)) by @
|
|
91
105
|
|
|
92
106
|
## [3.1.7](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.6...@alwatr/fetch@3.1.7) (2024-08-31)
|
|
93
107
|
|
|
@@ -97,19 +111,19 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
97
111
|
|
|
98
112
|
### Dependencies update
|
|
99
113
|
|
|
100
|
-
|
|
114
|
+
- update all dependencies ([1e0c30e](https://github.com/Alwatr/nanolib/commit/1e0c30e6a3a8e19deb5185814e24ab6c08dca573)) by @AliMD
|
|
101
115
|
|
|
102
116
|
## [3.1.5](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.4...@alwatr/fetch@3.1.5) (2024-07-04)
|
|
103
117
|
|
|
104
118
|
### Dependencies update
|
|
105
119
|
|
|
106
|
-
|
|
120
|
+
- update all dependencies ([0e908b4](https://github.com/Alwatr/nanolib/commit/0e908b476a6b976ec2447f864c8cafcbb8a0f099)) by @
|
|
107
121
|
|
|
108
122
|
## [3.1.4](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.3...@alwatr/fetch@3.1.4) (2024-05-12)
|
|
109
123
|
|
|
110
124
|
### Dependencies update
|
|
111
125
|
|
|
112
|
-
|
|
126
|
+
- upgrade ([6dbd300](https://github.com/Alwatr/nanolib/commit/6dbd300642c9bcc9e7d0b281e244bf1b06eb1c38)) by @AliMD
|
|
113
127
|
|
|
114
128
|
## [3.1.3](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.2...@alwatr/fetch@3.1.3) (2024-04-25)
|
|
115
129
|
|
|
@@ -123,11 +137,11 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
123
137
|
|
|
124
138
|
### Bug Fixes
|
|
125
139
|
|
|
126
|
-
|
|
140
|
+
- exported types by add .js extensions to all imports ([fc3d83e](https://github.com/Alwatr/nanolib/commit/fc3d83e8f375da97ba276314b2e6966aa82c9b3f)) by @AliMD
|
|
127
141
|
|
|
128
142
|
### Miscellaneous Chores
|
|
129
143
|
|
|
130
|
-
|
|
144
|
+
- **deps:** update ([1a45030](https://github.com/Alwatr/nanolib/commit/1a450305440b710a300787d4ca24b1ed8c6a39d7)) by @AliMD
|
|
131
145
|
|
|
132
146
|
## [3.1.0](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.0.0...@alwatr/fetch@3.1.0) (2024-01-24)
|
|
133
147
|
|
package/dist/core.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,WAAW,CAAC;AAI5C,eAAO,MAAM,OAAO,uCAAgD,CAAC;AAKrE,eAAO,MAAM,cAAc,SAAuD,CAAC;AAInF;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,YAAY,GAAG,QAAQ,CAAC,YAAY,CAAC,CA+C7E;AAED;;GAEG;AACH,wBAAsB,oBAAoB,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoF7F;AAED;;GAEG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2B/F;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8B5F;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoCvE"}
|
package/dist/main.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @alwatr/fetch v4.
|
|
1
|
+
/* @alwatr/fetch v4.2.0 */
|
|
2
2
|
"use strict";
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -26,14 +26,16 @@ __export(main_exports, {
|
|
|
26
26
|
fetchJson: () => fetchJson
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(main_exports);
|
|
29
|
+
var import_http_primer2 = require("@alwatr/http-primer");
|
|
29
30
|
|
|
30
31
|
// src/core.ts
|
|
31
32
|
var import_delay = require("@alwatr/delay");
|
|
32
33
|
var import_global_this = require("@alwatr/global-this");
|
|
34
|
+
var import_http_primer = require("@alwatr/http-primer");
|
|
33
35
|
var import_logger = require("@alwatr/logger");
|
|
34
36
|
var import_package_tracer = require("@alwatr/package-tracer");
|
|
35
37
|
var import_parse_duration = require("@alwatr/parse-duration");
|
|
36
|
-
__dev_mode__: import_package_tracer.packageTracer.add("@alwatr/fetch", "4.
|
|
38
|
+
__dev_mode__: import_package_tracer.packageTracer.add("@alwatr/fetch", "4.2.0");
|
|
37
39
|
var logger_ = /* @__PURE__ */ (0, import_logger.createLogger)("@alwatr/fetch");
|
|
38
40
|
var globalThis_ = /* @__PURE__ */ (0, import_global_this.getGlobalThis)();
|
|
39
41
|
var cacheStorage_;
|
|
@@ -66,12 +68,12 @@ function processOptions_(options) {
|
|
|
66
68
|
}
|
|
67
69
|
if (options.bodyJson !== void 0) {
|
|
68
70
|
options.body = JSON.stringify(options.bodyJson);
|
|
69
|
-
options.headers["
|
|
71
|
+
options.headers["content-type"] = import_http_primer.MimeTypes.JSON;
|
|
70
72
|
}
|
|
71
73
|
if (options.bearerToken !== void 0) {
|
|
72
|
-
options.headers.
|
|
74
|
+
options.headers.authorization = `Bearer ${options.bearerToken}`;
|
|
73
75
|
} else if (options.alwatrAuth !== void 0) {
|
|
74
|
-
options.headers.
|
|
76
|
+
options.headers.authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;
|
|
75
77
|
}
|
|
76
78
|
return options;
|
|
77
79
|
}
|
|
@@ -170,7 +172,7 @@ async function handleRetryPattern_(options) {
|
|
|
170
172
|
const externalAbortSignal = options.signal;
|
|
171
173
|
try {
|
|
172
174
|
const response = await handleTimeout_(options);
|
|
173
|
-
if (response.status <
|
|
175
|
+
if (response.status < import_http_primer.HttpStatusCodes.Error_Server_500_Internal_Server_Error) {
|
|
174
176
|
return response;
|
|
175
177
|
}
|
|
176
178
|
throw new Error("fetch_server_error");
|
|
@@ -223,7 +225,7 @@ async function fetchJson(options) {
|
|
|
223
225
|
} catch (error) {
|
|
224
226
|
const responseError = {
|
|
225
227
|
ok: false,
|
|
226
|
-
statusCode: response?.status ??
|
|
228
|
+
statusCode: response?.status ?? import_http_primer2.HttpStatusCodes.Error_Server_500_Internal_Server_Error,
|
|
227
229
|
errorCode: responseJson?.errorCode ?? error.message,
|
|
228
230
|
errorMessage: responseJson?.errorMessage ?? error.message,
|
|
229
231
|
responseText,
|
package/dist/main.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts", "../src/core.ts"],
|
|
4
|
-
"sourcesContent": ["import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\n\nexport {cacheSupported};\nexport type * from './type.js';\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 * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: response?.status ?? 500,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n errorMessage: (responseJson?.errorMessage as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_json_failed', {responseError, error});\n return responseError;\n }\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 * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_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", "import {delay} from '@alwatr/delay';\nimport {getGlobalThis} from '@alwatr/global-this';\nimport {createLogger} from '@alwatr/logger';\nimport {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration} from '@alwatr/parse-duration';\n\nimport type {FetchOptions} from './type.js';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport const logger_ = /* #__PURE__ */ createLogger('@alwatr/fetch');\n\nconst globalThis_ = /* #__PURE__ */ getGlobalThis();\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = /* #__PURE__ */ Object.hasOwn(globalThis_, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_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.queryParams != null) {\n const queryParams = options.queryParams;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParams)\n .map((key) => `${key}=${String(queryParams[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['Content-Type'] = 'application/json';\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.Authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.Authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async 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 (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\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 */\nexport async 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 */\nexport async 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 (globalThis_.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await delay.by(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 */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalThis_.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof 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 }, parseDuration(options.timeout!));\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalThis_\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"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["import {HttpStatusCodes, type HttpStatusCode} from '@alwatr/http-primer';\n\nimport {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\n\nexport {cacheSupported};\nexport type * from './type.js';\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 * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status as HttpStatusCode;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: (response?.status as HttpStatusCode) ?? HttpStatusCodes.Error_Server_500_Internal_Server_Error,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n errorMessage: (responseJson?.errorMessage as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_json_failed', {responseError, error});\n return responseError;\n }\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 * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_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", "import {delay} from '@alwatr/delay';\nimport {getGlobalThis} from '@alwatr/global-this';\nimport {HttpStatusCodes, MimeTypes} from '@alwatr/http-primer';\nimport {createLogger} from '@alwatr/logger';\nimport {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration} from '@alwatr/parse-duration';\n\nimport type {FetchOptions} from './type.js';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport const logger_ = /* #__PURE__ */ createLogger('@alwatr/fetch');\n\nconst globalThis_ = /* #__PURE__ */ getGlobalThis();\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = /* #__PURE__ */ Object.hasOwn(globalThis_, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_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.queryParams != null) {\n const queryParams = options.queryParams;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParams)\n .map((key) => `${key}=${String(queryParams[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['content-type'] = MimeTypes.JSON;\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async 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 (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\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 */\nexport async 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 */\nexport async 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 < HttpStatusCodes.Error_Server_500_Internal_Server_Error) {\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 (globalThis_.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await delay.by(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 */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalThis_.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof 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 }, parseDuration(options.timeout!));\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalThis_\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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,sBAAmD;;;ACAnD,mBAAoB;AACpB,yB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Q,CAAC;AAQlC,gBACG,MAAM,QAAQ,KAAK,OAAO,EAC1B,KAAK,CAAC,aAAa,SAAS,QAAQ,CAAC,EACrC,MAAM,CAAC,WAAW,OAAO,MAAM,CAAC,EAChC,QAAQ,MAAM;AACb,aAAO,QAAQ;AACf,mBAAa,SAAS;AAAA,IACxB,CAAC;AAAA,EACL,CAAC;AACH;;;AD9OA,eAAsB,UAAgC,SAAoE;AACxH,MAAI;AACJ,MAAI;AACJ,MAAI;AAEJ,MAAI;AACF,eAAW,MAAM,MAAM,OAAO;AAC9B,mBAAe,MAAM,SAAS,KAAK;AACnC,mBAAe,KAAK,MAAM,YAAY;AACtC,iBAAa,KAAK;AAClB,iBAAa,aAAa,SAAS;AACnC,WAAO;AAAA,EACT,SACO,OAAO;AACZ,UAAM,gBAA+B;AAAA,MACnC,IAAI;AAAA,MACJ,YAAa,UAAU,UAA6B,oCAAgB;AAAA,MACpE,WAAY,cAAc,aAAyB,MAAgB;AAAA,MACnE,cAAe,cAAc,gBAA4B,MAAgB;AAAA,MACzE;AAAA,MACA,MAAM,cAAc;AAAA,IACtB;AAEA,YAAQ,SAAS,aAAa,qBAAqB,EAAC,eAAe,MAAK,CAAC;AACzE,WAAO;AAAA,EACT;AACF;AAwBO,SAAS,MAAM,SAA0C;AAC9D,YAAU,gBAAgB,OAAO;AACjC,UAAQ,gBAAgB,SAAS,EAAC,QAAO,CAAC;AAC1C,SAAO,qBAAqB,OAAiC;AAC/D;",
|
|
6
|
+
"names": ["import_http_primer"]
|
|
7
7
|
}
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAEA,OAAO,EAAiD,cAAc,EAAC,MAAM,WAAW,CAAC;AAEzF,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAE,eAAe,EAAC,MAAM,WAAW,CAAC;AAE5E,OAAO,EAAC,cAAc,EAAC,CAAC;AACxB,mBAAmB,WAAW,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,SAAS,CAAC,CAAC,SAAS,UAAU,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CA0BxH;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI9D"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
/* @alwatr/fetch v4.
|
|
1
|
+
/* @alwatr/fetch v4.2.0 */
|
|
2
|
+
|
|
3
|
+
// src/main.ts
|
|
4
|
+
import { HttpStatusCodes as HttpStatusCodes2 } from "@alwatr/http-primer";
|
|
2
5
|
|
|
3
6
|
// src/core.ts
|
|
4
7
|
import { delay } from "@alwatr/delay";
|
|
5
8
|
import { getGlobalThis } from "@alwatr/global-this";
|
|
9
|
+
import { HttpStatusCodes, MimeTypes } from "@alwatr/http-primer";
|
|
6
10
|
import { createLogger } from "@alwatr/logger";
|
|
7
11
|
import { packageTracer } from "@alwatr/package-tracer";
|
|
8
12
|
import { parseDuration } from "@alwatr/parse-duration";
|
|
9
|
-
__dev_mode__: packageTracer.add("@alwatr/fetch", "4.
|
|
13
|
+
__dev_mode__: packageTracer.add("@alwatr/fetch", "4.2.0");
|
|
10
14
|
var logger_ = /* @__PURE__ */ createLogger("@alwatr/fetch");
|
|
11
15
|
var globalThis_ = /* @__PURE__ */ getGlobalThis();
|
|
12
16
|
var cacheStorage_;
|
|
@@ -39,12 +43,12 @@ function processOptions_(options) {
|
|
|
39
43
|
}
|
|
40
44
|
if (options.bodyJson !== void 0) {
|
|
41
45
|
options.body = JSON.stringify(options.bodyJson);
|
|
42
|
-
options.headers["
|
|
46
|
+
options.headers["content-type"] = MimeTypes.JSON;
|
|
43
47
|
}
|
|
44
48
|
if (options.bearerToken !== void 0) {
|
|
45
|
-
options.headers.
|
|
49
|
+
options.headers.authorization = `Bearer ${options.bearerToken}`;
|
|
46
50
|
} else if (options.alwatrAuth !== void 0) {
|
|
47
|
-
options.headers.
|
|
51
|
+
options.headers.authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;
|
|
48
52
|
}
|
|
49
53
|
return options;
|
|
50
54
|
}
|
|
@@ -143,7 +147,7 @@ async function handleRetryPattern_(options) {
|
|
|
143
147
|
const externalAbortSignal = options.signal;
|
|
144
148
|
try {
|
|
145
149
|
const response = await handleTimeout_(options);
|
|
146
|
-
if (response.status <
|
|
150
|
+
if (response.status < HttpStatusCodes.Error_Server_500_Internal_Server_Error) {
|
|
147
151
|
return response;
|
|
148
152
|
}
|
|
149
153
|
throw new Error("fetch_server_error");
|
|
@@ -196,7 +200,7 @@ async function fetchJson(options) {
|
|
|
196
200
|
} catch (error) {
|
|
197
201
|
const responseError = {
|
|
198
202
|
ok: false,
|
|
199
|
-
statusCode: response?.status ??
|
|
203
|
+
statusCode: response?.status ?? HttpStatusCodes2.Error_Server_500_Internal_Server_Error,
|
|
200
204
|
errorCode: responseJson?.errorCode ?? error.message,
|
|
201
205
|
errorMessage: responseJson?.errorMessage ?? error.message,
|
|
202
206
|
responseText,
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/
|
|
4
|
-
"sourcesContent": ["import {delay} from '@alwatr/delay';\nimport {getGlobalThis} from '@alwatr/global-this';\nimport {createLogger} from '@alwatr/logger';\nimport {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration} from '@alwatr/parse-duration';\n\nimport type {FetchOptions} from './type.js';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport const logger_ = /* #__PURE__ */ createLogger('@alwatr/fetch');\n\nconst globalThis_ = /* #__PURE__ */ getGlobalThis();\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = /* #__PURE__ */ Object.hasOwn(globalThis_, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_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.queryParams != null) {\n const queryParams = options.queryParams;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParams)\n .map((key) => `${key}=${String(queryParams[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['Content-Type'] = 'application/json';\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.Authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.Authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async 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 (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\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 */\nexport async 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 */\nexport async 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 (globalThis_.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await delay.by(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 */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalThis_.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof 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 }, parseDuration(options.timeout!));\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalThis_\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", "import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\n\nexport {cacheSupported};\nexport type * from './type.js';\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 * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: response?.status ?? 500,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n errorMessage: (responseJson?.errorMessage as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_json_failed', {responseError, error});\n return responseError;\n }\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 * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_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"],
|
|
5
|
-
"mappings": ";;;AAAA,SAAQ,aAAY;AACpB,SAAQ,qBAAoB;AAC5B,SAAQ,oBAAmB;AAC3B,SAAQ,qBAAoB;AAC5B,SAAQ,qBAAoB;AAI5B,aAAc,eAAc,IAAI,iBAAkB,OAAmB;AAE9D,IAAM,UAA0B,6BAAa,eAAe;AAEnE,IAAM,cAA8B,8BAAc;AAElD,IAAI;AACG,IAAM,iBAAiC,uBAAO,OAAO,aAAa,QAAQ;AAEjF,IAAM,2BAA8D,CAAC;AAS9D,SAAS,gBAAgB,SAA+C;AAC7E,UAAQ,WAAR,QAAQ,SAAW;AACnB,UAAQ,WAAR,QAAQ,SAAW;AAEnB,UAAQ,YAAR,QAAQ,UAAY;AACpB,UAAQ,UAAR,QAAQ,QAAU;AAClB,UAAQ,eAAR,QAAQ,aAAe;AACvB,UAAQ,kBAAR,QAAQ,gBAAkB;AAC1B,UAAQ,oBAAR,QAAQ,kBAAoB;AAC5B,UAAQ,YAAR,QAAQ,UAAY,CAAC;AAErB,MAAI,QAAQ,kBAAkB,kBAAkB,mBAAmB,MAAM;AACvE,YAAQ,WAAW,SAAS,oCAAoC;AAAA,MAC9D;AAAA,IACF,CAAC;AACD,YAAQ,gBAAgB;AAAA,EAC1B;AAEA,MAAI,QAAQ,oBAAoB,QAAQ;AACtC,YAAQ,kBAAkB,iBAAiB,eAAe;AAAA,EAC5D;AAEA,MAAI,QAAQ,IAAI,YAAY,GAAG,MAAM,MAAM,QAAQ,eAAe,MAAM;AACtE,UAAM,cAAc,QAAQ;AAE5B,UAAM,aAAa,OAChB,KAAK,WAAW,EAChB,IAAI,CAAC,QAAQ,GAAG,GAAG,IAAI,OAAO,YAAY,GAAG,CAAC,CAAC,EAAE;AAEpD,QAAI,WAAW,SAAS,GAAG;AACzB,cAAQ,OAAO,MAAM,WAAW,KAAK,GAAG;AAAA,IAC1C;AAAA,EACF;AAEA,MAAI,QAAQ,aAAa,QAAW;AAClC,YAAQ,OAAO,KAAK,UAAU,QAAQ,QAAQ;AAC9C,YAAQ,QAAQ,cAAc,IAAI;AAAA,
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../src/main.ts", "../src/core.ts"],
|
|
4
|
+
"sourcesContent": ["import {HttpStatusCodes, type HttpStatusCode} from '@alwatr/http-primer';\n\nimport {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\n\nexport {cacheSupported};\nexport type * from './type.js';\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 * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status as HttpStatusCode;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: (response?.status as HttpStatusCode) ?? HttpStatusCodes.Error_Server_500_Internal_Server_Error,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n errorMessage: (responseJson?.errorMessage as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_json_failed', {responseError, error});\n return responseError;\n }\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 * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_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", "import {delay} from '@alwatr/delay';\nimport {getGlobalThis} from '@alwatr/global-this';\nimport {HttpStatusCodes, MimeTypes} from '@alwatr/http-primer';\nimport {createLogger} from '@alwatr/logger';\nimport {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration} from '@alwatr/parse-duration';\n\nimport type {FetchOptions} from './type.js';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport const logger_ = /* #__PURE__ */ createLogger('@alwatr/fetch');\n\nconst globalThis_ = /* #__PURE__ */ getGlobalThis();\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = /* #__PURE__ */ Object.hasOwn(globalThis_, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_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.queryParams != null) {\n const queryParams = options.queryParams;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParams)\n .map((key) => `${key}=${String(queryParams[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['content-type'] = MimeTypes.JSON;\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async 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 (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\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 */\nexport async 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 */\nexport async 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 < HttpStatusCodes.Error_Server_500_Internal_Server_Error) {\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 (globalThis_.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await delay.by(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 */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalThis_.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof 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 }, parseDuration(options.timeout!));\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalThis_\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"],
|
|
5
|
+
"mappings": ";;;AAAA,SAAQ,mBAAAA,wBAA2C;;;ACAnD,SAAQ,aAAY;AACpB,SAAQ,qBAAoB;AAC5B,SAAQ,iBAAiB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,OAAQ,CAAC;AAQlC,gBACG,MAAM,QAAQ,KAAK,OAAO,EAC1B,KAAK,CAAC,aAAa,SAAS,QAAQ,CAAC,EACrC,MAAM,CAAC,WAAW,OAAO,MAAM,CAAC,EAChC,QAAQ,MAAM;AACb,aAAO,QAAQ;AACf,mBAAa,SAAS;AAAA,IACxB,CAAC;AAAA,EACL,CAAC;AACH;;;AD9OA,eAAsB,UAAgC,SAAoE;AACxH,MAAI;AACJ,MAAI;AACJ,MAAI;AAEJ,MAAI;AACF,eAAW,MAAM,MAAM,OAAO;AAC9B,mBAAe,MAAM,SAAS,KAAK;AACnC,mBAAe,KAAK,MAAM,YAAY;AACtC,iBAAa,KAAK;AAClB,iBAAa,aAAa,SAAS;AACnC,WAAO;AAAA,EACT,SACO,OAAO;AACZ,UAAM,gBAA+B;AAAA,MACnC,IAAI;AAAA,MACJ,YAAa,UAAU,UAA6BC,iBAAgB;AAAA,MACpE,WAAY,cAAc,aAAyB,MAAgB;AAAA,MACnE,cAAe,cAAc,gBAA4B,MAAgB;AAAA,MACzE;AAAA,MACA,MAAM,cAAc;AAAA,IACtB;AAEA,YAAQ,SAAS,aAAa,qBAAqB,EAAC,eAAe,MAAK,CAAC;AACzE,WAAO;AAAA,EACT;AACF;AAwBO,SAAS,MAAM,SAA0C;AAC9D,YAAU,gBAAgB,OAAO;AACjC,UAAQ,gBAAgB,SAAS,EAAC,QAAO,CAAC;AAC1C,SAAO,qBAAqB,OAAiC;AAC/D;",
|
|
6
|
+
"names": ["HttpStatusCodes", "HttpStatusCodes"]
|
|
7
7
|
}
|
package/dist/type.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
+
import type { HttpMethod, HttpRequestHeaders, HttpStatusCode } from '@alwatr/http-primer';
|
|
1
2
|
import type { Duration } from '@alwatr/parse-duration';
|
|
2
|
-
/**
|
|
3
|
-
* Represents the available HTTP methods.
|
|
4
|
-
*/
|
|
5
|
-
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE';
|
|
6
3
|
/**
|
|
7
4
|
* Represents a dictionary of query parameters.
|
|
8
5
|
* The keys are strings and the values can be strings, numbers, or booleans.
|
|
@@ -44,7 +41,7 @@ export interface FetchOptions extends RequestInit {
|
|
|
44
41
|
/**
|
|
45
42
|
* A Headers object to set the request's headers.
|
|
46
43
|
*/
|
|
47
|
-
headers?:
|
|
44
|
+
headers?: Record<string, string> & HttpRequestHeaders;
|
|
48
45
|
/**
|
|
49
46
|
* A timeout for the fetch request.
|
|
50
47
|
* Set `0` to disable it.
|
|
@@ -120,11 +117,11 @@ export interface FetchOptions extends RequestInit {
|
|
|
120
117
|
}
|
|
121
118
|
export type ResponseSuccess<T extends JsonObject> = T & {
|
|
122
119
|
ok: true;
|
|
123
|
-
statusCode:
|
|
120
|
+
statusCode: HttpStatusCode;
|
|
124
121
|
};
|
|
125
122
|
export type ResponseError = {
|
|
126
123
|
ok: false;
|
|
127
|
-
statusCode:
|
|
124
|
+
statusCode: HttpStatusCode;
|
|
128
125
|
errorCode: string;
|
|
129
126
|
errorMessage: string;
|
|
130
127
|
responseText?: string;
|
package/dist/type.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAE,kBAAkB,EAAE,cAAc,EAAC,MAAM,qBAAqB,CAAC;AACxF,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,wBAAwB,CAAC;AAErD;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,aAAa,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;AAEnE;;;;;;;;;GASG;AACH,MAAM,MAAM,aAAa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Ac,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,EAAE,EAAE,KAAK,CAAC;IACV,UAAU,EAAE,cAAc,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,IAAI,CAAC;CACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/fetch",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Enhanced fetch API with cache strategy, retry pattern, timeout, helper methods and enhanced types.",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"keywords": [
|
|
@@ -72,19 +72,20 @@
|
|
|
72
72
|
"clean": "rm -rfv dist *.tsbuildinfo"
|
|
73
73
|
},
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@alwatr/delay": "^1.0.
|
|
76
|
-
"@alwatr/global-this": "^1.0.
|
|
77
|
-
"@alwatr/
|
|
78
|
-
"@alwatr/
|
|
79
|
-
"@alwatr/
|
|
75
|
+
"@alwatr/delay": "^1.0.7",
|
|
76
|
+
"@alwatr/global-this": "^1.0.2",
|
|
77
|
+
"@alwatr/http-primer": "^1.0.0",
|
|
78
|
+
"@alwatr/logger": "^4.0.7",
|
|
79
|
+
"@alwatr/package-tracer": "^1.0.7",
|
|
80
|
+
"@alwatr/parse-duration": "^1.1.7"
|
|
80
81
|
},
|
|
81
82
|
"devDependencies": {
|
|
82
|
-
"@alwatr/nano-build": "^2.0.
|
|
83
|
+
"@alwatr/nano-build": "^2.0.3",
|
|
83
84
|
"@alwatr/prettier-config": "^1.0.6",
|
|
84
|
-
"@alwatr/tsconfig-base": "^1.3.
|
|
85
|
-
"@alwatr/type-helper": "^2.0.
|
|
85
|
+
"@alwatr/tsconfig-base": "^1.3.3",
|
|
86
|
+
"@alwatr/type-helper": "^2.0.3",
|
|
86
87
|
"jest": "^29.7.0",
|
|
87
88
|
"typescript": "^5.6.3"
|
|
88
89
|
},
|
|
89
|
-
"gitHead": "
|
|
90
|
+
"gitHead": "9b57df2f58636ddb4c064959950e707cf2422ecf"
|
|
90
91
|
}
|
package/src/main.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {fetch, fetchJson} from '@alwatr/fetch'
|
|
1
|
+
import {fetch, fetchJson} from '@alwatr/fetch';
|
|
2
2
|
|
|
3
3
|
describe('fetch with search params', () => {
|
|
4
4
|
it('should make a GET request to the specified URL', async () => {
|
|
@@ -29,7 +29,7 @@ describe('fetch json', () => {
|
|
|
29
29
|
url: 'http://httpbin.org/post',
|
|
30
30
|
method: 'POST',
|
|
31
31
|
bodyJson: {
|
|
32
|
-
a: 2
|
|
32
|
+
a: 2,
|
|
33
33
|
},
|
|
34
34
|
cacheStrategy: 'network_only',
|
|
35
35
|
};
|
|
@@ -38,7 +38,7 @@ describe('fetch json', () => {
|
|
|
38
38
|
|
|
39
39
|
expect(responseJson.ok).toBe(true);
|
|
40
40
|
expect(responseJson.json.a).toBe(2);
|
|
41
|
-
})
|
|
41
|
+
});
|
|
42
42
|
|
|
43
43
|
it('should make a GET request to the specified URL and parse json and handle 404 status code', async () => {
|
|
44
44
|
const options = {
|