es-fetcher
Version:
Enhance your frontend application's API implementation and calls with es-fetcher. Simplify and accelerate the process like never before.
81 lines (80 loc) • 3.22 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/**
* Omit properties from an object.
*
* @param {object} item - The input object.
* @param {string[]} keys - An array of keys to omit from the object.
* @returns {{ [key: string]: any }} - A new object with the specified keys omitted.
*/
const omitProperties = (item, keys) => {
return Object.fromEntries(Object.entries(item).filter(([key]) => !keys.includes(key)));
};
/**
* Choose properties from an object.
*
* @param {object} item - The input object.
* @param {string[]} keys - An array of keys to choose from the object.
* @returns {{ [key: string]: any }} - A new object with only the specified keys.
*/
const chooseProperties = (item, keys) => {
return Object.fromEntries(Object.entries(item).filter(([key]) => keys.includes(key)));
};
/**
* Check if a URL is absolute.
*
* @param {string} url - The URL to check.
* @returns {boolean} - True if the URL is absolute, false otherwise.
*/
const isAbsoluteUrl = (url) => {
return url.match(/^[^:\/\\]+:\/\//) ? true : false;
};
/**
* Construct an absolute URL from a relative URL and an optional base.
*
* @param {string} url - The relative URL.
* @param {string} [base] - The optional base URL.
* @returns {string} - The absolute URL.
*/
const constructAbsoluteUrl = (url, base) => {
base = (base || (location === null || location === void 0 ? void 0 : location.origin)).replace(/\/+$/, '');
url = url.replace(/^\/+/, '');
return `${base}/${url}`;
};
/**
* Try parsing the response body based on the content type.
*
* @param {Response} res - The response object.
* @returns {Promise<any>} - A promise that resolves to the parsed response body.
* @throws {Error} - Throws an error if parsing fails.
*/
const tryParsingBody = (res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const contentType = res.headers.get('Content-Type');
if (!contentType)
return yield res.blob();
else if (contentType.includes('application/json')) {
return yield res.json();
}
else if (contentType.includes('application/xml') || contentType.includes('text/xml')) {
return (new DOMParser()).parseFromString(yield res.text(), 'application/xml');
}
else if (contentType.includes('text/')) {
return yield res.text();
}
else {
return yield res.blob();
}
}
catch (error) {
throw error;
}
});
export { omitProperties, chooseProperties, isAbsoluteUrl, constructAbsoluteUrl, tryParsingBody };