Files
pocket-id-pocket-id-1/frontend/src/lib/services/app-config-service.ts

105 lines
2.7 KiB
TypeScript
Raw Normal View History

import { version as currentVersion } from '$app/environment';
import type { AllAppConfig, AppConfigRawResponse } from '$lib/types/application-configuration';
import axios, { AxiosError } from 'axios';
2024-08-12 11:00:25 +02:00
import APIService from './api-service';
export default class AppConfigService extends APIService {
2024-08-12 11:00:25 +02:00
async list(showAll = false) {
let url = '/application-configuration';
if (showAll) {
url += '/all';
}
const { data } = await this.api.get<AppConfigRawResponse>(url);
2025-01-19 15:30:31 +01:00
return this.parseConfigList(data);
2024-08-12 11:00:25 +02:00
}
async update(appConfig: AllAppConfig) {
2024-10-25 21:33:54 +02:00
// Convert all values to string
const appConfigConvertedToString = {};
for (const key in appConfig) {
(appConfigConvertedToString as any)[key] = (appConfig as any)[key].toString();
}
const res = await this.api.put('/application-configuration', appConfigConvertedToString);
2025-01-19 15:30:31 +01:00
return this.parseConfigList(res.data);
2024-08-12 11:00:25 +02:00
}
async updateFavicon(favicon: File) {
const formData = new FormData();
formData.append('file', favicon!);
await this.api.put(`/application-configuration/favicon`, formData);
}
async updateLogo(logo: File, light = true) {
2024-08-12 11:00:25 +02:00
const formData = new FormData();
formData.append('file', logo!);
await this.api.put(`/application-configuration/logo`, formData, {
params: { light }
});
2024-08-12 11:00:25 +02:00
}
async updateBackgroundImage(backgroundImage: File) {
const formData = new FormData();
formData.append('file', backgroundImage!);
await this.api.put(`/application-configuration/background-image`, formData);
}
async sendTestEmail() {
await this.api.post('/application-configuration/test-email');
}
async syncLdap() {
await this.api.post('/application-configuration/sync-ldap');
}
async getVersionInformation() {
const response = await axios
.get('https://api.github.com/repos/stonith404/pocket-id/releases/latest')
.then((res) => res.data)
.catch((e) => {
console.error(
'Failed to fetch version information',
e instanceof AxiosError && e.response ? e.response.data.message : e
);
return null;
});
let newestVersion: string | null = null;
let isUpToDate: boolean | null = null;
if (response) {
newestVersion = response.tag_name.replace('v', '');
isUpToDate = newestVersion === currentVersion;
}
return {
isUpToDate,
newestVersion,
currentVersion
};
}
2024-10-25 21:33:54 +02:00
2025-01-19 15:30:31 +01:00
private parseConfigList(data: AppConfigRawResponse) {
const appConfig: Partial<AllAppConfig> = {};
data.forEach(({ key, value }) => {
(appConfig as any)[key] = this.parseValue(value);
});
return appConfig as AllAppConfig;
}
2024-10-25 21:33:54 +02:00
private parseValue(value: string) {
if (value === 'true') {
return true;
} else if (value === 'false') {
return false;
} else if (!isNaN(parseFloat(value))) {
return parseFloat(value);
2024-10-25 21:33:54 +02:00
} else {
return value;
}
}
2024-08-12 11:00:25 +02:00
}