Attempts to a broken dynamic service loading.
This commit is contained in:
@@ -1,32 +1,33 @@
|
||||
import configData from '../config.yml';
|
||||
|
||||
export interface Brand {
|
||||
export interface BrandConfig {
|
||||
logo?: string;
|
||||
icon?: string;
|
||||
usemask?: boolean;
|
||||
}
|
||||
|
||||
export interface Section extends Brand {
|
||||
export interface SectionConfig extends BrandConfig {
|
||||
title: string;
|
||||
subtitle?: string;
|
||||
}
|
||||
|
||||
export interface Service extends Section {
|
||||
export interface ServiceConfig extends SectionConfig {
|
||||
url: string;
|
||||
target?: string;
|
||||
type?: string;
|
||||
data?: Record<string, unknown>;
|
||||
|
||||
[x: string]: unknown;
|
||||
}
|
||||
|
||||
export interface ServiceGroup extends Section {
|
||||
items: Service[];
|
||||
export interface ServiceGroupConfig extends SectionConfig {
|
||||
items: ServiceConfig[];
|
||||
|
||||
[x: string]: unknown;
|
||||
}
|
||||
|
||||
export interface Config extends Section {
|
||||
services: ServiceGroup[];
|
||||
export interface Config extends SectionConfig {
|
||||
services: ServiceGroupConfig[];
|
||||
|
||||
[x: string]: unknown;
|
||||
}
|
||||
@@ -46,9 +47,9 @@ const requiredConfig: DeepRequired<Config> = {
|
||||
items: [
|
||||
{
|
||||
title: '',
|
||||
url: '',
|
||||
url: ''
|
||||
}
|
||||
],
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
@@ -63,47 +64,44 @@ export const defaultConfig: Config = {
|
||||
services: []
|
||||
};
|
||||
|
||||
type SPOJO = Record<string, unknown>;
|
||||
|
||||
type SPOJO = Record<string,unknown>
|
||||
function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
|
||||
const res: Type = { ...toStrip };
|
||||
const referenceNames = Object.entries(reference).map(([key, value]) => key);
|
||||
|
||||
function strip<Type extends SPOJO> (toStrip: Type, reference: Type): Type {
|
||||
const res: Type = {...toStrip}
|
||||
const referenceNames = Object.entries(reference).map(([key,value]) => key)
|
||||
|
||||
for ( const [key,value] of Object.entries(res) ) {
|
||||
if ( referenceNames.includes(key) == false ) {
|
||||
for (const [key, value] of Object.entries(res)) {
|
||||
if (referenceNames.includes(key) == false) {
|
||||
// remove the object
|
||||
delete res[key];
|
||||
continue
|
||||
}
|
||||
|
||||
// strips further arrays
|
||||
if ( value instanceof Array ) {
|
||||
const stripped : SPOJO = {};
|
||||
const childRef = (reference[key] as Array<SPOJO>)[0];
|
||||
stripped[key] = value.map((v: SPOJO) => strip(v,childRef));
|
||||
Object.assign(res,stripped);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( typeof value != "object") {
|
||||
continue
|
||||
// strips further arrays
|
||||
if (value instanceof Array) {
|
||||
const stripped: SPOJO = {};
|
||||
const childRef = (reference[key] as Array<SPOJO>)[0];
|
||||
stripped[key] = value.map((v: SPOJO) => strip(v, childRef));
|
||||
Object.assign(res, stripped);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeof value != 'object') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// it is a child object, we strip it further
|
||||
const stripped : SPOJO = {};
|
||||
stripped[key] = strip(value as SPOJO,reference[key] as SPOJO);
|
||||
Object.assign(res,stripped);
|
||||
|
||||
const stripped: SPOJO = {};
|
||||
stripped[key] = strip(value as SPOJO, reference[key] as SPOJO);
|
||||
Object.assign(res, stripped);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export function stripPrivateFields(config: Config): Config {
|
||||
return strip(config,requiredConfig);
|
||||
return strip(config, requiredConfig);
|
||||
}
|
||||
|
||||
|
||||
export const config: Config = mergeConfig(defaultConfig, configData);
|
||||
|
||||
export const clientConfig :Config = stripPrivateFields(config)
|
||||
export const clientConfig: Config = stripPrivateFields(config);
|
||||
|
||||
9
src/lib/services/generic/GenericServiceCard.svelte
Normal file
9
src/lib/services/generic/GenericServiceCard.svelte
Normal file
@@ -0,0 +1,9 @@
|
||||
<script lang="ts">
|
||||
import type { ServiceConfig } from '$lib/config';
|
||||
|
||||
export let data: ServiceConfig;
|
||||
</script>
|
||||
|
||||
<div class="service-card">
|
||||
<title>{data.title}</title>
|
||||
</div>
|
||||
27
src/lib/services/service.ts
Normal file
27
src/lib/services/service.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { ServiceConfig } from '$lib/config';
|
||||
|
||||
interface ServiceHandlerArgs {
|
||||
fetch: typeof fetch;
|
||||
config: ServiceConfig;
|
||||
}
|
||||
|
||||
export type ServiceHandler = (input: Partial<ServiceHandlerArgs>) => {
|
||||
data: Record<string, unknown>;
|
||||
componentPath: string;
|
||||
};
|
||||
|
||||
const services: Record<string, ServiceHandler> = {};
|
||||
|
||||
export function registerService(type: string, handler: ServiceHandler) {
|
||||
services[type] = handler;
|
||||
}
|
||||
|
||||
export function getService(type: string): ServiceHandler {
|
||||
const handler = services[type];
|
||||
if (handler == undefined) {
|
||||
return () => {
|
||||
return { data: {}, componentPath: 'generic/GenericServiceCard.svelte' };
|
||||
};
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
Reference in New Issue
Block a user