feat: add boards and tasks management endpoints
This commit is contained in:
@@ -1,44 +1,23 @@
|
||||
function getActorId(): string | undefined {
|
||||
if (typeof window !== "undefined") {
|
||||
const stored = window.localStorage.getItem("actor_employee_id");
|
||||
if (stored) return stored;
|
||||
const env = process.env.NEXT_PUBLIC_ACTOR_EMPLOYEE_ID;
|
||||
if (env) {
|
||||
window.localStorage.setItem("actor_employee_id", env);
|
||||
return env;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
return process.env.NEXT_PUBLIC_ACTOR_EMPLOYEE_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Orval-generated client expects the fetcher to return an object like:
|
||||
* { data: <json>, status: <number>, headers: Headers }
|
||||
*/
|
||||
export async function customFetch<T>(
|
||||
export const customFetch = async <T>(
|
||||
url: string,
|
||||
options: RequestInit,
|
||||
): Promise<T> {
|
||||
const base = process.env.NEXT_PUBLIC_API_URL;
|
||||
if (!base) throw new Error("NEXT_PUBLIC_API_URL is not set");
|
||||
|
||||
const res = await fetch(`${base}${url}`, {
|
||||
options: RequestInit
|
||||
): Promise<T> => {
|
||||
const baseUrl = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000";
|
||||
const response = await fetch(`${baseUrl}${url}`, {
|
||||
...options,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(getActorId() ? { "X-Actor-Employee-Id": String(getActorId()) } : {}),
|
||||
...(options.headers ?? {}),
|
||||
},
|
||||
});
|
||||
|
||||
const text = await res.text().catch(() => "");
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`${res.status} ${res.statusText}${text ? `: ${text}` : ""}`);
|
||||
if (!response.ok) {
|
||||
throw new Error("Request failed");
|
||||
}
|
||||
|
||||
const json = text ? JSON.parse(text) : null;
|
||||
// Match the types generated by Orval (status + headers + data)
|
||||
return ({ data: json, status: res.status, headers: res.headers } as unknown) as T;
|
||||
}
|
||||
if (response.status === 204) {
|
||||
return undefined as T;
|
||||
}
|
||||
|
||||
return (await response.json()) as T;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user