UNPKG

@ou-imdt/utils

Version:

Utility library for interactive media development

28 lines (26 loc) 868 B
import getFetchDetail from './getFetchDetail.js'; import fetchBlob from './fetchBlob.js'; import fetchText from './fetchText.js'; import fetchJSON from './fetchJSON.js'; import fetchXML from './fetchXML.js'; /** * Fetches the url with type as, defaults to file extension * @param {string} url * @param {object} [as] * @returns {Promise} - that resolves to an object containg type and response */ export default async function fetchAs(url, as) { const detail = getFetchDetail(url); const type = (as ?? detail.type); const map = { raw: () => fetch(url), blob: () => fetchBlob(url), text: () => fetchText(url), json: () => fetchJSON(url), xml: () => fetchXML(url), default: () => Promise.resolve(url) }; const handler = (map[type] ?? map.default); const response = await handler(url); return { ...detail, type, response }; }