|
|
|
|
|
by mikewhy
2453 days ago
|
|
That's a whole lot of code / diatribe for what is: const _fetch = window.fetch
/** Fetch overload that handles error status */
export function fetch(input: RequestInfo, init?: RequestInit) {
return _fetch(input, init).then(checkResponseCode)
}
function checkResponseCode(response: Response) {
if (response.status >= 400) {
const err = new Error(`Error ${response.status} from ${response.url}`) as any
err.fetchHttpResponse = response
throw err
}
return response
}
/** Convenience method */
export function getJson<TJsonBody = any>(
input: RequestInfo,
init?: RequestInitJson,
) {
return fetch(input, init).then((response) => response.json()) as Promise<TJsonBody>
}
|
|