@ou-imdt/utils
Version:
Utility library for interactive media development
21 lines (20 loc) • 962 B
JavaScript
import isSupportedFetchType from './isSupportedFetchType.js';
/**
* Parses a URL and determines the fetch type, if supported.
* @param {string} value - The value to parse for fetch type information.
* @returns {Object} An object containing:
* - `input`: The original input value.
* - `url`: The URL extracted from the input.
* - `suffix`: An optional suffix in the query string.
* - `type`: The inferred fetch type from either the suffix or the file extension.
* - `ext`: The file extension extracted from the URL.
* - `supported`: Boolean indicating if the fetch type is supported.
*/
export default function getFetchDetail(value) {
const regex = /^(.*?)(?:\?([a-z]{2,4}))?$/; // any char one or more not greedy, with optional '.json?text'
const [_, url, as] = value?.match(regex) ?? [];
const ext = url.split('.').pop();
const type = (as ?? ext);
const supported = isSupportedFetchType(type);
return { url, as, type, ext, supported };
};