| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | import bind from './helpers/bind.js';
|
|---|
| 4 |
|
|---|
| 5 | // utils is a library of generic helper functions non-specific to axios
|
|---|
| 6 |
|
|---|
| 7 | const { toString } = Object.prototype;
|
|---|
| 8 | const { getPrototypeOf } = Object;
|
|---|
| 9 | const { iterator, toStringTag } = Symbol;
|
|---|
| 10 |
|
|---|
| 11 | const kindOf = ((cache) => (thing) => {
|
|---|
| 12 | const str = toString.call(thing);
|
|---|
| 13 | return cache[str] || (cache[str] = str.slice(8, -1).toLowerCase());
|
|---|
| 14 | })(Object.create(null));
|
|---|
| 15 |
|
|---|
| 16 | const kindOfTest = (type) => {
|
|---|
| 17 | type = type.toLowerCase();
|
|---|
| 18 | return (thing) => kindOf(thing) === type;
|
|---|
| 19 | };
|
|---|
| 20 |
|
|---|
| 21 | const typeOfTest = (type) => (thing) => typeof thing === type;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Determine if a value is a non-null object
|
|---|
| 25 | *
|
|---|
| 26 | * @param {Object} val The value to test
|
|---|
| 27 | *
|
|---|
| 28 | * @returns {boolean} True if value is an Array, otherwise false
|
|---|
| 29 | */
|
|---|
| 30 | const { isArray } = Array;
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Determine if a value is undefined
|
|---|
| 34 | *
|
|---|
| 35 | * @param {*} val The value to test
|
|---|
| 36 | *
|
|---|
| 37 | * @returns {boolean} True if the value is undefined, otherwise false
|
|---|
| 38 | */
|
|---|
| 39 | const isUndefined = typeOfTest('undefined');
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Determine if a value is a Buffer
|
|---|
| 43 | *
|
|---|
| 44 | * @param {*} val The value to test
|
|---|
| 45 | *
|
|---|
| 46 | * @returns {boolean} True if value is a Buffer, otherwise false
|
|---|
| 47 | */
|
|---|
| 48 | function isBuffer(val) {
|
|---|
| 49 | return (
|
|---|
| 50 | val !== null &&
|
|---|
| 51 | !isUndefined(val) &&
|
|---|
| 52 | val.constructor !== null &&
|
|---|
| 53 | !isUndefined(val.constructor) &&
|
|---|
| 54 | isFunction(val.constructor.isBuffer) &&
|
|---|
| 55 | val.constructor.isBuffer(val)
|
|---|
| 56 | );
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Determine if a value is an ArrayBuffer
|
|---|
| 61 | *
|
|---|
| 62 | * @param {*} val The value to test
|
|---|
| 63 | *
|
|---|
| 64 | * @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
|---|
| 65 | */
|
|---|
| 66 | const isArrayBuffer = kindOfTest('ArrayBuffer');
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Determine if a value is a view on an ArrayBuffer
|
|---|
| 70 | *
|
|---|
| 71 | * @param {*} val The value to test
|
|---|
| 72 | *
|
|---|
| 73 | * @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
|---|
| 74 | */
|
|---|
| 75 | function isArrayBufferView(val) {
|
|---|
| 76 | let result;
|
|---|
| 77 | if (typeof ArrayBuffer !== 'undefined' && ArrayBuffer.isView) {
|
|---|
| 78 | result = ArrayBuffer.isView(val);
|
|---|
| 79 | } else {
|
|---|
| 80 | result = val && val.buffer && isArrayBuffer(val.buffer);
|
|---|
| 81 | }
|
|---|
| 82 | return result;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Determine if a value is a String
|
|---|
| 87 | *
|
|---|
| 88 | * @param {*} val The value to test
|
|---|
| 89 | *
|
|---|
| 90 | * @returns {boolean} True if value is a String, otherwise false
|
|---|
| 91 | */
|
|---|
| 92 | const isString = typeOfTest('string');
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Determine if a value is a Function
|
|---|
| 96 | *
|
|---|
| 97 | * @param {*} val The value to test
|
|---|
| 98 | * @returns {boolean} True if value is a Function, otherwise false
|
|---|
| 99 | */
|
|---|
| 100 | const isFunction = typeOfTest('function');
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Determine if a value is a Number
|
|---|
| 104 | *
|
|---|
| 105 | * @param {*} val The value to test
|
|---|
| 106 | *
|
|---|
| 107 | * @returns {boolean} True if value is a Number, otherwise false
|
|---|
| 108 | */
|
|---|
| 109 | const isNumber = typeOfTest('number');
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Determine if a value is an Object
|
|---|
| 113 | *
|
|---|
| 114 | * @param {*} thing The value to test
|
|---|
| 115 | *
|
|---|
| 116 | * @returns {boolean} True if value is an Object, otherwise false
|
|---|
| 117 | */
|
|---|
| 118 | const isObject = (thing) => thing !== null && typeof thing === 'object';
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Determine if a value is a Boolean
|
|---|
| 122 | *
|
|---|
| 123 | * @param {*} thing The value to test
|
|---|
| 124 | * @returns {boolean} True if value is a Boolean, otherwise false
|
|---|
| 125 | */
|
|---|
| 126 | const isBoolean = (thing) => thing === true || thing === false;
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Determine if a value is a plain Object
|
|---|
| 130 | *
|
|---|
| 131 | * @param {*} val The value to test
|
|---|
| 132 | *
|
|---|
| 133 | * @returns {boolean} True if value is a plain Object, otherwise false
|
|---|
| 134 | */
|
|---|
| 135 | const isPlainObject = (val) => {
|
|---|
| 136 | if (kindOf(val) !== 'object') {
|
|---|
| 137 | return false;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | const prototype = getPrototypeOf(val);
|
|---|
| 141 | return (
|
|---|
| 142 | (prototype === null ||
|
|---|
| 143 | prototype === Object.prototype ||
|
|---|
| 144 | Object.getPrototypeOf(prototype) === null) &&
|
|---|
| 145 | !(toStringTag in val) &&
|
|---|
| 146 | !(iterator in val)
|
|---|
| 147 | );
|
|---|
| 148 | };
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Determine if a value is an empty object (safely handles Buffers)
|
|---|
| 152 | *
|
|---|
| 153 | * @param {*} val The value to test
|
|---|
| 154 | *
|
|---|
| 155 | * @returns {boolean} True if value is an empty object, otherwise false
|
|---|
| 156 | */
|
|---|
| 157 | const isEmptyObject = (val) => {
|
|---|
| 158 | // Early return for non-objects or Buffers to prevent RangeError
|
|---|
| 159 | if (!isObject(val) || isBuffer(val)) {
|
|---|
| 160 | return false;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | try {
|
|---|
| 164 | return Object.keys(val).length === 0 && Object.getPrototypeOf(val) === Object.prototype;
|
|---|
| 165 | } catch (e) {
|
|---|
| 166 | // Fallback for any other objects that might cause RangeError with Object.keys()
|
|---|
| 167 | return false;
|
|---|
| 168 | }
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | /**
|
|---|
| 172 | * Determine if a value is a Date
|
|---|
| 173 | *
|
|---|
| 174 | * @param {*} val The value to test
|
|---|
| 175 | *
|
|---|
| 176 | * @returns {boolean} True if value is a Date, otherwise false
|
|---|
| 177 | */
|
|---|
| 178 | const isDate = kindOfTest('Date');
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Determine if a value is a File
|
|---|
| 182 | *
|
|---|
| 183 | * @param {*} val The value to test
|
|---|
| 184 | *
|
|---|
| 185 | * @returns {boolean} True if value is a File, otherwise false
|
|---|
| 186 | */
|
|---|
| 187 | const isFile = kindOfTest('File');
|
|---|
| 188 |
|
|---|
| 189 | /**
|
|---|
| 190 | * Determine if a value is a React Native Blob
|
|---|
| 191 | * React Native "blob": an object with a `uri` attribute. Optionally, it can
|
|---|
| 192 | * also have a `name` and `type` attribute to specify filename and content type
|
|---|
| 193 | *
|
|---|
| 194 | * @see https://github.com/facebook/react-native/blob/26684cf3adf4094eb6c405d345a75bf8c7c0bf88/Libraries/Network/FormData.js#L68-L71
|
|---|
| 195 | *
|
|---|
| 196 | * @param {*} value The value to test
|
|---|
| 197 | *
|
|---|
| 198 | * @returns {boolean} True if value is a React Native Blob, otherwise false
|
|---|
| 199 | */
|
|---|
| 200 | const isReactNativeBlob = (value) => {
|
|---|
| 201 | return !!(value && typeof value.uri !== 'undefined');
|
|---|
| 202 | };
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | * Determine if environment is React Native
|
|---|
| 206 | * ReactNative `FormData` has a non-standard `getParts()` method
|
|---|
| 207 | *
|
|---|
| 208 | * @param {*} formData The formData to test
|
|---|
| 209 | *
|
|---|
| 210 | * @returns {boolean} True if environment is React Native, otherwise false
|
|---|
| 211 | */
|
|---|
| 212 | const isReactNative = (formData) => formData && typeof formData.getParts !== 'undefined';
|
|---|
| 213 |
|
|---|
| 214 | /**
|
|---|
| 215 | * Determine if a value is a Blob
|
|---|
| 216 | *
|
|---|
| 217 | * @param {*} val The value to test
|
|---|
| 218 | *
|
|---|
| 219 | * @returns {boolean} True if value is a Blob, otherwise false
|
|---|
| 220 | */
|
|---|
| 221 | const isBlob = kindOfTest('Blob');
|
|---|
| 222 |
|
|---|
| 223 | /**
|
|---|
| 224 | * Determine if a value is a FileList
|
|---|
| 225 | *
|
|---|
| 226 | * @param {*} val The value to test
|
|---|
| 227 | *
|
|---|
| 228 | * @returns {boolean} True if value is a FileList, otherwise false
|
|---|
| 229 | */
|
|---|
| 230 | const isFileList = kindOfTest('FileList');
|
|---|
| 231 |
|
|---|
| 232 | /**
|
|---|
| 233 | * Determine if a value is a Stream
|
|---|
| 234 | *
|
|---|
| 235 | * @param {*} val The value to test
|
|---|
| 236 | *
|
|---|
| 237 | * @returns {boolean} True if value is a Stream, otherwise false
|
|---|
| 238 | */
|
|---|
| 239 | const isStream = (val) => isObject(val) && isFunction(val.pipe);
|
|---|
| 240 |
|
|---|
| 241 | /**
|
|---|
| 242 | * Determine if a value is a FormData
|
|---|
| 243 | *
|
|---|
| 244 | * @param {*} thing The value to test
|
|---|
| 245 | *
|
|---|
| 246 | * @returns {boolean} True if value is an FormData, otherwise false
|
|---|
| 247 | */
|
|---|
| 248 | function getGlobal() {
|
|---|
| 249 | if (typeof globalThis !== 'undefined') return globalThis;
|
|---|
| 250 | if (typeof self !== 'undefined') return self;
|
|---|
| 251 | if (typeof window !== 'undefined') return window;
|
|---|
| 252 | if (typeof global !== 'undefined') return global;
|
|---|
| 253 | return {};
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | const G = getGlobal();
|
|---|
| 257 | const FormDataCtor = typeof G.FormData !== 'undefined' ? G.FormData : undefined;
|
|---|
| 258 |
|
|---|
| 259 | const isFormData = (thing) => {
|
|---|
| 260 | if (!thing) return false;
|
|---|
| 261 | if (FormDataCtor && thing instanceof FormDataCtor) return true;
|
|---|
| 262 | // Reject plain objects inheriting directly from Object.prototype so prototype-pollution gadgets can't spoof FormData.
|
|---|
| 263 | const proto = getPrototypeOf(thing);
|
|---|
| 264 | if (!proto || proto === Object.prototype) return false;
|
|---|
| 265 | if (!isFunction(thing.append)) return false;
|
|---|
| 266 | const kind = kindOf(thing);
|
|---|
| 267 | return (
|
|---|
| 268 | kind === 'formdata' ||
|
|---|
| 269 | // detect form-data instance
|
|---|
| 270 | (kind === 'object' && isFunction(thing.toString) && thing.toString() === '[object FormData]')
|
|---|
| 271 | );
|
|---|
| 272 | };
|
|---|
| 273 |
|
|---|
| 274 | /**
|
|---|
| 275 | * Determine if a value is a URLSearchParams object
|
|---|
| 276 | *
|
|---|
| 277 | * @param {*} val The value to test
|
|---|
| 278 | *
|
|---|
| 279 | * @returns {boolean} True if value is a URLSearchParams object, otherwise false
|
|---|
| 280 | */
|
|---|
| 281 | const isURLSearchParams = kindOfTest('URLSearchParams');
|
|---|
| 282 |
|
|---|
| 283 | const [isReadableStream, isRequest, isResponse, isHeaders] = [
|
|---|
| 284 | 'ReadableStream',
|
|---|
| 285 | 'Request',
|
|---|
| 286 | 'Response',
|
|---|
| 287 | 'Headers',
|
|---|
| 288 | ].map(kindOfTest);
|
|---|
| 289 |
|
|---|
| 290 | /**
|
|---|
| 291 | * Trim excess whitespace off the beginning and end of a string
|
|---|
| 292 | *
|
|---|
| 293 | * @param {String} str The String to trim
|
|---|
| 294 | *
|
|---|
| 295 | * @returns {String} The String freed of excess whitespace
|
|---|
| 296 | */
|
|---|
| 297 | const trim = (str) => {
|
|---|
| 298 | return str.trim ? str.trim() : str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
|
|---|
| 299 | };
|
|---|
| 300 | /**
|
|---|
| 301 | * Iterate over an Array or an Object invoking a function for each item.
|
|---|
| 302 | *
|
|---|
| 303 | * If `obj` is an Array callback will be called passing
|
|---|
| 304 | * the value, index, and complete array for each item.
|
|---|
| 305 | *
|
|---|
| 306 | * If 'obj' is an Object callback will be called passing
|
|---|
| 307 | * the value, key, and complete object for each property.
|
|---|
| 308 | *
|
|---|
| 309 | * @param {Object|Array<unknown>} obj The object to iterate
|
|---|
| 310 | * @param {Function} fn The callback to invoke for each item
|
|---|
| 311 | *
|
|---|
| 312 | * @param {Object} [options]
|
|---|
| 313 | * @param {Boolean} [options.allOwnKeys = false]
|
|---|
| 314 | * @returns {any}
|
|---|
| 315 | */
|
|---|
| 316 | function forEach(obj, fn, { allOwnKeys = false } = {}) {
|
|---|
| 317 | // Don't bother if no value provided
|
|---|
| 318 | if (obj === null || typeof obj === 'undefined') {
|
|---|
| 319 | return;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | let i;
|
|---|
| 323 | let l;
|
|---|
| 324 |
|
|---|
| 325 | // Force an array if not already something iterable
|
|---|
| 326 | if (typeof obj !== 'object') {
|
|---|
| 327 | /*eslint no-param-reassign:0*/
|
|---|
| 328 | obj = [obj];
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | if (isArray(obj)) {
|
|---|
| 332 | // Iterate over array values
|
|---|
| 333 | for (i = 0, l = obj.length; i < l; i++) {
|
|---|
| 334 | fn.call(null, obj[i], i, obj);
|
|---|
| 335 | }
|
|---|
| 336 | } else {
|
|---|
| 337 | // Buffer check
|
|---|
| 338 | if (isBuffer(obj)) {
|
|---|
| 339 | return;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | // Iterate over object keys
|
|---|
| 343 | const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
|
|---|
| 344 | const len = keys.length;
|
|---|
| 345 | let key;
|
|---|
| 346 |
|
|---|
| 347 | for (i = 0; i < len; i++) {
|
|---|
| 348 | key = keys[i];
|
|---|
| 349 | fn.call(null, obj[key], key, obj);
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | /**
|
|---|
| 355 | * Finds a key in an object, case-insensitive, returning the actual key name.
|
|---|
| 356 | * Returns null if the object is a Buffer or if no match is found.
|
|---|
| 357 | *
|
|---|
| 358 | * @param {Object} obj - The object to search.
|
|---|
| 359 | * @param {string} key - The key to find (case-insensitive).
|
|---|
| 360 | * @returns {?string} The actual key name if found, otherwise null.
|
|---|
| 361 | */
|
|---|
| 362 | function findKey(obj, key) {
|
|---|
| 363 | if (isBuffer(obj)) {
|
|---|
| 364 | return null;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | key = key.toLowerCase();
|
|---|
| 368 | const keys = Object.keys(obj);
|
|---|
| 369 | let i = keys.length;
|
|---|
| 370 | let _key;
|
|---|
| 371 | while (i-- > 0) {
|
|---|
| 372 | _key = keys[i];
|
|---|
| 373 | if (key === _key.toLowerCase()) {
|
|---|
| 374 | return _key;
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 | return null;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | const _global = (() => {
|
|---|
| 381 | /*eslint no-undef:0*/
|
|---|
| 382 | if (typeof globalThis !== 'undefined') return globalThis;
|
|---|
| 383 | return typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : global;
|
|---|
| 384 | })();
|
|---|
| 385 |
|
|---|
| 386 | const isContextDefined = (context) => !isUndefined(context) && context !== _global;
|
|---|
| 387 |
|
|---|
| 388 | /**
|
|---|
| 389 | * Accepts varargs expecting each argument to be an object, then
|
|---|
| 390 | * immutably merges the properties of each object and returns result.
|
|---|
| 391 | *
|
|---|
| 392 | * When multiple objects contain the same key the later object in
|
|---|
| 393 | * the arguments list will take precedence.
|
|---|
| 394 | *
|
|---|
| 395 | * Example:
|
|---|
| 396 | *
|
|---|
| 397 | * ```js
|
|---|
| 398 | * const result = merge({foo: 123}, {foo: 456});
|
|---|
| 399 | * console.log(result.foo); // outputs 456
|
|---|
| 400 | * ```
|
|---|
| 401 | *
|
|---|
| 402 | * @param {Object} obj1 Object to merge
|
|---|
| 403 | *
|
|---|
| 404 | * @returns {Object} Result of all merge properties
|
|---|
| 405 | */
|
|---|
| 406 | function merge(...objs) {
|
|---|
| 407 | const { caseless, skipUndefined } = (isContextDefined(this) && this) || {};
|
|---|
| 408 | const result = {};
|
|---|
| 409 | const assignValue = (val, key) => {
|
|---|
| 410 | // Skip dangerous property names to prevent prototype pollution
|
|---|
| 411 | if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|---|
| 412 | return;
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | const targetKey = (caseless && findKey(result, key)) || key;
|
|---|
| 416 | // Read via own-prop only — a bare `result[targetKey]` walks the prototype
|
|---|
| 417 | // chain, so a polluted Object.prototype value could surface here and get
|
|---|
| 418 | // copied into the merged result.
|
|---|
| 419 | const existing = hasOwnProperty(result, targetKey) ? result[targetKey] : undefined;
|
|---|
| 420 | if (isPlainObject(existing) && isPlainObject(val)) {
|
|---|
| 421 | result[targetKey] = merge(existing, val);
|
|---|
| 422 | } else if (isPlainObject(val)) {
|
|---|
| 423 | result[targetKey] = merge({}, val);
|
|---|
| 424 | } else if (isArray(val)) {
|
|---|
| 425 | result[targetKey] = val.slice();
|
|---|
| 426 | } else if (!skipUndefined || !isUndefined(val)) {
|
|---|
| 427 | result[targetKey] = val;
|
|---|
| 428 | }
|
|---|
| 429 | };
|
|---|
| 430 |
|
|---|
| 431 | for (let i = 0, l = objs.length; i < l; i++) {
|
|---|
| 432 | objs[i] && forEach(objs[i], assignValue);
|
|---|
| 433 | }
|
|---|
| 434 | return result;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | /**
|
|---|
| 438 | * Extends object a by mutably adding to it the properties of object b.
|
|---|
| 439 | *
|
|---|
| 440 | * @param {Object} a The object to be extended
|
|---|
| 441 | * @param {Object} b The object to copy properties from
|
|---|
| 442 | * @param {Object} thisArg The object to bind function to
|
|---|
| 443 | *
|
|---|
| 444 | * @param {Object} [options]
|
|---|
| 445 | * @param {Boolean} [options.allOwnKeys]
|
|---|
| 446 | * @returns {Object} The resulting value of object a
|
|---|
| 447 | */
|
|---|
| 448 | const extend = (a, b, thisArg, { allOwnKeys } = {}) => {
|
|---|
| 449 | forEach(
|
|---|
| 450 | b,
|
|---|
| 451 | (val, key) => {
|
|---|
| 452 | if (thisArg && isFunction(val)) {
|
|---|
| 453 | Object.defineProperty(a, key, {
|
|---|
| 454 | // Null-proto descriptor so a polluted Object.prototype.get cannot
|
|---|
| 455 | // hijack defineProperty's accessor-vs-data resolution.
|
|---|
| 456 | __proto__: null,
|
|---|
| 457 | value: bind(val, thisArg),
|
|---|
| 458 | writable: true,
|
|---|
| 459 | enumerable: true,
|
|---|
| 460 | configurable: true,
|
|---|
| 461 | });
|
|---|
| 462 | } else {
|
|---|
| 463 | Object.defineProperty(a, key, {
|
|---|
| 464 | __proto__: null,
|
|---|
| 465 | value: val,
|
|---|
| 466 | writable: true,
|
|---|
| 467 | enumerable: true,
|
|---|
| 468 | configurable: true,
|
|---|
| 469 | });
|
|---|
| 470 | }
|
|---|
| 471 | },
|
|---|
| 472 | { allOwnKeys }
|
|---|
| 473 | );
|
|---|
| 474 | return a;
|
|---|
| 475 | };
|
|---|
| 476 |
|
|---|
| 477 | /**
|
|---|
| 478 | * Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
|
|---|
| 479 | *
|
|---|
| 480 | * @param {string} content with BOM
|
|---|
| 481 | *
|
|---|
| 482 | * @returns {string} content value without BOM
|
|---|
| 483 | */
|
|---|
| 484 | const stripBOM = (content) => {
|
|---|
| 485 | if (content.charCodeAt(0) === 0xfeff) {
|
|---|
| 486 | content = content.slice(1);
|
|---|
| 487 | }
|
|---|
| 488 | return content;
|
|---|
| 489 | };
|
|---|
| 490 |
|
|---|
| 491 | /**
|
|---|
| 492 | * Inherit the prototype methods from one constructor into another
|
|---|
| 493 | * @param {function} constructor
|
|---|
| 494 | * @param {function} superConstructor
|
|---|
| 495 | * @param {object} [props]
|
|---|
| 496 | * @param {object} [descriptors]
|
|---|
| 497 | *
|
|---|
| 498 | * @returns {void}
|
|---|
| 499 | */
|
|---|
| 500 | const inherits = (constructor, superConstructor, props, descriptors) => {
|
|---|
| 501 | constructor.prototype = Object.create(superConstructor.prototype, descriptors);
|
|---|
| 502 | Object.defineProperty(constructor.prototype, 'constructor', {
|
|---|
| 503 | __proto__: null,
|
|---|
| 504 | value: constructor,
|
|---|
| 505 | writable: true,
|
|---|
| 506 | enumerable: false,
|
|---|
| 507 | configurable: true,
|
|---|
| 508 | });
|
|---|
| 509 | Object.defineProperty(constructor, 'super', {
|
|---|
| 510 | __proto__: null,
|
|---|
| 511 | value: superConstructor.prototype,
|
|---|
| 512 | });
|
|---|
| 513 | props && Object.assign(constructor.prototype, props);
|
|---|
| 514 | };
|
|---|
| 515 |
|
|---|
| 516 | /**
|
|---|
| 517 | * Resolve object with deep prototype chain to a flat object
|
|---|
| 518 | * @param {Object} sourceObj source object
|
|---|
| 519 | * @param {Object} [destObj]
|
|---|
| 520 | * @param {Function|Boolean} [filter]
|
|---|
| 521 | * @param {Function} [propFilter]
|
|---|
| 522 | *
|
|---|
| 523 | * @returns {Object}
|
|---|
| 524 | */
|
|---|
| 525 | const toFlatObject = (sourceObj, destObj, filter, propFilter) => {
|
|---|
| 526 | let props;
|
|---|
| 527 | let i;
|
|---|
| 528 | let prop;
|
|---|
| 529 | const merged = {};
|
|---|
| 530 |
|
|---|
| 531 | destObj = destObj || {};
|
|---|
| 532 | // eslint-disable-next-line no-eq-null,eqeqeq
|
|---|
| 533 | if (sourceObj == null) return destObj;
|
|---|
| 534 |
|
|---|
| 535 | do {
|
|---|
| 536 | props = Object.getOwnPropertyNames(sourceObj);
|
|---|
| 537 | i = props.length;
|
|---|
| 538 | while (i-- > 0) {
|
|---|
| 539 | prop = props[i];
|
|---|
| 540 | if ((!propFilter || propFilter(prop, sourceObj, destObj)) && !merged[prop]) {
|
|---|
| 541 | destObj[prop] = sourceObj[prop];
|
|---|
| 542 | merged[prop] = true;
|
|---|
| 543 | }
|
|---|
| 544 | }
|
|---|
| 545 | sourceObj = filter !== false && getPrototypeOf(sourceObj);
|
|---|
| 546 | } while (sourceObj && (!filter || filter(sourceObj, destObj)) && sourceObj !== Object.prototype);
|
|---|
| 547 |
|
|---|
| 548 | return destObj;
|
|---|
| 549 | };
|
|---|
| 550 |
|
|---|
| 551 | /**
|
|---|
| 552 | * Determines whether a string ends with the characters of a specified string
|
|---|
| 553 | *
|
|---|
| 554 | * @param {String} str
|
|---|
| 555 | * @param {String} searchString
|
|---|
| 556 | * @param {Number} [position= 0]
|
|---|
| 557 | *
|
|---|
| 558 | * @returns {boolean}
|
|---|
| 559 | */
|
|---|
| 560 | const endsWith = (str, searchString, position) => {
|
|---|
| 561 | str = String(str);
|
|---|
| 562 | if (position === undefined || position > str.length) {
|
|---|
| 563 | position = str.length;
|
|---|
| 564 | }
|
|---|
| 565 | position -= searchString.length;
|
|---|
| 566 | const lastIndex = str.indexOf(searchString, position);
|
|---|
| 567 | return lastIndex !== -1 && lastIndex === position;
|
|---|
| 568 | };
|
|---|
| 569 |
|
|---|
| 570 | /**
|
|---|
| 571 | * Returns new array from array like object or null if failed
|
|---|
| 572 | *
|
|---|
| 573 | * @param {*} [thing]
|
|---|
| 574 | *
|
|---|
| 575 | * @returns {?Array}
|
|---|
| 576 | */
|
|---|
| 577 | const toArray = (thing) => {
|
|---|
| 578 | if (!thing) return null;
|
|---|
| 579 | if (isArray(thing)) return thing;
|
|---|
| 580 | let i = thing.length;
|
|---|
| 581 | if (!isNumber(i)) return null;
|
|---|
| 582 | const arr = new Array(i);
|
|---|
| 583 | while (i-- > 0) {
|
|---|
| 584 | arr[i] = thing[i];
|
|---|
| 585 | }
|
|---|
| 586 | return arr;
|
|---|
| 587 | };
|
|---|
| 588 |
|
|---|
| 589 | /**
|
|---|
| 590 | * Checking if the Uint8Array exists and if it does, it returns a function that checks if the
|
|---|
| 591 | * thing passed in is an instance of Uint8Array
|
|---|
| 592 | *
|
|---|
| 593 | * @param {TypedArray}
|
|---|
| 594 | *
|
|---|
| 595 | * @returns {Array}
|
|---|
| 596 | */
|
|---|
| 597 | // eslint-disable-next-line func-names
|
|---|
| 598 | const isTypedArray = ((TypedArray) => {
|
|---|
| 599 | // eslint-disable-next-line func-names
|
|---|
| 600 | return (thing) => {
|
|---|
| 601 | return TypedArray && thing instanceof TypedArray;
|
|---|
| 602 | };
|
|---|
| 603 | })(typeof Uint8Array !== 'undefined' && getPrototypeOf(Uint8Array));
|
|---|
| 604 |
|
|---|
| 605 | /**
|
|---|
| 606 | * For each entry in the object, call the function with the key and value.
|
|---|
| 607 | *
|
|---|
| 608 | * @param {Object<any, any>} obj - The object to iterate over.
|
|---|
| 609 | * @param {Function} fn - The function to call for each entry.
|
|---|
| 610 | *
|
|---|
| 611 | * @returns {void}
|
|---|
| 612 | */
|
|---|
| 613 | const forEachEntry = (obj, fn) => {
|
|---|
| 614 | const generator = obj && obj[iterator];
|
|---|
| 615 |
|
|---|
| 616 | const _iterator = generator.call(obj);
|
|---|
| 617 |
|
|---|
| 618 | let result;
|
|---|
| 619 |
|
|---|
| 620 | while ((result = _iterator.next()) && !result.done) {
|
|---|
| 621 | const pair = result.value;
|
|---|
| 622 | fn.call(obj, pair[0], pair[1]);
|
|---|
| 623 | }
|
|---|
| 624 | };
|
|---|
| 625 |
|
|---|
| 626 | /**
|
|---|
| 627 | * It takes a regular expression and a string, and returns an array of all the matches
|
|---|
| 628 | *
|
|---|
| 629 | * @param {string} regExp - The regular expression to match against.
|
|---|
| 630 | * @param {string} str - The string to search.
|
|---|
| 631 | *
|
|---|
| 632 | * @returns {Array<boolean>}
|
|---|
| 633 | */
|
|---|
| 634 | const matchAll = (regExp, str) => {
|
|---|
| 635 | let matches;
|
|---|
| 636 | const arr = [];
|
|---|
| 637 |
|
|---|
| 638 | while ((matches = regExp.exec(str)) !== null) {
|
|---|
| 639 | arr.push(matches);
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | return arr;
|
|---|
| 643 | };
|
|---|
| 644 |
|
|---|
| 645 | /* Checking if the kindOfTest function returns true when passed an HTMLFormElement. */
|
|---|
| 646 | const isHTMLForm = kindOfTest('HTMLFormElement');
|
|---|
| 647 |
|
|---|
| 648 | const toCamelCase = (str) => {
|
|---|
| 649 | return str.toLowerCase().replace(/[-_\s]([a-z\d])(\w*)/g, function replacer(m, p1, p2) {
|
|---|
| 650 | return p1.toUpperCase() + p2;
|
|---|
| 651 | });
|
|---|
| 652 | };
|
|---|
| 653 |
|
|---|
| 654 | /* Creating a function that will check if an object has a property. */
|
|---|
| 655 | const hasOwnProperty = (
|
|---|
| 656 | ({ hasOwnProperty }) =>
|
|---|
| 657 | (obj, prop) =>
|
|---|
| 658 | hasOwnProperty.call(obj, prop)
|
|---|
| 659 | )(Object.prototype);
|
|---|
| 660 |
|
|---|
| 661 | /**
|
|---|
| 662 | * Determine if a value is a RegExp object
|
|---|
| 663 | *
|
|---|
| 664 | * @param {*} val The value to test
|
|---|
| 665 | *
|
|---|
| 666 | * @returns {boolean} True if value is a RegExp object, otherwise false
|
|---|
| 667 | */
|
|---|
| 668 | const isRegExp = kindOfTest('RegExp');
|
|---|
| 669 |
|
|---|
| 670 | const reduceDescriptors = (obj, reducer) => {
|
|---|
| 671 | const descriptors = Object.getOwnPropertyDescriptors(obj);
|
|---|
| 672 | const reducedDescriptors = {};
|
|---|
| 673 |
|
|---|
| 674 | forEach(descriptors, (descriptor, name) => {
|
|---|
| 675 | let ret;
|
|---|
| 676 | if ((ret = reducer(descriptor, name, obj)) !== false) {
|
|---|
| 677 | reducedDescriptors[name] = ret || descriptor;
|
|---|
| 678 | }
|
|---|
| 679 | });
|
|---|
| 680 |
|
|---|
| 681 | Object.defineProperties(obj, reducedDescriptors);
|
|---|
| 682 | };
|
|---|
| 683 |
|
|---|
| 684 | /**
|
|---|
| 685 | * Makes all methods read-only
|
|---|
| 686 | * @param {Object} obj
|
|---|
| 687 | */
|
|---|
| 688 |
|
|---|
| 689 | const freezeMethods = (obj) => {
|
|---|
| 690 | reduceDescriptors(obj, (descriptor, name) => {
|
|---|
| 691 | // skip restricted props in strict mode
|
|---|
| 692 | if (isFunction(obj) && ['arguments', 'caller', 'callee'].includes(name)) {
|
|---|
| 693 | return false;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | const value = obj[name];
|
|---|
| 697 |
|
|---|
| 698 | if (!isFunction(value)) return;
|
|---|
| 699 |
|
|---|
| 700 | descriptor.enumerable = false;
|
|---|
| 701 |
|
|---|
| 702 | if ('writable' in descriptor) {
|
|---|
| 703 | descriptor.writable = false;
|
|---|
| 704 | return;
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | if (!descriptor.set) {
|
|---|
| 708 | descriptor.set = () => {
|
|---|
| 709 | throw Error("Can not rewrite read-only method '" + name + "'");
|
|---|
| 710 | };
|
|---|
| 711 | }
|
|---|
| 712 | });
|
|---|
| 713 | };
|
|---|
| 714 |
|
|---|
| 715 | /**
|
|---|
| 716 | * Converts an array or a delimited string into an object set with values as keys and true as values.
|
|---|
| 717 | * Useful for fast membership checks.
|
|---|
| 718 | *
|
|---|
| 719 | * @param {Array|string} arrayOrString - The array or string to convert.
|
|---|
| 720 | * @param {string} delimiter - The delimiter to use if input is a string.
|
|---|
| 721 | * @returns {Object} An object with keys from the array or string, values set to true.
|
|---|
| 722 | */
|
|---|
| 723 | const toObjectSet = (arrayOrString, delimiter) => {
|
|---|
| 724 | const obj = {};
|
|---|
| 725 |
|
|---|
| 726 | const define = (arr) => {
|
|---|
| 727 | arr.forEach((value) => {
|
|---|
| 728 | obj[value] = true;
|
|---|
| 729 | });
|
|---|
| 730 | };
|
|---|
| 731 |
|
|---|
| 732 | isArray(arrayOrString) ? define(arrayOrString) : define(String(arrayOrString).split(delimiter));
|
|---|
| 733 |
|
|---|
| 734 | return obj;
|
|---|
| 735 | };
|
|---|
| 736 |
|
|---|
| 737 | const noop = () => {};
|
|---|
| 738 |
|
|---|
| 739 | const toFiniteNumber = (value, defaultValue) => {
|
|---|
| 740 | return value != null && Number.isFinite((value = +value)) ? value : defaultValue;
|
|---|
| 741 | };
|
|---|
| 742 |
|
|---|
| 743 | /**
|
|---|
| 744 | * If the thing is a FormData object, return true, otherwise return false.
|
|---|
| 745 | *
|
|---|
| 746 | * @param {unknown} thing - The thing to check.
|
|---|
| 747 | *
|
|---|
| 748 | * @returns {boolean}
|
|---|
| 749 | */
|
|---|
| 750 | function isSpecCompliantForm(thing) {
|
|---|
| 751 | return !!(
|
|---|
| 752 | thing &&
|
|---|
| 753 | isFunction(thing.append) &&
|
|---|
| 754 | thing[toStringTag] === 'FormData' &&
|
|---|
| 755 | thing[iterator]
|
|---|
| 756 | );
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | /**
|
|---|
| 760 | * Recursively converts an object to a JSON-compatible object, handling circular references and Buffers.
|
|---|
| 761 | *
|
|---|
| 762 | * @param {Object} obj - The object to convert.
|
|---|
| 763 | * @returns {Object} The JSON-compatible object.
|
|---|
| 764 | */
|
|---|
| 765 | const toJSONObject = (obj) => {
|
|---|
| 766 | const visited = new WeakSet();
|
|---|
| 767 |
|
|---|
| 768 | const visit = (source) => {
|
|---|
| 769 | if (isObject(source)) {
|
|---|
| 770 | if (visited.has(source)) {
|
|---|
| 771 | return;
|
|---|
| 772 | }
|
|---|
| 773 |
|
|---|
| 774 | //Buffer check
|
|---|
| 775 | if (isBuffer(source)) {
|
|---|
| 776 | return source;
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | if (!('toJSON' in source)) {
|
|---|
| 780 | // add-on descent / delete-on-ascent: preserves path semantics, so DAG nodes serialise at every occurrence (see #7230).
|
|---|
| 781 | visited.add(source);
|
|---|
| 782 | const target = isArray(source) ? [] : {};
|
|---|
| 783 |
|
|---|
| 784 | forEach(source, (value, key) => {
|
|---|
| 785 | const reducedValue = visit(value);
|
|---|
| 786 | !isUndefined(reducedValue) && (target[key] = reducedValue);
|
|---|
| 787 | });
|
|---|
| 788 |
|
|---|
| 789 | visited.delete(source);
|
|---|
| 790 |
|
|---|
| 791 | return target;
|
|---|
| 792 | }
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | return source;
|
|---|
| 796 | };
|
|---|
| 797 |
|
|---|
| 798 | return visit(obj);
|
|---|
| 799 | };
|
|---|
| 800 |
|
|---|
| 801 | /**
|
|---|
| 802 | * Determines if a value is an async function.
|
|---|
| 803 | *
|
|---|
| 804 | * @param {*} thing - The value to test.
|
|---|
| 805 | * @returns {boolean} True if value is an async function, otherwise false.
|
|---|
| 806 | */
|
|---|
| 807 | const isAsyncFn = kindOfTest('AsyncFunction');
|
|---|
| 808 |
|
|---|
| 809 | /**
|
|---|
| 810 | * Determines if a value is thenable (has then and catch methods).
|
|---|
| 811 | *
|
|---|
| 812 | * @param {*} thing - The value to test.
|
|---|
| 813 | * @returns {boolean} True if value is thenable, otherwise false.
|
|---|
| 814 | */
|
|---|
| 815 | const isThenable = (thing) =>
|
|---|
| 816 | thing &&
|
|---|
| 817 | (isObject(thing) || isFunction(thing)) &&
|
|---|
| 818 | isFunction(thing.then) &&
|
|---|
| 819 | isFunction(thing.catch);
|
|---|
| 820 |
|
|---|
| 821 | // original code
|
|---|
| 822 | // https://github.com/DigitalBrainJS/AxiosPromise/blob/16deab13710ec09779922131f3fa5954320f83ab/lib/utils.js#L11-L34
|
|---|
| 823 |
|
|---|
| 824 | /**
|
|---|
| 825 | * Provides a cross-platform setImmediate implementation.
|
|---|
| 826 | * Uses native setImmediate if available, otherwise falls back to postMessage or setTimeout.
|
|---|
| 827 | *
|
|---|
| 828 | * @param {boolean} setImmediateSupported - Whether setImmediate is supported.
|
|---|
| 829 | * @param {boolean} postMessageSupported - Whether postMessage is supported.
|
|---|
| 830 | * @returns {Function} A function to schedule a callback asynchronously.
|
|---|
| 831 | */
|
|---|
| 832 | const _setImmediate = ((setImmediateSupported, postMessageSupported) => {
|
|---|
| 833 | if (setImmediateSupported) {
|
|---|
| 834 | return setImmediate;
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | return postMessageSupported
|
|---|
| 838 | ? ((token, callbacks) => {
|
|---|
| 839 | _global.addEventListener(
|
|---|
| 840 | 'message',
|
|---|
| 841 | ({ source, data }) => {
|
|---|
| 842 | if (source === _global && data === token) {
|
|---|
| 843 | callbacks.length && callbacks.shift()();
|
|---|
| 844 | }
|
|---|
| 845 | },
|
|---|
| 846 | false
|
|---|
| 847 | );
|
|---|
| 848 |
|
|---|
| 849 | return (cb) => {
|
|---|
| 850 | callbacks.push(cb);
|
|---|
| 851 | _global.postMessage(token, '*');
|
|---|
| 852 | };
|
|---|
| 853 | })(`axios@${Math.random()}`, [])
|
|---|
| 854 | : (cb) => setTimeout(cb);
|
|---|
| 855 | })(typeof setImmediate === 'function', isFunction(_global.postMessage));
|
|---|
| 856 |
|
|---|
| 857 | /**
|
|---|
| 858 | * Schedules a microtask or asynchronous callback as soon as possible.
|
|---|
| 859 | * Uses queueMicrotask if available, otherwise falls back to process.nextTick or _setImmediate.
|
|---|
| 860 | *
|
|---|
| 861 | * @type {Function}
|
|---|
| 862 | */
|
|---|
| 863 | const asap =
|
|---|
| 864 | typeof queueMicrotask !== 'undefined'
|
|---|
| 865 | ? queueMicrotask.bind(_global)
|
|---|
| 866 | : (typeof process !== 'undefined' && process.nextTick) || _setImmediate;
|
|---|
| 867 |
|
|---|
| 868 | // *********************
|
|---|
| 869 |
|
|---|
| 870 | const isIterable = (thing) => thing != null && isFunction(thing[iterator]);
|
|---|
| 871 |
|
|---|
| 872 | export default {
|
|---|
| 873 | isArray,
|
|---|
| 874 | isArrayBuffer,
|
|---|
| 875 | isBuffer,
|
|---|
| 876 | isFormData,
|
|---|
| 877 | isArrayBufferView,
|
|---|
| 878 | isString,
|
|---|
| 879 | isNumber,
|
|---|
| 880 | isBoolean,
|
|---|
| 881 | isObject,
|
|---|
| 882 | isPlainObject,
|
|---|
| 883 | isEmptyObject,
|
|---|
| 884 | isReadableStream,
|
|---|
| 885 | isRequest,
|
|---|
| 886 | isResponse,
|
|---|
| 887 | isHeaders,
|
|---|
| 888 | isUndefined,
|
|---|
| 889 | isDate,
|
|---|
| 890 | isFile,
|
|---|
| 891 | isReactNativeBlob,
|
|---|
| 892 | isReactNative,
|
|---|
| 893 | isBlob,
|
|---|
| 894 | isRegExp,
|
|---|
| 895 | isFunction,
|
|---|
| 896 | isStream,
|
|---|
| 897 | isURLSearchParams,
|
|---|
| 898 | isTypedArray,
|
|---|
| 899 | isFileList,
|
|---|
| 900 | forEach,
|
|---|
| 901 | merge,
|
|---|
| 902 | extend,
|
|---|
| 903 | trim,
|
|---|
| 904 | stripBOM,
|
|---|
| 905 | inherits,
|
|---|
| 906 | toFlatObject,
|
|---|
| 907 | kindOf,
|
|---|
| 908 | kindOfTest,
|
|---|
| 909 | endsWith,
|
|---|
| 910 | toArray,
|
|---|
| 911 | forEachEntry,
|
|---|
| 912 | matchAll,
|
|---|
| 913 | isHTMLForm,
|
|---|
| 914 | hasOwnProperty,
|
|---|
| 915 | hasOwnProp: hasOwnProperty, // an alias to avoid ESLint no-prototype-builtins detection
|
|---|
| 916 | reduceDescriptors,
|
|---|
| 917 | freezeMethods,
|
|---|
| 918 | toObjectSet,
|
|---|
| 919 | toCamelCase,
|
|---|
| 920 | noop,
|
|---|
| 921 | toFiniteNumber,
|
|---|
| 922 | findKey,
|
|---|
| 923 | global: _global,
|
|---|
| 924 | isContextDefined,
|
|---|
| 925 | isSpecCompliantForm,
|
|---|
| 926 | toJSONObject,
|
|---|
| 927 | isAsyncFn,
|
|---|
| 928 | isThenable,
|
|---|
| 929 | setImmediate: _setImmediate,
|
|---|
| 930 | asap,
|
|---|
| 931 | isIterable,
|
|---|
| 932 | };
|
|---|