axios-light-my-request-adapter
Version:
Axios adapter for Light my Request
154 lines • 5.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toFlatObject = exports.stripBOM = exports.forEach = exports.isURLSearchParams = exports.isFormData = exports.isStream = exports.isFunction = exports.isDate = exports.isObject = exports.isArray = void 0;
// eslint-disable-next-line @typescript-eslint/unbound-method
const toString = Object.prototype.toString;
// eslint-disable-next-line func-names
const kindOf = (function (cache) {
// eslint-disable-next-line func-names
return function (thing) {
const str = toString.call(thing);
return cache[str] || (cache[str] = str.slice(8, -1).toLowerCase());
};
})(Object.create(null));
function kindOfTest(type) {
type = type.toLowerCase();
return function isKindOf(thing) {
return kindOf(thing) === type;
};
}
/**
* Determine if a value is an Array
*
* @param {Object} val The value to test
* @returns {boolean} True if value is an Array, otherwise false
*/
function isArray(val) {
return Array.isArray(val);
}
exports.isArray = isArray;
/**
* Determine if a value is an Object
*
* @param {Object} val The value to test
* @returns {boolean} True if value is an Object, otherwise false
*/
function isObject(val) {
return val !== null && typeof val === "object";
}
exports.isObject = isObject;
exports.isDate = kindOfTest("Date");
/**
* Determine if a value is a Function
*
* @param {Object} val The value to test
* @returns {boolean} True if value is a Function, otherwise false
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function isFunction(val) {
return toString.call(val) === "[object Function]";
}
exports.isFunction = isFunction;
/**
* Determine if a value is a Stream
*
* @param {Object} val The value to test
* @returns {boolean} True if value is a Stream, otherwise false
*/
function isStream(val) {
return isObject(val) && isFunction(val.pipe);
}
exports.isStream = isStream;
/**
* Determine if a value is a FormData
*
* @param {Object} thing The value to test
* @returns {boolean} True if value is an FormData, otherwise false
*/
function isFormData(thing) {
const pattern = "[object FormData]";
return (thing != null &&
((typeof FormData === "function" && thing instanceof FormData) ||
toString.call(thing) === pattern ||
(isFunction(thing?.toString) &&
thing.toString() === pattern)));
}
exports.isFormData = isFormData;
/**
* Determine if a value is a URLSearchParams object
* @function
* @param {Object} val The value to test
* @returns {boolean} True if value is a URLSearchParams object, otherwise false
*/
exports.isURLSearchParams = kindOfTest("URLSearchParams");
function forEach(obj, fn) {
// Don't bother if no value provided
if (obj === null || typeof obj === "undefined") {
return;
}
// Force an array if not already something iterable
if (typeof obj !== "object") {
fn.call(null, obj, 0, obj);
}
else if (isArray(obj)) {
// Iterate over array values
for (let i = 0, l = obj.length; i < l; i++) {
fn.call(null, obj[i], i, obj);
}
}
else {
// Iterate over object keys
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
fn.call(null, obj[key], key, obj);
}
}
}
}
exports.forEach = forEach;
/**
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
*
* @param {string} content with BOM
* @return {string} content value without BOM
*/
function stripBOM(content) {
if (content.charCodeAt(0) === 0xfeff) {
content = content.slice(1);
}
return content;
}
exports.stripBOM = stripBOM;
/**
* Resolve object with deep prototype chain to a flat object
* @param {Object} sourceObj source object
* @param {Object} [destObj]
* @param {Function} [filter]
* @returns {Object}
*/
function toFlatObject(sourceObj, destObj, filter) {
let props;
let i;
let prop;
const merged = {};
destObj = destObj || {};
do {
props = Object.getOwnPropertyNames(sourceObj);
i = props.length;
while (i-- > 0) {
prop = props[i];
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any */
if (!merged[prop]) {
destObj[prop] = sourceObj[prop];
merged[prop] = true;
}
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any */
}
sourceObj = Object.getPrototypeOf(sourceObj);
} while (sourceObj &&
(!filter || filter(sourceObj, destObj)) &&
sourceObj !== Object.prototype);
return destObj;
}
exports.toFlatObject = toFlatObject;
//# sourceMappingURL=axios-utils.js.map