9 lines
329 B
TypeScript
9 lines
329 B
TypeScript
// 带认证的 fetch 封装
|
|||
|
|
export async function authFetch(url: string, options: RequestInit = {}): Promise<Response> {
|
||
|
|
const token = localStorage.getItem("wauth_token");
|
||
|
|
const headers = new Headers(options.headers);
|
||
|
|
if (token) {
|
||
|
|
headers.set("X-Auth-User", token);
|
||
|
|
}
|
||
|
|
return fetch(url, { ...options, headers });
|
||
|
|
}
|