@microlink/mql 0.10.11 → 0.10.15
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 +406 -501
- package/dist/mql.js.map +1 -1
- package/dist/mql.min.js +2 -3
- package/dist/mql.min.js.map +1 -1
- package/index.d.ts +1 -1
- package/package.json +8 -4
- package/src/browser.js +6 -3
- package/src/factory.js +4 -4
- package/src/ky.js +401 -499
- package/CHANGELOG.md +0 -665
package/dist/mql.js
CHANGED
|
@@ -349,10 +349,10 @@
|
|
|
349
349
|
const mapRules = rules => {
|
|
350
350
|
if (!isObject(rules)) return
|
|
351
351
|
const flatRules = flatten(rules);
|
|
352
|
-
return Object.keys(flatRules).reduce(
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
)
|
|
352
|
+
return Object.keys(flatRules).reduce((acc, key) => {
|
|
353
|
+
acc[`data.${key}`] = flatRules[key].toString();
|
|
354
|
+
return acc
|
|
355
|
+
}, {})
|
|
356
356
|
};
|
|
357
357
|
|
|
358
358
|
const fetchFromApi = async (apiUrl, opts = {}, retryCount = 0) => {
|
|
@@ -432,503 +432,405 @@
|
|
|
432
432
|
|
|
433
433
|
(function (module, exports) {
|
|
434
434
|
(function (global, factory) {
|
|
435
|
-
|
|
436
|
-
}(commonjsGlobal, (function () {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
result.delete(key);
|
|
452
|
-
} else {
|
|
453
|
-
result.set(key, value);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
return result;
|
|
458
|
-
};
|
|
459
|
-
|
|
460
|
-
const deepMerge = (...sources) => {
|
|
461
|
-
let returnValue = {};
|
|
462
|
-
let headers = {};
|
|
463
|
-
|
|
464
|
-
for (const source of sources) {
|
|
465
|
-
if (Array.isArray(source)) {
|
|
466
|
-
if (!(Array.isArray(returnValue))) {
|
|
467
|
-
returnValue = [];
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
returnValue = [...returnValue, ...source];
|
|
471
|
-
} else if (isObject(source)) {
|
|
472
|
-
for (let [key, value] of Object.entries(source)) {
|
|
473
|
-
if (isObject(value) && (key in returnValue)) {
|
|
474
|
-
value = deepMerge(returnValue[key], value);
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
returnValue = {...returnValue, [key]: value};
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (isObject(source.headers)) {
|
|
481
|
-
headers = mergeHeaders(headers, source.headers);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
returnValue.headers = headers;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
return returnValue;
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
const requestMethods = [
|
|
492
|
-
'get',
|
|
493
|
-
'post',
|
|
494
|
-
'put',
|
|
495
|
-
'patch',
|
|
496
|
-
'head',
|
|
497
|
-
'delete'
|
|
498
|
-
];
|
|
499
|
-
|
|
500
|
-
const responseTypes = {
|
|
501
|
-
json: 'application/json',
|
|
502
|
-
text: 'text/*',
|
|
503
|
-
formData: 'multipart/form-data',
|
|
504
|
-
arrayBuffer: '*/*',
|
|
505
|
-
blob: '*/*'
|
|
506
|
-
};
|
|
507
|
-
|
|
508
|
-
const retryMethods = [
|
|
509
|
-
'get',
|
|
510
|
-
'put',
|
|
511
|
-
'head',
|
|
512
|
-
'delete',
|
|
513
|
-
'options',
|
|
514
|
-
'trace'
|
|
515
|
-
];
|
|
516
|
-
|
|
517
|
-
const retryStatusCodes = [
|
|
518
|
-
408,
|
|
519
|
-
413,
|
|
520
|
-
429,
|
|
521
|
-
500,
|
|
522
|
-
502,
|
|
523
|
-
503,
|
|
524
|
-
504
|
|
525
|
-
];
|
|
526
|
-
|
|
527
|
-
const retryAfterStatusCodes = [
|
|
528
|
-
413,
|
|
529
|
-
429,
|
|
530
|
-
503
|
|
531
|
-
];
|
|
532
|
-
|
|
533
|
-
const stop = Symbol('stop');
|
|
534
|
-
|
|
535
|
-
class HTTPError extends Error {
|
|
536
|
-
constructor(response, request, options) {
|
|
537
|
-
// Set the message to the status text, such as Unauthorized,
|
|
538
|
-
// with some fallbacks. This message should never be undefined.
|
|
539
|
-
super(
|
|
540
|
-
response.statusText ||
|
|
541
|
-
String(
|
|
542
|
-
(response.status === 0 || response.status) ?
|
|
543
|
-
response.status : 'Unknown response error'
|
|
544
|
-
)
|
|
545
|
-
);
|
|
546
|
-
this.name = 'HTTPError';
|
|
547
|
-
this.response = response;
|
|
548
|
-
this.request = request;
|
|
549
|
-
this.options = options;
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
class TimeoutError extends Error {
|
|
554
|
-
constructor(request) {
|
|
555
|
-
super('Request timed out');
|
|
556
|
-
this.name = 'TimeoutError';
|
|
557
|
-
this.request = request;
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
562
|
-
|
|
563
|
-
// `Promise.race()` workaround (#91)
|
|
564
|
-
const timeout = (request, abortController, options) =>
|
|
565
|
-
new Promise((resolve, reject) => {
|
|
566
|
-
const timeoutID = setTimeout(() => {
|
|
567
|
-
if (abortController) {
|
|
568
|
-
abortController.abort();
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
reject(new TimeoutError(request));
|
|
572
|
-
}, options.timeout);
|
|
573
|
-
|
|
574
|
-
/* eslint-disable promise/prefer-await-to-then */
|
|
575
|
-
options.fetch(request)
|
|
576
|
-
.then(resolve)
|
|
577
|
-
.catch(reject)
|
|
578
|
-
.then(() => {
|
|
579
|
-
clearTimeout(timeoutID);
|
|
580
|
-
});
|
|
581
|
-
/* eslint-enable promise/prefer-await-to-then */
|
|
582
|
-
});
|
|
583
|
-
|
|
584
|
-
const normalizeRequestMethod = input => requestMethods.includes(input) ? input.toUpperCase() : input;
|
|
585
|
-
|
|
586
|
-
const defaultRetryOptions = {
|
|
587
|
-
limit: 2,
|
|
588
|
-
methods: retryMethods,
|
|
589
|
-
statusCodes: retryStatusCodes,
|
|
590
|
-
afterStatusCodes: retryAfterStatusCodes
|
|
591
|
-
};
|
|
592
|
-
|
|
593
|
-
const normalizeRetryOptions = (retry = {}) => {
|
|
594
|
-
if (typeof retry === 'number') {
|
|
595
|
-
return {
|
|
596
|
-
...defaultRetryOptions,
|
|
597
|
-
limit: retry
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
if (retry.methods && !Array.isArray(retry.methods)) {
|
|
602
|
-
throw new Error('retry.methods must be an array');
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
if (retry.statusCodes && !Array.isArray(retry.statusCodes)) {
|
|
606
|
-
throw new Error('retry.statusCodes must be an array');
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
return {
|
|
610
|
-
...defaultRetryOptions,
|
|
611
|
-
...retry,
|
|
612
|
-
afterStatusCodes: retryAfterStatusCodes
|
|
613
|
-
};
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
// The maximum value of a 32bit int (see issue #117)
|
|
617
|
-
const maxSafeTimeout = 2147483647;
|
|
618
|
-
|
|
619
|
-
class Ky {
|
|
620
|
-
constructor(input, options = {}) {
|
|
621
|
-
this._retryCount = 0;
|
|
622
|
-
this._input = input;
|
|
623
|
-
this._options = {
|
|
624
|
-
// TODO: credentials can be removed when the spec change is implemented in all browsers. Context: https://www.chromestatus.com/feature/4539473312350208
|
|
625
|
-
credentials: this._input.credentials || 'same-origin',
|
|
626
|
-
...options,
|
|
627
|
-
headers: mergeHeaders(this._input.headers, options.headers),
|
|
628
|
-
hooks: deepMerge({
|
|
629
|
-
beforeRequest: [],
|
|
630
|
-
beforeRetry: [],
|
|
631
|
-
afterResponse: []
|
|
632
|
-
}, options.hooks),
|
|
633
|
-
method: normalizeRequestMethod(options.method || this._input.method),
|
|
634
|
-
prefixUrl: String(options.prefixUrl || ''),
|
|
635
|
-
retry: normalizeRetryOptions(options.retry),
|
|
636
|
-
throwHttpErrors: options.throwHttpErrors !== false,
|
|
637
|
-
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout,
|
|
638
|
-
fetch: options.fetch || globalThis.fetch.bind(globalThis)
|
|
639
|
-
};
|
|
640
|
-
|
|
641
|
-
if (typeof this._input !== 'string' && !(this._input instanceof URL || this._input instanceof globalThis.Request)) {
|
|
642
|
-
throw new TypeError('`input` must be a string, URL, or Request');
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
if (this._options.prefixUrl && typeof this._input === 'string') {
|
|
646
|
-
if (this._input.startsWith('/')) {
|
|
647
|
-
throw new Error('`input` must not begin with a slash when using `prefixUrl`');
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
if (!this._options.prefixUrl.endsWith('/')) {
|
|
651
|
-
this._options.prefixUrl += '/';
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
this._input = this._options.prefixUrl + this._input;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
if (supportsAbortController) {
|
|
658
|
-
this.abortController = new globalThis.AbortController();
|
|
659
|
-
if (this._options.signal) {
|
|
660
|
-
this._options.signal.addEventListener('abort', () => {
|
|
661
|
-
this.abortController.abort();
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
this._options.signal = this.abortController.signal;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
this.request = new globalThis.Request(this._input, this._options);
|
|
669
|
-
|
|
670
|
-
if (this._options.searchParams) {
|
|
671
|
-
const textSearchParams = typeof this._options.searchParams === 'string' ?
|
|
672
|
-
this._options.searchParams.replace(/^\?/, '') :
|
|
673
|
-
new URLSearchParams(this._options.searchParams).toString();
|
|
674
|
-
const searchParams = '?' + textSearchParams;
|
|
675
|
-
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);
|
|
676
|
-
|
|
677
|
-
// To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one
|
|
678
|
-
if (((supportsFormData && this._options.body instanceof globalThis.FormData) || this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers['content-type'])) {
|
|
679
|
-
this.request.headers.delete('content-type');
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
this.request = new globalThis.Request(new globalThis.Request(url, this.request), this._options);
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
if (this._options.json !== undefined) {
|
|
686
|
-
this._options.body = JSON.stringify(this._options.json);
|
|
687
|
-
this.request.headers.set('content-type', 'application/json');
|
|
688
|
-
this.request = new globalThis.Request(this.request, {body: this._options.body});
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
const fn = async () => {
|
|
692
|
-
if (this._options.timeout > maxSafeTimeout) {
|
|
693
|
-
throw new RangeError(`The \`timeout\` option cannot be greater than ${maxSafeTimeout}`);
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
await delay(1);
|
|
697
|
-
let response = await this._fetch();
|
|
698
|
-
|
|
699
|
-
for (const hook of this._options.hooks.afterResponse) {
|
|
700
|
-
// eslint-disable-next-line no-await-in-loop
|
|
701
|
-
const modifiedResponse = await hook(
|
|
702
|
-
this.request,
|
|
703
|
-
this._options,
|
|
704
|
-
this._decorateResponse(response.clone())
|
|
705
|
-
);
|
|
706
|
-
|
|
707
|
-
if (modifiedResponse instanceof globalThis.Response) {
|
|
708
|
-
response = modifiedResponse;
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
this._decorateResponse(response);
|
|
713
|
-
|
|
714
|
-
if (!response.ok && this._options.throwHttpErrors) {
|
|
715
|
-
throw new HTTPError(response, this.request, this._options);
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
// If `onDownloadProgress` is passed, it uses the stream API internally
|
|
719
|
-
/* istanbul ignore next */
|
|
720
|
-
if (this._options.onDownloadProgress) {
|
|
721
|
-
if (typeof this._options.onDownloadProgress !== 'function') {
|
|
722
|
-
throw new TypeError('The `onDownloadProgress` option must be a function');
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
if (!supportsStreams) {
|
|
726
|
-
throw new Error('Streams are not supported in your environment. `ReadableStream` is missing.');
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
return this._stream(response.clone(), this._options.onDownloadProgress);
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
return response;
|
|
733
|
-
};
|
|
734
|
-
|
|
735
|
-
const isRetriableMethod = this._options.retry.methods.includes(this.request.method.toLowerCase());
|
|
736
|
-
const result = isRetriableMethod ? this._retry(fn) : fn();
|
|
737
|
-
|
|
738
|
-
for (const [type, mimeType] of Object.entries(responseTypes)) {
|
|
739
|
-
result[type] = async () => {
|
|
740
|
-
this.request.headers.set('accept', this.request.headers.get('accept') || mimeType);
|
|
741
|
-
|
|
742
|
-
const response = (await result).clone();
|
|
743
|
-
|
|
744
|
-
if (type === 'json') {
|
|
745
|
-
if (response.status === 204) {
|
|
746
|
-
return '';
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
if (options.parseJson) {
|
|
750
|
-
return options.parseJson(await response.text());
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
return response[type]();
|
|
755
|
-
};
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
return result;
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
_calculateRetryDelay(error) {
|
|
762
|
-
this._retryCount++;
|
|
763
|
-
|
|
764
|
-
if (this._retryCount < this._options.retry.limit && !(error instanceof TimeoutError)) {
|
|
765
|
-
if (error instanceof HTTPError) {
|
|
766
|
-
if (!this._options.retry.statusCodes.includes(error.response.status)) {
|
|
767
|
-
return 0;
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
const retryAfter = error.response.headers.get('Retry-After');
|
|
771
|
-
if (retryAfter && this._options.retry.afterStatusCodes.includes(error.response.status)) {
|
|
772
|
-
let after = Number(retryAfter);
|
|
773
|
-
if (Number.isNaN(after)) {
|
|
774
|
-
after = Date.parse(retryAfter) - Date.now();
|
|
775
|
-
} else {
|
|
776
|
-
after *= 1000;
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
if (typeof this._options.retry.maxRetryAfter !== 'undefined' && after > this._options.retry.maxRetryAfter) {
|
|
780
|
-
return 0;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
return after;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
if (error.response.status === 413) {
|
|
787
|
-
return 0;
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
const BACKOFF_FACTOR = 0.3;
|
|
792
|
-
return BACKOFF_FACTOR * (2 ** (this._retryCount - 1)) * 1000;
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
return 0;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
_decorateResponse(response) {
|
|
799
|
-
if (this._options.parseJson) {
|
|
800
|
-
response.json = async () => {
|
|
801
|
-
return this._options.parseJson(await response.text());
|
|
802
|
-
};
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
return response;
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
async _retry(fn) {
|
|
809
|
-
try {
|
|
810
|
-
return await fn();
|
|
811
|
-
} catch (error) {
|
|
812
|
-
const ms = Math.min(this._calculateRetryDelay(error), maxSafeTimeout);
|
|
813
|
-
if (ms !== 0 && this._retryCount > 0) {
|
|
814
|
-
await delay(ms);
|
|
815
|
-
|
|
816
|
-
for (const hook of this._options.hooks.beforeRetry) {
|
|
817
|
-
// eslint-disable-next-line no-await-in-loop
|
|
818
|
-
const hookResult = await hook({
|
|
819
|
-
request: this.request,
|
|
820
|
-
options: this._options,
|
|
821
|
-
error,
|
|
822
|
-
retryCount: this._retryCount
|
|
823
|
-
});
|
|
824
|
-
|
|
825
|
-
// If `stop` is returned from the hook, the retry process is stopped
|
|
826
|
-
if (hookResult === stop) {
|
|
827
|
-
return;
|
|
828
|
-
}
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
return this._retry(fn);
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
if (this._options.throwHttpErrors) {
|
|
835
|
-
throw error;
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
async _fetch() {
|
|
841
|
-
for (const hook of this._options.hooks.beforeRequest) {
|
|
842
|
-
// eslint-disable-next-line no-await-in-loop
|
|
843
|
-
const result = await hook(this.request, this._options);
|
|
844
|
-
|
|
845
|
-
if (result instanceof Request) {
|
|
846
|
-
this.request = result;
|
|
847
|
-
break;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
if (result instanceof Response) {
|
|
851
|
-
return result;
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
if (this._options.timeout === false) {
|
|
856
|
-
return this._options.fetch(this.request.clone());
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
return timeout(this.request.clone(), this.abortController, this._options);
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
/* istanbul ignore next */
|
|
863
|
-
_stream(response, onDownloadProgress) {
|
|
864
|
-
const totalBytes = Number(response.headers.get('content-length')) || 0;
|
|
865
|
-
let transferredBytes = 0;
|
|
866
|
-
|
|
867
|
-
return new globalThis.Response(
|
|
868
|
-
new globalThis.ReadableStream({
|
|
869
|
-
async start(controller) {
|
|
870
|
-
const reader = response.body.getReader();
|
|
871
|
-
|
|
872
|
-
if (onDownloadProgress) {
|
|
873
|
-
onDownloadProgress({percent: 0, transferredBytes: 0, totalBytes}, new Uint8Array());
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
async function read() {
|
|
877
|
-
const {done, value} = await reader.read();
|
|
878
|
-
if (done) {
|
|
879
|
-
controller.close();
|
|
880
|
-
return;
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
if (onDownloadProgress) {
|
|
884
|
-
transferredBytes += value.byteLength;
|
|
885
|
-
const percent = totalBytes === 0 ? 0 : transferredBytes / totalBytes;
|
|
886
|
-
onDownloadProgress({percent, transferredBytes, totalBytes}, value);
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
controller.enqueue(value);
|
|
890
|
-
await read();
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
await read();
|
|
894
|
-
}
|
|
895
|
-
})
|
|
896
|
-
);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
const validateAndMerge = (...sources) => {
|
|
901
|
-
for (const source of sources) {
|
|
902
|
-
if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {
|
|
903
|
-
throw new TypeError('The `options` argument must be an object');
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
return deepMerge({}, ...sources);
|
|
908
|
-
};
|
|
909
|
-
|
|
910
|
-
const createInstance = defaults => {
|
|
911
|
-
const ky = (input, options) => new Ky(input, validateAndMerge(defaults, options));
|
|
912
|
-
|
|
913
|
-
for (const method of requestMethods) {
|
|
914
|
-
ky[method] = (input, options) => new Ky(input, validateAndMerge(defaults, options, {method}));
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
ky.HTTPError = HTTPError;
|
|
918
|
-
ky.TimeoutError = TimeoutError;
|
|
919
|
-
ky.create = newDefaults => createInstance(validateAndMerge(newDefaults));
|
|
920
|
-
ky.extend = newDefaults => createInstance(validateAndMerge(defaults, newDefaults));
|
|
921
|
-
ky.stop = stop;
|
|
435
|
+
factory(exports) ;
|
|
436
|
+
})(commonjsGlobal, (function (exports) {
|
|
437
|
+
// eslint-lint-disable-next-line @typescript-eslint/naming-convention
|
|
438
|
+
class HTTPError extends Error {
|
|
439
|
+
constructor(response, request, options) {
|
|
440
|
+
const code = (response.status || response.status === 0) ? response.status : '';
|
|
441
|
+
const title = response.statusText || '';
|
|
442
|
+
const status = `${code} ${title}`.trim();
|
|
443
|
+
const reason = status ? `status code ${status}` : 'an unknown error';
|
|
444
|
+
super(`Request failed with ${reason}`);
|
|
445
|
+
this.name = 'HTTPError';
|
|
446
|
+
this.response = response;
|
|
447
|
+
this.request = request;
|
|
448
|
+
this.options = options;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
922
451
|
|
|
923
|
-
|
|
924
|
-
|
|
452
|
+
class TimeoutError extends Error {
|
|
453
|
+
constructor(request) {
|
|
454
|
+
super('Request timed out');
|
|
455
|
+
this.name = 'TimeoutError';
|
|
456
|
+
this.request = request;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
925
459
|
|
|
926
|
-
|
|
460
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
461
|
+
const isObject = (value) => value !== null && typeof value === 'object';
|
|
462
|
+
|
|
463
|
+
const validateAndMerge = (...sources) => {
|
|
464
|
+
for (const source of sources) {
|
|
465
|
+
if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {
|
|
466
|
+
throw new TypeError('The `options` argument must be an object');
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return deepMerge({}, ...sources);
|
|
470
|
+
};
|
|
471
|
+
const mergeHeaders = (source1 = {}, source2 = {}) => {
|
|
472
|
+
const result = new globalThis.Headers(source1);
|
|
473
|
+
const isHeadersInstance = source2 instanceof globalThis.Headers;
|
|
474
|
+
const source = new globalThis.Headers(source2);
|
|
475
|
+
for (const [key, value] of source.entries()) {
|
|
476
|
+
if ((isHeadersInstance && value === 'undefined') || value === undefined) {
|
|
477
|
+
result.delete(key);
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
result.set(key, value);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return result;
|
|
484
|
+
};
|
|
485
|
+
// TODO: Make this strongly-typed (no `any`).
|
|
486
|
+
const deepMerge = (...sources) => {
|
|
487
|
+
let returnValue = {};
|
|
488
|
+
let headers = {};
|
|
489
|
+
for (const source of sources) {
|
|
490
|
+
if (Array.isArray(source)) {
|
|
491
|
+
if (!Array.isArray(returnValue)) {
|
|
492
|
+
returnValue = [];
|
|
493
|
+
}
|
|
494
|
+
returnValue = [...returnValue, ...source];
|
|
495
|
+
}
|
|
496
|
+
else if (isObject(source)) {
|
|
497
|
+
for (let [key, value] of Object.entries(source)) {
|
|
498
|
+
if (isObject(value) && key in returnValue) {
|
|
499
|
+
value = deepMerge(returnValue[key], value);
|
|
500
|
+
}
|
|
501
|
+
returnValue = { ...returnValue, [key]: value };
|
|
502
|
+
}
|
|
503
|
+
if (isObject(source.headers)) {
|
|
504
|
+
headers = mergeHeaders(headers, source.headers);
|
|
505
|
+
returnValue.headers = headers;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return returnValue;
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
const supportsAbortController = typeof globalThis.AbortController === 'function';
|
|
513
|
+
const supportsStreams = typeof globalThis.ReadableStream === 'function';
|
|
514
|
+
const supportsFormData = typeof globalThis.FormData === 'function';
|
|
515
|
+
const requestMethods = ['get', 'post', 'put', 'patch', 'head', 'delete'];
|
|
516
|
+
const responseTypes = {
|
|
517
|
+
json: 'application/json',
|
|
518
|
+
text: 'text/*',
|
|
519
|
+
formData: 'multipart/form-data',
|
|
520
|
+
arrayBuffer: '*/*',
|
|
521
|
+
blob: '*/*',
|
|
522
|
+
};
|
|
523
|
+
// The maximum value of a 32bit int (see issue #117)
|
|
524
|
+
const maxSafeTimeout = 2147483647;
|
|
525
|
+
const stop = Symbol('stop');
|
|
526
|
+
|
|
527
|
+
const normalizeRequestMethod = (input) => requestMethods.includes(input) ? input.toUpperCase() : input;
|
|
528
|
+
const retryMethods = ['get', 'put', 'head', 'delete', 'options', 'trace'];
|
|
529
|
+
const retryStatusCodes = [408, 413, 429, 500, 502, 503, 504];
|
|
530
|
+
const retryAfterStatusCodes = [413, 429, 503];
|
|
531
|
+
const defaultRetryOptions = {
|
|
532
|
+
limit: 2,
|
|
533
|
+
methods: retryMethods,
|
|
534
|
+
statusCodes: retryStatusCodes,
|
|
535
|
+
afterStatusCodes: retryAfterStatusCodes,
|
|
536
|
+
maxRetryAfter: Number.POSITIVE_INFINITY,
|
|
537
|
+
};
|
|
538
|
+
const normalizeRetryOptions = (retry = {}) => {
|
|
539
|
+
if (typeof retry === 'number') {
|
|
540
|
+
return {
|
|
541
|
+
...defaultRetryOptions,
|
|
542
|
+
limit: retry,
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
if (retry.methods && !Array.isArray(retry.methods)) {
|
|
546
|
+
throw new Error('retry.methods must be an array');
|
|
547
|
+
}
|
|
548
|
+
if (retry.statusCodes && !Array.isArray(retry.statusCodes)) {
|
|
549
|
+
throw new Error('retry.statusCodes must be an array');
|
|
550
|
+
}
|
|
551
|
+
return {
|
|
552
|
+
...defaultRetryOptions,
|
|
553
|
+
...retry,
|
|
554
|
+
afterStatusCodes: retryAfterStatusCodes,
|
|
555
|
+
};
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
// `Promise.race()` workaround (#91)
|
|
559
|
+
const timeout = async (request, abortController, options) => new Promise((resolve, reject) => {
|
|
560
|
+
const timeoutId = setTimeout(() => {
|
|
561
|
+
if (abortController) {
|
|
562
|
+
abortController.abort();
|
|
563
|
+
}
|
|
564
|
+
reject(new TimeoutError(request));
|
|
565
|
+
}, options.timeout);
|
|
566
|
+
/* eslint-disable promise/prefer-await-to-then */
|
|
567
|
+
void options
|
|
568
|
+
.fetch(request)
|
|
569
|
+
.then(resolve)
|
|
570
|
+
.catch(reject)
|
|
571
|
+
.then(() => {
|
|
572
|
+
clearTimeout(timeoutId);
|
|
573
|
+
});
|
|
574
|
+
/* eslint-enable promise/prefer-await-to-then */
|
|
575
|
+
});
|
|
576
|
+
const delay = async (ms) => new Promise(resolve => {
|
|
577
|
+
setTimeout(resolve, ms);
|
|
578
|
+
});
|
|
927
579
|
|
|
928
|
-
|
|
580
|
+
class Ky {
|
|
581
|
+
// eslint-disable-next-line complexity
|
|
582
|
+
constructor(input, options = {}) {
|
|
583
|
+
var _a, _b;
|
|
584
|
+
this._retryCount = 0;
|
|
585
|
+
this._input = input;
|
|
586
|
+
this._options = {
|
|
587
|
+
// TODO: credentials can be removed when the spec change is implemented in all browsers. Context: https://www.chromestatus.com/feature/4539473312350208
|
|
588
|
+
credentials: this._input.credentials || 'same-origin',
|
|
589
|
+
...options,
|
|
590
|
+
headers: mergeHeaders(this._input.headers, options.headers),
|
|
591
|
+
hooks: deepMerge({
|
|
592
|
+
beforeRequest: [],
|
|
593
|
+
beforeRetry: [],
|
|
594
|
+
afterResponse: [],
|
|
595
|
+
}, options.hooks),
|
|
596
|
+
method: normalizeRequestMethod((_a = options.method) !== null && _a !== void 0 ? _a : this._input.method),
|
|
597
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
598
|
+
prefixUrl: String(options.prefixUrl || ''),
|
|
599
|
+
retry: normalizeRetryOptions(options.retry),
|
|
600
|
+
throwHttpErrors: options.throwHttpErrors !== false,
|
|
601
|
+
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout,
|
|
602
|
+
fetch: (_b = options.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch.bind(globalThis),
|
|
603
|
+
};
|
|
604
|
+
if (typeof this._input !== 'string' && !(this._input instanceof URL || this._input instanceof globalThis.Request)) {
|
|
605
|
+
throw new TypeError('`input` must be a string, URL, or Request');
|
|
606
|
+
}
|
|
607
|
+
if (this._options.prefixUrl && typeof this._input === 'string') {
|
|
608
|
+
if (this._input.startsWith('/')) {
|
|
609
|
+
throw new Error('`input` must not begin with a slash when using `prefixUrl`');
|
|
610
|
+
}
|
|
611
|
+
if (!this._options.prefixUrl.endsWith('/')) {
|
|
612
|
+
this._options.prefixUrl += '/';
|
|
613
|
+
}
|
|
614
|
+
this._input = this._options.prefixUrl + this._input;
|
|
615
|
+
}
|
|
616
|
+
if (supportsAbortController) {
|
|
617
|
+
this.abortController = new globalThis.AbortController();
|
|
618
|
+
if (this._options.signal) {
|
|
619
|
+
this._options.signal.addEventListener('abort', () => {
|
|
620
|
+
this.abortController.abort();
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
this._options.signal = this.abortController.signal;
|
|
624
|
+
}
|
|
625
|
+
this.request = new globalThis.Request(this._input, this._options);
|
|
626
|
+
if (this._options.searchParams) {
|
|
627
|
+
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
628
|
+
const textSearchParams = typeof this._options.searchParams === 'string'
|
|
629
|
+
? this._options.searchParams.replace(/^\?/, '')
|
|
630
|
+
: new URLSearchParams(this._options.searchParams).toString();
|
|
631
|
+
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
632
|
+
const searchParams = '?' + textSearchParams;
|
|
633
|
+
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);
|
|
634
|
+
// To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one
|
|
635
|
+
if (((supportsFormData && this._options.body instanceof globalThis.FormData)
|
|
636
|
+
|| this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers['content-type'])) {
|
|
637
|
+
this.request.headers.delete('content-type');
|
|
638
|
+
}
|
|
639
|
+
this.request = new globalThis.Request(new globalThis.Request(url, this.request), this._options);
|
|
640
|
+
}
|
|
641
|
+
if (this._options.json !== undefined) {
|
|
642
|
+
this._options.body = JSON.stringify(this._options.json);
|
|
643
|
+
this.request.headers.set('content-type', 'application/json');
|
|
644
|
+
this.request = new globalThis.Request(this.request, { body: this._options.body });
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
648
|
+
static create(input, options) {
|
|
649
|
+
const ky = new Ky(input, options);
|
|
650
|
+
const fn = async () => {
|
|
651
|
+
if (ky._options.timeout > maxSafeTimeout) {
|
|
652
|
+
throw new RangeError(`The \`timeout\` option cannot be greater than ${maxSafeTimeout}`);
|
|
653
|
+
}
|
|
654
|
+
// Delay the fetch so that body method shortcuts can set the Accept header
|
|
655
|
+
await Promise.resolve();
|
|
656
|
+
let response = await ky._fetch();
|
|
657
|
+
for (const hook of ky._options.hooks.afterResponse) {
|
|
658
|
+
// eslint-disable-next-line no-await-in-loop
|
|
659
|
+
const modifiedResponse = await hook(ky.request, ky._options, ky._decorateResponse(response.clone()));
|
|
660
|
+
if (modifiedResponse instanceof globalThis.Response) {
|
|
661
|
+
response = modifiedResponse;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
ky._decorateResponse(response);
|
|
665
|
+
if (!response.ok && ky._options.throwHttpErrors) {
|
|
666
|
+
throw new HTTPError(response, ky.request, ky._options);
|
|
667
|
+
}
|
|
668
|
+
// If `onDownloadProgress` is passed, it uses the stream API internally
|
|
669
|
+
/* istanbul ignore next */
|
|
670
|
+
if (ky._options.onDownloadProgress) {
|
|
671
|
+
if (typeof ky._options.onDownloadProgress !== 'function') {
|
|
672
|
+
throw new TypeError('The `onDownloadProgress` option must be a function');
|
|
673
|
+
}
|
|
674
|
+
if (!supportsStreams) {
|
|
675
|
+
throw new Error('Streams are not supported in your environment. `ReadableStream` is missing.');
|
|
676
|
+
}
|
|
677
|
+
return ky._stream(response.clone(), ky._options.onDownloadProgress);
|
|
678
|
+
}
|
|
679
|
+
return response;
|
|
680
|
+
};
|
|
681
|
+
const isRetriableMethod = ky._options.retry.methods.includes(ky.request.method.toLowerCase());
|
|
682
|
+
const result = (isRetriableMethod ? ky._retry(fn) : fn());
|
|
683
|
+
for (const [type, mimeType] of Object.entries(responseTypes)) {
|
|
684
|
+
result[type] = async () => {
|
|
685
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
686
|
+
ky.request.headers.set('accept', ky.request.headers.get('accept') || mimeType);
|
|
687
|
+
const response = (await result).clone();
|
|
688
|
+
if (type === 'json') {
|
|
689
|
+
if (response.status === 204) {
|
|
690
|
+
return '';
|
|
691
|
+
}
|
|
692
|
+
if (options.parseJson) {
|
|
693
|
+
return options.parseJson(await response.text());
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return response[type]();
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
return result;
|
|
700
|
+
}
|
|
701
|
+
_calculateRetryDelay(error) {
|
|
702
|
+
this._retryCount++;
|
|
703
|
+
if (this._retryCount < this._options.retry.limit && !(error instanceof TimeoutError)) {
|
|
704
|
+
if (error instanceof HTTPError) {
|
|
705
|
+
if (!this._options.retry.statusCodes.includes(error.response.status)) {
|
|
706
|
+
return 0;
|
|
707
|
+
}
|
|
708
|
+
const retryAfter = error.response.headers.get('Retry-After');
|
|
709
|
+
if (retryAfter && this._options.retry.afterStatusCodes.includes(error.response.status)) {
|
|
710
|
+
let after = Number(retryAfter);
|
|
711
|
+
if (Number.isNaN(after)) {
|
|
712
|
+
after = Date.parse(retryAfter) - Date.now();
|
|
713
|
+
}
|
|
714
|
+
else {
|
|
715
|
+
after *= 1000;
|
|
716
|
+
}
|
|
717
|
+
if (typeof this._options.retry.maxRetryAfter !== 'undefined' && after > this._options.retry.maxRetryAfter) {
|
|
718
|
+
return 0;
|
|
719
|
+
}
|
|
720
|
+
return after;
|
|
721
|
+
}
|
|
722
|
+
if (error.response.status === 413) {
|
|
723
|
+
return 0;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
const BACKOFF_FACTOR = 0.3;
|
|
727
|
+
return BACKOFF_FACTOR * (2 ** (this._retryCount - 1)) * 1000;
|
|
728
|
+
}
|
|
729
|
+
return 0;
|
|
730
|
+
}
|
|
731
|
+
_decorateResponse(response) {
|
|
732
|
+
if (this._options.parseJson) {
|
|
733
|
+
response.json = async () => this._options.parseJson(await response.text());
|
|
734
|
+
}
|
|
735
|
+
return response;
|
|
736
|
+
}
|
|
737
|
+
async _retry(fn) {
|
|
738
|
+
try {
|
|
739
|
+
return await fn();
|
|
740
|
+
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
|
|
741
|
+
}
|
|
742
|
+
catch (error) {
|
|
743
|
+
const ms = Math.min(this._calculateRetryDelay(error), maxSafeTimeout);
|
|
744
|
+
if (ms !== 0 && this._retryCount > 0) {
|
|
745
|
+
await delay(ms);
|
|
746
|
+
for (const hook of this._options.hooks.beforeRetry) {
|
|
747
|
+
// eslint-disable-next-line no-await-in-loop
|
|
748
|
+
const hookResult = await hook({
|
|
749
|
+
request: this.request,
|
|
750
|
+
options: this._options,
|
|
751
|
+
error: error,
|
|
752
|
+
retryCount: this._retryCount,
|
|
753
|
+
});
|
|
754
|
+
// If `stop` is returned from the hook, the retry process is stopped
|
|
755
|
+
if (hookResult === stop) {
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return this._retry(fn);
|
|
760
|
+
}
|
|
761
|
+
throw error;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
async _fetch() {
|
|
765
|
+
for (const hook of this._options.hooks.beforeRequest) {
|
|
766
|
+
// eslint-disable-next-line no-await-in-loop
|
|
767
|
+
const result = await hook(this.request, this._options);
|
|
768
|
+
if (result instanceof Request) {
|
|
769
|
+
this.request = result;
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
if (result instanceof Response) {
|
|
773
|
+
return result;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
if (this._options.timeout === false) {
|
|
777
|
+
return this._options.fetch(this.request.clone());
|
|
778
|
+
}
|
|
779
|
+
return timeout(this.request.clone(), this.abortController, this._options);
|
|
780
|
+
}
|
|
781
|
+
/* istanbul ignore next */
|
|
782
|
+
_stream(response, onDownloadProgress) {
|
|
783
|
+
const totalBytes = Number(response.headers.get('content-length')) || 0;
|
|
784
|
+
let transferredBytes = 0;
|
|
785
|
+
return new globalThis.Response(new globalThis.ReadableStream({
|
|
786
|
+
async start(controller) {
|
|
787
|
+
const reader = response.body.getReader();
|
|
788
|
+
if (onDownloadProgress) {
|
|
789
|
+
onDownloadProgress({ percent: 0, transferredBytes: 0, totalBytes }, new Uint8Array());
|
|
790
|
+
}
|
|
791
|
+
async function read() {
|
|
792
|
+
const { done, value } = await reader.read();
|
|
793
|
+
if (done) {
|
|
794
|
+
controller.close();
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
if (onDownloadProgress) {
|
|
798
|
+
transferredBytes += value.byteLength;
|
|
799
|
+
const percent = totalBytes === 0 ? 0 : transferredBytes / totalBytes;
|
|
800
|
+
onDownloadProgress({ percent, transferredBytes, totalBytes }, value);
|
|
801
|
+
}
|
|
802
|
+
controller.enqueue(value);
|
|
803
|
+
await read();
|
|
804
|
+
}
|
|
805
|
+
await read();
|
|
806
|
+
},
|
|
807
|
+
}));
|
|
808
|
+
}
|
|
809
|
+
}
|
|
929
810
|
|
|
930
|
-
|
|
931
|
-
|
|
811
|
+
/*! MIT License © Sindre Sorhus */
|
|
812
|
+
const createInstance = (defaults) => {
|
|
813
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
814
|
+
const ky = (input, options) => Ky.create(input, validateAndMerge(defaults, options));
|
|
815
|
+
for (const method of requestMethods) {
|
|
816
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
817
|
+
ky[method] = (input, options) => Ky.create(input, validateAndMerge(defaults, options, { method }));
|
|
818
|
+
}
|
|
819
|
+
ky.create = (newDefaults) => createInstance(validateAndMerge(newDefaults));
|
|
820
|
+
ky.extend = (newDefaults) => createInstance(validateAndMerge(defaults, newDefaults));
|
|
821
|
+
ky.stop = stop;
|
|
822
|
+
return ky;
|
|
823
|
+
};
|
|
824
|
+
const ky = createInstance();
|
|
825
|
+
|
|
826
|
+
exports.HTTPError = HTTPError;
|
|
827
|
+
exports.TimeoutError = TimeoutError;
|
|
828
|
+
exports["default"] = ky;
|
|
829
|
+
|
|
830
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
831
|
+
|
|
832
|
+
}));
|
|
833
|
+
}(ky$1, ky$1.exports));
|
|
932
834
|
|
|
933
835
|
const isUrlHttp = lightweight;
|
|
934
836
|
const { flattie: flatten } = dist;
|
|
@@ -936,7 +838,7 @@
|
|
|
936
838
|
const whoops = lib.exports;
|
|
937
839
|
|
|
938
840
|
const factory = factory_1;
|
|
939
|
-
const ky = ky$1.exports;
|
|
841
|
+
const { default: ky } = ky$1.exports;
|
|
940
842
|
|
|
941
843
|
const MicrolinkError = whoops('MicrolinkError');
|
|
942
844
|
|
|
@@ -952,8 +854,11 @@
|
|
|
952
854
|
const { response } = err;
|
|
953
855
|
err.response = {
|
|
954
856
|
...response,
|
|
955
|
-
headers:
|
|
956
|
-
(acc, [key, value]) =>
|
|
857
|
+
headers: Array.from(response.headers.entries()).reduce(
|
|
858
|
+
(acc, [key, value]) => {
|
|
859
|
+
acc[key] = value;
|
|
860
|
+
return acc
|
|
861
|
+
},
|
|
957
862
|
{}
|
|
958
863
|
),
|
|
959
864
|
statusCode: response.status,
|
|
@@ -970,7 +875,7 @@
|
|
|
970
875
|
stringify,
|
|
971
876
|
got,
|
|
972
877
|
flatten,
|
|
973
|
-
VERSION: '0.10.
|
|
878
|
+
VERSION: '0.10.15'
|
|
974
879
|
});
|
|
975
880
|
|
|
976
881
|
return browser;
|