CoffeeChat/webapp/helper/api/ApiConnector.ts

26 lines
719 B
TypeScript
Raw Permalink Normal View History

2026-04-03 12:35:13 +02:00
/* eslint-disable @typescript-eslint/no-explicit-any */
"use server";
import { cookies } from "next/headers";
const _URL = `http://localhost:8081/api`;
export async function CallApi<T>(
endPoint: string,
method: "GET" | "POST",
body?: any,
){
const session = (await cookies()).get("ccsession")?.value ?? ("" as string);
const res = await fetch(`${_URL}${endPoint}`, {
method,
headers: {
"content-type": "application/json",
Authorization: `Bearer ${session}`,
},
body: JSON.stringify(body),
}).catch((e) => console.error(e));
if (!((res as Response).status === 200))
throw Error(JSON.stringify(await (res as Response).json()));
return await (res as Response).json();
}