utils.js 23 KB

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