mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-09-19 17:50:13 +00:00
Dashboard Redesign (#48)
* feat: add user_template setting * style: header * style: page padding * style: header * feat: header now time * style: login page * feat: nav indicator * style: button inset shadow * style: footer text size * feat: header show login_ip * fix: error toast * fix: frontend_templates setting * fix: lint * feat: pr auto format * chore: auto-fix linting and formatting issues --------- Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
co-authored by
hamster1963
parent
811835ffe5
commit
540bb0185d
+21
-19
@@ -1,17 +1,16 @@
|
||||
interface CommonResponse<T> {
|
||||
success: boolean;
|
||||
error: string;
|
||||
data: T;
|
||||
success: boolean
|
||||
error: string
|
||||
data: T
|
||||
}
|
||||
|
||||
function buildUrl(path: string, data?: any): string {
|
||||
if (!data)
|
||||
return path
|
||||
const url = new URL(path);
|
||||
if (!data) return path
|
||||
const url = new URL(path)
|
||||
for (const key in data) {
|
||||
url.searchParams.append(key, data[key]);
|
||||
url.searchParams.append(key, data[key])
|
||||
}
|
||||
return url.toString();
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
export enum FetcherMethod {
|
||||
@@ -22,14 +21,14 @@ export enum FetcherMethod {
|
||||
DELETE = "DELETE",
|
||||
}
|
||||
|
||||
let lastestRefreshTokenAt = 0;
|
||||
let lastestRefreshTokenAt = 0
|
||||
|
||||
export async function fetcher<T>(method: FetcherMethod, path: string, data?: any): Promise<T> {
|
||||
let response;
|
||||
let response
|
||||
if (method === FetcherMethod.GET || method === FetcherMethod.DELETE) {
|
||||
response = await fetch(buildUrl(path, data), {
|
||||
method: "GET",
|
||||
});
|
||||
})
|
||||
} else {
|
||||
response = await fetch(path, {
|
||||
method: method,
|
||||
@@ -37,25 +36,28 @@ export async function fetcher<T>(method: FetcherMethod, path: string, data?: any
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: data ? JSON.stringify(data) : null,
|
||||
});
|
||||
})
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
throw new Error(response.statusText)
|
||||
}
|
||||
const responseData: CommonResponse<T> = await response.json();
|
||||
const responseData: CommonResponse<T> = await response.json()
|
||||
if (!responseData.success) {
|
||||
throw new Error(responseData.error);
|
||||
throw new Error(responseData.error)
|
||||
}
|
||||
|
||||
// auto refresh token
|
||||
if (document.cookie && (!lastestRefreshTokenAt || Date.now() - lastestRefreshTokenAt > 1000 * 60 * 60)) {
|
||||
lastestRefreshTokenAt = Date.now();
|
||||
if (
|
||||
document.cookie &&
|
||||
(!lastestRefreshTokenAt || Date.now() - lastestRefreshTokenAt > 1000 * 60 * 60)
|
||||
) {
|
||||
lastestRefreshTokenAt = Date.now()
|
||||
fetch("/api/v1/refresh-token")
|
||||
}
|
||||
|
||||
return responseData.data;
|
||||
return responseData.data
|
||||
}
|
||||
|
||||
export async function swrFetcher<T>(input: string | URL | globalThis.Request, init?: RequestInit) {
|
||||
return fetcher<T>(init?.method as FetcherMethod, input.toString(), init?.body);
|
||||
return fetcher<T>(init?.method as FetcherMethod, input.toString(), init?.body)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user