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 >
Parameter Type Description 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
Name conflict
A helper named fetchOrThrow also exists in @helpers4/fetch . If you need both in the same file, rename at import with as:
import { fetchOrThrow as fetchOrThrow4promise } from '@helpers4/promise' ;
import { fetchOrThrow as fetchOrThrow4fetch } from '@helpers4/fetch' ; See Name Conflicts for the full resolution guide.
View source on GitHub