登陆修改
This commit is contained in:
@@ -14,6 +14,7 @@ interface RequestOptions {
|
||||
auth?: boolean;
|
||||
encryptBody?: boolean;
|
||||
signal?: AbortSignal;
|
||||
skipAuthRedirect?: boolean;
|
||||
}
|
||||
|
||||
/** Convert snake_case string to camelCase */
|
||||
@@ -110,7 +111,7 @@ export async function apiRequest<T>(path: string, options: RequestOptions = {}):
|
||||
// Handle error responses
|
||||
if (!res.ok) {
|
||||
const msg = parsed?.detail || `请求失败 (${res.status})`;
|
||||
if (res.status === 401) {
|
||||
if (res.status === 401 && !options.skipAuthRedirect) {
|
||||
clearToken();
|
||||
window.location.href = '/login';
|
||||
}
|
||||
@@ -122,11 +123,12 @@ if (!res.ok) {
|
||||
|
||||
// Convenience methods
|
||||
export const api = {
|
||||
get: <T>(path: string, options: boolean | { auth?: boolean; signal?: AbortSignal } = true) => {
|
||||
get: <T>(path: string, options: boolean | { auth?: boolean; signal?: AbortSignal; skipAuthRedirect?: boolean } = true) => {
|
||||
const opts = typeof options === 'boolean' ? { auth: options } : options;
|
||||
return apiRequest<T>(path, opts);
|
||||
},
|
||||
post: <T>(path: string, body?: unknown, auth = true) => apiRequest<T>(path, { method: 'POST', body, auth }),
|
||||
post: <T>(path: string, body?: unknown, auth = true, skipAuthRedirect = false) =>
|
||||
apiRequest<T>(path, { method: 'POST', body, auth, skipAuthRedirect }),
|
||||
put: <T>(path: string, body?: unknown, auth = true) => apiRequest<T>(path, { method: 'PUT', body, auth }),
|
||||
delete: <T>(path: string, auth = true) => apiRequest<T>(path, { method: 'DELETE', auth }),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user