2d847e154f
中华文明全图鉴——文物全图系统(PC Web 地图 + NestJS API + 管理后台)。 含三大 IP(文物南迁北归 / 国宝海外回归 / 博物馆手艺人)、AI 文物对话、 文物地图与详情、以及 demo-video-kit 演示视频生成工具。
32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
const BASE_URL = import.meta.env.VITE_API_URL ?? "http://localhost:3002";
|
|
|
|
function getToken() {
|
|
return localStorage.getItem("access_token");
|
|
}
|
|
|
|
async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
|
|
const token = getToken();
|
|
const res = await fetch(`${BASE_URL}${path}`, {
|
|
...options,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
...(options.headers ?? {}),
|
|
},
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({}));
|
|
throw new Error((err as { message?: string }).message ?? `HTTP ${res.status}`);
|
|
}
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
export const api = {
|
|
get: <T>(path: string) => request<T>(path),
|
|
post: <T>(path: string, body: unknown) =>
|
|
request<T>(path, { method: "POST", body: JSON.stringify(body) }),
|
|
patch: <T>(path: string, body: unknown) =>
|
|
request<T>(path, { method: "PATCH", body: JSON.stringify(body) }),
|
|
delete: <T>(path: string) => request<T>(path, { method: "DELETE" }),
|
|
};
|