11import crypto from 'node:crypto'
22import yaml from 'yaml'
3- import type { BaseService , Config } from '~/types'
3+ import defu from 'defu'
4+ import type { BaseService , CompleteConfig } from '~/types'
45
56type draftService = Omit < BaseService , 'id' >
67
@@ -11,18 +12,27 @@ function determineServiceId(items: draftService[]): BaseService[] {
1112 } ) )
1213}
1314
14- export async function getLocalConfig ( ) : Promise < Config | null > {
15+ export function getDefaultConfig ( ) : CompleteConfig {
16+ return {
17+ title : 'Mafl' ,
18+ lang : 'en' ,
19+ theme : 'system' ,
20+ services : [ ] ,
21+ }
22+ }
23+
24+ export async function loadLocalConfig ( ) : Promise < CompleteConfig > {
1525 const storage = useStorage ( 'data' )
1626 const file = 'config.yml'
1727
1828 try {
1929 if ( ! await storage . hasItem ( file ) ) {
20- return null
30+ return getDefaultConfig ( )
2131 }
2232
2333 const raw = await storage . getItem < string > ( file )
2434 const config = yaml . parse ( raw || '' ) || { }
25- const services : Config [ 'services' ] = [ ]
35+ const services : CompleteConfig [ 'services' ] = [ ]
2636
2737 if ( Array . isArray ( config . services ) ) {
2838 services . push ( {
@@ -39,22 +49,26 @@ export async function getLocalConfig(): Promise<Config | null> {
3949 }
4050 }
4151
42- return {
43- ...config ,
44- services,
45- }
52+ return defu ( { ...config , services } , getDefaultConfig ( ) )
4653 } catch ( e ) {
4754 // ...
4855 }
4956
50- return null
57+ return getDefaultConfig ( )
58+ }
59+
60+ export async function getLocalConfig ( ) : Promise < CompleteConfig | null > {
61+ const storage = useStorage ( 'main' )
62+ await storage . getKeys ( )
63+
64+ return storage . getItem < CompleteConfig > ( 'config' )
5165}
5266
5367/**
5468 * Safely retrieves a list of services for frontend.
5569 * Omit "secrets" fields.
5670 */
57- export function extractSafelyConfig ( config : Config ) {
71+ export function extractSafelyConfig ( config : CompleteConfig ) {
5872 return JSON . parse ( JSON . stringify (
5973 config , ( key , val ) => key === 'secrets' ? undefined : val ,
6074 ) )
@@ -63,7 +77,7 @@ export function extractSafelyConfig(config: Config) {
6377/**
6478 * Create Map services
6579 */
66- export function extractServicesFromConfig ( config : Config ) : Record < string , BaseService > {
80+ export function extractServicesFromConfig ( config : CompleteConfig ) : Record < string , BaseService > {
6781 return config . services . reduce < Record < string , BaseService > > ( ( acc , group ) => {
6882 for ( const item of group . items ) {
6983 acc [ item . id ] = item
0 commit comments