@microlink/mql 0.10.13 → 0.10.14
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/dist/mql.js +397 -495
- package/dist/mql.js.map +1 -1
- package/dist/mql.min.js +2 -3
- package/dist/mql.min.js.map +1 -1
- package/package.json +2 -2
- package/src/browser.js +1 -1
- package/src/ky.js +401 -499
package/src/ky.js
CHANGED
|
@@ -1,500 +1,402 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}(this, (function () { 'use strict';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
if (this._options.throwHttpErrors) {
|
|
405
|
-
throw error;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
async _fetch() {
|
|
411
|
-
for (const hook of this._options.hooks.beforeRequest) {
|
|
412
|
-
// eslint-disable-next-line no-await-in-loop
|
|
413
|
-
const result = await hook(this.request, this._options);
|
|
414
|
-
|
|
415
|
-
if (result instanceof Request) {
|
|
416
|
-
this.request = result;
|
|
417
|
-
break;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
if (result instanceof Response) {
|
|
421
|
-
return result;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
if (this._options.timeout === false) {
|
|
426
|
-
return this._options.fetch(this.request.clone());
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
return timeout(this.request.clone(), this.abortController, this._options);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/* istanbul ignore next */
|
|
433
|
-
_stream(response, onDownloadProgress) {
|
|
434
|
-
const totalBytes = Number(response.headers.get('content-length')) || 0;
|
|
435
|
-
let transferredBytes = 0;
|
|
436
|
-
|
|
437
|
-
return new globalThis.Response(
|
|
438
|
-
new globalThis.ReadableStream({
|
|
439
|
-
async start(controller) {
|
|
440
|
-
const reader = response.body.getReader();
|
|
441
|
-
|
|
442
|
-
if (onDownloadProgress) {
|
|
443
|
-
onDownloadProgress({percent: 0, transferredBytes: 0, totalBytes}, new Uint8Array());
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
async function read() {
|
|
447
|
-
const {done, value} = await reader.read();
|
|
448
|
-
if (done) {
|
|
449
|
-
controller.close();
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
if (onDownloadProgress) {
|
|
454
|
-
transferredBytes += value.byteLength;
|
|
455
|
-
const percent = totalBytes === 0 ? 0 : transferredBytes / totalBytes;
|
|
456
|
-
onDownloadProgress({percent, transferredBytes, totalBytes}, value);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
controller.enqueue(value);
|
|
460
|
-
await read();
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
await read();
|
|
464
|
-
}
|
|
465
|
-
})
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
const validateAndMerge = (...sources) => {
|
|
471
|
-
for (const source of sources) {
|
|
472
|
-
if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {
|
|
473
|
-
throw new TypeError('The `options` argument must be an object');
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
return deepMerge({}, ...sources);
|
|
478
|
-
};
|
|
479
|
-
|
|
480
|
-
const createInstance = defaults => {
|
|
481
|
-
const ky = (input, options) => new Ky(input, validateAndMerge(defaults, options));
|
|
482
|
-
|
|
483
|
-
for (const method of requestMethods) {
|
|
484
|
-
ky[method] = (input, options) => new Ky(input, validateAndMerge(defaults, options, {method}));
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
ky.HTTPError = HTTPError;
|
|
488
|
-
ky.TimeoutError = TimeoutError;
|
|
489
|
-
ky.create = newDefaults => createInstance(validateAndMerge(newDefaults));
|
|
490
|
-
ky.extend = newDefaults => createInstance(validateAndMerge(defaults, newDefaults));
|
|
491
|
-
ky.stop = stop;
|
|
492
|
-
|
|
493
|
-
return ky;
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
const ky = createInstance();
|
|
497
|
-
|
|
498
|
-
return ky;
|
|
499
|
-
|
|
500
|
-
})));
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ky = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
// eslint-lint-disable-next-line @typescript-eslint/naming-convention
|
|
8
|
+
class HTTPError extends Error {
|
|
9
|
+
constructor(response, request, options) {
|
|
10
|
+
const code = (response.status || response.status === 0) ? response.status : '';
|
|
11
|
+
const title = response.statusText || '';
|
|
12
|
+
const status = `${code} ${title}`.trim();
|
|
13
|
+
const reason = status ? `status code ${status}` : 'an unknown error';
|
|
14
|
+
super(`Request failed with ${reason}`);
|
|
15
|
+
this.name = 'HTTPError';
|
|
16
|
+
this.response = response;
|
|
17
|
+
this.request = request;
|
|
18
|
+
this.options = options;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class TimeoutError extends Error {
|
|
23
|
+
constructor(request) {
|
|
24
|
+
super('Request timed out');
|
|
25
|
+
this.name = 'TimeoutError';
|
|
26
|
+
this.request = request;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
31
|
+
const isObject = (value) => value !== null && typeof value === 'object';
|
|
32
|
+
|
|
33
|
+
const validateAndMerge = (...sources) => {
|
|
34
|
+
for (const source of sources) {
|
|
35
|
+
if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {
|
|
36
|
+
throw new TypeError('The `options` argument must be an object');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return deepMerge({}, ...sources);
|
|
40
|
+
};
|
|
41
|
+
const mergeHeaders = (source1 = {}, source2 = {}) => {
|
|
42
|
+
const result = new globalThis.Headers(source1);
|
|
43
|
+
const isHeadersInstance = source2 instanceof globalThis.Headers;
|
|
44
|
+
const source = new globalThis.Headers(source2);
|
|
45
|
+
for (const [key, value] of source.entries()) {
|
|
46
|
+
if ((isHeadersInstance && value === 'undefined') || value === undefined) {
|
|
47
|
+
result.delete(key);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
result.set(key, value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
// TODO: Make this strongly-typed (no `any`).
|
|
56
|
+
const deepMerge = (...sources) => {
|
|
57
|
+
let returnValue = {};
|
|
58
|
+
let headers = {};
|
|
59
|
+
for (const source of sources) {
|
|
60
|
+
if (Array.isArray(source)) {
|
|
61
|
+
if (!Array.isArray(returnValue)) {
|
|
62
|
+
returnValue = [];
|
|
63
|
+
}
|
|
64
|
+
returnValue = [...returnValue, ...source];
|
|
65
|
+
}
|
|
66
|
+
else if (isObject(source)) {
|
|
67
|
+
for (let [key, value] of Object.entries(source)) {
|
|
68
|
+
if (isObject(value) && key in returnValue) {
|
|
69
|
+
value = deepMerge(returnValue[key], value);
|
|
70
|
+
}
|
|
71
|
+
returnValue = { ...returnValue, [key]: value };
|
|
72
|
+
}
|
|
73
|
+
if (isObject(source.headers)) {
|
|
74
|
+
headers = mergeHeaders(headers, source.headers);
|
|
75
|
+
returnValue.headers = headers;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return returnValue;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const supportsAbortController = typeof globalThis.AbortController === 'function';
|
|
83
|
+
const supportsStreams = typeof globalThis.ReadableStream === 'function';
|
|
84
|
+
const supportsFormData = typeof globalThis.FormData === 'function';
|
|
85
|
+
const requestMethods = ['get', 'post', 'put', 'patch', 'head', 'delete'];
|
|
86
|
+
const responseTypes = {
|
|
87
|
+
json: 'application/json',
|
|
88
|
+
text: 'text/*',
|
|
89
|
+
formData: 'multipart/form-data',
|
|
90
|
+
arrayBuffer: '*/*',
|
|
91
|
+
blob: '*/*',
|
|
92
|
+
};
|
|
93
|
+
// The maximum value of a 32bit int (see issue #117)
|
|
94
|
+
const maxSafeTimeout = 2147483647;
|
|
95
|
+
const stop = Symbol('stop');
|
|
96
|
+
|
|
97
|
+
const normalizeRequestMethod = (input) => requestMethods.includes(input) ? input.toUpperCase() : input;
|
|
98
|
+
const retryMethods = ['get', 'put', 'head', 'delete', 'options', 'trace'];
|
|
99
|
+
const retryStatusCodes = [408, 413, 429, 500, 502, 503, 504];
|
|
100
|
+
const retryAfterStatusCodes = [413, 429, 503];
|
|
101
|
+
const defaultRetryOptions = {
|
|
102
|
+
limit: 2,
|
|
103
|
+
methods: retryMethods,
|
|
104
|
+
statusCodes: retryStatusCodes,
|
|
105
|
+
afterStatusCodes: retryAfterStatusCodes,
|
|
106
|
+
maxRetryAfter: Number.POSITIVE_INFINITY,
|
|
107
|
+
};
|
|
108
|
+
const normalizeRetryOptions = (retry = {}) => {
|
|
109
|
+
if (typeof retry === 'number') {
|
|
110
|
+
return {
|
|
111
|
+
...defaultRetryOptions,
|
|
112
|
+
limit: retry,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (retry.methods && !Array.isArray(retry.methods)) {
|
|
116
|
+
throw new Error('retry.methods must be an array');
|
|
117
|
+
}
|
|
118
|
+
if (retry.statusCodes && !Array.isArray(retry.statusCodes)) {
|
|
119
|
+
throw new Error('retry.statusCodes must be an array');
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
...defaultRetryOptions,
|
|
123
|
+
...retry,
|
|
124
|
+
afterStatusCodes: retryAfterStatusCodes,
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// `Promise.race()` workaround (#91)
|
|
129
|
+
const timeout = async (request, abortController, options) => new Promise((resolve, reject) => {
|
|
130
|
+
const timeoutId = setTimeout(() => {
|
|
131
|
+
if (abortController) {
|
|
132
|
+
abortController.abort();
|
|
133
|
+
}
|
|
134
|
+
reject(new TimeoutError(request));
|
|
135
|
+
}, options.timeout);
|
|
136
|
+
/* eslint-disable promise/prefer-await-to-then */
|
|
137
|
+
void options
|
|
138
|
+
.fetch(request)
|
|
139
|
+
.then(resolve)
|
|
140
|
+
.catch(reject)
|
|
141
|
+
.then(() => {
|
|
142
|
+
clearTimeout(timeoutId);
|
|
143
|
+
});
|
|
144
|
+
/* eslint-enable promise/prefer-await-to-then */
|
|
145
|
+
});
|
|
146
|
+
const delay = async (ms) => new Promise(resolve => {
|
|
147
|
+
setTimeout(resolve, ms);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
class Ky {
|
|
151
|
+
// eslint-disable-next-line complexity
|
|
152
|
+
constructor(input, options = {}) {
|
|
153
|
+
var _a, _b;
|
|
154
|
+
this._retryCount = 0;
|
|
155
|
+
this._input = input;
|
|
156
|
+
this._options = {
|
|
157
|
+
// TODO: credentials can be removed when the spec change is implemented in all browsers. Context: https://www.chromestatus.com/feature/4539473312350208
|
|
158
|
+
credentials: this._input.credentials || 'same-origin',
|
|
159
|
+
...options,
|
|
160
|
+
headers: mergeHeaders(this._input.headers, options.headers),
|
|
161
|
+
hooks: deepMerge({
|
|
162
|
+
beforeRequest: [],
|
|
163
|
+
beforeRetry: [],
|
|
164
|
+
afterResponse: [],
|
|
165
|
+
}, options.hooks),
|
|
166
|
+
method: normalizeRequestMethod((_a = options.method) !== null && _a !== void 0 ? _a : this._input.method),
|
|
167
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
168
|
+
prefixUrl: String(options.prefixUrl || ''),
|
|
169
|
+
retry: normalizeRetryOptions(options.retry),
|
|
170
|
+
throwHttpErrors: options.throwHttpErrors !== false,
|
|
171
|
+
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout,
|
|
172
|
+
fetch: (_b = options.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch.bind(globalThis),
|
|
173
|
+
};
|
|
174
|
+
if (typeof this._input !== 'string' && !(this._input instanceof URL || this._input instanceof globalThis.Request)) {
|
|
175
|
+
throw new TypeError('`input` must be a string, URL, or Request');
|
|
176
|
+
}
|
|
177
|
+
if (this._options.prefixUrl && typeof this._input === 'string') {
|
|
178
|
+
if (this._input.startsWith('/')) {
|
|
179
|
+
throw new Error('`input` must not begin with a slash when using `prefixUrl`');
|
|
180
|
+
}
|
|
181
|
+
if (!this._options.prefixUrl.endsWith('/')) {
|
|
182
|
+
this._options.prefixUrl += '/';
|
|
183
|
+
}
|
|
184
|
+
this._input = this._options.prefixUrl + this._input;
|
|
185
|
+
}
|
|
186
|
+
if (supportsAbortController) {
|
|
187
|
+
this.abortController = new globalThis.AbortController();
|
|
188
|
+
if (this._options.signal) {
|
|
189
|
+
this._options.signal.addEventListener('abort', () => {
|
|
190
|
+
this.abortController.abort();
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
this._options.signal = this.abortController.signal;
|
|
194
|
+
}
|
|
195
|
+
this.request = new globalThis.Request(this._input, this._options);
|
|
196
|
+
if (this._options.searchParams) {
|
|
197
|
+
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
198
|
+
const textSearchParams = typeof this._options.searchParams === 'string'
|
|
199
|
+
? this._options.searchParams.replace(/^\?/, '')
|
|
200
|
+
: new URLSearchParams(this._options.searchParams).toString();
|
|
201
|
+
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
202
|
+
const searchParams = '?' + textSearchParams;
|
|
203
|
+
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);
|
|
204
|
+
// To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one
|
|
205
|
+
if (((supportsFormData && this._options.body instanceof globalThis.FormData)
|
|
206
|
+
|| this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers['content-type'])) {
|
|
207
|
+
this.request.headers.delete('content-type');
|
|
208
|
+
}
|
|
209
|
+
this.request = new globalThis.Request(new globalThis.Request(url, this.request), this._options);
|
|
210
|
+
}
|
|
211
|
+
if (this._options.json !== undefined) {
|
|
212
|
+
this._options.body = JSON.stringify(this._options.json);
|
|
213
|
+
this.request.headers.set('content-type', 'application/json');
|
|
214
|
+
this.request = new globalThis.Request(this.request, { body: this._options.body });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
218
|
+
static create(input, options) {
|
|
219
|
+
const ky = new Ky(input, options);
|
|
220
|
+
const fn = async () => {
|
|
221
|
+
if (ky._options.timeout > maxSafeTimeout) {
|
|
222
|
+
throw new RangeError(`The \`timeout\` option cannot be greater than ${maxSafeTimeout}`);
|
|
223
|
+
}
|
|
224
|
+
// Delay the fetch so that body method shortcuts can set the Accept header
|
|
225
|
+
await Promise.resolve();
|
|
226
|
+
let response = await ky._fetch();
|
|
227
|
+
for (const hook of ky._options.hooks.afterResponse) {
|
|
228
|
+
// eslint-disable-next-line no-await-in-loop
|
|
229
|
+
const modifiedResponse = await hook(ky.request, ky._options, ky._decorateResponse(response.clone()));
|
|
230
|
+
if (modifiedResponse instanceof globalThis.Response) {
|
|
231
|
+
response = modifiedResponse;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
ky._decorateResponse(response);
|
|
235
|
+
if (!response.ok && ky._options.throwHttpErrors) {
|
|
236
|
+
throw new HTTPError(response, ky.request, ky._options);
|
|
237
|
+
}
|
|
238
|
+
// If `onDownloadProgress` is passed, it uses the stream API internally
|
|
239
|
+
/* istanbul ignore next */
|
|
240
|
+
if (ky._options.onDownloadProgress) {
|
|
241
|
+
if (typeof ky._options.onDownloadProgress !== 'function') {
|
|
242
|
+
throw new TypeError('The `onDownloadProgress` option must be a function');
|
|
243
|
+
}
|
|
244
|
+
if (!supportsStreams) {
|
|
245
|
+
throw new Error('Streams are not supported in your environment. `ReadableStream` is missing.');
|
|
246
|
+
}
|
|
247
|
+
return ky._stream(response.clone(), ky._options.onDownloadProgress);
|
|
248
|
+
}
|
|
249
|
+
return response;
|
|
250
|
+
};
|
|
251
|
+
const isRetriableMethod = ky._options.retry.methods.includes(ky.request.method.toLowerCase());
|
|
252
|
+
const result = (isRetriableMethod ? ky._retry(fn) : fn());
|
|
253
|
+
for (const [type, mimeType] of Object.entries(responseTypes)) {
|
|
254
|
+
result[type] = async () => {
|
|
255
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
256
|
+
ky.request.headers.set('accept', ky.request.headers.get('accept') || mimeType);
|
|
257
|
+
const response = (await result).clone();
|
|
258
|
+
if (type === 'json') {
|
|
259
|
+
if (response.status === 204) {
|
|
260
|
+
return '';
|
|
261
|
+
}
|
|
262
|
+
if (options.parseJson) {
|
|
263
|
+
return options.parseJson(await response.text());
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return response[type]();
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
_calculateRetryDelay(error) {
|
|
272
|
+
this._retryCount++;
|
|
273
|
+
if (this._retryCount < this._options.retry.limit && !(error instanceof TimeoutError)) {
|
|
274
|
+
if (error instanceof HTTPError) {
|
|
275
|
+
if (!this._options.retry.statusCodes.includes(error.response.status)) {
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
const retryAfter = error.response.headers.get('Retry-After');
|
|
279
|
+
if (retryAfter && this._options.retry.afterStatusCodes.includes(error.response.status)) {
|
|
280
|
+
let after = Number(retryAfter);
|
|
281
|
+
if (Number.isNaN(after)) {
|
|
282
|
+
after = Date.parse(retryAfter) - Date.now();
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
after *= 1000;
|
|
286
|
+
}
|
|
287
|
+
if (typeof this._options.retry.maxRetryAfter !== 'undefined' && after > this._options.retry.maxRetryAfter) {
|
|
288
|
+
return 0;
|
|
289
|
+
}
|
|
290
|
+
return after;
|
|
291
|
+
}
|
|
292
|
+
if (error.response.status === 413) {
|
|
293
|
+
return 0;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const BACKOFF_FACTOR = 0.3;
|
|
297
|
+
return BACKOFF_FACTOR * (2 ** (this._retryCount - 1)) * 1000;
|
|
298
|
+
}
|
|
299
|
+
return 0;
|
|
300
|
+
}
|
|
301
|
+
_decorateResponse(response) {
|
|
302
|
+
if (this._options.parseJson) {
|
|
303
|
+
response.json = async () => this._options.parseJson(await response.text());
|
|
304
|
+
}
|
|
305
|
+
return response;
|
|
306
|
+
}
|
|
307
|
+
async _retry(fn) {
|
|
308
|
+
try {
|
|
309
|
+
return await fn();
|
|
310
|
+
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
const ms = Math.min(this._calculateRetryDelay(error), maxSafeTimeout);
|
|
314
|
+
if (ms !== 0 && this._retryCount > 0) {
|
|
315
|
+
await delay(ms);
|
|
316
|
+
for (const hook of this._options.hooks.beforeRetry) {
|
|
317
|
+
// eslint-disable-next-line no-await-in-loop
|
|
318
|
+
const hookResult = await hook({
|
|
319
|
+
request: this.request,
|
|
320
|
+
options: this._options,
|
|
321
|
+
error: error,
|
|
322
|
+
retryCount: this._retryCount,
|
|
323
|
+
});
|
|
324
|
+
// If `stop` is returned from the hook, the retry process is stopped
|
|
325
|
+
if (hookResult === stop) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return this._retry(fn);
|
|
330
|
+
}
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async _fetch() {
|
|
335
|
+
for (const hook of this._options.hooks.beforeRequest) {
|
|
336
|
+
// eslint-disable-next-line no-await-in-loop
|
|
337
|
+
const result = await hook(this.request, this._options);
|
|
338
|
+
if (result instanceof Request) {
|
|
339
|
+
this.request = result;
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
if (result instanceof Response) {
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (this._options.timeout === false) {
|
|
347
|
+
return this._options.fetch(this.request.clone());
|
|
348
|
+
}
|
|
349
|
+
return timeout(this.request.clone(), this.abortController, this._options);
|
|
350
|
+
}
|
|
351
|
+
/* istanbul ignore next */
|
|
352
|
+
_stream(response, onDownloadProgress) {
|
|
353
|
+
const totalBytes = Number(response.headers.get('content-length')) || 0;
|
|
354
|
+
let transferredBytes = 0;
|
|
355
|
+
return new globalThis.Response(new globalThis.ReadableStream({
|
|
356
|
+
async start(controller) {
|
|
357
|
+
const reader = response.body.getReader();
|
|
358
|
+
if (onDownloadProgress) {
|
|
359
|
+
onDownloadProgress({ percent: 0, transferredBytes: 0, totalBytes }, new Uint8Array());
|
|
360
|
+
}
|
|
361
|
+
async function read() {
|
|
362
|
+
const { done, value } = await reader.read();
|
|
363
|
+
if (done) {
|
|
364
|
+
controller.close();
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (onDownloadProgress) {
|
|
368
|
+
transferredBytes += value.byteLength;
|
|
369
|
+
const percent = totalBytes === 0 ? 0 : transferredBytes / totalBytes;
|
|
370
|
+
onDownloadProgress({ percent, transferredBytes, totalBytes }, value);
|
|
371
|
+
}
|
|
372
|
+
controller.enqueue(value);
|
|
373
|
+
await read();
|
|
374
|
+
}
|
|
375
|
+
await read();
|
|
376
|
+
},
|
|
377
|
+
}));
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/*! MIT License © Sindre Sorhus */
|
|
382
|
+
const createInstance = (defaults) => {
|
|
383
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
384
|
+
const ky = (input, options) => Ky.create(input, validateAndMerge(defaults, options));
|
|
385
|
+
for (const method of requestMethods) {
|
|
386
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
387
|
+
ky[method] = (input, options) => Ky.create(input, validateAndMerge(defaults, options, { method }));
|
|
388
|
+
}
|
|
389
|
+
ky.create = (newDefaults) => createInstance(validateAndMerge(newDefaults));
|
|
390
|
+
ky.extend = (newDefaults) => createInstance(validateAndMerge(defaults, newDefaults));
|
|
391
|
+
ky.stop = stop;
|
|
392
|
+
return ky;
|
|
393
|
+
};
|
|
394
|
+
const ky = createInstance();
|
|
395
|
+
|
|
396
|
+
exports.HTTPError = HTTPError;
|
|
397
|
+
exports.TimeoutError = TimeoutError;
|
|
398
|
+
exports["default"] = ky;
|
|
399
|
+
|
|
400
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
401
|
+
|
|
402
|
+
}));
|