@alwatr/fetch 2.2.0 → 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,468 +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.0](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.5...@alwatr/fetch@2.2.0) (2023-12-19)
6
+ ## 3.0.0 (2024-01-20)
7
7
 
8
- ### Bug Fixes
8
+ ### BREAKING CHANGES
9
9
 
10
- * **fetch:** AbortController not support issue ([10f6dbc](https://github.com/Alwatr/alwatr-es-sdk/commit/10f6dbc816d6f07a81d49945ec0291b132a75fb7)) by @AliMD
10
+ * **fetch:** remove serviceRequest
11
11
 
12
- ### Features
13
-
14
- * use globalScope instead globalThis ([94342bb](https://github.com/Alwatr/alwatr-es-sdk/commit/94342bb7b467376173a65d8cdf6bb106338f425b)) by @njfamirm
15
-
16
- ## [2.1.5](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.4...@alwatr/fetch@2.1.5) (2023-12-11)
17
-
18
- **Note:** Version bump only for package @alwatr/fetch
19
-
20
- ## [2.1.4](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.3...@alwatr/fetch@2.1.4) (2023-12-11)
21
-
22
- **Note:** Version bump only for package @alwatr/fetch
23
-
24
- ## [2.1.3](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/fetch@2.1.2...@alwatr/fetch@2.1.3) (2023-12-09)
25
-
26
- **Note:** Version bump only for package @alwatr/fetch
27
-
28
- ## [2.1.2](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.1.1...@alwatr/fetch@2.1.2) (2023-11-29)
29
-
30
- **Note:** Version bump only for package @alwatr/fetch
31
-
32
- ## [2.1.1](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.1.0...@alwatr/fetch@2.1.1) (2023-11-23)
33
-
34
- **Note:** Version bump only for package @alwatr/fetch
35
-
36
- # [2.1.0](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.8...@alwatr/fetch@2.1.0) (2023-11-14)
37
-
38
- ### Features
39
-
40
- * **yarn:** pnp ([b6a562c](https://github.com/Alwatr/eslib/commit/b6a562c909a35b3e626442e5c325da3fce448e1b)) by @AliMD
41
-
42
- ## [2.0.8](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.7...@alwatr/fetch@2.0.8) (2023-11-08)
43
-
44
- ### Bug Fixes
45
-
46
- * move repo urls ([719aa3e](https://github.com/Alwatr/eslib/commit/719aa3e7462d3c9c6272958fc93f89fda6793fb1)) by @AliMD
47
-
48
- ## [2.0.7](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.6...@alwatr/fetch@2.0.7) (2023-11-01)
49
-
50
- **Note:** Version bump only for package @alwatr/fetch
51
-
52
- ## [2.0.6](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.5...@alwatr/fetch@2.0.6) (2023-10-23)
53
-
54
- **Note:** Version bump only for package @alwatr/fetch
55
-
56
- ## [2.0.5](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.4...@alwatr/fetch@2.0.5) (2023-10-23)
57
-
58
- **Note:** Version bump only for package @alwatr/fetch
59
-
60
- ## [2.0.4](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.3...@alwatr/fetch@2.0.4) (2023-10-23)
61
-
62
- ### Bug Fixes
63
-
64
- * **fetch:** compatible with new logger api ([a51d7d2](https://github.com/Alwatr/eslib/commit/a51d7d23e9f7760c4d68685375cc8010456128fc)) by @AliMD
65
-
66
- ## [2.0.3](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.2...@alwatr/fetch@2.0.3) (2023-10-23)
67
-
68
- **Note:** Version bump only for package @alwatr/fetch
69
-
70
- ## [2.0.2](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.1...@alwatr/fetch@2.0.2) (2023-10-23)
71
-
72
- **Note:** Version bump only for package @alwatr/fetch
73
-
74
- ## [2.0.1](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@2.0.0...@alwatr/fetch@2.0.1) (2023-09-19)
75
-
76
- ### Bug Fixes
77
-
78
- - package repo urls ([466cbe9](https://github.com/Alwatr/eslib/commit/466cbe9188f24e1a1bc36d879a95b52538a58f16)) by @AliMD
79
-
80
- # [2.0.0](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@1.1.3...@alwatr/fetch@2.0.0) (2023-09-19)
81
-
82
- ### Bug Fixes
83
-
84
- - **fetch:** remove @alwatr/signal ([03d947b](https://github.com/Alwatr/eslib/commit/03d947be3889d63805eabb7f4be618fed6da0d55)) by @AliMD
85
-
86
- - fix(fetch)!: remove fetchContext ([eba9cf0](https://github.com/Alwatr/eslib/commit/eba9cf0a443564478b5b985a69d0f54fbd514440)) by @AliMD
87
-
88
- ### BREAKING CHANGES
89
-
90
- - fetchContext method removed
91
-
92
- ## 1.1.3 (2023-09-19)
93
-
94
- ### Bug Fixes
95
-
96
- - cleanup extra packages ([dfc0bd2](https://github.com/Alwatr/eslib/commit/dfc0bd2b42d49e1094ae0303d34e653aa289d358)) by @AliMD
97
-
98
- ## [1.1.2](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@1.1.1...@alwatr/fetch@1.1.2) (2023-09-12)
99
-
100
- **Note:** Version bump only for package @alwatr/fetch
101
-
102
- ## [1.1.1](https://github.com/Alwatr/eslib/compare/@alwatr/fetch@1.1.0...@alwatr/fetch@1.1.1) (2023-09-12)
103
-
104
- **Note:** Version bump only for package @alwatr/fetch
105
-
106
- # 1.1.0 (2023-09-12)
107
-
108
- # 1.0.0 (2023-06-14)
109
-
110
- # 0.32.0 (2023-05-27)
111
-
112
- ### Bug Fixes
113
-
114
- - **fetch:** serviceRequest type ([44c1086](https://github.com/Alwatr/eslib/commit/44c10866e1c630bc3cf85b3e9be38b268b5b0c31))
115
- - **fetch:** userAuth in StringifyableFetchOptions ([f554b57](https://github.com/Alwatr/eslib/commit/f554b575584cb1464d48fbe8a8c4006125365853))
116
-
117
- # 0.31.0 (2023-05-08)
118
-
119
- ### Bug Fixes
120
-
121
- - **fetch.ts:** compatible with node ([c3acb53](https://github.com/Alwatr/eslib/commit/c3acb53effd4a79e389bc4de1567b0285c03d71d))
122
- - **fetch:** bodyJson type ([5740467](https://github.com/Alwatr/eslib/commit/57404673e7f031a73d1cf23501e6fa8d89759769))
123
- - **fetch:** check node_mode before use navigator.online ([a688f48](https://github.com/Alwatr/eslib/commit/a688f48e9ce4b103c25ffd50f1b8c96e89c205d1))
124
- - **fetch:** serviceRequest generic type ([074ef38](https://github.com/Alwatr/eslib/commit/074ef3876086f93486753559d8e3db0b60c52a0e))
125
- - **fetch:** suppress fetch_cache_not_found extra error ([c04f609](https://github.com/Alwatr/eslib/commit/c04f60944106dbd8d1bf2fb5ce0e7be644c3abc6))
126
- - new logger api ([9d83a7d](https://github.com/Alwatr/eslib/commit/9d83a7dc5c103bc3bb4282dacfd85fa998915300))
127
-
128
- ### Features
129
-
130
- - **fetch:** add user auth ([811fbc0](https://github.com/Alwatr/eslib/commit/811fbc018c2b4e3b808704e8623bd5dff2b2f3f7))
131
- - **fetch:** StringifyableFetchOptions type ([e13fdb2](https://github.com/Alwatr/eslib/commit/e13fdb2550cf3a2102c331aa7980234ca6f86e08))
132
-
133
- # 0.30.0 (2023-03-06)
134
-
135
- ### Bug Fixes
136
-
137
- - **fetch:** fetchContext duplicate setSignalValue ([8aa9b00](https://github.com/Alwatr/eslib/commit/8aa9b003a2d7b7ffc8243ff7092d9560763d7273))
138
- - **fetch:** getClientId in nodejs ([c99e41c](https://github.com/Alwatr/eslib/commit/c99e41c9e9a979ca50cc01aeb5371a3487d9da2e))
139
-
140
- ### Features
141
-
142
- - **fetch:** enhance fetchContext with dispatchOptions and only cache_only request in first time ([1f6b575](https://github.com/Alwatr/eslib/commit/1f6b575437104d64ede8a2c23d141160360d2cd5))
143
- - **fetch:** fetchContext ([d63d17e](https://github.com/Alwatr/eslib/commit/d63d17ecb58221159dc45a473fdd1c992d306d2b))
144
- - **fetch:** fetchContext skip online request when offline ([7c2b649](https://github.com/Alwatr/eslib/commit/7c2b6491f78acb961cdc0583112e3c709a177b79))
145
- - **fetch:** fetchContext work on background ([2e12bfd](https://github.com/Alwatr/eslib/commit/2e12bfde0a0636c30e60906c4812ec66b2d36742))
146
- - **fetch:** handle offline for all requests ([6a8d7b7](https://github.com/Alwatr/eslib/commit/6a8d7b7159732d834be052191935580331e36962))
147
-
148
- # 0.29.0 (2023-02-10)
149
-
150
- ### Bug Fixes
151
-
152
- - **fetch:** device id to client id ([077e467](https://github.com/Alwatr/eslib/commit/077e4671d804d43c8f61a9d163d8d51d7a69134e))
153
- - **fetch:** headers type ([cd4b14e](https://github.com/Alwatr/eslib/commit/cd4b14e46b5090b6df8cb4f89d22b53966b9151e))
154
-
155
- ### Features
156
-
157
- - **fetch:** add update_cache to CacheStrategy ([e1406b1](https://github.com/Alwatr/eslib/commit/e1406b12e1276de3295c47506c8102ab3f8bfa79))
158
- - **fetch:** clientId on service request ([9575a1d](https://github.com/Alwatr/eslib/commit/9575a1d595a0a4757581a29a66837cbe4be46ed3))
159
-
160
- # 0.28.0 (2023-01-20)
161
-
162
- # 0.27.0 (2022-12-29)
163
-
164
- ### Bug Fixes
165
-
166
- - review issue ([e5d192c](https://github.com/Alwatr/eslib/commit/e5d192cbee6917c2de01146cf8bd026895724ab8))
167
-
168
- ### Features
169
-
170
- - **type:** add types ([2de3842](https://github.com/Alwatr/eslib/commit/2de384297b70b9818ec8d62ce8d31be4822d95a4))
171
-
172
- # 0.26.0 (2022-12-22)
173
-
174
- ### Bug Fixes
175
-
176
- - all import without .js ([fa739ab](https://github.com/Alwatr/eslib/commit/fa739ab23f67bae1d10bfcc146920b71377a26fc))
177
- - **packages/fetch:** tsconfig refrences ([e6b9542](https://github.com/Alwatr/eslib/commit/e6b9542284e67b67600da4dcebd19daedb031c9c))
178
- - **fetch:** always call revalidateCallback ([444edc5](https://github.com/Alwatr/eslib/commit/444edc52b6dd5de5418ae8630e1f2270f0114372))
179
- - **fetch:** enhance handle and report errors in serviceRequest ([dbfc7ff](https://github.com/Alwatr/eslib/commit/dbfc7ff2a036216498dc8e2ff58b74b7eb8d9bc0))
180
- - **fetch:** fetch_cache_not_found is not error ([3d1aa4e](https://github.com/Alwatr/eslib/commit/3d1aa4e572fbf4bc7b0a527a3fdcf4c521af18dd))
181
- - **fetch:** fetch_failed error message ([3e98bd9](https://github.com/Alwatr/eslib/commit/3e98bd926a495c13d476f2f1cfb9e546a059d4d8))
182
- - **fetch:** remove extra \_options ([ad24c81](https://github.com/Alwatr/eslib/commit/ad24c8104b944ebda52308e60d41976ac1eeee81))
183
- - **fetch:** revalidateCallback issue ([7207909](https://github.com/Alwatr/eslib/commit/72079098edb438c28256dad03d7802bc2fab0858))
184
- - set correct path ([d01ce6f](https://github.com/Alwatr/eslib/commit/d01ce6ffa749a5e3e0e11e35b4ed61d75d61fec9))
185
- - **storage-engine:** AlwatrDocumentStorage ([ca9a7bf](https://github.com/Alwatr/eslib/commit/ca9a7bf5e17b333538e749ada5d481a06c847101))
186
- - tsconfig ([e96dcd3](https://github.com/Alwatr/eslib/commit/e96dcd30774a9f06f7d051e0504192cbbe019e35))
187
- - tsconfig refrences ([cea0562](https://github.com/Alwatr/eslib/commit/cea05621ecfa499476c5b10a412e60f27ba6a06d))
188
-
189
- ### Features
190
-
191
- - **fetch:** add AlwatrDocumentStorage type ([44b84f0](https://github.com/Alwatr/eslib/commit/44b84f0754169a707472655811020f6e542a6e5d))
192
- - **fetch:** serviceRequest ([b0b5478](https://github.com/Alwatr/eslib/commit/b0b5478d16b2cb590cfc29d644006e4a459dd6d6))
193
- - **fetch:** serviceRequest ([#513](https://github.com/Alwatr/eslib/issues/513)) ([113ee49](https://github.com/Alwatr/eslib/commit/113ee498c778d9efc00cc4807bfef09e52336a31))
194
- - improve accident debugging ([d697e6c](https://github.com/Alwatr/eslib/commit/d697e6c4c7d0a4172f4dba9caf6fbd478f79de9c))
195
- - improve error debugging ([1fba504](https://github.com/Alwatr/eslib/commit/1fba50400a1e8ececc10bbe8ea11cc8dcea2289c))
196
-
197
- # [1.0.0](https://github.com/Alwatr/eslib/compare/v0.32.0...v1.0.0) (2023-06-14)
198
-
199
- **Note:** Version bump only for package @alwatr/fetch
200
-
201
- # [0.32.0](https://github.com/Alwatr/eslib/compare/v0.31.0...v0.32.0) (2023-05-27)
202
-
203
- ### Bug Fixes
204
-
205
- - **fetch:** serviceRequest type ([44c1086](https://github.com/Alwatr/eslib/commit/44c10866e1c630bc3cf85b3e9be38b268b5b0c31))
206
- - **fetch:** userAuth in StringifyableFetchOptions ([f554b57](https://github.com/Alwatr/eslib/commit/f554b575584cb1464d48fbe8a8c4006125365853))
207
-
208
- # [0.31.0](https://github.com/Alwatr/eslib/compare/v0.30.0...v0.31.0) (2023-05-08)
209
-
210
- ### Bug Fixes
211
-
212
- - **fetch.ts:** compatible with node ([c3acb53](https://github.com/Alwatr/eslib/commit/c3acb53effd4a79e389bc4de1567b0285c03d71d))
213
- - **fetch:** bodyJson type ([5740467](https://github.com/Alwatr/eslib/commit/57404673e7f031a73d1cf23501e6fa8d89759769))
214
- - **fetch:** check node_mode before use navigator.online ([a688f48](https://github.com/Alwatr/eslib/commit/a688f48e9ce4b103c25ffd50f1b8c96e89c205d1))
215
- - **fetch:** serviceRequest generic type ([074ef38](https://github.com/Alwatr/eslib/commit/074ef3876086f93486753559d8e3db0b60c52a0e))
216
- - **fetch:** suppress fetch_cache_not_found extra error ([c04f609](https://github.com/Alwatr/eslib/commit/c04f60944106dbd8d1bf2fb5ce0e7be644c3abc6))
217
- - new logger api ([9d83a7d](https://github.com/Alwatr/eslib/commit/9d83a7dc5c103bc3bb4282dacfd85fa998915300))
218
-
219
- ### Features
220
-
221
- - **fetch:** add user auth ([811fbc0](https://github.com/Alwatr/eslib/commit/811fbc018c2b4e3b808704e8623bd5dff2b2f3f7))
222
- - **fetch:** StringifyableFetchOptions type ([e13fdb2](https://github.com/Alwatr/eslib/commit/e13fdb2550cf3a2102c331aa7980234ca6f86e08))
223
-
224
- # [0.30.0](https://github.com/Alwatr/eslib/compare/v0.29.0...v0.30.0) (2023-03-06)
225
-
226
- ### Bug Fixes
227
-
228
- - **fetch:** fetchContext duplicate setSignalValue ([8aa9b00](https://github.com/Alwatr/eslib/commit/8aa9b003a2d7b7ffc8243ff7092d9560763d7273))
229
- - **fetch:** getClientId in nodejs ([c99e41c](https://github.com/Alwatr/eslib/commit/c99e41c9e9a979ca50cc01aeb5371a3487d9da2e))
230
-
231
- ### Features
232
-
233
- - **fetch:** enhance fetchContext with dispatchOptions and only cache_only request in first time ([1f6b575](https://github.com/Alwatr/eslib/commit/1f6b575437104d64ede8a2c23d141160360d2cd5))
234
- - **fetch:** fetchContext ([d63d17e](https://github.com/Alwatr/eslib/commit/d63d17ecb58221159dc45a473fdd1c992d306d2b))
235
- - **fetch:** fetchContext skip online request when offline ([7c2b649](https://github.com/Alwatr/eslib/commit/7c2b6491f78acb961cdc0583112e3c709a177b79))
236
- - **fetch:** fetchContext work on background ([2e12bfd](https://github.com/Alwatr/eslib/commit/2e12bfde0a0636c30e60906c4812ec66b2d36742))
237
- - **fetch:** handle offline for all requests ([6a8d7b7](https://github.com/Alwatr/eslib/commit/6a8d7b7159732d834be052191935580331e36962))
238
-
239
- # [0.29.0](https://github.com/Alwatr/eslib/compare/v0.28.0...v0.29.0) (2023-02-10)
240
-
241
- ### Bug Fixes
242
-
243
- - **fetch:** device id to client id ([077e467](https://github.com/Alwatr/eslib/commit/077e4671d804d43c8f61a9d163d8d51d7a69134e))
244
- - **fetch:** headers type ([cd4b14e](https://github.com/Alwatr/eslib/commit/cd4b14e46b5090b6df8cb4f89d22b53966b9151e))
245
-
246
- ### Features
247
-
248
- - **fetch:** add update_cache to CacheStrategy ([e1406b1](https://github.com/Alwatr/eslib/commit/e1406b12e1276de3295c47506c8102ab3f8bfa79))
249
- - **fetch:** clientId on service request ([9575a1d](https://github.com/Alwatr/eslib/commit/9575a1d595a0a4757581a29a66837cbe4be46ed3))
250
-
251
- # [0.28.0](https://github.com/Alwatr/eslib/compare/v0.27.0...v0.28.0) (2023-01-20)
252
-
253
- ### Bug Fixes
254
-
255
- - review issue ([e5d192c](https://github.com/Alwatr/eslib/commit/e5d192cbee6917c2de01146cf8bd026895724ab8))
256
-
257
- ### Features
258
-
259
- - **type:** add types ([2de3842](https://github.com/Alwatr/eslib/commit/2de384297b70b9818ec8d62ce8d31be4822d95a4))
260
-
261
- # [0.27.0](https://github.com/Alwatr/eslib/compare/v0.26.0...v0.27.0) (2022-12-29)
262
-
263
- **Note:** Version bump only for package @alwatr/fetch
264
-
265
- # [0.26.0](https://github.com/Alwatr/eslib/compare/v0.25.0...v0.26.0) (2022-12-22)
266
-
267
- ### Bug Fixes
268
-
269
- - all import without .js ([fa739ab](https://github.com/Alwatr/eslib/commit/fa739ab23f67bae1d10bfcc146920b71377a26fc))
270
- - **packages/fetch:** tsconfig refrences ([e6b9542](https://github.com/Alwatr/eslib/commit/e6b9542284e67b67600da4dcebd19daedb031c9c))
271
- - **fetch:** always call revalidateCallback ([444edc5](https://github.com/Alwatr/eslib/commit/444edc52b6dd5de5418ae8630e1f2270f0114372))
272
- - **fetch:** enhance handle and report errors in serviceRequest ([dbfc7ff](https://github.com/Alwatr/eslib/commit/dbfc7ff2a036216498dc8e2ff58b74b7eb8d9bc0))
273
- - **fetch:** fetch_cache_not_found is not error ([3d1aa4e](https://github.com/Alwatr/eslib/commit/3d1aa4e572fbf4bc7b0a527a3fdcf4c521af18dd))
274
- - **fetch:** fetch_failed error message ([3e98bd9](https://github.com/Alwatr/eslib/commit/3e98bd926a495c13d476f2f1cfb9e546a059d4d8))
275
- - **fetch:** remove extra \_options ([ad24c81](https://github.com/Alwatr/eslib/commit/ad24c8104b944ebda52308e60d41976ac1eeee81))
276
- - **fetch:** revalidateCallback issue ([7207909](https://github.com/Alwatr/eslib/commit/72079098edb438c28256dad03d7802bc2fab0858))
277
- - set correct path ([d01ce6f](https://github.com/Alwatr/eslib/commit/d01ce6ffa749a5e3e0e11e35b4ed61d75d61fec9))
278
- - **storage-engine:** AlwatrDocumentStorage ([ca9a7bf](https://github.com/Alwatr/eslib/commit/ca9a7bf5e17b333538e749ada5d481a06c847101))
279
- - tsconfig ([e96dcd3](https://github.com/Alwatr/eslib/commit/e96dcd30774a9f06f7d051e0504192cbbe019e35))
280
- - tsconfig refrences ([cea0562](https://github.com/Alwatr/eslib/commit/cea05621ecfa499476c5b10a412e60f27ba6a06d))
281
-
282
- ### Features
283
-
284
- - **fetch:** add AlwatrDocumentStorage type ([44b84f0](https://github.com/Alwatr/eslib/commit/44b84f0754169a707472655811020f6e542a6e5d))
285
- - **fetch:** serviceRequest ([b0b5478](https://github.com/Alwatr/eslib/commit/b0b5478d16b2cb590cfc29d644006e4a459dd6d6))
286
- - **fetch:** serviceRequest ([#513](https://github.com/Alwatr/eslib/issues/513)) ([113ee49](https://github.com/Alwatr/eslib/commit/113ee498c778d9efc00cc4807bfef09e52336a31))
287
- - improve accident debugging ([d697e6c](https://github.com/Alwatr/eslib/commit/d697e6c4c7d0a4172f4dba9caf6fbd478f79de9c))
288
- - improve error debugging ([1fba504](https://github.com/Alwatr/eslib/commit/1fba50400a1e8ececc10bbe8ea11cc8dcea2289c))
289
-
290
- # [0.25.0](https://github.com/Alwatr/eslib/compare/v0.24.1...v0.25.0) (2022-12-07)
291
-
292
- **Note:** Version bump only for package @alwatr/fetch
293
-
294
- ## [0.24.1](https://github.com/Alwatr/eslib/compare/v0.24.0...v0.24.1) (2022-12-01)
295
-
296
- **Note:** Version bump only for package @alwatr/fetch
297
-
298
- # [0.24.0](https://github.com/Alwatr/eslib/compare/v0.23.0...v0.24.0) (2022-11-28)
299
-
300
- ### Bug Fixes
301
-
302
- - **fetch:** AlwatrServiceResponseSuccessWithMeta types ([b46f3f2](https://github.com/Alwatr/eslib/commit/b46f3f22a7c5a671385b0a8f58d0881501ce1b94))
303
- - **fetch:** use nre method type ([1c63eca](https://github.com/Alwatr/eslib/commit/1c63eca994582c241112c3e5ae79bac49831532f))
304
- - update types ([44cc57f](https://github.com/Alwatr/eslib/commit/44cc57f97a99ed810dae3fde39c9525bd0e8ff3c))
305
- - use ~ for package version ([4e027ff](https://github.com/Alwatr/eslib/commit/4e027ff63875e03b088ebcdc1bdf2495f4494eec))
306
-
307
- # [0.23.0](https://github.com/Alwatr/eslib/compare/v0.22.1...v0.23.0) (2022-11-23)
308
-
309
- ### Bug Fixes
310
-
311
- - **fetch:** types ([4ce81a7](https://github.com/Alwatr/eslib/commit/4ce81a7b82d01ab938754bc5e483ccb074671e33))
312
-
313
- ### Features
314
-
315
- - **fetch:** set token in options ([033a638](https://github.com/Alwatr/eslib/commit/033a63846f8c43b86c7d0a662e4bb6aa7cae3af5))
316
-
317
- ## [0.22.1](https://github.com/Alwatr/eslib/compare/v0.22.0...v0.22.1) (2022-11-21)
318
-
319
- **Note:** Version bump only for package @alwatr/fetch
320
-
321
- # [0.22.0](https://github.com/Alwatr/eslib/compare/v0.21.0...v0.22.0) (2022-11-20)
322
-
323
- ### Bug Fixes
324
-
325
- - **fetch:** \_handleRetryPattern ([fe4ac12](https://github.com/Alwatr/eslib/commit/fe4ac12b4ad3086d44e545591c767cb322e32b8d))
326
- - **fetch:** bodyJson issue ([5d8d6fa](https://github.com/Alwatr/eslib/commit/5d8d6fa4a0f608c2c5ed583aa54de9b836daaa5f))
327
-
328
- ### Features
329
-
330
- - **fetch:** support nodejs ([2ed2ef4](https://github.com/Alwatr/eslib/commit/2ed2ef42e9f204d4896ada4e20b839cfabdc7284))
331
-
332
- ### Performance Improvements
333
-
334
- - **fetch:** Improve performance and decrease memory usage ([7c29333](https://github.com/Alwatr/eslib/commit/7c293339215bea3a5013effe4aea5b6267a7a75e))
335
-
336
- # [0.21.0](https://github.com/Alwatr/eslib/compare/v0.20.0...v0.21.0) (2022-11-13)
337
-
338
- **Note:** Version bump only for package @alwatr/fetch
339
-
340
- # [0.20.0](https://github.com/Alwatr/eslib/compare/v0.19.0...v0.20.0) (2022-11-05)
341
-
342
- ### Bug Fixes
343
-
344
- - **fetch:** \_handleRemoveDuplicate ([6207b48](https://github.com/Alwatr/eslib/commit/6207b4867935d05a0e5a1c795de9efab4ff77c68))
345
- - **fetch:** cacheStorageName issue ([e641ff7](https://github.com/Alwatr/eslib/commit/e641ff740055a68b6397f00bf46f1d05943c348a))
346
- - **fetch:** method type and case ([8d4ba77](https://github.com/Alwatr/eslib/commit/8d4ba77e56ecfe2aeaa459dcd26039c38c4b2199))
347
- - **fetch:** prevent duplicate revalidateCallback on first time ([2da7dc5](https://github.com/Alwatr/eslib/commit/2da7dc579e4850af06914590a231548cd469cd75))
348
-
349
- ### Features
350
-
351
- - **fetch:** add retryDelay ([28e3244](https://github.com/Alwatr/eslib/commit/28e3244a2d35e7a1a91f9a5a4942f1bb4a56d981))
352
- - **fetch:** revalidate callback for `stale_while_revalidate` cache strategy. ([8976cc4](https://github.com/Alwatr/eslib/commit/8976cc424b2a9e96e4cfa69941d5378a6640af6f))
353
- - **fetch:** simple memory caching for remove duplicate/parallel requests ([74cd7f1](https://github.com/Alwatr/eslib/commit/74cd7f1500d730021a0f577c0ecfd7a2460fdb15))
354
-
355
- # [0.19.0](https://github.com/Alwatr/eslib/compare/v0.18.0...v0.19.0) (2022-11-01)
356
-
357
- **Note:** Version bump only for package @alwatr/fetch
358
-
359
- # [0.18.0](https://github.com/Alwatr/eslib/compare/v0.17.0...v0.18.0) (2022-10-22)
360
-
361
- ### Bug Fixes
362
-
363
- - **fetch:** update cache on network_first ([54b59cd](https://github.com/Alwatr/eslib/commit/54b59cdc64a543a0afcd09d2ec78d20661f3a06d))
12
+ Co-authored-by: Ali Mihandoost <ali@mihandoost.com>
364
13
 
365
14
  ### Features
366
15
 
367
- - **fetch:** cache strategy ([106eabd](https://github.com/Alwatr/eslib/commit/106eabdd10574b24f9919049d0b509ae40168a9a))
368
- - **fetch:** optional cacheStorageName ([f2bd8f2](https://github.com/Alwatr/eslib/commit/f2bd8f2a899466628417294cb269fd569b87e17a))
369
- - **fetch:** stale_while_revalidate cache strategy ([f758f49](https://github.com/Alwatr/eslib/commit/f758f495efb42ce7441e8f2058c1b67de0615a8c))
370
-
371
- # [0.17.0](https://github.com/Alwatr/eslib/compare/v0.16.1...v0.17.0) (2022-10-21)
372
-
373
- ### Bug Fixes
374
-
375
- - **fetch:** not ok retry condition ([0da1edd](https://github.com/Alwatr/eslib/commit/0da1edda64ec5abf70a83361d159e207691e368e))
376
-
377
- ### Features
378
-
379
- - **fetch:** docs & pattern ([459ad1c](https://github.com/Alwatr/eslib/commit/459ad1c5996f851769306639136d79c0f7270770))
380
- - **fetch:** logger & jsdocs ([36f652d](https://github.com/Alwatr/eslib/commit/36f652d5dad23b2aeb824af77d1b0e442119c6e8))
381
- - **fetch:** retry pattern ([5350d1a](https://github.com/Alwatr/eslib/commit/5350d1a81b9134d598f46006a1baa880b280ea98))
382
-
383
- ### Performance Improvements
384
-
385
- - **fetch:** syntax ([1fdd02e](https://github.com/Alwatr/eslib/commit/1fdd02ec8b52e32a124b8d7c1d1c7fd7c993e3af))
386
-
387
- # [0.16.0](https://github.com/Alwatr/eslib/compare/v0.15.0...v0.16.0) (2022-09-08)
388
-
389
- **Note:** Version bump only for package @alwatr/fetch
390
-
391
- # [0.15.0](https://github.com/Alwatr/eslib/compare/v0.14.0...v0.15.0) (2022-09-01)
392
-
393
- **Note:** Version bump only for package @alwatr/fetch
394
-
395
- # [0.14.0](https://github.com/Alwatr/eslib/compare/v0.13.0...v0.14.0) (2022-08-19)
396
-
397
- **Note:** Version bump only for package @alwatr/fetch
398
-
399
- # [0.13.0](https://github.com/Alwatr/eslib/compare/v0.12.0...v0.13.0) (2022-08-06)
400
-
401
- **Note:** Version bump only for package @alwatr/fetch
402
-
403
- # [0.12.0](https://github.com/Alwatr/eslib/compare/v0.11.0...v0.12.0) (2022-07-22)
404
-
405
- ### Bug Fixes
406
-
407
- - **fetch:** compatible with new ts types AbortSignal ([949655a](https://github.com/Alwatr/eslib/commit/949655a257852aaaaa311e32603ecc0819eedf51))
408
-
409
- # [0.11.0](https://github.com/Alwatr/eslib/compare/v0.10.1...v0.11.0) (2022-04-16)
410
-
411
- **Note:** Version bump only for package @alwatr/fetch
412
-
413
- # [0.10.0](https://github.com/Alwatr/eslib/compare/v0.9.0...v0.10.0) (2022-04-02)
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
414
19
 
415
- **Note:** Version bump only for package @alwatr/fetch
416
-
417
- # [0.9.0](https://github.com/Alwatr/eslib/compare/v0.8.0...v0.9.0) (2022-03-22)
418
-
419
- **Note:** Version bump only for package @alwatr/fetch
420
-
421
- # [0.8.0](https://github.com/Alwatr/eslib/compare/v0.7.2...v0.8.0) (2022-03-14)
422
-
423
- **Note:** Version bump only for package @alwatr/fetch
424
-
425
- ## [0.7.2](https://github.com/Alwatr/eslib/compare/v0.7.1...v0.7.2) (2022-03-12)
426
-
427
- **Note:** Version bump only for package @alwatr/fetch
428
-
429
- # [0.7.0](https://github.com/Alwatr/eslib/compare/v0.6.1...v0.7.0) (2022-03-12)
430
-
431
- **Note:** Version bump only for package @alwatr/fetch
432
-
433
- # [0.6.0](https://github.com/Alwatr/eslib/compare/v0.5.0...v0.6.0) (2022-03-11)
434
-
435
- ### Bug Fixes
436
-
437
- - alalwatr ([898aa6e](https://github.com/Alwatr/eslib/commit/898aa6ed0888eab9265c83b96a50f1b8c216d143))
438
- - **packages:** duplicate alwatr keyword ([77d4aa2](https://github.com/Alwatr/eslib/commit/77d4aa2105ad47515c3eee251fd6b8c281d0d1fc))
439
-
440
- # [0.5.0](https://github.com/Alwatr/eslib/compare/v0.4.0...v0.5.0) (2022-03-11)
441
-
442
- **Note:** Version bump only for package @alwatr/fetch
443
-
444
- # [0.4.0](https://github.com/Alwatr/eslib/compare/v0.3.0...v0.4.0) (2022-03-11)
445
-
446
- ### Bug Fixes
447
-
448
- - **fetch:** build issue [#73](https://github.com/Alwatr/eslib/issues/73) ([fb74463](https://github.com/Alwatr/eslib/commit/fb74463d367393706d16e482488a565bdfef70a1))
449
-
450
- # [0.3.0](https://github.com/Alwatr/eslib/compare/v0.2.1...v0.3.0) (2022-03-06)
451
-
452
- **Note:** Version bump only for package @alwatr/fetch
453
-
454
- ## [0.2.1](https://github.com/Alwatr/eslib/compare/v0.2.0...v0.2.1) (2022-03-05)
455
-
456
- **Note:** Version bump only for package @alwatr/fetch
457
-
458
- # [0.2.0](https://github.com/Alwatr/eslib/compare/v0.1.2...v0.2.0) (2022-03-05)
459
-
460
- ### Bug Fixes
461
-
462
- - **fetch:** error codes ([4e0be80](https://github.com/Alwatr/eslib/commit/4e0be80786b6667035ae82750f2351bd2da4f341))
463
-
464
- ### Features
20
+ ### Code Refactoring
465
21
 
466
- - **fetch:** add getData ([4a627c4](https://github.com/Alwatr/eslib/commit/4a627c43d2ad07acb340f058f02d41ce57288405))
467
- - **fetch:** add postData and fetchJson ([b0262b8](https://github.com/Alwatr/eslib/commit/b0262b83b5b7d8b7c376dce68aae3e16bd8e2610))
468
- - **fetch:** base fetch api ([925cdab](https://github.com/Alwatr/eslib/commit/925cdab8700ffb801e1d836ffc5d3245b66815cf))
469
- - **fetch:** FetchOptions type ([dd2d141](https://github.com/Alwatr/eslib/commit/dd2d1418d090387e1afa8277f78ae88ace17fd17))
470
- - **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,SAC5BA,EAAQ,UAARA,EAAQ,QAAY,CAAC,GAEjBA,EAAQ,gBAAkB,gBAAkBT,IAAmB,KACjEM,EAAO,WAAW,QAAS,mCAAoC,CAC7D,eAAAN,CACF,CAA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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