Add basic services; Base layout

This commit is contained in:
Aslan 2026-01-01 17:21:56 +01:00
parent be6467cd2c
commit ef526cd2db
30 changed files with 291 additions and 34 deletions

18
src/api/auth/auth.ts Normal file
View file

@ -0,0 +1,18 @@
import { callApi, HTTP } from "../tools";
import {
IFetchLoginRequest,
IFetchLoginResponse,
IFetchRefreshResponse,
} from "./types";
const fetchLoginApi = async (
request: IFetchLoginRequest,
): Promise<IFetchLoginResponse> => {
return await callApi(HTTP.POST, `auth/login`, request);
};
const fetchRefreshApi = async (): Promise<IFetchRefreshResponse> => {
return await callApi(HTTP.GET_REFRESH, `auth/refresh`);
};
export { fetchLoginApi, fetchRefreshApi };

2
src/api/auth/index.ts Normal file
View file

@ -0,0 +1,2 @@
export * from "./auth";
export * from "./types";

21
src/api/auth/types.ts Normal file
View file

@ -0,0 +1,21 @@
interface IFetchLoginRequest {
username: string;
password: string;
}
interface IFetchLoginResponse {
id: string;
ownerId: string;
}
interface IFetchRefreshResponse {
id: string;
ownerId: string;
token: string;
}
export {
type IFetchLoginRequest,
type IFetchLoginResponse,
type IFetchRefreshResponse,
};

View file

@ -1,5 +1,6 @@
{
"schema": "http",
"url": "localhost",
"port": 3012
"port": 3012,
"path": "api/v1"
}

View file

@ -1,31 +1,75 @@
import { state } from "../store/state";
import config from "./config.json";
enum HTTP {
GET_REFRESH,
GET,
POST,
PATCH,
DELETE,
}
async function callApi(type: HTTP, path: string, body?: any) {
async function callApi(type: HTTP, path: string, body?: object) {
let response: Response;
switch (type) {
case HTTP.GET:
case HTTP.GET_REFRESH:
response = await fetch(
`${config.schema}://${config.url}:${config.port}/${path}`,
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
},
);
break;
case HTTP.GET:
response = await fetch(
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`,
},
},
);
break;
case HTTP.POST:
response = await fetch(
`${config.schema}://${config.url}:${config.port}/${path}`,
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`,
},
body: JSON.stringify(body ?? {}),
},
);
break;
case HTTP.PATCH:
response = await fetch(
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`,
},
body: JSON.stringify(body ?? {}),
},
);
break;
case HTTP.DELETE:
response = await fetch(
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`,
},
body: JSON.stringify(body ?? {}),
},