@alwatr/fetch 2.2.1 → 3.0.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,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
- ## [2.2.1](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.2.0...@alwatr/fetch@2.2.1) (2023-12-19)
6
+ ## 3.0.0 (2024-01-20)
7
7
 
8
- **Note:** Version bump only for package @alwatr/fetch
8
+ ### BREAKING CHANGES
9
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)
10
+ * **fetch:** remove serviceRequest
11
11
 
12
- ### Bug Fixes
13
-
14
- * **fetch:** AbortController not support issue ([10f6dbc](https://github.com/Alwatr/alwatr-es-sdk/commit/10f6dbc816d6f07a81d49945ec0291b132a75fb7)) by @AliMD
15
-
16
- ### Features
17
-
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)
47
-
48
- ### Bug Fixes
49
-
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
67
-
68
- * **fetch:** compatible with new logger api ([a51d7d2](https://github.com/Alwatr/eslib/commit/a51d7d23e9f7760c4d68685375cc8010456128fc)) by @AliMD
69
-
70
- ## [2.0.3](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.2...@alwatr/fetch@2.0.3) (2023-10-23)
71
-
72
- **Note:** Version bump only for package @alwatr/fetch
73
-
74
- ## [2.0.2](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.1...@alwatr/fetch@2.0.2) (2023-10-23)
75
-
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
246
-
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))
249
-
250
- ### Features
251
-
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))
254
-
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))
12
+ Co-authored-by: Ali Mihandoost <ali@mihandoost.com>
352
13
 
353
14
  ### Features
