import fetch from 'fetch'; /** * Describes the attributes on the fetch configuration object */ interface FetchConfig { url: string; headers?: { [key: string]: string }; } /** * Conveniently gets a JSON response using the fetch api * @param {FetchConfig} config * @return {Promise} */ const getJSON = (config: FetchConfig): Promise => { const fetchConfig = { method: 'GET', Accept: 'application/json', 'Content-Type': 'application/json', ...(config.headers || {}) }; return fetch(config.url, fetchConfig).then(response => response.json()); }; /** * Requests the headers from a resource endpoint * @param {FetchConfig} config * @return {Promise>} */ const getHeaders = async (config: FetchConfig): Promise => { const fetchConfig = { method: 'HEAD', ...(config.headers || {}) }; const { ok, headers, statusText } = await fetch(config.url, fetchConfig); if (ok) { return headers; } throw new Error(statusText); }; export { getJSON, getHeaders };