add types and fix loading

This commit is contained in:
Fran Jurmanović
2025-02-12 00:48:43 +01:00
parent e9487ba38f
commit d29165261c
22 changed files with 410 additions and 146 deletions

View File

@@ -1,5 +1,5 @@
import { authStore } from '$stores/authStore';
import { redirect } from '@sveltejs/kit';
import { redirect, type RequestEvent } from '@sveltejs/kit';
import { redisSessionManager } from '$stores/redisSessionManager';
import { env } from '$env/dynamic/private';
@@ -35,7 +35,7 @@ async function fetchAPI(endpoint: string, method: string = 'GET', body?: object,
}
export async function fetchAPIEvent(
event: object,
event: RequestEvent,
endpoint: string,
method: string = 'GET',
body?: object

View File

@@ -1,9 +1,10 @@
import fetchAPI, { fetchAPIEvent } from '$api/apiService';
import { authStore } from '$stores/authStore';
import { redisSessionManager } from '$stores/redisSessionManager';
import type { RequestEvent } from '@sveltejs/kit';
import { v4 as uuidv4 } from 'uuid';
export const login = async (event: object, username: string, password: string) => {
export const login = async (event: RequestEvent, username: string, password: string) => {
const token = btoa(`${username}:${password}`);
await redisSessionManager.createSession(event.cookies, { token }, uuidv4());
if (!(await checkAuth(event))) {
@@ -15,11 +16,11 @@ export const login = async (event: object, username: string, password: string) =
return true;
};
export const logout = (event) => {
export const logout = (event: RequestEvent) => {
return redisSessionManager.deleteCookie(event.cookies);
};
export const checkAuth = async (event: object) => {
export const checkAuth = async (event: RequestEvent) => {
try {
await fetchAPIEvent(event, '/api');
return true;

View File

@@ -1,21 +1,23 @@
import { fetchAPIEvent } from '$api/apiService';
import type { CarModel, CupCategory, DriverCategory, SessionType, Track } from '$models/lookups';
import type { RequestEvent } from '@sveltejs/kit';
export const getCarModels = async (event: object) => {
export const getCarModels = async (event: RequestEvent): Promise<CarModel[]> => {
return fetchAPIEvent(event, '/lookup/car-models');
};
export const getCupCategories = async (event: object) => {
export const getCupCategories = async (event: RequestEvent): Promise<CupCategory[]> => {
return fetchAPIEvent(event, '/lookup/cup-categories');
};
export const getDriverCategories = async (event: object) => {
export const getDriverCategories = async (event: RequestEvent): Promise<DriverCategory[]> => {
return fetchAPIEvent(event, '/lookup/driver-categories');
};
export const getSessionTypes = async (event: object) => {
export const getSessionTypes = async (event: RequestEvent): Promise<SessionType[]> => {
return fetchAPIEvent(event, '/lookup/session-types');
};
export const getTracks = async (event: object) => {
export const getTracks = async (event: RequestEvent): Promise<Track[]> => {
return fetchAPIEvent(event, '/lookup/tracks');
};

View File

@@ -1,22 +1,74 @@
import { fetchAPIEvent } from '$api/apiService';
import {
configFile,
type AssistRules,
type Config,
type ConfigFile,
type Configuration,
type Configurations,
type EventConfig,
type EventRules,
type ServerSettings
} from '$models/config';
import type { Server } from '$models/server';
import type { RequestEvent } from '@sveltejs/kit';
export const getServers = async (event: object) => {
export const getServers = async (event: RequestEvent): Promise<Server[]> => {
return fetchAPIEvent(event, '/server');
};
export const getConfigFiles = async (event: object, serverId = '') => {
export const getConfigFiles = async (
event: RequestEvent,
serverId: string
): Promise<Configurations> => {
return fetchAPIEvent(event, `/server/${serverId}/config`);
};
export const getConfigFile = async (event: object, serverId = '', file = '') => {
export const getConfigFile = async (
event: RequestEvent,
serverId: string,
file: ConfigFile
): Promise<Config> => {
return fetchAPIEvent(event, `/server/${serverId}/config/${file}`);
};
export const getEventFile = async (event: RequestEvent, serverId: string): Promise<EventConfig> => {
return fetchAPIEvent(event, `/server/${serverId}/config/${configFile.event}`);
};
export const getConfigurationFile = async (
event: RequestEvent,
serverId: string
): Promise<Configuration> => {
return fetchAPIEvent(event, `/server/${serverId}/config/${configFile.configuration}`);
};
export const getAssistRulesFile = async (
event: RequestEvent,
serverId: string
): Promise<AssistRules> => {
return fetchAPIEvent(event, `/server/${serverId}/config/${configFile.assistRules}`);
};
export const getEventRulesFile = async (
event: RequestEvent,
serverId: string
): Promise<EventRules> => {
return fetchAPIEvent(event, `/server/${serverId}/config/${configFile.eventRules}`);
};
export const getSettingsFile = async (
event: RequestEvent,
serverId: string
): Promise<ServerSettings> => {
return fetchAPIEvent(event, `/server/${serverId}/config/${configFile.settings}`);
};
export const updateConfig = async (
event: object,
event: RequestEvent,
serverId: string,
file: string,
newConfig?: object,
file: ConfigFile,
newConfig?: Config,
override = false,
restart = true
) => {
@@ -28,18 +80,18 @@ export const updateConfig = async (
);
};
export const restartService = async (event: object, serverId: number) => {
export const restartService = async (event: RequestEvent, serverId: number) => {
return fetchAPIEvent(event, '/api/restart', 'POST', { serverId });
};
export const startService = async (event: object, serverId: number) => {
export const startService = async (event: RequestEvent, serverId: number) => {
return fetchAPIEvent(event, '/api/start', 'POST', { serverId });
};
export const stopService = async (event: object, serverId: number) => {
export const stopService = async (event: RequestEvent, serverId: number) => {
return fetchAPIEvent(event, '/api/stop', 'POST', { serverId });
};
export const getServiceStatus = async (event: object, serviceName: number) => {
export const getServiceStatus = async (event: RequestEvent, serviceName: string) => {
return fetchAPIEvent(event, `/api/${serviceName}`);
};