/** * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback. */ export async function safeFetch( url: Parameters[0], options: Parameters[1] = {}, onNotOK: (response: Response) => void | Promise = () => { throw new Error(`Request to ${url} returned a non-OK status code.`); } ): Promise { const response = await fetch(url, options); if (!response.ok) { await onNotOK(response); } return response; }