Skip to content

fetchOrThrow

Wraps fetch so that a non-2xx response rejects with an HttpError.

Available since v3.1.3

import { fetchOrThrow } from '@helpers4/promise';
// or, from the all-in-one package (same code, one install):
import { fetchOrThrow } from 'helpers4/promise';
fetchOrThrow(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
ParameterTypeDescription
inputRequestInfo | URLURL or `Request` object passed to `fetch`
initRequestInitOptional `RequestInit` options passed to `fetch` (optional)

Promise<Response> — The response, body unread, when its status is 2xx

const response = await fetchOrThrow('https://api.example.com/repo');
type HttpError = FetchHttpError

View source on GitHub