26 lines
719 B
TypeScript
26 lines
719 B
TypeScript
|
|
/* 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();
|
||
|
|
}
|