automatic service registration à la Svelte

This commit is contained in:
2023-08-11 18:49:06 +02:00
parent 9903497867
commit f23f268b60
11 changed files with 95 additions and 49 deletions

View File

@@ -1,9 +0,0 @@
<script lang="ts">
import type { ServiceConfig } from '$lib/config';
export let data: ServiceConfig;
</script>
<div class="service-card">
<title>{data.title}</title>
</div>

View File

@@ -0,0 +1,9 @@
import { type ServiceHandler } from '../service';
export const handle: ServiceHandler = ({ config }) => {
const data = {};
if (config.apikey != undefined) {
//TODO: fetch data
}
return { data, componentPath: 'pihole/PiHoleContent.svelte' };
};

View File

@@ -0,0 +1,5 @@
<script lang="ts">
export const data = undefined;
</script>
<p>pihole status</p>

View File

@@ -0,0 +1,5 @@
import type { ServiceHandler } from '../service';
export const handle: ServiceHandler = () => {
return { data: {}, componentPath: 'prowlarr/ProwlarrContent.svelte' };
};

View File

@@ -0,0 +1,5 @@
<script lang="ts">
export const data = undefined;
</script>
<p>Prowlarr content</p>

View File

@@ -5,26 +5,7 @@ interface ServiceHandlerArgs {
config: ServiceConfig;
}
export type ServiceHandler = (input: Partial<ServiceHandlerArgs>) => {
export type ServiceHandler = (input: ServiceHandlerArgs) => {
data: Record<string, unknown>;
componentPath: string;
};
const services: Record<string, [ServiceHandler, string]> = {};
export function registerService(type: string, handler: ServiceHandler) {
services[type] = [handler, type];
}
export function getService(type: string): [ServiceHandler, string] {
const handler = services[type];
if (handler == undefined) {
return [
() => {
return { data: {}, componentPath: 'generic/GenericServiceCard.svelte' };
},
'generic'
];
}
return handler;
}

View File

@@ -0,0 +1,33 @@
import type { ServiceHandler } from './service';
const services: Record<string, [ServiceHandler, string]> = {};
function registerService(type: string, handler: ServiceHandler) {
services[type] = [handler, type];
}
export function getService(type: string): [ServiceHandler, string] {
const handler = services[type];
if (handler == undefined) {
return [
() => {
return { data: {}, componentPath: '' };
},
''
];
}
return handler;
}
export async function initServices() {
const services = import.meta.glob('/src/lib/services/**/+service.ts');
for (const [path, load] of Object.entries(services)) {
const { handle } = (await load()) as any;
if (handle == undefined) {
continue;
}
const typeName = path.slice(18, -12);
registerService(typeName, handle);
}
}