@microlink/mql 0.13.7 → 0.13.9

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.
@@ -31,41 +31,34 @@ var lightweight$1 = {exports: {}};
31
31
 
32
32
  var dist = {};
33
33
 
34
- var hasRequiredDist;
35
-
36
- function requireDist () {
37
- if (hasRequiredDist) return dist;
38
- hasRequiredDist = 1;
39
- function iter(output, nullish, sep, val, key) {
40
- var k, pfx = key ? (key + sep) : key;
41
-
42
- if (val == null) {
43
- if (nullish) output[key] = val;
44
- } else if (typeof val != 'object') {
45
- output[key] = val;
46
- } else if (Array.isArray(val)) {
47
- for (k=0; k < val.length; k++) {
48
- iter(output, nullish, sep, val[k], pfx + k);
49
- }
50
- } else {
51
- for (k in val) {
52
- iter(output, nullish, sep, val[k], pfx + k);
53
- }
34
+ function iter(output, nullish, sep, val, key) {
35
+ var k, pfx = key ? (key + sep) : key;
36
+
37
+ if (val == null) {
38
+ if (nullish) output[key] = val;
39
+ } else if (typeof val != 'object') {
40
+ output[key] = val;
41
+ } else if (Array.isArray(val)) {
42
+ for (k=0; k < val.length; k++) {
43
+ iter(output, nullish, sep, val[k], pfx + k);
54
44
  }
55
- }
56
-
57
- function flattie(input, glue, toNull) {
58
- var output = {};
59
- if (typeof input == 'object') {
60
- iter(output, !!toNull, glue || '.', input, '');
45
+ } else {
46
+ for (k in val) {
47
+ iter(output, nullish, sep, val[k], pfx + k);
61
48
  }
62
- return output;
63
49
  }
50
+ }
64
51
 
65
- dist.flattie = flattie;
66
- return dist;
52
+ function flattie(input, glue, toNull) {
53
+ var output = {};
54
+ if (typeof input == 'object') {
55
+ iter(output, !!toNull, glue || '.', input, '');
56
+ }
57
+ return output;
67
58
  }
68
59
 
60
+ dist.flattie = flattie;
61
+
69
62
  class HTTPError extends Error {
70
63
  response;
71
64
  request;
@@ -93,11 +86,11 @@ class TimeoutError extends Error {
93
86
  }
94
87
 
95
88
  // eslint-disable-next-line @typescript-eslint/ban-types
96
- const isObject = (value) => value !== null && typeof value === 'object';
89
+ const isObject$1 = (value) => value !== null && typeof value === 'object';
97
90
 
98
91
  const validateAndMerge = (...sources) => {
99
92
  for (const source of sources) {
100
- if ((!isObject(source) || Array.isArray(source)) && source !== undefined) {
93
+ if ((!isObject$1(source) || Array.isArray(source)) && source !== undefined) {
101
94
  throw new TypeError('The `options` argument must be an object');
102
95
  }
103
96
  }
@@ -140,18 +133,18 @@ const deepMerge = (...sources) => {
140
133
  }
141
134
  returnValue = [...returnValue, ...source];
142
135
  }
143
- else if (isObject(source)) {
136
+ else if (isObject$1(source)) {
144
137
  for (let [key, value] of Object.entries(source)) {
145
- if (isObject(value) && key in returnValue) {
138
+ if (isObject$1(value) && key in returnValue) {
146
139
  value = deepMerge(returnValue[key], value);
147
140
  }
148
141
  returnValue = { ...returnValue, [key]: value };
149
142
  }
150
- if (isObject(source.hooks)) {
143
+ if (isObject$1(source.hooks)) {
151
144
  hooks = mergeHooks(hooks, source.hooks);
152
145
  returnValue.hooks = hooks;
153
146
  }
154
- if (isObject(source.headers)) {
147
+ if (isObject$1(source.headers)) {
155
148
  headers = mergeHeaders(headers, source.headers);
156
149
  returnValue.headers = headers;
157
150
  }
@@ -595,241 +588,225 @@ const createInstance = (defaults) => {
595
588
  ky.stop = stop;
596
589
  return ky;
597
590
  };
598
- const ky = createInstance();
591
+ const ky$1 = createInstance();
599
592
 
600
593
  var distribution = /*#__PURE__*/Object.freeze({
601
594
  __proto__: null,
602
595
  HTTPError: HTTPError,
603
596
  TimeoutError: TimeoutError,
604
- default: ky
597
+ default: ky$1
605
598
  });
606
599
 
607
600
  var require$$1 = /*@__PURE__*/getAugmentedNamespace(distribution);
608
601
 
609
- var factory_1;
610
- var hasRequiredFactory;
611
-
612
- function requireFactory () {
613
- if (hasRequiredFactory) return factory_1;
614
- hasRequiredFactory = 1;
615
- const ENDPOINT = {
616
- FREE: 'https://api.microlink.io/',
617
- PRO: 'https://pro.microlink.io/'
618
- };
619
-
620
- const isObject = input => input !== null && typeof input === 'object';
621
-
622
- const isBuffer = input =>
623
- input != null &&
624
- input.constructor != null &&
625
- typeof input.constructor.isBuffer === 'function' &&
626
- input.constructor.isBuffer(input);
627
-
628
- const parseBody = (input, error, url) => {
629
- try {
630
- return JSON.parse(input)
631
- } catch (_) {
632
- const message = input || error.message;
633
-
634
- return {
635
- status: 'error',
636
- data: { url: message },
637
- more: 'https://microlink.io/efatalclient',
638
- code: 'EFATALCLIENT',
639
- message,
640
- url
641
- }
642
- }
643
- };
644
-
645
- const isURL = url => {
646
- try {
647
- return /^https?:\/\//i.test(new URL(url).href)
648
- } catch (_) {
649
- return false
650
- }
651
- };
652
-
653
- const factory = streamResponseType => ({
654
- VERSION,
655
- MicrolinkError,
656
- got,
657
- flatten
658
- }) => {
659
- const assertUrl = (url = '') => {
660
- if (!isURL(url)) {
661
- const message = `The \`url\` as \`${url}\` is not valid. Ensure it has protocol (http or https) and hostname.`;
662
- throw new MicrolinkError({
663
- status: 'fail',
664
- data: { url: message },
665
- more: 'https://microlink.io/einvalurlclient',
666
- code: 'EINVALURLCLIENT',
667
- message,
668
- url
669
- })
670
- }
671
- };
672
-
673
- const mapRules = rules => {
674
- if (!isObject(rules)) return
675
- const flatRules = flatten(rules);
676
- return Object.keys(flatRules).reduce((acc, key) => {
677
- acc[`data.${key}`] = flatRules[key].toString();
678
- return acc
679
- }, {})
680
- };
681
-
682
- const fetchFromApi = async (apiUrl, opts = {}) => {
683
- try {
684
- const response = await got(apiUrl, opts);
685
- return opts.responseType === streamResponseType
686
- ? response
687
- : { ...response.body, response }
688
- } catch (error) {
689
- const { response = {} } = error;
690
- const {
691
- statusCode,
692
- body: rawBody,
693
- headers = {},
694
- url: uri = apiUrl
695
- } = response;
696
- const isBodyBuffer = isBuffer(rawBody);
697
-
698
- const body =
699
- isObject(rawBody) && !isBodyBuffer
700
- ? rawBody
701
- : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, error, uri);
702
-
703
- throw new MicrolinkError({
704
- ...body,
705
- message: body.message,
706
- url: uri,
707
- statusCode,
708
- headers
709
- })
710
- }
711
- };
712
-
713
- const getApiUrl = (
714
- url,
715
- { data, apiKey, endpoint, retry, cache, ...opts } = {},
716
- { responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
717
- ) => {
718
- const isPro = !!apiKey;
719
- const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE'];
720
-
721
- const apiUrl = `${apiEndpoint}?${new URLSearchParams({
722
- url,
723
- ...mapRules(data),
724
- ...flatten(opts)
725
- }).toString()}`;
726
-
727
- const headers = isPro
728
- ? { ...gotHeaders, 'x-api-key': apiKey }
729
- : { ...gotHeaders };
730
-
731
- if (opts.stream) {
732
- responseType = streamResponseType;
733
- }
734
- return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
735
- };
736
-
737
- const createMql = defaultOpts => async (url, opts, gotOpts) => {
738
- assertUrl(url);
739
- const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
740
- ...defaultOpts,
741
- ...gotOpts
742
- });
743
- return fetchFromApi(apiUrl, fetchOpts)
744
- };
745
-
746
- const mql = createMql();
747
- mql.extend = createMql;
748
- mql.MicrolinkError = MicrolinkError;
749
- mql.getApiUrl = getApiUrl;
750
- mql.fetchFromApi = fetchFromApi;
751
- mql.mapRules = mapRules;
752
- mql.version = VERSION;
753
- mql.stream = got.stream;
754
-
755
- return mql
756
- };
757
-
758
- factory_1 = factory;
759
- return factory_1;
760
- }
602
+ const ENDPOINT = {
603
+ FREE: 'https://api.microlink.io/',
604
+ PRO: 'https://pro.microlink.io/'
605
+ };
761
606
 
762
- var hasRequiredLightweight;
607
+ const isObject = input => input !== null && typeof input === 'object';
763
608
 
764
- function requireLightweight () {
765
- if (hasRequiredLightweight) return lightweight$1.exports;
766
- hasRequiredLightweight = 1;
609
+ const isBuffer = input =>
610
+ input != null &&
611
+ input.constructor != null &&
612
+ typeof input.constructor.isBuffer === 'function' &&
613
+ input.constructor.isBuffer(input);
767
614
 
768
- const { flattie: flatten } = requireDist();
769
- const { default: ky } = require$$1;
615
+ const parseBody = (input, error, url) => {
616
+ try {
617
+ return JSON.parse(input)
618
+ } catch (_) {
619
+ const message = input || error.message;
770
620
 
771
- const factory = requireFactory()('arrayBuffer');
621
+ return {
622
+ status: 'error',
623
+ data: { url: message },
624
+ more: 'https://microlink.io/efatalclient',
625
+ code: 'EFATALCLIENT',
626
+ message,
627
+ url
628
+ }
629
+ }
630
+ };
772
631
 
773
- class MicrolinkError extends Error {
774
- constructor (props) {
775
- super();
776
- this.name = 'MicrolinkError';
777
- Object.assign(this, props);
778
- this.description = this.message;
779
- this.message = this.code
780
- ? `${this.code}, ${this.description}`
781
- : this.description;
782
- }
783
- }
632
+ const isURL = url => {
633
+ try {
634
+ return /^https?:\/\//i.test(new URL(url).href)
635
+ } catch (_) {
636
+ return false
637
+ }
638
+ };
784
639
 
785
- const got = async (url, { responseType, ...opts }) => {
786
- try {
787
- if (opts.timeout === undefined) opts.timeout = false;
788
- const response = await ky(url, opts);
789
- const body = await response[responseType]();
790
- const { headers, status: statusCode } = response;
791
- return { url: response.url, body, headers, statusCode }
792
- } catch (error) {
793
- if (error.response) {
794
- const { response } = error;
795
- error.response = {
796
- ...response,
797
- headers: Array.from(response.headers.entries()).reduce(
798
- (acc, [key, value]) => {
799
- acc[key] = value;
800
- return acc
801
- },
802
- {}
803
- ),
804
- statusCode: response.status,
805
- body: await response.text()
806
- };
807
- }
808
- throw error
809
- }
810
- };
811
-
812
- got.stream = (...args) => ky(...args).then(res => res.body);
813
-
814
- const mql = factory({
815
- MicrolinkError,
816
- got,
817
- flatten,
818
- VERSION: '0.13.7'
819
- });
640
+ const factory$1 = streamResponseType => ({
641
+ VERSION,
642
+ MicrolinkError,
643
+ got,
644
+ flatten
645
+ }) => {
646
+ const assertUrl = (url = '') => {
647
+ if (!isURL(url)) {
648
+ const message = `The \`url\` as \`${url}\` is not valid. Ensure it has protocol (http or https) and hostname.`;
649
+ throw new MicrolinkError({
650
+ status: 'fail',
651
+ data: { url: message },
652
+ more: 'https://microlink.io/einvalurlclient',
653
+ code: 'EINVALURLCLIENT',
654
+ message,
655
+ url
656
+ })
657
+ }
658
+ };
659
+
660
+ const mapRules = rules => {
661
+ if (!isObject(rules)) return
662
+ const flatRules = flatten(rules);
663
+ return Object.keys(flatRules).reduce((acc, key) => {
664
+ acc[`data.${key}`] = flatRules[key].toString();
665
+ return acc
666
+ }, {})
667
+ };
668
+
669
+ const fetchFromApi = async (apiUrl, opts = {}) => {
670
+ try {
671
+ const response = await got(apiUrl, opts);
672
+ return opts.responseType === streamResponseType
673
+ ? response
674
+ : { ...response.body, response }
675
+ } catch (error) {
676
+ const { response = {} } = error;
677
+ const {
678
+ statusCode,
679
+ body: rawBody,
680
+ headers = {},
681
+ url: uri = apiUrl
682
+ } = response;
683
+ const isBodyBuffer = isBuffer(rawBody);
684
+
685
+ const body =
686
+ isObject(rawBody) && !isBodyBuffer
687
+ ? rawBody
688
+ : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, error, uri);
689
+
690
+ throw new MicrolinkError({
691
+ ...body,
692
+ message: body.message,
693
+ url: uri,
694
+ statusCode,
695
+ headers
696
+ })
697
+ }
698
+ };
699
+
700
+ const getApiUrl = (
701
+ url,
702
+ { data, apiKey, endpoint, retry, cache, ...opts } = {},
703
+ { responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
704
+ ) => {
705
+ const isPro = !!apiKey;
706
+ const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE'];
707
+
708
+ const apiUrl = `${apiEndpoint}?${new URLSearchParams({
709
+ url,
710
+ ...mapRules(data),
711
+ ...flatten(opts)
712
+ }).toString()}`;
713
+
714
+ const headers = isPro
715
+ ? { ...gotHeaders, 'x-api-key': apiKey }
716
+ : { ...gotHeaders };
717
+
718
+ if (opts.stream) {
719
+ responseType = streamResponseType;
720
+ }
721
+ return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
722
+ };
723
+
724
+ const createMql = defaultOpts => async (url, opts, gotOpts) => {
725
+ assertUrl(url);
726
+ const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
727
+ ...defaultOpts,
728
+ ...gotOpts
729
+ });
730
+ return fetchFromApi(apiUrl, fetchOpts)
731
+ };
732
+
733
+ const mql = createMql();
734
+ mql.extend = createMql;
735
+ mql.MicrolinkError = MicrolinkError;
736
+ mql.getApiUrl = getApiUrl;
737
+ mql.fetchFromApi = fetchFromApi;
738
+ mql.mapRules = mapRules;
739
+ mql.version = VERSION;
740
+ mql.stream = got.stream;
741
+
742
+ return mql
743
+ };
744
+
745
+ var factory_1 = factory$1;
746
+
747
+ const { flattie: flatten } = dist;
748
+ const { default: ky } = require$$1;
749
+
750
+ const factory = factory_1('arrayBuffer');
820
751
 
821
- lightweight$1.exports = mql;
822
- lightweight$1.exports.arrayBuffer = mql.extend({ responseType: 'arrayBuffer' });
823
- lightweight$1.exports.extend = mql.extend;
824
- lightweight$1.exports.fetchFromApi = mql.fetchFromApi;
825
- lightweight$1.exports.getApiUrl = mql.getApiUrl;
826
- lightweight$1.exports.mapRules = mql.mapRules;
827
- lightweight$1.exports.MicrolinkError = mql.MicrolinkError;
828
- lightweight$1.exports.version = mql.version;
829
- return lightweight$1.exports;
752
+ class MicrolinkError extends Error {
753
+ constructor (props) {
754
+ super();
755
+ this.name = 'MicrolinkError';
756
+ Object.assign(this, props);
757
+ this.description = this.message;
758
+ this.message = this.code
759
+ ? `${this.code}, ${this.description}`
760
+ : this.description;
761
+ }
830
762
  }
831
763
 
832
- var lightweightExports = requireLightweight();
764
+ const got = async (url, { responseType, ...opts }) => {
765
+ try {
766
+ if (opts.timeout === undefined) opts.timeout = false;
767
+ const response = await ky(url, opts);
768
+ const body = await response[responseType]();
769
+ const { headers, status: statusCode } = response;
770
+ return { url: response.url, body, headers, statusCode }
771
+ } catch (error) {
772
+ if (error.response) {
773
+ const { response } = error;
774
+ error.response = {
775
+ ...response,
776
+ headers: Array.from(response.headers.entries()).reduce(
777
+ (acc, [key, value]) => {
778
+ acc[key] = value;
779
+ return acc
780
+ },
781
+ {}
782
+ ),
783
+ statusCode: response.status,
784
+ body: await response.text()
785
+ };
786
+ }
787
+ throw error
788
+ }
789
+ };
790
+
791
+ got.stream = (...args) => ky(...args).then(res => res.body);
792
+
793
+ const mql = factory({
794
+ MicrolinkError,
795
+ got,
796
+ flatten,
797
+ VERSION: '0.13.9'
798
+ });
799
+
800
+ lightweight$1.exports = mql;
801
+ var arrayBuffer = lightweight$1.exports.arrayBuffer = mql.extend({ responseType: 'arrayBuffer' });
802
+ var extend = lightweight$1.exports.extend = mql.extend;
803
+ var fetchFromApi = lightweight$1.exports.fetchFromApi = mql.fetchFromApi;
804
+ var getApiUrl = lightweight$1.exports.getApiUrl = mql.getApiUrl;
805
+ var mapRules = lightweight$1.exports.mapRules = mql.mapRules;
806
+ var MicrolinkError_1 = lightweight$1.exports.MicrolinkError = mql.MicrolinkError;
807
+ var version = lightweight$1.exports.version = mql.version;
808
+
809
+ var lightweightExports = lightweight$1.exports;
833
810
  var lightweight = /*@__PURE__*/getDefaultExportFromCjs(lightweightExports);
834
811
 
835
- export { lightweight as default };
812
+ export { MicrolinkError_1 as MicrolinkError, arrayBuffer, lightweight as default, extend, fetchFromApi, getApiUrl, mapRules, version };
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.mql = factory());
5
- })(this, (function () { 'use strict';
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.mql = {}));
5
+ })(this, (function (exports) { 'use strict';
6
6
 
7
7
  function getDefaultExportFromCjs (x) {
8
8
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -37,41 +37,34 @@
37
37
 
38
38
  var dist = {};
39
39
 
40
- var hasRequiredDist;
41
-
42
- function requireDist () {
43
- if (hasRequiredDist) return dist;
44
- hasRequiredDist = 1;
45
- function iter(output, nullish, sep, val, key) {
46
- var k, pfx = key ? (key + sep) : key;
47
-
48
- if (val == null) {
49
- if (nullish) output[key] = val;
50
- } else if (typeof val != 'object') {
51
- output[key] = val;
52
- } else if (Array.isArray(val)) {
53
- for (k=0; k < val.length; k++) {
54
- iter(output, nullish, sep, val[k], pfx + k);
55
- }
56
- } else {
57
- for (k in val) {
58
- iter(output, nullish, sep, val[k], pfx + k);
59
- }
60
- }
61
- }
40
+ function iter(output, nullish, sep, val, key) {
41
+ var k, pfx = key ? (key + sep) : key;
62
42
 
63
- function flattie(input, glue, toNull) {
64
- var output = {};
65
- if (typeof input == 'object') {
66
- iter(output, !!toNull, glue || '.', input, '');
43
+ if (val == null) {
44
+ if (nullish) output[key] = val;
45
+ } else if (typeof val != 'object') {
46
+ output[key] = val;
47
+ } else if (Array.isArray(val)) {
48
+ for (k=0; k < val.length; k++) {
49
+ iter(output, nullish, sep, val[k], pfx + k);
50
+ }
51
+ } else {
52
+ for (k in val) {
53
+ iter(output, nullish, sep, val[k], pfx + k);
67
54
  }
68
- return output;
69
55
  }
56
+ }
70
57
 
71
- dist.flattie = flattie;
72
- return dist;
58
+ function flattie(input, glue, toNull) {
59
+ var output = {};
60
+ if (typeof input == 'object') {
61
+ iter(output, !!toNull, glue || '.', input, '');
62
+ }
63
+ return output;
73
64
  }
74
65
 
66
+ dist.flattie = flattie;
67
+
75
68
  class HTTPError extends Error {
76
69
  response;
77
70
  request;
@@ -99,11 +92,11 @@
99
92
  }
100
93
 
101
94
  // eslint-disable-next-line @typescript-eslint/ban-types
102
- const isObject = (value) => value !== null && typeof value === 'object';
95
+ const isObject$1 = (value) => value !== null && typeof value === 'object';
103
96
 
104
97
  const validateAndMerge = (...sources) => {
105
98
  for (const source of sources) {
106
- if ((!isObject(source) || Array.isArray(source)) && source !== undefined) {
99
+ if ((!isObject$1(source) || Array.isArray(source)) && source !== undefined) {
107
100
  throw new TypeError('The `options` argument must be an object');
108
101
  }
109
102
  }
@@ -146,18 +139,18 @@
146
139
  }
147
140
  returnValue = [...returnValue, ...source];
148
141
  }
149
- else if (isObject(source)) {
142
+ else if (isObject$1(source)) {
150
143
  for (let [key, value] of Object.entries(source)) {
151
- if (isObject(value) && key in returnValue) {
144
+ if (isObject$1(value) && key in returnValue) {
152
145
  value = deepMerge(returnValue[key], value);
153
146
  }
154
147
  returnValue = { ...returnValue, [key]: value };
155
148
  }
156
- if (isObject(source.hooks)) {
149
+ if (isObject$1(source.hooks)) {
157
150
  hooks = mergeHooks(hooks, source.hooks);
158
151
  returnValue.hooks = hooks;
159
152
  }
160
- if (isObject(source.headers)) {
153
+ if (isObject$1(source.headers)) {
161
154
  headers = mergeHeaders(headers, source.headers);
162
155
  returnValue.headers = headers;
163
156
  }
@@ -601,243 +594,236 @@
601
594
  ky.stop = stop;
602
595
  return ky;
603
596
  };
604
- const ky = createInstance();
597
+ const ky$1 = createInstance();
605
598
 
606
599
  var distribution = /*#__PURE__*/Object.freeze({
607
600
  __proto__: null,
608
601
  HTTPError: HTTPError,
609
602
  TimeoutError: TimeoutError,
610
- default: ky
603
+ default: ky$1
611
604
  });
612
605
 
613
606
  var require$$1 = /*@__PURE__*/getAugmentedNamespace(distribution);
614
607
 
615
- var factory_1;
616
- var hasRequiredFactory;
617
-
618
- function requireFactory () {
619
- if (hasRequiredFactory) return factory_1;
620
- hasRequiredFactory = 1;
621
- const ENDPOINT = {
622
- FREE: 'https://api.microlink.io/',
623
- PRO: 'https://pro.microlink.io/'
624
- };
625
-
626
- const isObject = input => input !== null && typeof input === 'object';
627
-
628
- const isBuffer = input =>
629
- input != null &&
630
- input.constructor != null &&
631
- typeof input.constructor.isBuffer === 'function' &&
632
- input.constructor.isBuffer(input);
633
-
634
- const parseBody = (input, error, url) => {
635
- try {
636
- return JSON.parse(input)
637
- } catch (_) {
638
- const message = input || error.message;
639
-
640
- return {
641
- status: 'error',
642
- data: { url: message },
643
- more: 'https://microlink.io/efatalclient',
644
- code: 'EFATALCLIENT',
645
- message,
646
- url
647
- }
648
- }
649
- };
650
-
651
- const isURL = url => {
652
- try {
653
- return /^https?:\/\//i.test(new URL(url).href)
654
- } catch (_) {
655
- return false
656
- }
657
- };
658
-
659
- const factory = streamResponseType => ({
660
- VERSION,
661
- MicrolinkError,
662
- got,
663
- flatten
664
- }) => {
665
- const assertUrl = (url = '') => {
666
- if (!isURL(url)) {
667
- const message = `The \`url\` as \`${url}\` is not valid. Ensure it has protocol (http or https) and hostname.`;
668
- throw new MicrolinkError({
669
- status: 'fail',
670
- data: { url: message },
671
- more: 'https://microlink.io/einvalurlclient',
672
- code: 'EINVALURLCLIENT',
673
- message,
674
- url
675
- })
676
- }
677
- };
678
-
679
- const mapRules = rules => {
680
- if (!isObject(rules)) return
681
- const flatRules = flatten(rules);
682
- return Object.keys(flatRules).reduce((acc, key) => {
683
- acc[`data.${key}`] = flatRules[key].toString();
684
- return acc
685
- }, {})
686
- };
687
-
688
- const fetchFromApi = async (apiUrl, opts = {}) => {
689
- try {
690
- const response = await got(apiUrl, opts);
691
- return opts.responseType === streamResponseType
692
- ? response
693
- : { ...response.body, response }
694
- } catch (error) {
695
- const { response = {} } = error;
696
- const {
697
- statusCode,
698
- body: rawBody,
699
- headers = {},
700
- url: uri = apiUrl
701
- } = response;
702
- const isBodyBuffer = isBuffer(rawBody);
703
-
704
- const body =
705
- isObject(rawBody) && !isBodyBuffer
706
- ? rawBody
707
- : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, error, uri);
708
-
709
- throw new MicrolinkError({
710
- ...body,
711
- message: body.message,
712
- url: uri,
713
- statusCode,
714
- headers
715
- })
716
- }
717
- };
718
-
719
- const getApiUrl = (
720
- url,
721
- { data, apiKey, endpoint, retry, cache, ...opts } = {},
722
- { responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
723
- ) => {
724
- const isPro = !!apiKey;
725
- const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE'];
726
-
727
- const apiUrl = `${apiEndpoint}?${new URLSearchParams({
728
- url,
729
- ...mapRules(data),
730
- ...flatten(opts)
731
- }).toString()}`;
732
-
733
- const headers = isPro
734
- ? { ...gotHeaders, 'x-api-key': apiKey }
735
- : { ...gotHeaders };
736
-
737
- if (opts.stream) {
738
- responseType = streamResponseType;
739
- }
740
- return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
741
- };
742
-
743
- const createMql = defaultOpts => async (url, opts, gotOpts) => {
744
- assertUrl(url);
745
- const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
746
- ...defaultOpts,
747
- ...gotOpts
748
- });
749
- return fetchFromApi(apiUrl, fetchOpts)
750
- };
751
-
752
- const mql = createMql();
753
- mql.extend = createMql;
754
- mql.MicrolinkError = MicrolinkError;
755
- mql.getApiUrl = getApiUrl;
756
- mql.fetchFromApi = fetchFromApi;
757
- mql.mapRules = mapRules;
758
- mql.version = VERSION;
759
- mql.stream = got.stream;
760
-
761
- return mql
762
- };
763
-
764
- factory_1 = factory;
765
- return factory_1;
766
- }
608
+ const ENDPOINT = {
609
+ FREE: 'https://api.microlink.io/',
610
+ PRO: 'https://pro.microlink.io/'
611
+ };
767
612
 
768
- var hasRequiredLightweight;
613
+ const isObject = input => input !== null && typeof input === 'object';
769
614
 
770
- function requireLightweight () {
771
- if (hasRequiredLightweight) return lightweight$1.exports;
772
- hasRequiredLightweight = 1;
615
+ const isBuffer = input =>
616
+ input != null &&
617
+ input.constructor != null &&
618
+ typeof input.constructor.isBuffer === 'function' &&
619
+ input.constructor.isBuffer(input);
773
620
 
774
- const { flattie: flatten } = requireDist();
775
- const { default: ky } = require$$1;
621
+ const parseBody = (input, error, url) => {
622
+ try {
623
+ return JSON.parse(input)
624
+ } catch (_) {
625
+ const message = input || error.message;
776
626
 
777
- const factory = requireFactory()('arrayBuffer');
627
+ return {
628
+ status: 'error',
629
+ data: { url: message },
630
+ more: 'https://microlink.io/efatalclient',
631
+ code: 'EFATALCLIENT',
632
+ message,
633
+ url
634
+ }
635
+ }
636
+ };
778
637
 
779
- class MicrolinkError extends Error {
780
- constructor (props) {
781
- super();
782
- this.name = 'MicrolinkError';
783
- Object.assign(this, props);
784
- this.description = this.message;
785
- this.message = this.code
786
- ? `${this.code}, ${this.description}`
787
- : this.description;
788
- }
789
- }
638
+ const isURL = url => {
639
+ try {
640
+ return /^https?:\/\//i.test(new URL(url).href)
641
+ } catch (_) {
642
+ return false
643
+ }
644
+ };
790
645
 
791
- const got = async (url, { responseType, ...opts }) => {
792
- try {
793
- if (opts.timeout === undefined) opts.timeout = false;
794
- const response = await ky(url, opts);
795
- const body = await response[responseType]();
796
- const { headers, status: statusCode } = response;
797
- return { url: response.url, body, headers, statusCode }
798
- } catch (error) {
799
- if (error.response) {
800
- const { response } = error;
801
- error.response = {
802
- ...response,
803
- headers: Array.from(response.headers.entries()).reduce(
804
- (acc, [key, value]) => {
805
- acc[key] = value;
806
- return acc
807
- },
808
- {}
809
- ),
810
- statusCode: response.status,
811
- body: await response.text()
812
- };
813
- }
814
- throw error
815
- }
816
- };
817
-
818
- got.stream = (...args) => ky(...args).then(res => res.body);
819
-
820
- const mql = factory({
821
- MicrolinkError,
822
- got,
823
- flatten,
824
- VERSION: '0.13.7'
825
- });
646
+ const factory$1 = streamResponseType => ({
647
+ VERSION,
648
+ MicrolinkError,
649
+ got,
650
+ flatten
651
+ }) => {
652
+ const assertUrl = (url = '') => {
653
+ if (!isURL(url)) {
654
+ const message = `The \`url\` as \`${url}\` is not valid. Ensure it has protocol (http or https) and hostname.`;
655
+ throw new MicrolinkError({
656
+ status: 'fail',
657
+ data: { url: message },
658
+ more: 'https://microlink.io/einvalurlclient',
659
+ code: 'EINVALURLCLIENT',
660
+ message,
661
+ url
662
+ })
663
+ }
664
+ };
665
+
666
+ const mapRules = rules => {
667
+ if (!isObject(rules)) return
668
+ const flatRules = flatten(rules);
669
+ return Object.keys(flatRules).reduce((acc, key) => {
670
+ acc[`data.${key}`] = flatRules[key].toString();
671
+ return acc
672
+ }, {})
673
+ };
674
+
675
+ const fetchFromApi = async (apiUrl, opts = {}) => {
676
+ try {
677
+ const response = await got(apiUrl, opts);
678
+ return opts.responseType === streamResponseType
679
+ ? response
680
+ : { ...response.body, response }
681
+ } catch (error) {
682
+ const { response = {} } = error;
683
+ const {
684
+ statusCode,
685
+ body: rawBody,
686
+ headers = {},
687
+ url: uri = apiUrl
688
+ } = response;
689
+ const isBodyBuffer = isBuffer(rawBody);
690
+
691
+ const body =
692
+ isObject(rawBody) && !isBodyBuffer
693
+ ? rawBody
694
+ : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, error, uri);
695
+
696
+ throw new MicrolinkError({
697
+ ...body,
698
+ message: body.message,
699
+ url: uri,
700
+ statusCode,
701
+ headers
702
+ })
703
+ }
704
+ };
705
+
706
+ const getApiUrl = (
707
+ url,
708
+ { data, apiKey, endpoint, retry, cache, ...opts } = {},
709
+ { responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
710
+ ) => {
711
+ const isPro = !!apiKey;
712
+ const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE'];
713
+
714
+ const apiUrl = `${apiEndpoint}?${new URLSearchParams({
715
+ url,
716
+ ...mapRules(data),
717
+ ...flatten(opts)
718
+ }).toString()}`;
719
+
720
+ const headers = isPro
721
+ ? { ...gotHeaders, 'x-api-key': apiKey }
722
+ : { ...gotHeaders };
723
+
724
+ if (opts.stream) {
725
+ responseType = streamResponseType;
726
+ }
727
+ return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
728
+ };
729
+
730
+ const createMql = defaultOpts => async (url, opts, gotOpts) => {
731
+ assertUrl(url);
732
+ const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
733
+ ...defaultOpts,
734
+ ...gotOpts
735
+ });
736
+ return fetchFromApi(apiUrl, fetchOpts)
737
+ };
738
+
739
+ const mql = createMql();
740
+ mql.extend = createMql;
741
+ mql.MicrolinkError = MicrolinkError;
742
+ mql.getApiUrl = getApiUrl;
743
+ mql.fetchFromApi = fetchFromApi;
744
+ mql.mapRules = mapRules;
745
+ mql.version = VERSION;
746
+ mql.stream = got.stream;
747
+
748
+ return mql
749
+ };
750
+
751
+ var factory_1 = factory$1;
752
+
753
+ const { flattie: flatten } = dist;
754
+ const { default: ky } = require$$1;
755
+
756
+ const factory = factory_1('arrayBuffer');
826
757
 
827
- lightweight$1.exports = mql;
828
- lightweight$1.exports.arrayBuffer = mql.extend({ responseType: 'arrayBuffer' });
829
- lightweight$1.exports.extend = mql.extend;
830
- lightweight$1.exports.fetchFromApi = mql.fetchFromApi;
831
- lightweight$1.exports.getApiUrl = mql.getApiUrl;
832
- lightweight$1.exports.mapRules = mql.mapRules;
833
- lightweight$1.exports.MicrolinkError = mql.MicrolinkError;
834
- lightweight$1.exports.version = mql.version;
835
- return lightweight$1.exports;
758
+ class MicrolinkError extends Error {
759
+ constructor (props) {
760
+ super();
761
+ this.name = 'MicrolinkError';
762
+ Object.assign(this, props);
763
+ this.description = this.message;
764
+ this.message = this.code
765
+ ? `${this.code}, ${this.description}`
766
+ : this.description;
767
+ }
836
768
  }
837
769
 
838
- var lightweightExports = requireLightweight();
770
+ const got = async (url, { responseType, ...opts }) => {
771
+ try {
772
+ if (opts.timeout === undefined) opts.timeout = false;
773
+ const response = await ky(url, opts);
774
+ const body = await response[responseType]();
775
+ const { headers, status: statusCode } = response;
776
+ return { url: response.url, body, headers, statusCode }
777
+ } catch (error) {
778
+ if (error.response) {
779
+ const { response } = error;
780
+ error.response = {
781
+ ...response,
782
+ headers: Array.from(response.headers.entries()).reduce(
783
+ (acc, [key, value]) => {
784
+ acc[key] = value;
785
+ return acc
786
+ },
787
+ {}
788
+ ),
789
+ statusCode: response.status,
790
+ body: await response.text()
791
+ };
792
+ }
793
+ throw error
794
+ }
795
+ };
796
+
797
+ got.stream = (...args) => ky(...args).then(res => res.body);
798
+
799
+ const mql = factory({
800
+ MicrolinkError,
801
+ got,
802
+ flatten,
803
+ VERSION: '0.13.9'
804
+ });
805
+
806
+ lightweight$1.exports = mql;
807
+ var arrayBuffer = lightweight$1.exports.arrayBuffer = mql.extend({ responseType: 'arrayBuffer' });
808
+ var extend = lightweight$1.exports.extend = mql.extend;
809
+ var fetchFromApi = lightweight$1.exports.fetchFromApi = mql.fetchFromApi;
810
+ var getApiUrl = lightweight$1.exports.getApiUrl = mql.getApiUrl;
811
+ var mapRules = lightweight$1.exports.mapRules = mql.mapRules;
812
+ var MicrolinkError_1 = lightweight$1.exports.MicrolinkError = mql.MicrolinkError;
813
+ var version = lightweight$1.exports.version = mql.version;
814
+
815
+ var lightweightExports = lightweight$1.exports;
839
816
  var lightweight = /*@__PURE__*/getDefaultExportFromCjs(lightweightExports);
840
817
 
841
- return lightweight;
818
+ exports.MicrolinkError = MicrolinkError_1;
819
+ exports.arrayBuffer = arrayBuffer;
820
+ exports.default = lightweight;
821
+ exports.extend = extend;
822
+ exports.fetchFromApi = fetchFromApi;
823
+ exports.getApiUrl = getApiUrl;
824
+ exports.mapRules = mapRules;
825
+ exports.version = version;
826
+
827
+ Object.defineProperty(exports, '__esModule', { value: true });
842
828
 
843
829
  }));
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@microlink/mql",
3
3
  "description": "Microlink Query Language. The official HTTP client to interact with Microlink API for Node.js, browsers & Deno.",
4
4
  "homepage": "https://microlink.io/mql",
5
- "version": "0.13.7",
5
+ "version": "0.13.9",
6
6
  "types": "lightweight/index.d.ts",
7
7
  "exports": {
8
8
  "require": "./src/node.js",
@@ -46,7 +46,7 @@
46
46
  "query"
47
47
  ],
48
48
  "dependencies": {
49
- "flattie": "~1.1.0",
49
+ "flattie": "~1.1.1",
50
50
  "got": "~11.8.6",
51
51
  "whoops": "~4.1.7"
52
52
  },