354
15
 
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
16
+ * **fetch:** alwatrAuth ([28e365c](https://github.com/Alwatr/nanolib/commit/28e365c839b0ea80060c0f44ed4dc4473468d5c4)) by @AliMD
17
+ * **fetch:** move from last repo ([4b86bb5](https://github.com/Alwatr/nanolib/commit/4b86bb542af296c91bc1db36b4e08fdbad501db2)) by @njfamirm
18
+ * **fetch:** Update fetch type definitions with document ([38398cc](https://github.com/Alwatr/nanolib/commit/38398cc33f311a569a53cc3e06c3191e17dbd45b)) by @AliMD
366
19
 
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
20
+ ### Code Refactoring
469
21
 
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))
22
+ * **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/main.cjs ADDED
@@ -0,0 +1,3 @@
1
+ /* @alwatr/fetch v3.0.0 */
2
+ "use strict";var y=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var k=(e,r)=>{for(var a in r)y(e,a,{get:r[a],enumerable:!0})},v=(e,r,a,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let c of b(r))!S.call(e,c)&&c!==a&&y(e,c,{get:()=>r[c],enumerable:!(t=R(r,c))||t.enumerable});return e};var O=e=>v(y({},"__esModule",{value:!0}),e);var F={};k(F,{cacheSupported:()=>d,fetch:()=>P});module.exports=O(F);var f=require("@alwatr/global-scope"),g=require("@alwatr/logger"),s=require("@alwatr/wait"),l=(0,g.definePackage)("@alwatr/fetch","3.0.0"),_,d=Object.hasOwn(f.globalScope,"caches"),u={};function P(e){return e=A(e),l.logMethodArgs?.("fetch",{options:e}),q(e)}function A(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"&&d!==!0&&(l.incident?.("fetch","fetch_cache_strategy_unsupported",{cacheSupported:d}),e.cacheStrategy="network_only"),e.removeDuplicate==="auto"&&(e.removeDuplicate=d?"until_load":"always"),e.url.lastIndexOf("?")===-1&&e.queryParameters!=null){let r=e.queryParameters,a=Object.keys(r).map(t=>`${t}=${String(r[t])}`);a.length>0&&(e.url+="?"+a.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 q(e){if(e.cacheStrategy==="network_only")return h(e);l.logMethod?.("_handleCacheStrategy"),_==null&&e.cacheStorageName==null&&(_=await caches.open("fetch_cache"));let r=e.cacheStorageName!=null?await caches.open(e.cacheStorageName):_,a=new Request(e.url,e);switch(e.cacheStrategy){case"cache_first":{let t=await r.match(a);if(t!=null)return t;let c=await h(e);return c.ok&&r.put(a,c.clone()),c}case"cache_only":{let t=await r.match(a);if(t==null)throw l.accident("_handleCacheStrategy","fetch_cache_not_found",{url:a.url}),new Error("fetch_cache_not_found");return t}case"network_first":try{let t=await h(e);return t.ok&&r.put(a,t.clone()),t}catch(t){let c=await r.match(a);if(c!=null)return c;throw t}case"update_cache":{let t=await h(e);return t.ok&&r.put(a,t.clone()),t}case"stale_while_revalidate":{let t=await r.match(a),c=h(e).then(n=>(n.ok&&(r.put(a,n.clone()),typeof e.revalidateCallback=="function"&&setTimeout(e.revalidateCallback,0,n.clone())),n));return t??c}default:return h(e)}}async function h(e){if(e.removeDuplicate==="never")return i(e);l.logMethod?.("handleRemoveDuplicate_");let r=e.method+" "+e.url;u[r]??(u[r]=i(e));try{let a=await u[r];return u[r]!=null&&(a.ok!==!0||e.removeDuplicate==="until_load")&&delete u[r],a.clone()}catch(a){throw delete u[r],a}}async function i(e){if(!(e.retry>1))return w(e);l.logMethod?.("_handleRetryPattern"),e.retry--;let r=e.signal;try{let a=await w(e);if(a.status<500)return a;throw new Error("fetch_server_error")}catch(a){if(l.accident("fetch","fetch_failed_retry",a),f.globalScope.navigator?.onLine===!1)throw new Error("offline");return await(0,s.waitForTimeout)(e.retryDelay),e.signal=r,i(e)}}function w(e){return e.timeout===0?f.globalScope.fetch(e.url,e):(l.logMethod?.("handleTimeout_"),new Promise((r,a)=>{let t=typeof AbortController=="function"?new AbortController:null,c=e.signal;e.signal=t?.signal,t!==null&&c!=null&&c.addEventListener("abort",()=>t.abort(),{once:!0});let n=setTimeout(()=>{a(new Error("fetch_timeout")),t?.abort("fetch_timeout")},e.timeout);f.globalScope.fetch(e.url,e).then(m=>r(m)).catch(m=>a(m)).finally(()=>{delete e.signal,clearTimeout(n)})}))}0&&(module.exports={cacheSupported,fetch});
3
+ //# sourceMappingURL=main.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/main.ts"],
4
+ "sourcesContent": ["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 type * from './type';\n\nconst 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 * 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\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nfunction 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 */\nasync function handleCacheStrategy_(options: Required<FetchOptions>): Promise<Response> {\n if (options.cacheStrategy === 'network_only') {\n return handleRemoveDuplicate_(options);\n }\n // else handle cache strategies!\n logger.logMethod?.('_handleCacheStrategy');\n\n if (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 */\nasync function handleRemoveDuplicate_(options: Required<FetchOptions>): Promise<Response> {\n if (options.removeDuplicate === 'never') return handleRetryPattern_(options);\n\n logger.logMethod?.('handleRemoveDuplicate_');\n\n const cacheKey = options.method + ' ' + options.url;\n\n // We must cache fetch promise without await for handle other parallel requests.\n duplicateRequestStorage_[cacheKey] ??= handleRetryPattern_(options);\n\n try {\n // For all requests need to await for clone responses.\n const response = await duplicateRequestStorage_[cacheKey];\n\n if (duplicateRequestStorage_[cacheKey] != null) {\n if (response.ok !== true || options.removeDuplicate === 'until_load') {\n delete duplicateRequestStorage_[cacheKey];\n }\n }\n\n return response.clone();\n }\n catch (err) {\n // clean cache on any error.\n delete duplicateRequestStorage_[cacheKey];\n throw err;\n }\n}\n\n/**\n * Handle retry pattern over `handleTimeout_`.\n */\nasync function handleRetryPattern_(options: Required<FetchOptions>): Promise<Response> {\n if (!(options.retry > 1)) return handleTimeout_(options);\n\n logger.logMethod?.('_handleRetryPattern');\n options.retry--;\n\n const externalAbortSignal = options.signal;\n\n try {\n const response = await handleTimeout_(options);\n\n if (response.status < 500) {\n return response;\n }\n // else\n throw new Error('fetch_server_error');\n }\n catch (err) {\n logger.accident('fetch', 'fetch_failed_retry', err);\n\n if (globalScope.navigator?.onLine === false) {\n throw new Error('offline');\n }\n\n await 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 */\nfunction handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalScope.fetch(options.url, options);\n }\n // else\n logger.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof 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,IAAA,eAAAC,EAAAJ,GAAA,IAAAK,EAA0B,gCAC1BC,EAA4B,0BAC5BC,EAA6B,wBAOvBC,KAAS,iBAAc,gBAAiB,OAAmB,EAE7DC,EACSP,EAAiB,OAAO,OAAO,cAAa,QAAQ,EAE3DQ,EAA8D,CAAC,EAwB9D,SAASP,EAAMQ,EAA0C,CAC9D,OAAAA,EAAUC,EAAgBD,CAAO,EACjCH,EAAO,gBAAgB,QAAS,CAAC,QAAAG,CAAO,CAAC,EAClCE,EAAqBF,CAAiC,CAC/D,CASA,SAASC,EAAgBD,EAA+C,CAsBtE,GArBAA,EAAQ,SAARA,EAAQ,OAAW,OACnBA,EAAQ,SAARA,EAAQ,OAAW,MAEnBA,EAAQ,UAARA,EAAQ,QAAY,KACpBA,EAAQ,QAARA,EAAQ,MAAU,GAClBA,EAAQ,aAARA,EAAQ,WAAe,KACvBA,EAAQ,gBAARA,EAAQ,cAAkB,gBAC1BA,EAAQ,kBAARA,EAAQ,gBAAoB,SAC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AAY,WAAW,IAAM,CACjCF,EAAO,IAAI,MAAM,eAAe,CAAC,EACjCC,GAAiB,MAAM,eAAe,CACxC,EAAGpB,EAAQ,OAAO,EAQlB,cACG,MAAMA,EAAQ,IAAKA,CAAO,EAC1B,KAAMU,GAAaQ,EAASR,CAAQ,CAAC,EACrC,MAAOY,GAAWH,EAAOG,CAAM,CAAC,EAChC,QAAQ,IAAM,CACb,OAAOtB,EAAQ,OACf,aAAaqB,CAAS,CACxB,CAAC,CACL,CAAC,EACH",
6
+ "names": ["main_exports", "__export", "cacheSupported", "fetch", "__toCommonJS", "import_global_scope", "import_logger", "import_wait", "logger", "cacheStorage_", "duplicateRequestStorage_", "options", "processOptions_", "handleCacheStrategy_", "queryParameters", "queryArray", "key", "handleRemoveDuplicate_", "cacheStorage", "request", "cachedResponse", "response", "networkResponse", "err", "fetchedResponsePromise", "handleRetryPattern_", "cacheKey", "handleTimeout_", "externalAbortSignal", "resolved", "reject", "abortController", "timeoutId", "reason"]
7
+ }
@@ -1,21 +1,22 @@
1
- import type { FetchOptions } from './type.js';
2
- import type { AlwatrServiceResponse } from '@alwatr/type';
3
- export type * from './type.js';
4
- /**
5
- * Fetch from alwatr services and return standard response.
6
- */
7
- export declare function serviceRequest<T extends AlwatrServiceResponse = AlwatrServiceResponse>(options: FetchOptions): Promise<T>;
1
+ import type { FetchOptions } from './type';
2
+ export type * from './type';
3
+ export declare const cacheSupported: boolean;
8
4
  /**
9
5
  * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,
10
6
  * remove duplicates, etc.
11
7
  *
12
- * Example:
8
+ * @see {@link FetchOptions}
9
+ *
10
+ * @param options Fetch options.
11
+ *
12
+ * @returns A promise that resolves to the Response to that request, whether it is successful or not.
13
13
  *
14
- * ```ts
14
+ * @example
15
+ * ```typescript
15
16
  * const response = await fetch({
16
17
  * url: '/api/products',
17
18
  * queryParameters: {limit: 10},
18
- * timeout: 10_000,
19
+ * timeout: 8_000,
19
20
  * retry: 3,
20
21
  * cacheStrategy: 'stale_while_revalidate',
21
22
  * cacheDuplicate: 'auto',
@@ -23,4 +24,4 @@ export declare function serviceRequest<T extends AlwatrServiceResponse = AlwatrS
23
24
  * ```
24
25
  */
25
26
  export declare function fetch(options: FetchOptions): Promise<Response>;
26
- //# sourceMappingURL=fetch.d.ts.map
27
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AAGzC,mBAAmB,QAAQ,CAAC;AAK5B,eAAO,MAAM,cAAc,SAAuC,CAAC;AAInE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI9D"}
package/dist/main.mjs ADDED
@@ -0,0 +1,3 @@
1
+ /* @alwatr/fetch v3.0.0 */
2
+ import{globalScope as f}from"@alwatr/global-scope";import{definePackage as w}from"@alwatr/logger";import{waitForTimeout as g}from"@alwatr/wait";var l=w("@alwatr/fetch","3.0.0"),m,y=Object.hasOwn(f,"caches"),u={};function v(e){return e=s(e),l.logMethodArgs?.("fetch",{options:e}),R(e)}function s(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"&&y!==!0&&(l.incident?.("fetch","fetch_cache_strategy_unsupported",{cacheSupported:y}),e.cacheStrategy="network_only"),e.removeDuplicate==="auto"&&(e.removeDuplicate=y?"until_load":"always"),e.url.lastIndexOf("?")===-1&&e.queryParameters!=null){let t=e.queryParameters,r=Object.keys(t).map(a=>`${a}=${String(t[a])}`);r.length>0&&(e.url+="?"+r.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);l.logMethod?.("_handleCacheStrategy"),m==null&&e.cacheStorageName==null&&(m=await caches.open("fetch_cache"));let t=e.cacheStorageName!=null?await caches.open(e.cacheStorageName):m,r=new Request(e.url,e);switch(e.cacheStrategy){case"cache_first":{let a=await t.match(r);if(a!=null)return a;let c=await h(e);return c.ok&&t.put(r,c.clone()),c}case"cache_only":{let a=await t.match(r);if(a==null)throw l.accident("_handleCacheStrategy","fetch_cache_not_found",{url:r.url}),new Error("fetch_cache_not_found");return a}case"network_first":try{let a=await h(e);return a.ok&&t.put(r,a.clone()),a}catch(a){let c=await t.match(r);if(c!=null)return c;throw a}case"update_cache":{let a=await h(e);return a.ok&&t.put(r,a.clone()),a}case"stale_while_revalidate":{let a=await t.match(r),c=h(e).then(n=>(n.ok&&(t.put(r,n.clone()),typeof e.revalidateCallback=="function"&&setTimeout(e.revalidateCallback,0,n.clone())),n));return a??c}default:return h(e)}}async function h(e){if(e.removeDuplicate==="never")return _(e);l.logMethod?.("handleRemoveDuplicate_");let t=e.method+" "+e.url;u[t]??(u[t]=_(e));try{let r=await u[t];return u[t]!=null&&(r.ok!==!0||e.removeDuplicate==="until_load")&&delete u[t],r.clone()}catch(r){throw delete u[t],r}}async function _(e){if(!(e.retry>1))return i(e);l.logMethod?.("_handleRetryPattern"),e.retry--;let t=e.signal;try{let r=await i(e);if(r.status<500)return r;throw new Error("fetch_server_error")}catch(r){if(l.accident("fetch","fetch_failed_retry",r),f.navigator?.onLine===!1)throw new Error("offline");return await g(e.retryDelay),e.signal=t,_(e)}}function i(e){return e.timeout===0?f.fetch(e.url,e):(l.logMethod?.("handleTimeout_"),new Promise((t,r)=>{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 n=setTimeout(()=>{r(new Error("fetch_timeout")),a?.abort("fetch_timeout")},e.timeout);f.fetch(e.url,e).then(d=>t(d)).catch(d=>r(d)).finally(()=>{delete e.signal,clearTimeout(n)})}))}export{y as cacheSupported,v as fetch};
3
+ //# sourceMappingURL=main.mjs.map