2020-12-19 17:46:41 +01:00
|
|
|
import { env } from '../env/index';
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
export async function fetchOrThrow(
|
|
|
|
url: string,
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
init?: RequestInit,
|
2020-08-18 13:54:53 +02:00
|
|
|
): Promise<Response> {
|
2020-12-23 17:26:55 +01:00
|
|
|
const { fetch } = env.getEnv();
|
|
|
|
const res = await fetch(url, init);
|
2020-08-18 13:54:53 +02:00
|
|
|
if (!(res.status < 400)) {
|
2020-12-23 17:26:55 +01:00
|
|
|
throw new Error(`failed to fetch: (${res.status}) ${res.statusText}, from url: ${res.url}`);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
2020-12-23 17:26:55 +01:00
|
|
|
return res;
|
|
|
|
}
|