Or, alternatively, not ideal. For example: https://www.programming-idioms.org/idiom/174/make-http-post-...
On Fetch: https://gomakethings.com/why-i-still-use-xhr-instead-of-the-...
On Fetch: https://gomakethings.com/why-i-still-use-xhr-instead-of-the-...
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>
}