3 ms·
Or, alternatively, not ideal. For example: https://www.programming-idioms.org/idiom/174/make-http-post-request https://www.programming-idioms.org/idiom/174/make
by mfoy_ 7y ago
Or, alternatively, not ideal. For example: https://www.programming-idioms.org/idiom/174/make-http-post-request https://www.programming-idioms.org/idiom/174/make-http-post-...
On Fetch: https://gomakethings.com/why-i-still-use-xhr-instead-of-the-fetch-api/ https://gomakethings.com/why-i-still-use-xhr-instead-of-the-...
- mikewhy 7y agoThat'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> }