mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-09-19 09:40:13 +00:00
feat: login & check user
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
interface CommonResponse<T> {
|
||||
success: boolean;
|
||||
error: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
function buildUrl(path: string, data?: any): string {
|
||||
if (!data)
|
||||
return path
|
||||
const url = new URL(path);
|
||||
for (const key in data) {
|
||||
url.searchParams.append(key, data[key]);
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
export enum FetcherMethod {
|
||||
GET = "GET",
|
||||
POST = "POST",
|
||||
PUT = "PUT",
|
||||
PATCH = "PATCH",
|
||||
DELETE = "DELETE",
|
||||
}
|
||||
|
||||
export async function fetcher<T>(method: FetcherMethod, path: string, data?: any): Promise<T> {
|
||||
let response;
|
||||
if (method === FetcherMethod.GET || method === FetcherMethod.DELETE) {
|
||||
response = await fetch(buildUrl(path, data), {
|
||||
method: "GET",
|
||||
});
|
||||
} else {
|
||||
response = await fetch(path, {
|
||||
method: method,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: data ? JSON.stringify(data) : null,
|
||||
});
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
const responseData: CommonResponse<T> = await response.json();
|
||||
if (!responseData.success) {
|
||||
throw new Error(responseData.error);
|
||||
}
|
||||
return responseData.data;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { User } from "@/types"
|
||||
import { fetcher, FetcherMethod } from "./api"
|
||||
|
||||
export const getProfile = async (): Promise<User> => {
|
||||
return fetcher<User>(FetcherMethod.GET, '/api/v1/profile', null)
|
||||
}
|
||||
|
||||
export const login = async (username: string, password: string): Promise<any> => {
|
||||
return fetcher<any>(FetcherMethod.POST, '/api/v1/login', { username, password })
|
||||
}
|
||||
Reference in New Issue
Block a user