fix: add frontend/src/lib/ to git (was ignored by /lib/ pattern)
The root .gitignore had `lib/` which matched frontend/src/lib/, causing Docker build to fail with "Module not found: Can't resolve '@/lib/utils'" and '@/lib/i18n'. Changed to `/lib/` so it only ignores the Python lib at repo root. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
63
frontend/src/lib/apiClient.ts
Normal file
63
frontend/src/lib/apiClient.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { API_BASE } from "./config";
|
||||
|
||||
export class ApiClientError extends Error {
|
||||
status: number;
|
||||
code?: string;
|
||||
|
||||
constructor(status: number, message: string, code?: string) {
|
||||
super(message);
|
||||
this.name = "ApiClientError";
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
async function request<T>(
|
||||
method: string,
|
||||
url: string,
|
||||
body?: unknown,
|
||||
): Promise<T> {
|
||||
const headers: Record<string, string> = {};
|
||||
|
||||
const token =
|
||||
typeof window !== "undefined" ? localStorage.getItem("token") : null;
|
||||
if (token) {
|
||||
headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
if (body !== undefined) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_BASE}${url}`, {
|
||||
method,
|
||||
headers,
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
let message = `HTTP ${res.status}`;
|
||||
let code: string | undefined;
|
||||
try {
|
||||
const json = await res.json();
|
||||
message = json.message || json.error || json.detail || message;
|
||||
code = json.code;
|
||||
} catch {}
|
||||
throw new ApiClientError(res.status, message, code);
|
||||
}
|
||||
|
||||
if (res.status === 204) return undefined as T;
|
||||
|
||||
const json = await res.json();
|
||||
return json as T;
|
||||
}
|
||||
|
||||
export const apiClient = {
|
||||
get: <T>(url: string) => request<T>("GET", url),
|
||||
post: <T>(url: string, body?: unknown) => request<T>("POST", url, body),
|
||||
patch: <T>(url: string, body?: unknown) => request<T>("PATCH", url, body),
|
||||
put: <T>(url: string, body?: unknown) => request<T>("PUT", url, body),
|
||||
delete: <T>(url: string) => request<T>("DELETE", url),
|
||||
};
|
||||
|
||||
export const API_BASE_URL = API_BASE;
|
||||
Reference in New Issue
Block a user