user login WIP
This commit is contained in:
37
www/src/functions/request.ts
Normal file
37
www/src/functions/request.ts
Normal file
@ -0,0 +1,37 @@
|
||||
enum Methods {
|
||||
'GET' = 'GET',
|
||||
'POST' = 'POST',
|
||||
'PUT' = 'PUT',
|
||||
}
|
||||
|
||||
export async function get<T>(route: string): Promise<T | null> {
|
||||
return request<T>('GET', route);
|
||||
}
|
||||
|
||||
export async function post<T>(route: string, data: unknown): Promise<T | null> {
|
||||
return request<T>('POST', route, data);
|
||||
}
|
||||
|
||||
export async function put<T>(route: string, data: unknown): Promise<T | null> {
|
||||
return request<T>('PUT', route, data);
|
||||
}
|
||||
|
||||
async function request<T>(
|
||||
method: keyof typeof Methods,
|
||||
route: string,
|
||||
data?: unknown,
|
||||
): Promise<T | null> {
|
||||
const response = await fetch(`${process.env.APIURL}${route}`, {
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
mode: process.env.CORS ? 'cors' : 'same-origin',
|
||||
method: method,
|
||||
body: data ? JSON.stringify(data) : null,
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
return response.json() as T;
|
||||
}
|
||||
return null;
|
||||
}
|
7
www/src/functions/user.ts
Normal file
7
www/src/functions/user.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import type { LoginUserBody, UserInfo } from '@core';
|
||||
import { post } from './request';
|
||||
|
||||
export async function login(raw: LoginUserBody) {
|
||||
const user = await post<UserInfo>('/user/login', raw);
|
||||
console.debug(user);
|
||||
}
|
Reference in New Issue
Block a user