@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/CHANGELOG.md CHANGED
@@ -3,472 +3,34 @@
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
- ## [2.2.1](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.2.0...@alwatr/fetch@2.2.1) (2023-12-19)
7
-
8
- **Note:** Version bump only for package @alwatr/fetch
9
-
10
- # [2.2.0](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.5...@alwatr/fetch@2.2.0) (2023-12-19)
11
-
12
- ### Bug Fixes
13
-
14
- * **fetch:** AbortController not support issue ([10f6dbc](https://github.com/Alwatr/alwatr-es-sdk/commit/10f6dbc816d6f07a81d49945ec0291b132a75fb7)) by @AliMD
6
+ ## [3.1.0](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.0.0...@alwatr/fetch@3.1.0) (2024-01-24)
15
7
 
16
8
  ### Features
17
9
 
18
- * use globalScope instead globalThis ([94342bb](https://github.com/Alwatr/alwatr-es-sdk/commit/94342bb7b467376173a65d8cdf6bb106338f425b)) by @njfamirm
19
-
20
- ## [2.1.5](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.4...@alwatr/fetch@2.1.5) (2023-12-11)
21
-
22
- **Note:** Version bump only for package @alwatr/fetch
23
-
24
- ## [2.1.4](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.3...@alwatr/fetch@2.1.4) (2023-12-11)
25
-
26
- **Note:** Version bump only for package @alwatr/fetch
27
-
28
- ## [2.1.3](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.2...@alwatr/fetch@2.1.3) (2023-12-09)
29
-
30
- **Note:** Version bump only for package @alwatr/fetch
31
-
32
- ## [2.1.2](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.1.1...@alwatr/fetch@2.1.2) (2023-11-29)
33
-
34
- **Note:** Version bump only for package @alwatr/fetch
35
-
36
- ## [2.1.1](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.1.0...@alwatr/fetch@2.1.1) (2023-11-23)
37
-
38
- **Note:** Version bump only for package @alwatr/fetch
39
-
40
- # [2.1.0](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.8...@alwatr/fetch@2.1.0) (2023-11-14)
41
-
42
- ### Features
43
-
44
- * **yarn:** pnp ([b6a562c](https://github.com/Alwatr/eslib/commit/b6a562c909a35b3e626442e5c325da3fce448e1b)) by @AliMD
45
-
46
- ## [2.0.8](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.7...@alwatr/fetch@2.0.8) (2023-11-08)
10
+ * **fetch:** fetch json ([b089f12](https://github.com/Alwatr/nanolib/commit/b089f12cef6f1f3b60bc7559dc5e9b8b63c57273)) by @njfamirm
47
11
 
48
12
  ### Bug Fixes
49
13
 
50
- * move repo urls ([719aa3e](https://github.com/Alwatr/eslib/commit/719aa3e7462d3c9c6272958fc93f89fda6793fb1)) by @AliMD
51
-
52
- ## [2.0.7](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.6...@alwatr/fetch@2.0.7) (2023-11-01)
53
-
54
- **Note:** Version bump only for package @alwatr/fetch
55
-
56
- ## [2.0.6](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.5...@alwatr/fetch@2.0.6) (2023-10-23)
57
-
58
- **Note:** Version bump only for package @alwatr/fetch
59
-
60
- ## [2.0.5](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.4...@alwatr/fetch@2.0.5) (2023-10-23)
61
-
62
- **Note:** Version bump only for package @alwatr/fetch
63
-
64
- ## [2.0.4](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.3...@alwatr/fetch@2.0.4) (2023-10-23)
65
-
66
- ### Bug Fixes
14
+ * **fetch:** better error handling on handleRetryPattern_ when user is offline ([b867f30](https://github.com/Alwatr/nanolib/commit/b867f30b3eba529ec1aae0026f0ded252ce54332)) by @AliMD
67
15
 
68
- * **fetch:** compatible with new logger api ([a51d7d2](https://github.com/Alwatr/eslib/commit/a51d7d23e9f7760c4d68685375cc8010456128fc)) by @AliMD
16
+ ### Code Refactoring
69
17
 
70
- ## [2.0.3](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.2...@alwatr/fetch@2.0.3) (2023-10-23)
18
+ * **fetch:** separate core files ([c7e6b09](https://github.com/Alwatr/nanolib/commit/c7e6b096d747f868a2a1bfde1ffd3fd2a64dc7f3)) by @njfamirm
71
19
 
72
- **Note:** Version bump only for package @alwatr/fetch
20
+ ## 3.0.0 (2024-01-20)
73
21
 
74
- ## [2.0.2](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.1...@alwatr/fetch@2.0.2) (2023-10-23)
22
+ ### BREAKING CHANGES
75
23
 
76
- **Note:** Version bump only for package @alwatr/fetch
77
-
78
- ## [2.0.1](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.0...@alwatr/fetch@2.0.1) (2023-09-19)
79
-
80
- ### Bug Fixes
81
-
82
- - package repo urls ([466cbe9](https://github.com/Alwatr/eslib/commit/466cbe9188f24e1a1bc36d879a95b52538a58f16)) by @AliMD
83
-
84
- # [2.0.0](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@1.1.3...@alwatr/fetch@2.0.0) (2023-09-19)
85
-
86
- ### Bug Fixes
87
-
88
- - **fetch:** remove @alwatr/signal ([03d947b](https://github.com/Alwatr/eslib/commit/03d947be3889d63805eabb7f4be618fed6da0d55)) by @AliMD
89
-
90
- - fix(fetch)!: remove fetchContext ([eba9cf0](https://github.com/Alwatr/eslib/commit/eba9cf0a443564478b5b985a69d0f54fbd514440)) by @AliMD
91
-
92
- ### BREAKING CHANGES
93
-
94
- - fetchContext method removed
95
-
96
- ## 1.1.3 (2023-09-19)
97
-
98
- ### Bug Fixes
99
-
100
- - cleanup extra packages ([dfc0bd2](https://github.com/Alwatr/eslib/commit/dfc0bd2b42d49e1094ae0303d34e653aa289d358)) by @AliMD
101
-
102
- ## [1.1.2](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@1.1.1...@alwatr/fetch@1.1.2) (2023-09-12)
103
-
104
- **Note:** Version bump only for package @alwatr/fetch
105
-
106
- ## [1.1.1](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@1.1.0...@alwatr/fetch@1.1.1) (2023-09-12)
107
-
108
- **Note:** Version bump only for package @alwatr/fetch
109
-
110
- # 1.1.0 (2023-09-12)
111
-
112
- # 1.0.0 (2023-06-14)
113
-
114
- # 0.32.0 (2023-05-27)
115
-
116
- ### Bug Fixes
117
-
118
- - **fetch:** serviceRequest type ([44c1086](https://github.com/Alwatr/eslib/commit/44c10866e1c630bc3cf85b3e9be38b268b5b0c31))
119
- - **fetch:** userAuth in StringifyableFetchOptions ([f554b57](https://github.com/Alwatr/eslib/commit/f554b575584cb1464d48fbe8a8c4006125365853))
120
-
121
- # 0.31.0 (2023-05-08)
122
-
123
- ### Bug Fixes
124
-
125
- - **fetch.ts:** compatible with node ([c3acb53](https://github.com/Alwatr/eslib/commit/c3acb53effd4a79e389bc4de1567b0285c03d71d))
126
- - **fetch:** bodyJson type ([5740467](https://github.com/Alwatr/eslib/commit/57404673e7f031a73d1cf23501e6fa8d89759769))
127
- - **fetch:** check node_mode before use navigator.online ([a688f48](https://github.com/Alwatr/eslib/commit/a688f48e9ce4b103c25ffd50f1b8c96e89c205d1))
128
- - **fetch:** serviceRequest generic type ([074ef38](https://github.com/Alwatr/eslib/commit/074ef3876086f93486753559d8e3db0b60c52a0e))
129
- - **fetch:** suppress fetch_cache_not_found extra error ([c04f609](https://github.com/Alwatr/eslib/commit/c04f60944106dbd8d1bf2fb5ce0e7be644c3abc6))
130
- - new logger api ([9d83a7d](https://github.com/Alwatr/eslib/commit/9d83a7dc5c103bc3bb4282dacfd85fa998915300))
131
-
132
- ### Features
133
-
134
- - **fetch:** add user auth ([811fbc0](https://github.com/Alwatr/eslib/commit/811fbc018c2b4e3b808704e8623bd5dff2b2f3f7))
135
- - **fetch:** StringifyableFetchOptions type ([e13fdb2](https://github.com/Alwatr/eslib/commit/e13fdb2550cf3a2102c331aa7980234ca6f86e08))
136
-
137
- # 0.30.0 (2023-03-06)
138
-
139
- ### Bug Fixes
140
-
141
- - **fetch:** fetchContext duplicate setSignalValue ([8aa9b00](https://github.com/Alwatr/eslib/commit/8aa9b003a2d7b7ffc8243ff7092d9560763d7273))
142
- - **fetch:** getClientId in nodejs ([c99e41c](https://github.com/Alwatr/eslib/commit/c99e41c9e9a979ca50cc01aeb5371a3487d9da2e))
143
-
144
- ### Features
145
-
146
- - **fetch:** enhance fetchContext with dispatchOptions and only cache_only request in first time ([1f6b575](https://github.com/Alwatr/eslib/commit/1f6b575437104d64ede8a2c23d141160360d2cd5))
147
- - **fetch:** fetchContext ([d63d17e](https://github.com/Alwatr/eslib/commit/d63d17ecb58221159dc45a473fdd1c992d306d2b))
148
- - **fetch:** fetchContext skip online request when offline ([7c2b649](https://github.com/Alwatr/eslib/commit/7c2b6491f78acb961cdc0583112e3c709a177b79))
149
- - **fetch:** fetchContext work on background ([2e12bfd](https://github.com/Alwatr/eslib/commit/2e12bfde0a0636c30e60906c4812ec66b2d36742))
150
- - **fetch:** handle offline for all requests ([6a8d7b7](https://github.com/Alwatr/eslib/commit/6a8d7b7159732d834be052191935580331e36962))
151
-
152
- # 0.29.0 (2023-02-10)
153
-
154
- ### Bug Fixes
155
-
156
- - **fetch:** device id to client id ([077e467](https://github.com/Alwatr/eslib/commit/077e4671d804d43c8f61a9d163d8d51d7a69134e))
157
- - **fetch:** headers type ([cd4b14e](https://github.com/Alwatr/eslib/commit/cd4b14e46b5090b6df8cb4f89d22b53966b9151e))
158
-
159
- ### Features
160
-
161
- - **fetch:** add update_cache to CacheStrategy ([e1406b1](https://github.com/Alwatr/eslib/commit/e1406b12e1276de3295c47506c8102ab3f8bfa79))
162
- - **fetch:** clientId on service request ([9575a1d](https://github.com/Alwatr/eslib/commit/9575a1d595a0a4757581a29a66837cbe4be46ed3))
163
-
164
- # 0.28.0 (2023-01-20)
165
-
166
- # 0.27.0 (2022-12-29)
167
-
168
- ### Bug Fixes
169
-
170
- - review issue ([e5d192c](https://github.com/Alwatr/eslib/commit/e5d192cbee6917c2de01146cf8bd026895724ab8))
171
-
172
- ### Features
173
-
174
- - **type:** add types ([2de3842](https://github.com/Alwatr/eslib/commit/2de384297b70b9818ec8d62ce8d31be4822d95a4))
175
-
176
- # 0.26.0 (2022-12-22)
177
-
178
- ### Bug Fixes
179
-
180
- - all import without .js ([fa739ab](https://github.com/Alwatr/eslib/commit/fa739ab23f67bae1d10bfcc146920b71377a26fc))
181
- - **packages/fetch:** tsconfig refrences ([e6b9542](https://github.com/Alwatr/eslib/commit/e6b9542284e67b67600da4dcebd19daedb031c9c))
182
- - **fetch:** always call revalidateCallback ([444edc5](https://github.com/Alwatr/eslib/commit/444edc52b6dd5de5418ae8630e1f2270f0114372))
183
- - **fetch:** enhance handle and report errors in serviceRequest ([dbfc7ff](https://github.com/Alwatr/eslib/commit/dbfc7ff2a036216498dc8e2ff58b74b7eb8d9bc0))
184
- - **fetch:** fetch_cache_not_found is not error ([3d1aa4e](https://github.com/Alwatr/eslib/commit/3d1aa4e572fbf4bc7b0a527a3fdcf4c521af18dd))
185
- - **fetch:** fetch_failed error message ([3e98bd9](https://github.com/Alwatr/eslib/commit/3e98bd926a495c13d476f2f1cfb9e546a059d4d8))
186
- - **fetch:** remove extra \_options ([ad24c81](https://github.com/Alwatr/eslib/commit/ad24c8104b944ebda52308e60d41976ac1eeee81))
187
- - **fetch:** revalidateCallback issue ([7207909](https://github.com/Alwatr/eslib/commit/72079098edb438c28256dad03d7802bc2fab0858))
188
- - set correct path ([d01ce6f](https://github.com/Alwatr/eslib/commit/d01ce6ffa749a5e3e0e11e35b4ed61d75d61fec9))
189
- - **storage-engine:** AlwatrDocumentStorage ([ca9a7bf](https://github.com/Alwatr/eslib/commit/ca9a7bf5e17b333538e749ada5d481a06c847101))
190
- - tsconfig ([e96dcd3](https://github.com/Alwatr/eslib/commit/e96dcd30774a9f06f7d051e0504192cbbe019e35))
191
- - tsconfig refrences ([cea0562](https://github.com/Alwatr/eslib/commit/cea05621ecfa499476c5b10a412e60f27ba6a06d))
192
-
193
- ### Features
194
-
195
- - **fetch:** add AlwatrDocumentStorage type ([44b84f0](https://github.com/Alwatr/eslib/commit/44b84f0754169a707472655811020f6e542a6e5d))
196
- - **fetch:** serviceRequest ([b0b5478](https://github.com/Alwatr/eslib/commit/b0b5478d16b2cb590cfc29d644006e4a459dd6d6))
197
- - **fetch:** serviceRequest ([#513](https://github.com/Alwatr/eslib/issues/513)) ([113ee49](https://github.com/Alwatr/eslib/commit/113ee498c778d9efc00cc4807bfef09e52336a31))
198
- - improve accident debugging ([d697e6c](https://github.com/Alwatr/eslib/commit/d697e6c4c7d0a4172f4dba9caf6fbd478f79de9c))
199
- - improve error debugging ([1fba504](https://github.com/Alwatr/eslib/commit/1fba50400a1e8ececc10bbe8ea11cc8dcea2289c))
200
-
201
- # [1.0.0](https://github.com/Alwatr/eslib/compare/v0.32.0...v1.0.0) (2023-06-14)
202
-
203
- **Note:** Version bump only for package @alwatr/fetch
204
-
205
- # [0.32.0](https://github.com/Alwatr/eslib/compare/v0.31.0...v0.32.0) (2023-05-27)
206
-
207
- ### Bug Fixes
208
-
209
- - **fetch:** serviceRequest type ([44c1086](https://github.com/Alwatr/eslib/commit/44c10866e1c630bc3cf85b3e9be38b268b5b0c31))
210
- - **fetch:** userAuth in StringifyableFetchOptions ([f554b57](https://github.com/Alwatr/eslib/commit/f554b575584cb1464d48fbe8a8c4006125365853))
211
-
212
- # [0.31.0](https://github.com/Alwatr/eslib/compare/v0.30.0...v0.31.0) (2023-05-08)
213
-
214
- ### Bug Fixes
215
-
216
- - **fetch.ts:** compatible with node ([c3acb53](https://github.com/Alwatr/eslib/commit/c3acb53effd4a79e389bc4de1567b0285c03d71d))
217
- - **fetch:** bodyJson type ([5740467](https://github.com/Alwatr/eslib/commit/57404673e7f031a73d1cf23501e6fa8d89759769))
218
- - **fetch:** check node_mode before use navigator.online ([a688f48](https://github.com/Alwatr/eslib/commit/a688f48e9ce4b103c25ffd50f1b8c96e89c205d1))
219
- - **fetch:** serviceRequest generic type ([074ef38](https://github.com/Alwatr/eslib/commit/074ef3876086f93486753559d8e3db0b60c52a0e))
220
- - **fetch:** suppress fetch_cache_not_found extra error ([c04f609](https://github.com/Alwatr/eslib/commit/c04f60944106dbd8d1bf2fb5ce0e7be644c3abc6))
221
- - new logger api ([9d83a7d](https://github.com/Alwatr/eslib/commit/9d83a7dc5c103bc3bb4282dacfd85fa998915300))
222
-
223
- ### Features
224
-
225
- - **fetch:** add user auth ([811fbc0](https://github.com/Alwatr/eslib/commit/811fbc018c2b4e3b808704e8623bd5dff2b2f3f7))
226
- - **fetch:** StringifyableFetchOptions type ([e13fdb2](https://github.com/Alwatr/eslib/commit/e13fdb2550cf3a2102c331aa7980234ca6f86e08))
227
-
228
- # [0.30.0](https://github.com/Alwatr/eslib/compare/v0.29.0...v0.30.0) (2023-03-06)
229
-
230
- ### Bug Fixes
231
-
232
- - **fetch:** fetchContext duplicate setSignalValue ([8aa9b00](https://github.com/Alwatr/eslib/commit/8aa9b003a2d7b7ffc8243ff7092d9560763d7273))
233
- - **fetch:** getClientId in nodejs ([c99e41c](https://github.com/Alwatr/eslib/commit/c99e41c9e9a979ca50cc01aeb5371a3487d9da2e))
234
-
235
- ### Features
236
-
237
- - **fetch:** enhance fetchContext with dispatchOptions and only cache_only request in first time ([1f6b575](https://github.com/Alwatr/eslib/commit/1f6b575437104d64ede8a2c23d141160360d2cd5))
238
- - **fetch:** fetchContext ([d63d17e](https://github.com/Alwatr/eslib/commit/d63d17ecb58221159dc45a473fdd1c992d306d2b))
239
- - **fetch:** fetchContext skip online request when offline ([7c2b649](https://github.com/Alwatr/eslib/commit/7c2b6491f78acb961cdc0583112e3c709a177b79))
240
- - **fetch:** fetchContext work on background ([2e12bfd](https://github.com/Alwatr/eslib/commit/2e12bfde0a0636c30e60906c4812ec66b2d36742))
241
- - **fetch:** handle offline for all requests ([6a8d7b7](https://github.com/Alwatr/eslib/commit/6a8d7b7159732d834be052191935580331e36962))
242
-
243
- # [0.29.0](https://github.com/Alwatr/eslib/compare/v0.28.0...v0.29.0) (2023-02-10)
244
-
245
- ### Bug Fixes
24
+ * **fetch:** remove serviceRequest
246
25
 
247
- - **fetch:** device id to client id ([077e467](https://github.com/Alwatr/eslib/commit/077e4671d804d43c8f61a9d163d8d51d7a69134e))
248
- - **fetch:** headers type ([cd4b14e](https://github.com/Alwatr/eslib/commit/cd4b14e46b5090b6df8cb4f89d22b53966b9151e))
26
+ Co-authored-by: Ali Mihandoost <ali@mihandoost.com>
249
27
 
250
28
  ### Features
251
29
 
252
- - **fetch:** add update_cache to CacheStrategy ([e1406b1](https://github.com/Alwatr/eslib/commit/e1406b12e1276de3295c47506c8102ab3f8bfa79))
253
- - **fetch:** clientId on service request ([9575a1d](https://github.com/Alwatr/eslib/commit/9575a1d595a0a4757581a29a66837cbe4be46ed3))
30
+ * **fetch:** alwatrAuth ([28e365c](https://github.com/Alwatr/nanolib/commit/28e365c839b0ea80060c0f44ed4dc4473468d5c4)) by @AliMD
31
+ * **fetch:** move from last repo ([4b86bb5](https://github.com/Alwatr/nanolib/commit/4b86bb542af296c91bc1db36b4e08fdbad501db2)) by @njfamirm
32
+ * **fetch:** Update fetch type definitions with document ([38398cc](https://github.com/Alwatr/nanolib/commit/38398cc33f311a569a53cc3e06c3191e17dbd45b)) by @AliMD
254
33
 
255
- # [0.28.0](https://github.com/Alwatr/eslib/compare/v0.27.0...v0.28.0) (2023-01-20)
256
-
257
- ### Bug Fixes
258
-
259
- - review issue ([e5d192c](https://github.com/Alwatr/eslib/commit/e5d192cbee6917c2de01146cf8bd026895724ab8))
260
-
261
- ### Features
262
-
263
- - **type:** add types ([2de3842](https://github.com/Alwatr/eslib/commit/2de384297b70b9818ec8d62ce8d31be4822d95a4))
264
-
265
- # [0.27.0](https://github.com/Alwatr/eslib/compare/v0.26.0...v0.27.0) (2022-12-29)
266
-
267
- **Note:** Version bump only for package @alwatr/fetch
268
-
269
- # [0.26.0](https://github.com/Alwatr/eslib/compare/v0.25.0...v0.26.0) (2022-12-22)
270
-
271
- ### Bug Fixes
272
-
273
- - all import without .js ([fa739ab](https://github.com/Alwatr/eslib/commit/fa739ab23f67bae1d10bfcc146920b71377a26fc))
274
- - **packages/fetch:** tsconfig refrences ([e6b9542](https://github.com/Alwatr/eslib/commit/e6b9542284e67b67600da4dcebd19daedb031c9c))
275
- - **fetch:** always call revalidateCallback ([444edc5](https://github.com/Alwatr/eslib/commit/444edc52b6dd5de5418ae8630e1f2270f0114372))
276
- - **fetch:** enhance handle and report errors in serviceRequest ([dbfc7ff](https://github.com/Alwatr/eslib/commit/dbfc7ff2a036216498dc8e2ff58b74b7eb8d9bc0))
277
- - **fetch:** fetch_cache_not_found is not error ([3d1aa4e](https://github.com/Alwatr/eslib/commit/3d1aa4e572fbf4bc7b0a527a3fdcf4c521af18dd))
278
- - **fetch:** fetch_failed error message ([3e98bd9](https://github.com/Alwatr/eslib/commit/3e98bd926a495c13d476f2f1cfb9e546a059d4d8))
279
- - **fetch:** remove extra \_options ([ad24c81](https://github.com/Alwatr/eslib/commit/ad24c8104b944ebda52308e60d41976ac1eeee81))
280
- - **fetch:** revalidateCallback issue ([7207909](https://github.com/Alwatr/eslib/commit/72079098edb438c28256dad03d7802bc2fab0858))
281
- - set correct path ([d01ce6f](https://github.com/Alwatr/eslib/commit/d01ce6ffa749a5e3e0e11e35b4ed61d75d61fec9))
282
- - **storage-engine:** AlwatrDocumentStorage ([ca9a7bf](https://github.com/Alwatr/eslib/commit/ca9a7bf5e17b333538e749ada5d481a06c847101))
283
- - tsconfig ([e96dcd3](https://github.com/Alwatr/eslib/commit/e96dcd30774a9f06f7d051e0504192cbbe019e35))
284
- - tsconfig refrences ([cea0562](https://github.com/Alwatr/eslib/commit/cea05621ecfa499476c5b10a412e60f27ba6a06d))
285
-
286
- ### Features
287
-
288
- - **fetch:** add AlwatrDocumentStorage type ([44b84f0](https://github.com/Alwatr/eslib/commit/44b84f0754169a707472655811020f6e542a6e5d))
289
- - **fetch:** serviceRequest ([b0b5478](https://github.com/Alwatr/eslib/commit/b0b5478d16b2cb590cfc29d644006e4a459dd6d6))
290
- - **fetch:** serviceRequest ([#513](https://github.com/Alwatr/eslib/issues/513)) ([113ee49](https://github.com/Alwatr/eslib/commit/113ee498c778d9efc00cc4807bfef09e52336a31))
291
- - improve accident debugging ([d697e6c](https://github.com/Alwatr/eslib/commit/d697e6c4c7d0a4172f4dba9caf6fbd478f79de9c))
292
- - improve error debugging ([1fba504](https://github.com/Alwatr/eslib/commit/1fba50400a1e8ececc10bbe8ea11cc8dcea2289c))
293
-
294
- # [0.25.0](https://github.com/Alwatr/eslib/compare/v0.24.1...v0.25.0) (2022-12-07)
295
-
296
- **Note:** Version bump only for package @alwatr/fetch
297
-
298
- ## [0.24.1](https://github.com/Alwatr/eslib/compare/v0.24.0...v0.24.1) (2022-12-01)
299
-
300
- **Note:** Version bump only for package @alwatr/fetch
301
-
302
- # [0.24.0](https://github.com/Alwatr/eslib/compare/v0.23.0...v0.24.0) (2022-11-28)
303
-
304
- ### Bug Fixes
305
-
306
- - **fetch:** AlwatrServiceResponseSuccessWithMeta types ([b46f3f2](https://github.com/Alwatr/eslib/commit/b46f3f22a7c5a671385b0a8f58d0881501ce1b94))
307
- - **fetch:** use nre method type ([1c63eca](https://github.com/Alwatr/eslib/commit/1c63eca994582c241112c3e5ae79bac49831532f))
308
- - update types ([44cc57f](https://github.com/Alwatr/eslib/commit/44cc57f97a99ed810dae3fde39c9525bd0e8ff3c))
309
- - use ~ for package version ([4e027ff](https://github.com/Alwatr/eslib/commit/4e027ff63875e03b088ebcdc1bdf2495f4494eec))
310
-
311
- # [0.23.0](https://github.com/Alwatr/eslib/compare/v0.22.1...v0.23.0) (2022-11-23)
312
-
313
- ### Bug Fixes
314
-
315
- - **fetch:** types ([4ce81a7](https://github.com/Alwatr/eslib/commit/4ce81a7b82d01ab938754bc5e483ccb074671e33))
316
-
317
- ### Features
318
-
319
- - **fetch:** set token in options ([033a638](https://github.com/Alwatr/eslib/commit/033a63846f8c43b86c7d0a662e4bb6aa7cae3af5))
320
-
321
- ## [0.22.1](https://github.com/Alwatr/eslib/compare/v0.22.0...v0.22.1) (2022-11-21)
322
-
323
- **Note:** Version bump only for package @alwatr/fetch
324
-
325
- # [0.22.0](https://github.com/Alwatr/eslib/compare/v0.21.0...v0.22.0) (2022-11-20)
326
-
327
- ### Bug Fixes
328
-
329
- - **fetch:** \_handleRetryPattern ([fe4ac12](https://github.com/Alwatr/eslib/commit/fe4ac12b4ad3086d44e545591c767cb322e32b8d))
330
- - **fetch:** bodyJson issue ([5d8d6fa](https://github.com/Alwatr/eslib/commit/5d8d6fa4a0f608c2c5ed583aa54de9b836daaa5f))
331
-
332
- ### Features
333
-
334
- - **fetch:** support nodejs ([2ed2ef4](https://github.com/Alwatr/eslib/commit/2ed2ef42e9f204d4896ada4e20b839cfabdc7284))
335
-
336
- ### Performance Improvements
337
-
338
- - **fetch:** Improve performance and decrease memory usage ([7c29333](https://github.com/Alwatr/eslib/commit/7c293339215bea3a5013effe4aea5b6267a7a75e))
339
-
340
- # [0.21.0](https://github.com/Alwatr/eslib/compare/v0.20.0...v0.21.0) (2022-11-13)
341
-
342
- **Note:** Version bump only for package @alwatr/fetch
343
-
344
- # [0.20.0](https://github.com/Alwatr/eslib/compare/v0.19.0...v0.20.0) (2022-11-05)
345
-
346
- ### Bug Fixes
347
-
348
- - **fetch:** \_handleRemoveDuplicate ([6207b48](https://github.com/Alwatr/eslib/commit/6207b4867935d05a0e5a1c795de9efab4ff77c68))
349
- - **fetch:** cacheStorageName issue ([e641ff7](https://github.com/Alwatr/eslib/commit/e641ff740055a68b6397f00bf46f1d05943c348a))
350
- - **fetch:** method type and case ([8d4ba77](https://github.com/Alwatr/eslib/commit/8d4ba77e56ecfe2aeaa459dcd26039c38c4b2199))
351
- - **fetch:** prevent duplicate revalidateCallback on first time ([2da7dc5](https://github.com/Alwatr/eslib/commit/2da7dc579e4850af06914590a231548cd469cd75))
352
-
353
- ### Features
354
-
355
- - **fetch:** add retryDelay ([28e3244](https://github.com/Alwatr/eslib/commit/28e3244a2d35e7a1a91f9a5a4942f1bb4a56d981))
356
- - **fetch:** revalidate callback for `stale_while_revalidate` cache strategy. ([8976cc4](https://github.com/Alwatr/eslib/commit/8976cc424b2a9e96e4cfa69941d5378a6640af6f))
357
- - **fetch:** simple memory caching for remove duplicate/parallel requests ([74cd7f1](https://github.com/Alwatr/eslib/commit/74cd7f1500d730021a0f577c0ecfd7a2460fdb15))
358
-
359
- # [0.19.0](https://github.com/Alwatr/eslib/compare/v0.18.0...v0.19.0) (2022-11-01)
360
-
361
- **Note:** Version bump only for package @alwatr/fetch
362
-
363
- # [0.18.0](https://github.com/Alwatr/eslib/compare/v0.17.0...v0.18.0) (2022-10-22)
364
-
365
- ### Bug Fixes
366
-
367
- - **fetch:** update cache on network_first ([54b59cd](https://github.com/Alwatr/eslib/commit/54b59cdc64a543a0afcd09d2ec78d20661f3a06d))
368
-
369
- ### Features
370
-
371
- - **fetch:** cache strategy ([106eabd](https://github.com/Alwatr/eslib/commit/106eabdd10574b24f9919049d0b509ae40168a9a))
372
- - **fetch:** optional cacheStorageName ([f2bd8f2](https://github.com/Alwatr/eslib/commit/f2bd8f2a899466628417294cb269fd569b87e17a))
373
- - **fetch:** stale_while_revalidate cache strategy ([f758f49](https://github.com/Alwatr/eslib/commit/f758f495efb42ce7441e8f2058c1b67de0615a8c))
374
-
375
- # [0.17.0](https://github.com/Alwatr/eslib/compare/v0.16.1...v0.17.0) (2022-10-21)
376
-
377
- ### Bug Fixes
378
-
379
- - **fetch:** not ok retry condition ([0da1edd](https://github.com/Alwatr/eslib/commit/0da1edda64ec5abf70a83361d159e207691e368e))
380
-
381
- ### Features
382
-
383
- - **fetch:** docs & pattern ([459ad1c](https://github.com/Alwatr/eslib/commit/459ad1c5996f851769306639136d79c0f7270770))
384
- - **fetch:** logger & jsdocs ([36f652d](https://github.com/Alwatr/eslib/commit/36f652d5dad23b2aeb824af77d1b0e442119c6e8))
385
- - **fetch:** retry pattern ([5350d1a](https://github.com/Alwatr/eslib/commit/5350d1a81b9134d598f46006a1baa880b280ea98))
386
-
387
- ### Performance Improvements
388
-
389
- - **fetch:** syntax ([1fdd02e](https://github.com/Alwatr/eslib/commit/1fdd02ec8b52e32a124b8d7c1d1c7fd7c993e3af))
390
-
391
- # [0.16.0](https://github.com/Alwatr/eslib/compare/v0.15.0...v0.16.0) (2022-09-08)
392
-
393
- **Note:** Version bump only for package @alwatr/fetch
394
-
395
- # [0.15.0](https://github.com/Alwatr/eslib/compare/v0.14.0...v0.15.0) (2022-09-01)
396
-
397
- **Note:** Version bump only for package @alwatr/fetch
398
-
399
- # [0.14.0](https://github.com/Alwatr/eslib/compare/v0.13.0...v0.14.0) (2022-08-19)
400
-
401
- **Note:** Version bump only for package @alwatr/fetch
402
-
403
- # [0.13.0](https://github.com/Alwatr/eslib/compare/v0.12.0...v0.13.0) (2022-08-06)
404
-
405
- **Note:** Version bump only for package @alwatr/fetch
406
-
407
- # [0.12.0](https://github.com/Alwatr/eslib/compare/v0.11.0...v0.12.0) (2022-07-22)
408
-
409
- ### Bug Fixes
410
-
411
- - **fetch:** compatible with new ts types AbortSignal ([949655a](https://github.com/Alwatr/eslib/commit/949655a257852aaaaa311e32603ecc0819eedf51))
412
-
413
- # [0.11.0](https://github.com/Alwatr/eslib/compare/v0.10.1...v0.11.0) (2022-04-16)
414
-
415
- **Note:** Version bump only for package @alwatr/fetch
416
-
417
- # [0.10.0](https://github.com/Alwatr/eslib/compare/v0.9.0...v0.10.0) (2022-04-02)
418
-
419
- **Note:** Version bump only for package @alwatr/fetch
420
-
421
- # [0.9.0](https://github.com/Alwatr/eslib/compare/v0.8.0...v0.9.0) (2022-03-22)
422
-
423
- **Note:** Version bump only for package @alwatr/fetch
424
-
425
- # [0.8.0](https://github.com/Alwatr/eslib/compare/v0.7.2...v0.8.0) (2022-03-14)
426
-
427
- **Note:** Version bump only for package @alwatr/fetch
428
-
429
- ## [0.7.2](https://github.com/Alwatr/eslib/compare/v0.7.1...v0.7.2) (2022-03-12)
430
-
431
- **Note:** Version bump only for package @alwatr/fetch
432
-
433
- # [0.7.0](https://github.com/Alwatr/eslib/compare/v0.6.1...v0.7.0) (2022-03-12)
434
-
435
- **Note:** Version bump only for package @alwatr/fetch
436
-
437
- # [0.6.0](https://github.com/Alwatr/eslib/compare/v0.5.0...v0.6.0) (2022-03-11)
438
-
439
- ### Bug Fixes
440
-
441
- - alalwatr ([898aa6e](https://github.com/Alwatr/eslib/commit/898aa6ed0888eab9265c83b96a50f1b8c216d143))
442
- - **packages:** duplicate alwatr keyword ([77d4aa2](https://github.com/Alwatr/eslib/commit/77d4aa2105ad47515c3eee251fd6b8c281d0d1fc))
443
-
444
- # [0.5.0](https://github.com/Alwatr/eslib/compare/v0.4.0...v0.5.0) (2022-03-11)
445
-
446
- **Note:** Version bump only for package @alwatr/fetch
447
-
448
- # [0.4.0](https://github.com/Alwatr/eslib/compare/v0.3.0...v0.4.0) (2022-03-11)
449
-
450
- ### Bug Fixes
451
-
452
- - **fetch:** build issue [#73](https://github.com/Alwatr/eslib/issues/73) ([fb74463](https://github.com/Alwatr/eslib/commit/fb74463d367393706d16e482488a565bdfef70a1))
453
-
454
- # [0.3.0](https://github.com/Alwatr/eslib/compare/v0.2.1...v0.3.0) (2022-03-06)
455
-
456
- **Note:** Version bump only for package @alwatr/fetch
457
-
458
- ## [0.2.1](https://github.com/Alwatr/eslib/compare/v0.2.0...v0.2.1) (2022-03-05)
459
-
460
- **Note:** Version bump only for package @alwatr/fetch
461
-
462
- # [0.2.0](https://github.com/Alwatr/eslib/compare/v0.1.2...v0.2.0) (2022-03-05)
463
-
464
- ### Bug Fixes
465
-
466
- - **fetch:** error codes ([4e0be80](https://github.com/Alwatr/eslib/commit/4e0be80786b6667035ae82750f2351bd2da4f341))
467
-
468
- ### Features
34
+ ### Code Refactoring
469
35
 
470
- - **fetch:** add getData ([4a627c4](https://github.com/Alwatr/eslib/commit/4a627c43d2ad07acb340f058f02d41ce57288405))
471
- - **fetch:** add postData and fetchJson ([b0262b8](https://github.com/Alwatr/eslib/commit/b0262b83b5b7d8b7c376dce68aae3e16bd8e2610))
472
- - **fetch:** base fetch api ([925cdab](https://github.com/Alwatr/eslib/commit/925cdab8700ffb801e1d836ffc5d3245b66815cf))
473
- - **fetch:** FetchOptions type ([dd2d141](https://github.com/Alwatr/eslib/commit/dd2d1418d090387e1afa8277f78ae88ace17fd17))
474
- - **fetch:** new enhanced fetch package ([7e90472](https://github.com/Alwatr/eslib/commit/7e904720c983372317358ce107f6ad0f79c65647))
36
+ * **fetch:** review and update everything ([61ec38b](https://github.com/Alwatr/nanolib/commit/61ec38b2fde28ba26a7973fcd60a30c861faf4dd)) by @AliMD
package/README.md CHANGED
@@ -1,12 +1,14 @@
1
- # Alwatr Fetch - `@alwatr/fetch`
1
+ # Alwatr Fetch
2
2
 
3
- Enhanced fetch API with cache strategy, retry pattern, timeout, helper methods and enhanced types written in tiny TypeScript, ES module.
3
+ Enhanced fetch API with cache strategy, retry pattern, timeout, helper methods and enhanced types.
4
4
 
5
- ## Example usage
5
+ ## Installation
6
6
 
7
- ### `fetch(options: FetchOptions): Promise<Response>`
7
+ ```bash
8
+ yarn add @alwatr/fetch
9
+ ```
8
10
 
9
- It's a wrapper around the browser's `fetch` function that adds retry pattern with timeout and cacheStrategy.
11
+ ## Usage
10
12
 
11
13
  ```ts
12
14
  import {fetch} from 'https://esm.run/@alwatr/fetch';
@@ -26,7 +28,7 @@ const productList = await response.json();
26
28
  console.log(productList);
27
29
  ```
28
30
 
29
- ### Fetch Options
31
+ ## Fetch Options
30
32
 
31
33
  `FetchOptions` inherited from the [fetch standard parameters](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters) and some other...
32
34
 
package/dist/core.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { FetchOptions } from './type';
2
+ export declare const logger_: import("@alwatr/logger").AlwatrLogger;
3
+ export declare const cacheSupported: boolean;
4
+ /**
5
+ * Process fetch options and set defaults, etc.
6
+ *
7
+ * @param options Fetch options.
8
+ *
9
+ * @returns Required fetch options.
10
+ */
11
+ export declare function processOptions_(options: FetchOptions): Required<FetchOptions>;
12
+ /**
13
+ * Handle Cache Strategy over `handleRemoveDuplicate_`.
14
+ */
15
+ export declare function handleCacheStrategy_(options: Required<FetchOptions>): Promise<Response>;
16
+ /**
17
+ * Handle Remove Duplicates over `_handleRetryPattern`.
18
+ */
19
+ export declare function handleRemoveDuplicate_(options: Required<FetchOptions>): Promise<Response>;
20
+ /**
21
+ * Handle retry pattern over `handleTimeout_`.
22
+ */
23
+ export declare function handleRetryPattern_(options: Required<FetchOptions>): Promise<Response>;
24
+ /**
25
+ * It's a wrapper around the browser's `fetch` with timeout.
26
+ */
27
+ export declare function handleTimeout_(options: FetchOptions): Promise<Response>;
28
+ //# sourceMappingURL=core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AAGzC,eAAO,MAAM,OAAO,uCAAsD,CAAC;AAG3E,eAAO,MAAM,cAAc,SAAuC,CAAC;AAInE;;;;;;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 ADDED
@@ -0,0 +1,3 @@
1
+ /* @alwatr/fetch v3.1.0 */
2
+ "use strict";var m=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var x=(e,r)=>{for(var t in r)m(e,t,{get:r[t],enumerable:!0})},T=(e,r,t,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let c of O(r))!k.call(e,c)&&c!==t&&m(e,c,{get:()=>r[c],enumerable:!(a=b(r,c))||a.enumerable});return e};var v=e=>T(m({},"__esModule",{value:!0}),e);var C={};x(C,{cacheSupported:()=>s,fetch:()=>S,fetchJson:()=>P});module.exports=v(C);var f=require("@alwatr/global-scope"),_=require("@alwatr/logger"),w=require("@alwatr/wait"),n=(0,_.definePackage)("@alwatr/fetch","3.1.0"),i,s=Object.hasOwn(f.globalScope,"caches"),u={};function g(e){if(e.method??(e.method="GET"),e.window??(e.window=null),e.timeout??(e.timeout=8e3),e.retry??(e.retry=3),e.retryDelay??(e.retryDelay=1e3),e.cacheStrategy??(e.cacheStrategy="network_only"),e.removeDuplicate??(e.removeDuplicate="never"),e.headers??(e.headers={}),e.cacheStrategy!=="network_only"&&s!==!0&&(n.incident?.("fetch","fetch_cache_strategy_unsupported",{cacheSupported:s}),e.cacheStrategy="network_only"),e.removeDuplicate==="auto"&&(e.removeDuplicate=s?"until_load":"always"),e.url.lastIndexOf("?")===-1&&e.queryParameters!=null){let r=e.queryParameters,t=Object.keys(r).map(a=>`${a}=${String(r[a])}`);t.length>0&&(e.url+="?"+t.join("&"))}return e.bodyJson!==void 0&&(e.body=JSON.stringify(e.bodyJson),e.headers["Content-Type"]="application/json"),e.bearerToken!==void 0?e.headers.Authorization=`Bearer ${e.bearerToken}`:e.alwatrAuth!==void 0&&(e.headers.Authorization=`Alwatr ${e.alwatrAuth.userId}:${e.alwatrAuth.userToken}`),e}async function R(e){if(e.cacheStrategy==="network_only")return h(e);n.logMethod?.("_handleCacheStrategy"),i==null&&e.cacheStorageName==null&&(i=await caches.open("fetch_cache"));let r=e.cacheStorageName!=null?await caches.open(e.cacheStorageName):i,t=new Request(e.url,e);switch(e.cacheStrategy){case"cache_first":{let a=await r.match(t);if(a!=null)return a;let c=await h(e);return c.ok&&r.put(t,c.clone()),c}case"cache_only":{let a=await r.match(t);if(a==null)throw n.accident("_handleCacheStrategy","fetch_cache_not_found",{url:t.url}),new Error("fetch_cache_not_found");return a}case"network_first":try{let a=await h(e);return a.ok&&r.put(t,a.clone()),a}catch(a){let c=await r.match(t);if(c!=null)return c;throw a}case"update_cache":{let a=await h(e);return a.ok&&r.put(t,a.clone()),a}case"stale_while_revalidate":{let a=await r.match(t),c=h(e).then(l=>(l.ok&&(r.put(t,l.clone()),typeof e.revalidateCallback=="function"&&setTimeout(e.revalidateCallback,0,l.clone())),l));return a??c}default:return h(e)}}async function h(e){if(e.removeDuplicate==="never")return o(e);n.logMethod?.("handleRemoveDuplicate_");let r=e.method+" "+e.url;u[r]??(u[r]=o(e));try{let t=await u[r];return u[r]!=null&&(t.ok!==!0||e.removeDuplicate==="until_load")&&delete u[r],t.clone()}catch(t){throw delete u[r],t}}async function o(e){if(!(e.retry>1))return y(e);n.logMethod?.("_handleRetryPattern"),e.retry--;let r=e.signal;try{let t=await y(e);if(t.status<500)return t;throw new Error("fetch_server_error")}catch(t){if(n.accident("fetch","fetch_failed_retry",t),f.globalScope.navigator?.onLine===!1)throw n.accident("handleRetryPattern_","offline","Skip retry because offline"),t;return await(0,w.waitForTimeout)(e.retryDelay),e.signal=r,o(e)}}function y(e){return e.timeout===0?f.globalScope.fetch(e.url,e):(n.logMethod?.("handleTimeout_"),new Promise((r,t)=>{let a=typeof AbortController=="function"?new AbortController:null,c=e.signal;e.signal=a?.signal,a!==null&&c!=null&&c.addEventListener("abort",()=>a.abort(),{once:!0});let l=setTimeout(()=>{t(new Error("fetch_timeout")),a?.abort("fetch_timeout")},e.timeout);f.globalScope.fetch(e.url,e).then(d=>r(d)).catch(d=>t(d)).finally(()=>{delete e.signal,clearTimeout(l)})}))}async function P(e){let r,t,a;try{return r=await S(e),t=await r.text(),a=JSON.parse(t),a.ok=!0,a.statusCode=r.status,a}catch(c){let l={ok:!1,statusCode:r?.status,statusText:r?.statusText,errorCode:a?.errorCode??c.message,responseText:t,meta:a?.meta};return n.accident("fetchJson","fetch_failed",{responseError:l,error:c}),l}}function S(e){return e=g(e),n.logMethodArgs?.("fetch",{options:e}),R(e)}0&&(module.exports={cacheSupported,fetch,fetchJson});
3
+ //# sourceMappingURL=main.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/main.ts", "../src/core.ts"],
4
+ "sourcesContent": ["import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type';\nimport type {JsonObject} from '@alwatr/type-helper';\n\nexport {cacheSupported};\nexport type * from './type';\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 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,\n statusText: response?.statusText,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_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 {globalScope} from '@alwatr/global-scope';\nimport {definePackage} from '@alwatr/logger';\nimport {waitForTimeout} from '@alwatr/wait';\n\nimport type {FetchOptions} from './type';\nimport type {} from '@alwatr/nano-build';\n\nexport const logger_ = definePackage('@alwatr/fetch', __package_version__);\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = Object.hasOwn(globalScope, '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.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 !== 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 (globalScope.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await waitForTimeout(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 globalScope.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 }, 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"],
5
+ "mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,UAAAC,EAAA,cAAAC,IAAA,eAAAC,EAAAL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CAAC,cAAAD,EAAe,MAAAD,CAAK,CAAC,EAC7DC,CACT,CACF,CAwBO,SAASF,EAAMJ,EAA0C,CAC9D,OAAAA,EAAUQ,EAAgBR,CAAO,EACjCO,EAAQ,gBAAgB,QAAS,CAAC,QAAAP,CAAO,CAAC,EACnCS,EAAqBT,CAAiC,CAC/D",
6
+ "names": ["main_exports", "__export", "cacheSupported", "fetch", "fetchJson", "__toCommonJS", "import_global_scope", "import_logger", "import_wait", "logger_", "cacheStorage_", "cacheSupported", "duplicateRequestStorage_", "processOptions_", "options", "queryParameters", "queryArray", "key", "handleCacheStrategy_", "handleRemoveDuplicate_", "cacheStorage", "request", "cachedResponse", "response", "networkResponse", "err", "fetchedResponsePromise", "handleRetryPattern_", "cacheKey", "handleTimeout_", "externalAbortSignal", "resolved", "reject", "abortController", "timeoutId", "reason", "fetchJson", "options", "response", "responseText", "responseJson", "fetch", "error", "responseError", "logger_", "processOptions_", "handleCacheStrategy_"]
7
+ }