mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-09-19 09:40:13 +00:00
feat: auto generate api types
This commit is contained in:
@@ -22,6 +22,8 @@ export enum FetcherMethod {
|
||||
DELETE = "DELETE",
|
||||
}
|
||||
|
||||
let lastestRefreshTokenAt = 0;
|
||||
|
||||
export async function fetcher<T>(method: FetcherMethod, path: string, data?: any): Promise<T> {
|
||||
let response;
|
||||
if (method === FetcherMethod.GET || method === FetcherMethod.DELETE) {
|
||||
@@ -44,6 +46,13 @@ export async function fetcher<T>(method: FetcherMethod, path: string, data?: any
|
||||
if (!responseData.success) {
|
||||
throw new Error(responseData.error);
|
||||
}
|
||||
|
||||
// auto refresh token
|
||||
if (!lastestRefreshTokenAt || Date.now() - lastestRefreshTokenAt > 1000 * 60 * 60) {
|
||||
lastestRefreshTokenAt = Date.now();
|
||||
fetch("/api/v1/refresh-token")
|
||||
}
|
||||
|
||||
return responseData.data;
|
||||
}
|
||||
|
||||
|
||||
+3
-7
@@ -1,12 +1,8 @@
|
||||
import { User } from "@/types"
|
||||
import { ModelUser } from "@/types"
|
||||
import { fetcher, FetcherMethod } from "./api"
|
||||
|
||||
export const getProfile = async (): Promise<User> => {
|
||||
return fetcher<User>(FetcherMethod.GET, '/api/v1/profile', null)
|
||||
}
|
||||
|
||||
export const refreshToken = async (): Promise<any> => {
|
||||
return fetcher<any>(FetcherMethod.GET, '/api/v1/refresh_token', null)
|
||||
export const getProfile = async (): Promise<ModelUser> => {
|
||||
return fetcher<ModelUser>(FetcherMethod.GET, '/api/v1/profile', null)
|
||||
}
|
||||
|
||||
export const login = async (username: string, password: string): Promise<any> => {
|
||||
|
||||
Reference in New Issue
Block a user