26 lines
886 B
TypeScript
26 lines
886 B
TypeScript
export const API = "/api/admin/v1";
|
|
|
|
export type ApiErrorBody = { message?: string };
|
|
|
|
/** 统一发送管理后台 JSON 请求,并在会话失效时通知应用返回登录页。 */
|
|
export async function apiPost<T = any>(path: string, body: unknown = {}): Promise<T> {
|
|
const response = await fetch(API + path, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
if (response.status === 401) {
|
|
window.dispatchEvent(new Event("shoplix:unauthorized"));
|
|
}
|
|
if (!response.ok) {
|
|
const error = (await response.json().catch(() => ({}))) as ApiErrorBody;
|
|
throw new Error(error.message || "请求失败");
|
|
}
|
|
return response.json() as Promise<T>;
|
|
}
|
|
|
|
/** 将后端返回的相对素材地址转换为当前域名下的可访问地址。 */
|
|
export function assetUrl(url?: string): string {
|
|
return url || "";
|
|
}
|