31 lines
765 B
TypeScript
31 lines
765 B
TypeScript
import { RequestInit } from "next/dist/server/web/spec-extension/request";
|
|
|
|
const FRONTEND_URL = process.env.NEXT_PUBLIC_FRONTEND_URL;
|
|
|
|
export async function cmFetch<T>(
|
|
bffUrl: string | URL,
|
|
options?: RequestInit,
|
|
): Promise<T> {
|
|
const response = await fetch(`${FRONTEND_URL}/${bffUrl}`, options);
|
|
|
|
if (!response.ok) {
|
|
throw new Error("Request failed");
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function cmFetchRaw(
|
|
bffUrl: string | URL,
|
|
options?: RequestInit,
|
|
ignoreStatuses?: number[],
|
|
): Promise<Response> {
|
|
const response = await fetch(`${FRONTEND_URL}/${bffUrl}`, options);
|
|
|
|
if (!response.ok && !ignoreStatuses?.includes(response.status)) {
|
|
throw new Error("Request failed");
|
|
}
|
|
console.log(response);
|
|
|
|
return response;
|
|
}
|