mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-09 23:15:46 +01:00
🏗️ Migrate config save to tRPC
This commit is contained in:
@@ -4,6 +4,9 @@ import Consola from 'consola';
|
||||
import { z } from 'zod';
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import { createTRPCRouter, publicProcedure } from '../trpc';
|
||||
import { BackendConfigType, ConfigType } from '~/types/config';
|
||||
import { getConfig } from '../../../tools/config/getConfig';
|
||||
import { IRssWidget } from '~/widgets/rss/RssWidgetTile';
|
||||
|
||||
export const configRouter = createTRPCRouter({
|
||||
all: publicProcedure.query(async () => {
|
||||
@@ -56,4 +59,104 @@ export const configRouter = createTRPCRouter({
|
||||
message: 'Configuration deleted with success',
|
||||
};
|
||||
}),
|
||||
save: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
config: z.custom<ConfigType>((x) => !!x && typeof x === 'object'),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
Consola.info(`Saving updated configuration of '${input.name}' config.`);
|
||||
|
||||
const previousConfig = getConfig(input.name);
|
||||
|
||||
let newConfig: BackendConfigType = {
|
||||
...input.config,
|
||||
apps: [
|
||||
...input.config.apps.map((app) => ({
|
||||
...app,
|
||||
network: {
|
||||
...app.network,
|
||||
statusCodes:
|
||||
app.network.okStatus === undefined
|
||||
? app.network.statusCodes
|
||||
: app.network.okStatus.map((x) => x.toString()),
|
||||
okStatus: undefined,
|
||||
},
|
||||
integration: {
|
||||
...app.integration,
|
||||
properties: app.integration.properties.map((property) => {
|
||||
if (property.type === 'public') {
|
||||
return {
|
||||
field: property.field,
|
||||
type: property.type,
|
||||
value: property.value,
|
||||
};
|
||||
}
|
||||
|
||||
const previousApp = previousConfig.apps.find(
|
||||
(previousApp) => previousApp.id === app.id
|
||||
);
|
||||
|
||||
const previousProperty = previousApp?.integration?.properties.find(
|
||||
(previousProperty) => previousProperty.field === property.field
|
||||
);
|
||||
|
||||
if (property.value !== undefined && property.value !== null) {
|
||||
Consola.info(
|
||||
'Detected credential change of private secret. Value will be overwritten in configuration'
|
||||
);
|
||||
return {
|
||||
field: property.field,
|
||||
type: property.type,
|
||||
value: property.value,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
field: property.field,
|
||||
type: property.type,
|
||||
value: previousProperty?.value,
|
||||
};
|
||||
}),
|
||||
},
|
||||
})),
|
||||
],
|
||||
};
|
||||
|
||||
newConfig = {
|
||||
...newConfig,
|
||||
widgets: [
|
||||
...newConfig.widgets.map((x) => {
|
||||
if (x.type !== 'rss') {
|
||||
return x;
|
||||
}
|
||||
|
||||
const rssWidget = x as IRssWidget;
|
||||
|
||||
return {
|
||||
...rssWidget,
|
||||
properties: {
|
||||
...rssWidget.properties,
|
||||
rssFeedUrl:
|
||||
typeof rssWidget.properties.rssFeedUrl === 'string'
|
||||
? [rssWidget.properties.rssFeedUrl]
|
||||
: rssWidget.properties.rssFeedUrl,
|
||||
},
|
||||
} as IRssWidget;
|
||||
}),
|
||||
],
|
||||
};
|
||||
|
||||
// Save the body in the /data/config folder with the slug as filename
|
||||
const targetPath = path.join('data/configs', `${input.name}.json`);
|
||||
fs.writeFileSync(targetPath, JSON.stringify(newConfig, null, 2), 'utf8');
|
||||
|
||||
Consola.debug(`Config '${input.name}' has been updated and flushed to '${targetPath}'.`);
|
||||
|
||||
return {
|
||||
message: 'Configuration saved with success',
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user