user login WIP

This commit is contained in:
2022-10-18 00:28:19 +02:00
parent 8fc5a164a4
commit 90969cfe4c
9 changed files with 180 additions and 38 deletions

View 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;
}

View 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);
}