@microlink/function 0.1.7 → 0.2.2
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/README.md +205 -0
- package/lightweight/index-bundled.d.ts +24234 -0
- package/lightweight/index.d.ts +33 -0
- package/lightweight/index.js +1 -0
- package/lightweight/package.json +5 -0
- package/package.json +84 -25
- package/src/node.js +24 -5
- package/dist/microlink-function.js +0 -770
- package/dist/microlink-function.js.map +0 -1
- package/dist/microlink-function.min.js +0 -3
- package/dist/microlink-function.min.js.map +0 -1
- package/src/browser.js +0 -7
- package/src/factory.js +0 -30
|
@@ -1,770 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('url')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['url'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.microlink = factory(global.require$$0));
|
|
5
|
-
})(this, (function (require$$0) { 'use strict';
|
|
6
|
-
|
|
7
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
-
|
|
9
|
-
var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
|
|
10
|
-
|
|
11
|
-
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
12
|
-
|
|
13
|
-
function getAugmentedNamespace(n) {
|
|
14
|
-
var f = n.default;
|
|
15
|
-
if (typeof f == "function") {
|
|
16
|
-
var a = function () {
|
|
17
|
-
return f.apply(this, arguments);
|
|
18
|
-
};
|
|
19
|
-
a.prototype = f.prototype;
|
|
20
|
-
} else a = {};
|
|
21
|
-
Object.defineProperty(a, '__esModule', {value: true});
|
|
22
|
-
Object.keys(n).forEach(function (k) {
|
|
23
|
-
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
24
|
-
Object.defineProperty(a, k, d.get ? d : {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () {
|
|
27
|
-
return n[k];
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
return a;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const factory$2 = ({ mql, VERSION, toCompress }) => {
|
|
35
|
-
const microlink = (code, mqlOpts, gotOpts) => {
|
|
36
|
-
const compress = toCompress(code);
|
|
37
|
-
|
|
38
|
-
return async (url, opts) => {
|
|
39
|
-
const { data } = await mql(
|
|
40
|
-
url,
|
|
41
|
-
{
|
|
42
|
-
function: await compress,
|
|
43
|
-
meta: false,
|
|
44
|
-
...mqlOpts,
|
|
45
|
-
...opts
|
|
46
|
-
},
|
|
47
|
-
gotOpts
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
return data.function
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
microlink.mql = mql;
|
|
55
|
-
microlink.render = mql.render;
|
|
56
|
-
microlink.version = VERSION;
|
|
57
|
-
|
|
58
|
-
return microlink
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
var factory_1$1 = factory$2;
|
|
62
|
-
|
|
63
|
-
const URL$1 = globalThis ? globalThis.URL : require$$0__default["default"].URL;
|
|
64
|
-
|
|
65
|
-
const REGEX_HTTP_PROTOCOL = /^https?:\/\//i;
|
|
66
|
-
|
|
67
|
-
var lightweight = url => {
|
|
68
|
-
try {
|
|
69
|
-
const { href } = new URL$1(url);
|
|
70
|
-
return REGEX_HTTP_PROTOCOL.test(href) && href
|
|
71
|
-
} catch (err) {
|
|
72
|
-
return false
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
var dist = {};
|
|
77
|
-
|
|
78
|
-
function iter(output, nullish, sep, val, key) {
|
|
79
|
-
var k, pfx = key ? (key + sep) : key;
|
|
80
|
-
|
|
81
|
-
if (val == null) {
|
|
82
|
-
if (nullish) output[key] = val;
|
|
83
|
-
} else if (typeof val != 'object') {
|
|
84
|
-
output[key] = val;
|
|
85
|
-
} else if (Array.isArray(val)) {
|
|
86
|
-
for (k=0; k < val.length; k++) {
|
|
87
|
-
iter(output, nullish, sep, val[k], pfx + k);
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
90
|
-
for (k in val) {
|
|
91
|
-
iter(output, nullish, sep, val[k], pfx + k);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function flattie(input, glue, toNull) {
|
|
97
|
-
var output = {};
|
|
98
|
-
if (typeof input == 'object') {
|
|
99
|
-
iter(output, !!toNull, glue || '.', input, '');
|
|
100
|
-
}
|
|
101
|
-
return output;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
dist.flattie = flattie;
|
|
105
|
-
|
|
106
|
-
function encode(obj, pfx) {
|
|
107
|
-
var k, i, tmp, str='';
|
|
108
|
-
|
|
109
|
-
for (k in obj) {
|
|
110
|
-
if ((tmp = obj[k]) !== void 0) {
|
|
111
|
-
if (Array.isArray(tmp)) {
|
|
112
|
-
for (i=0; i < tmp.length; i++) {
|
|
113
|
-
str && (str += '&');
|
|
114
|
-
str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp[i]);
|
|
115
|
-
}
|
|
116
|
-
} else {
|
|
117
|
-
str && (str += '&');
|
|
118
|
-
str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return (pfx || '') + str;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function toValue(mix) {
|
|
127
|
-
if (!mix) return '';
|
|
128
|
-
var str = decodeURIComponent(mix);
|
|
129
|
-
if (str === 'false') return false;
|
|
130
|
-
if (str === 'true') return true;
|
|
131
|
-
return (+str * 0 === 0) ? (+str) : str;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function decode(str) {
|
|
135
|
-
var tmp, k, out={}, arr=str.split('&');
|
|
136
|
-
|
|
137
|
-
while (tmp = arr.shift()) {
|
|
138
|
-
tmp = tmp.split('=');
|
|
139
|
-
k = tmp.shift();
|
|
140
|
-
if (out[k] !== void 0) {
|
|
141
|
-
out[k] = [].concat(out[k], toValue(tmp.shift()));
|
|
142
|
-
} else {
|
|
143
|
-
out[k] = toValue(tmp.shift());
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return out;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
var qss_m = /*#__PURE__*/Object.freeze({
|
|
151
|
-
__proto__: null,
|
|
152
|
-
encode: encode,
|
|
153
|
-
decode: decode
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
var require$$2 = /*@__PURE__*/getAugmentedNamespace(qss_m);
|
|
157
|
-
|
|
158
|
-
const ENDPOINT = {
|
|
159
|
-
FREE: 'https://api.microlink.io',
|
|
160
|
-
PRO: 'https://pro.microlink.io'
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
const isObject = input => input !== null && typeof input === 'object';
|
|
164
|
-
|
|
165
|
-
const isBuffer = input =>
|
|
166
|
-
input != null &&
|
|
167
|
-
input.constructor != null &&
|
|
168
|
-
typeof input.constructor.isBuffer === 'function' &&
|
|
169
|
-
input.constructor.isBuffer(input);
|
|
170
|
-
|
|
171
|
-
const parseBody = (input, error, url) => {
|
|
172
|
-
try {
|
|
173
|
-
return JSON.parse(input)
|
|
174
|
-
} catch (_) {
|
|
175
|
-
const message = input || error.message;
|
|
176
|
-
|
|
177
|
-
return {
|
|
178
|
-
status: 'error',
|
|
179
|
-
data: { url: message },
|
|
180
|
-
more: 'https://microlink.io/efatalclient',
|
|
181
|
-
code: 'EFATALCLIENT',
|
|
182
|
-
message,
|
|
183
|
-
url
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
const factory$1 = ({
|
|
189
|
-
VERSION,
|
|
190
|
-
MicrolinkError,
|
|
191
|
-
urlHttp,
|
|
192
|
-
stringify,
|
|
193
|
-
got,
|
|
194
|
-
flatten
|
|
195
|
-
}) => {
|
|
196
|
-
const assertUrl = (url = '') => {
|
|
197
|
-
if (!urlHttp(url)) {
|
|
198
|
-
const message = `The \`url\` as \`${url}\` is not valid. Ensure it has protocol (http or https) and hostname.`;
|
|
199
|
-
throw new MicrolinkError({
|
|
200
|
-
status: 'fail',
|
|
201
|
-
data: { url: message },
|
|
202
|
-
more: 'https://microlink.io/docs/api/api-parameters/url',
|
|
203
|
-
code: 'EINVALURLCLIENT',
|
|
204
|
-
message,
|
|
205
|
-
url
|
|
206
|
-
})
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
const mapRules = rules => {
|
|
211
|
-
if (!isObject(rules)) return
|
|
212
|
-
const flatRules = flatten(rules);
|
|
213
|
-
return Object.keys(flatRules).reduce((acc, key) => {
|
|
214
|
-
acc[`data.${key}`] = flatRules[key].toString();
|
|
215
|
-
return acc
|
|
216
|
-
}, {})
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
const fetchFromApi = async (apiUrl, opts = {}, retryCount = 0) => {
|
|
220
|
-
try {
|
|
221
|
-
const response = await got(apiUrl, opts);
|
|
222
|
-
return opts.responseType === 'buffer'
|
|
223
|
-
? { body: response.body, response }
|
|
224
|
-
: { ...response.body, response }
|
|
225
|
-
} catch (err) {
|
|
226
|
-
const { response = {} } = err;
|
|
227
|
-
const {
|
|
228
|
-
statusCode,
|
|
229
|
-
body: rawBody,
|
|
230
|
-
headers = {},
|
|
231
|
-
url: uri = apiUrl
|
|
232
|
-
} = response;
|
|
233
|
-
const isBodyBuffer = isBuffer(rawBody);
|
|
234
|
-
|
|
235
|
-
const body =
|
|
236
|
-
isObject(rawBody) && !isBodyBuffer
|
|
237
|
-
? rawBody
|
|
238
|
-
: parseBody(isBodyBuffer ? rawBody.toString() : rawBody, err, uri);
|
|
239
|
-
|
|
240
|
-
if (body.code === 'EFATALCLIENT' && retryCount++ < 2) {
|
|
241
|
-
return fetchFromApi(apiUrl, opts, retryCount)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
throw new MicrolinkError({
|
|
245
|
-
...body,
|
|
246
|
-
message: body.message,
|
|
247
|
-
url: uri,
|
|
248
|
-
statusCode,
|
|
249
|
-
headers
|
|
250
|
-
})
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
const getApiUrl = (
|
|
255
|
-
url,
|
|
256
|
-
{ data, apiKey, endpoint, retry, cache, ...opts } = {},
|
|
257
|
-
{ responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
|
|
258
|
-
) => {
|
|
259
|
-
const isPro = !!apiKey;
|
|
260
|
-
const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE'];
|
|
261
|
-
|
|
262
|
-
const apiUrl = `${apiEndpoint}?${stringify({
|
|
263
|
-
url,
|
|
264
|
-
...mapRules(data),
|
|
265
|
-
...flatten(opts)
|
|
266
|
-
})}`;
|
|
267
|
-
|
|
268
|
-
const headers = isPro
|
|
269
|
-
? { ...gotHeaders, 'x-api-key': apiKey }
|
|
270
|
-
: { ...gotHeaders };
|
|
271
|
-
return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const createMql = defaultOpts => async (url, opts, gotOpts) => {
|
|
275
|
-
assertUrl(url);
|
|
276
|
-
const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
|
|
277
|
-
...defaultOpts,
|
|
278
|
-
...gotOpts
|
|
279
|
-
});
|
|
280
|
-
return fetchFromApi(apiUrl, fetchOpts)
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
const mql = createMql();
|
|
284
|
-
mql.MicrolinkError = MicrolinkError;
|
|
285
|
-
mql.getApiUrl = getApiUrl;
|
|
286
|
-
mql.fetchFromApi = fetchFromApi;
|
|
287
|
-
mql.mapRules = mapRules;
|
|
288
|
-
mql.version = VERSION;
|
|
289
|
-
mql.stream = got.stream;
|
|
290
|
-
mql.buffer = createMql({ responseType: 'buffer' });
|
|
291
|
-
|
|
292
|
-
return mql
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
var factory_1 = factory$1;
|
|
296
|
-
|
|
297
|
-
var ky$1 = {exports: {}};
|
|
298
|
-
|
|
299
|
-
(function (module, exports) {
|
|
300
|
-
(function (global, factory) {
|
|
301
|
-
factory(exports) ;
|
|
302
|
-
})(commonjsGlobal, (function (exports) {
|
|
303
|
-
// eslint-lint-disable-next-line @typescript-eslint/naming-convention
|
|
304
|
-
class HTTPError extends Error {
|
|
305
|
-
constructor(response, request, options) {
|
|
306
|
-
const code = (response.status || response.status === 0) ? response.status : '';
|
|
307
|
-
const title = response.statusText || '';
|
|
308
|
-
const status = `${code} ${title}`.trim();
|
|
309
|
-
const reason = status ? `status code ${status}` : 'an unknown error';
|
|
310
|
-
super(`Request failed with ${reason}`);
|
|
311
|
-
this.name = 'HTTPError';
|
|
312
|
-
this.response = response;
|
|
313
|
-
this.request = request;
|
|
314
|
-
this.options = options;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
class TimeoutError extends Error {
|
|
319
|
-
constructor(request) {
|
|
320
|
-
super('Request timed out');
|
|
321
|
-
this.name = 'TimeoutError';
|
|
322
|
-
this.request = request;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
327
|
-
const isObject = (value) => value !== null && typeof value === 'object';
|
|
328
|
-
|
|
329
|
-
const validateAndMerge = (...sources) => {
|
|
330
|
-
for (const source of sources) {
|
|
331
|
-
if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {
|
|
332
|
-
throw new TypeError('The `options` argument must be an object');
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
return deepMerge({}, ...sources);
|
|
336
|
-
};
|
|
337
|
-
const mergeHeaders = (source1 = {}, source2 = {}) => {
|
|
338
|
-
const result = new globalThis.Headers(source1);
|
|
339
|
-
const isHeadersInstance = source2 instanceof globalThis.Headers;
|
|
340
|
-
const source = new globalThis.Headers(source2);
|
|
341
|
-
for (const [key, value] of source.entries()) {
|
|
342
|
-
if ((isHeadersInstance && value === 'undefined') || value === undefined) {
|
|
343
|
-
result.delete(key);
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
result.set(key, value);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
return result;
|
|
350
|
-
};
|
|
351
|
-
// TODO: Make this strongly-typed (no `any`).
|
|
352
|
-
const deepMerge = (...sources) => {
|
|
353
|
-
let returnValue = {};
|
|
354
|
-
let headers = {};
|
|
355
|
-
for (const source of sources) {
|
|
356
|
-
if (Array.isArray(source)) {
|
|
357
|
-
if (!Array.isArray(returnValue)) {
|
|
358
|
-
returnValue = [];
|
|
359
|
-
}
|
|
360
|
-
returnValue = [...returnValue, ...source];
|
|
361
|
-
}
|
|
362
|
-
else if (isObject(source)) {
|
|
363
|
-
for (let [key, value] of Object.entries(source)) {
|
|
364
|
-
if (isObject(value) && key in returnValue) {
|
|
365
|
-
value = deepMerge(returnValue[key], value);
|
|
366
|
-
}
|
|
367
|
-
returnValue = { ...returnValue, [key]: value };
|
|
368
|
-
}
|
|
369
|
-
if (isObject(source.headers)) {
|
|
370
|
-
headers = mergeHeaders(headers, source.headers);
|
|
371
|
-
returnValue.headers = headers;
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
return returnValue;
|
|
376
|
-
};
|
|
377
|
-
|
|
378
|
-
const supportsAbortController = typeof globalThis.AbortController === 'function';
|
|
379
|
-
const supportsStreams = typeof globalThis.ReadableStream === 'function';
|
|
380
|
-
const supportsFormData = typeof globalThis.FormData === 'function';
|
|
381
|
-
const requestMethods = ['get', 'post', 'put', 'patch', 'head', 'delete'];
|
|
382
|
-
const responseTypes = {
|
|
383
|
-
json: 'application/json',
|
|
384
|
-
text: 'text/*',
|
|
385
|
-
formData: 'multipart/form-data',
|
|
386
|
-
arrayBuffer: '*/*',
|
|
387
|
-
blob: '*/*',
|
|
388
|
-
};
|
|
389
|
-
// The maximum value of a 32bit int (see issue #117)
|
|
390
|
-
const maxSafeTimeout = 2147483647;
|
|
391
|
-
const stop = Symbol('stop');
|
|
392
|
-
|
|
393
|
-
const normalizeRequestMethod = (input) => requestMethods.includes(input) ? input.toUpperCase() : input;
|
|
394
|
-
const retryMethods = ['get', 'put', 'head', 'delete', 'options', 'trace'];
|
|
395
|
-
const retryStatusCodes = [408, 413, 429, 500, 502, 503, 504];
|
|
396
|
-
const retryAfterStatusCodes = [413, 429, 503];
|
|
397
|
-
const defaultRetryOptions = {
|
|
398
|
-
limit: 2,
|
|
399
|
-
methods: retryMethods,
|
|
400
|
-
statusCodes: retryStatusCodes,
|
|
401
|
-
afterStatusCodes: retryAfterStatusCodes,
|
|
402
|
-
maxRetryAfter: Number.POSITIVE_INFINITY,
|
|
403
|
-
};
|
|
404
|
-
const normalizeRetryOptions = (retry = {}) => {
|
|
405
|
-
if (typeof retry === 'number') {
|
|
406
|
-
return {
|
|
407
|
-
...defaultRetryOptions,
|
|
408
|
-
limit: retry,
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
if (retry.methods && !Array.isArray(retry.methods)) {
|
|
412
|
-
throw new Error('retry.methods must be an array');
|
|
413
|
-
}
|
|
414
|
-
if (retry.statusCodes && !Array.isArray(retry.statusCodes)) {
|
|
415
|
-
throw new Error('retry.statusCodes must be an array');
|
|
416
|
-
}
|
|
417
|
-
return {
|
|
418
|
-
...defaultRetryOptions,
|
|
419
|
-
...retry,
|
|
420
|
-
afterStatusCodes: retryAfterStatusCodes,
|
|
421
|
-
};
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
// `Promise.race()` workaround (#91)
|
|
425
|
-
const timeout = async (request, abortController, options) => new Promise((resolve, reject) => {
|
|
426
|
-
const timeoutId = setTimeout(() => {
|
|
427
|
-
if (abortController) {
|
|
428
|
-
abortController.abort();
|
|
429
|
-
}
|
|
430
|
-
reject(new TimeoutError(request));
|
|
431
|
-
}, options.timeout);
|
|
432
|
-
void options
|
|
433
|
-
.fetch(request)
|
|
434
|
-
.then(resolve)
|
|
435
|
-
.catch(reject)
|
|
436
|
-
.then(() => {
|
|
437
|
-
clearTimeout(timeoutId);
|
|
438
|
-
});
|
|
439
|
-
});
|
|
440
|
-
const delay = async (ms) => new Promise(resolve => {
|
|
441
|
-
setTimeout(resolve, ms);
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
class Ky {
|
|
445
|
-
// eslint-disable-next-line complexity
|
|
446
|
-
constructor(input, options = {}) {
|
|
447
|
-
var _a, _b, _c;
|
|
448
|
-
this._retryCount = 0;
|
|
449
|
-
this._input = input;
|
|
450
|
-
this._options = {
|
|
451
|
-
// TODO: credentials can be removed when the spec change is implemented in all browsers. Context: https://www.chromestatus.com/feature/4539473312350208
|
|
452
|
-
credentials: this._input.credentials || 'same-origin',
|
|
453
|
-
...options,
|
|
454
|
-
headers: mergeHeaders(this._input.headers, options.headers),
|
|
455
|
-
hooks: deepMerge({
|
|
456
|
-
beforeRequest: [],
|
|
457
|
-
beforeRetry: [],
|
|
458
|
-
beforeError: [],
|
|
459
|
-
afterResponse: [],
|
|
460
|
-
}, options.hooks),
|
|
461
|
-
method: normalizeRequestMethod((_a = options.method) !== null && _a !== void 0 ? _a : this._input.method),
|
|
462
|
-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
463
|
-
prefixUrl: String(options.prefixUrl || ''),
|
|
464
|
-
retry: normalizeRetryOptions(options.retry),
|
|
465
|
-
throwHttpErrors: options.throwHttpErrors !== false,
|
|
466
|
-
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout,
|
|
467
|
-
fetch: (_b = options.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch.bind(globalThis),
|
|
468
|
-
};
|
|
469
|
-
if (typeof this._input !== 'string' && !(this._input instanceof URL || this._input instanceof globalThis.Request)) {
|
|
470
|
-
throw new TypeError('`input` must be a string, URL, or Request');
|
|
471
|
-
}
|
|
472
|
-
if (this._options.prefixUrl && typeof this._input === 'string') {
|
|
473
|
-
if (this._input.startsWith('/')) {
|
|
474
|
-
throw new Error('`input` must not begin with a slash when using `prefixUrl`');
|
|
475
|
-
}
|
|
476
|
-
if (!this._options.prefixUrl.endsWith('/')) {
|
|
477
|
-
this._options.prefixUrl += '/';
|
|
478
|
-
}
|
|
479
|
-
this._input = this._options.prefixUrl + this._input;
|
|
480
|
-
}
|
|
481
|
-
if (supportsAbortController) {
|
|
482
|
-
this.abortController = new globalThis.AbortController();
|
|
483
|
-
if (this._options.signal) {
|
|
484
|
-
this._options.signal.addEventListener('abort', () => {
|
|
485
|
-
this.abortController.abort();
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
this._options.signal = this.abortController.signal;
|
|
489
|
-
}
|
|
490
|
-
this.request = new globalThis.Request(this._input, this._options);
|
|
491
|
-
if (this._options.searchParams) {
|
|
492
|
-
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
493
|
-
const textSearchParams = typeof this._options.searchParams === 'string'
|
|
494
|
-
? this._options.searchParams.replace(/^\?/, '')
|
|
495
|
-
: new URLSearchParams(this._options.searchParams).toString();
|
|
496
|
-
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
497
|
-
const searchParams = '?' + textSearchParams;
|
|
498
|
-
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);
|
|
499
|
-
// To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one
|
|
500
|
-
if (((supportsFormData && this._options.body instanceof globalThis.FormData)
|
|
501
|
-
|| this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers['content-type'])) {
|
|
502
|
-
this.request.headers.delete('content-type');
|
|
503
|
-
}
|
|
504
|
-
this.request = new globalThis.Request(new globalThis.Request(url, this.request), this._options);
|
|
505
|
-
}
|
|
506
|
-
if (this._options.json !== undefined) {
|
|
507
|
-
this._options.body = JSON.stringify(this._options.json);
|
|
508
|
-
this.request.headers.set('content-type', (_c = this._options.headers.get('content-type')) !== null && _c !== void 0 ? _c : 'application/json');
|
|
509
|
-
this.request = new globalThis.Request(this.request, { body: this._options.body });
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
513
|
-
static create(input, options) {
|
|
514
|
-
const ky = new Ky(input, options);
|
|
515
|
-
const fn = async () => {
|
|
516
|
-
if (ky._options.timeout > maxSafeTimeout) {
|
|
517
|
-
throw new RangeError(`The \`timeout\` option cannot be greater than ${maxSafeTimeout}`);
|
|
518
|
-
}
|
|
519
|
-
// Delay the fetch so that body method shortcuts can set the Accept header
|
|
520
|
-
await Promise.resolve();
|
|
521
|
-
let response = await ky._fetch();
|
|
522
|
-
for (const hook of ky._options.hooks.afterResponse) {
|
|
523
|
-
// eslint-disable-next-line no-await-in-loop
|
|
524
|
-
const modifiedResponse = await hook(ky.request, ky._options, ky._decorateResponse(response.clone()));
|
|
525
|
-
if (modifiedResponse instanceof globalThis.Response) {
|
|
526
|
-
response = modifiedResponse;
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
ky._decorateResponse(response);
|
|
530
|
-
if (!response.ok && ky._options.throwHttpErrors) {
|
|
531
|
-
let error = new HTTPError(response, ky.request, ky._options);
|
|
532
|
-
for (const hook of ky._options.hooks.beforeError) {
|
|
533
|
-
// eslint-disable-next-line no-await-in-loop
|
|
534
|
-
error = await hook(error);
|
|
535
|
-
}
|
|
536
|
-
throw error;
|
|
537
|
-
}
|
|
538
|
-
// If `onDownloadProgress` is passed, it uses the stream API internally
|
|
539
|
-
/* istanbul ignore next */
|
|
540
|
-
if (ky._options.onDownloadProgress) {
|
|
541
|
-
if (typeof ky._options.onDownloadProgress !== 'function') {
|
|
542
|
-
throw new TypeError('The `onDownloadProgress` option must be a function');
|
|
543
|
-
}
|
|
544
|
-
if (!supportsStreams) {
|
|
545
|
-
throw new Error('Streams are not supported in your environment. `ReadableStream` is missing.');
|
|
546
|
-
}
|
|
547
|
-
return ky._stream(response.clone(), ky._options.onDownloadProgress);
|
|
548
|
-
}
|
|
549
|
-
return response;
|
|
550
|
-
};
|
|
551
|
-
const isRetriableMethod = ky._options.retry.methods.includes(ky.request.method.toLowerCase());
|
|
552
|
-
const result = (isRetriableMethod ? ky._retry(fn) : fn());
|
|
553
|
-
for (const [type, mimeType] of Object.entries(responseTypes)) {
|
|
554
|
-
result[type] = async () => {
|
|
555
|
-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
556
|
-
ky.request.headers.set('accept', ky.request.headers.get('accept') || mimeType);
|
|
557
|
-
const awaitedResult = await result;
|
|
558
|
-
const response = awaitedResult.clone();
|
|
559
|
-
if (type === 'json') {
|
|
560
|
-
if (response.status === 204) {
|
|
561
|
-
return '';
|
|
562
|
-
}
|
|
563
|
-
if (options.parseJson) {
|
|
564
|
-
return options.parseJson(await response.text());
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
return response[type]();
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
return result;
|
|
571
|
-
}
|
|
572
|
-
_calculateRetryDelay(error) {
|
|
573
|
-
this._retryCount++;
|
|
574
|
-
if (this._retryCount < this._options.retry.limit && !(error instanceof TimeoutError)) {
|
|
575
|
-
if (error instanceof HTTPError) {
|
|
576
|
-
if (!this._options.retry.statusCodes.includes(error.response.status)) {
|
|
577
|
-
return 0;
|
|
578
|
-
}
|
|
579
|
-
const retryAfter = error.response.headers.get('Retry-After');
|
|
580
|
-
if (retryAfter && this._options.retry.afterStatusCodes.includes(error.response.status)) {
|
|
581
|
-
let after = Number(retryAfter);
|
|
582
|
-
if (Number.isNaN(after)) {
|
|
583
|
-
after = Date.parse(retryAfter) - Date.now();
|
|
584
|
-
}
|
|
585
|
-
else {
|
|
586
|
-
after *= 1000;
|
|
587
|
-
}
|
|
588
|
-
if (typeof this._options.retry.maxRetryAfter !== 'undefined' && after > this._options.retry.maxRetryAfter) {
|
|
589
|
-
return 0;
|
|
590
|
-
}
|
|
591
|
-
return after;
|
|
592
|
-
}
|
|
593
|
-
if (error.response.status === 413) {
|
|
594
|
-
return 0;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
const BACKOFF_FACTOR = 0.3;
|
|
598
|
-
return BACKOFF_FACTOR * (2 ** (this._retryCount - 1)) * 1000;
|
|
599
|
-
}
|
|
600
|
-
return 0;
|
|
601
|
-
}
|
|
602
|
-
_decorateResponse(response) {
|
|
603
|
-
if (this._options.parseJson) {
|
|
604
|
-
response.json = async () => this._options.parseJson(await response.text());
|
|
605
|
-
}
|
|
606
|
-
return response;
|
|
607
|
-
}
|
|
608
|
-
async _retry(fn) {
|
|
609
|
-
try {
|
|
610
|
-
return await fn();
|
|
611
|
-
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
|
|
612
|
-
}
|
|
613
|
-
catch (error) {
|
|
614
|
-
const ms = Math.min(this._calculateRetryDelay(error), maxSafeTimeout);
|
|
615
|
-
if (ms !== 0 && this._retryCount > 0) {
|
|
616
|
-
await delay(ms);
|
|
617
|
-
for (const hook of this._options.hooks.beforeRetry) {
|
|
618
|
-
// eslint-disable-next-line no-await-in-loop
|
|
619
|
-
const hookResult = await hook({
|
|
620
|
-
request: this.request,
|
|
621
|
-
options: this._options,
|
|
622
|
-
error: error,
|
|
623
|
-
retryCount: this._retryCount,
|
|
624
|
-
});
|
|
625
|
-
// If `stop` is returned from the hook, the retry process is stopped
|
|
626
|
-
if (hookResult === stop) {
|
|
627
|
-
return;
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
return this._retry(fn);
|
|
631
|
-
}
|
|
632
|
-
throw error;
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
async _fetch() {
|
|
636
|
-
for (const hook of this._options.hooks.beforeRequest) {
|
|
637
|
-
// eslint-disable-next-line no-await-in-loop
|
|
638
|
-
const result = await hook(this.request, this._options);
|
|
639
|
-
if (result instanceof Request) {
|
|
640
|
-
this.request = result;
|
|
641
|
-
break;
|
|
642
|
-
}
|
|
643
|
-
if (result instanceof Response) {
|
|
644
|
-
return result;
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
if (this._options.timeout === false) {
|
|
648
|
-
return this._options.fetch(this.request.clone());
|
|
649
|
-
}
|
|
650
|
-
return timeout(this.request.clone(), this.abortController, this._options);
|
|
651
|
-
}
|
|
652
|
-
/* istanbul ignore next */
|
|
653
|
-
_stream(response, onDownloadProgress) {
|
|
654
|
-
const totalBytes = Number(response.headers.get('content-length')) || 0;
|
|
655
|
-
let transferredBytes = 0;
|
|
656
|
-
return new globalThis.Response(new globalThis.ReadableStream({
|
|
657
|
-
async start(controller) {
|
|
658
|
-
const reader = response.body.getReader();
|
|
659
|
-
if (onDownloadProgress) {
|
|
660
|
-
onDownloadProgress({ percent: 0, transferredBytes: 0, totalBytes }, new Uint8Array());
|
|
661
|
-
}
|
|
662
|
-
async function read() {
|
|
663
|
-
const { done, value } = await reader.read();
|
|
664
|
-
if (done) {
|
|
665
|
-
controller.close();
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
668
|
-
if (onDownloadProgress) {
|
|
669
|
-
transferredBytes += value.byteLength;
|
|
670
|
-
const percent = totalBytes === 0 ? 0 : transferredBytes / totalBytes;
|
|
671
|
-
onDownloadProgress({ percent, transferredBytes, totalBytes }, value);
|
|
672
|
-
}
|
|
673
|
-
controller.enqueue(value);
|
|
674
|
-
await read();
|
|
675
|
-
}
|
|
676
|
-
await read();
|
|
677
|
-
},
|
|
678
|
-
}));
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
/*! MIT License © Sindre Sorhus */
|
|
683
|
-
const createInstance = (defaults) => {
|
|
684
|
-
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
685
|
-
const ky = (input, options) => Ky.create(input, validateAndMerge(defaults, options));
|
|
686
|
-
for (const method of requestMethods) {
|
|
687
|
-
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
688
|
-
ky[method] = (input, options) => Ky.create(input, validateAndMerge(defaults, options, { method }));
|
|
689
|
-
}
|
|
690
|
-
ky.create = (newDefaults) => createInstance(validateAndMerge(newDefaults));
|
|
691
|
-
ky.extend = (newDefaults) => createInstance(validateAndMerge(defaults, newDefaults));
|
|
692
|
-
ky.stop = stop;
|
|
693
|
-
return ky;
|
|
694
|
-
};
|
|
695
|
-
const ky = createInstance();
|
|
696
|
-
|
|
697
|
-
exports.HTTPError = HTTPError;
|
|
698
|
-
exports.TimeoutError = TimeoutError;
|
|
699
|
-
exports["default"] = ky;
|
|
700
|
-
|
|
701
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
702
|
-
|
|
703
|
-
}));
|
|
704
|
-
} (ky$1, ky$1.exports));
|
|
705
|
-
|
|
706
|
-
const urlHttp = lightweight;
|
|
707
|
-
const { flattie: flatten } = dist;
|
|
708
|
-
const { encode: stringify } = require$$2;
|
|
709
|
-
|
|
710
|
-
const factory = factory_1;
|
|
711
|
-
const { default: ky } = ky$1.exports;
|
|
712
|
-
|
|
713
|
-
class MicrolinkError extends Error {
|
|
714
|
-
name = 'MicrolinkError'
|
|
715
|
-
constructor (props) {
|
|
716
|
-
super();
|
|
717
|
-
Object.assign(this, props);
|
|
718
|
-
this.description = this.message;
|
|
719
|
-
this.message = this.code
|
|
720
|
-
? `${this.code}, ${this.description}`
|
|
721
|
-
: this.description;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
const got = async (url, opts) => {
|
|
726
|
-
try {
|
|
727
|
-
if (opts.timeout === undefined) opts.timeout = false;
|
|
728
|
-
const response = await ky(url, opts);
|
|
729
|
-
const body = await response.json();
|
|
730
|
-
const { headers, status: statusCode } = response;
|
|
731
|
-
return { url: response.url, body, headers, statusCode }
|
|
732
|
-
} catch (err) {
|
|
733
|
-
if (err.response) {
|
|
734
|
-
const { response } = err;
|
|
735
|
-
err.response = {
|
|
736
|
-
...response,
|
|
737
|
-
headers: Array.from(response.headers.entries()).reduce(
|
|
738
|
-
(acc, [key, value]) => {
|
|
739
|
-
acc[key] = value;
|
|
740
|
-
return acc
|
|
741
|
-
},
|
|
742
|
-
{}
|
|
743
|
-
),
|
|
744
|
-
statusCode: response.status,
|
|
745
|
-
body: await response.text()
|
|
746
|
-
};
|
|
747
|
-
}
|
|
748
|
-
throw err
|
|
749
|
-
}
|
|
750
|
-
};
|
|
751
|
-
|
|
752
|
-
var browser$1 = factory({
|
|
753
|
-
MicrolinkError,
|
|
754
|
-
urlHttp,
|
|
755
|
-
stringify,
|
|
756
|
-
got,
|
|
757
|
-
flatten,
|
|
758
|
-
VERSION: '0.10.28'
|
|
759
|
-
});
|
|
760
|
-
|
|
761
|
-
var browser = factory_1$1({
|
|
762
|
-
mql: browser$1,
|
|
763
|
-
toCompress: code => code.toString(),
|
|
764
|
-
VERSION: '0.1.7'
|
|
765
|
-
});
|
|
766
|
-
|
|
767
|
-
return browser;
|
|
768
|
-
|
|
769
|
-
}));
|
|
770
|
-
//# sourceMappingURL=microlink-function.js.map
|