mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-12 00:15:48 +01:00
✨ Add update config logic
This commit is contained in:
1
src/modules/ModuleTypes.d.ts
vendored
1
src/modules/ModuleTypes.d.ts
vendored
@@ -5,6 +5,7 @@
|
|||||||
import { TablerIcon } from '@tabler/icons';
|
import { TablerIcon } from '@tabler/icons';
|
||||||
|
|
||||||
// Note: Maybe use context to keep track of the modules
|
// Note: Maybe use context to keep track of the modules
|
||||||
|
// TODO: Remove this old component and the entire file
|
||||||
export interface IModule {
|
export interface IModule {
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
|
|||||||
@@ -1,22 +1,61 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import { BackendConfigType, ConfigType } from '../../../types/config';
|
||||||
|
import { getConfig } from '../../../tools/config/getConfig';
|
||||||
|
|
||||||
function Put(req: NextApiRequest, res: NextApiResponse) {
|
function Put(req: NextApiRequest, res: NextApiResponse) {
|
||||||
// Get the slug of the request
|
// Get the slug of the request
|
||||||
const { slug } = req.query as { slug: string };
|
const { slug } = req.query as { slug: string };
|
||||||
// Get the body of the request
|
// Get the body of the request
|
||||||
const { body }: { body: string } = req;
|
const { body: config }: { body: ConfigType } = req;
|
||||||
if (!slug || !body) {
|
if (!slug || !config) {
|
||||||
res.status(400).json({
|
return res.status(400).json({
|
||||||
error: 'Wrong request',
|
error: 'Wrong request',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Save the body in the /data/config folder with the slug as filename
|
|
||||||
|
|
||||||
|
const previousConfig = getConfig(slug);
|
||||||
|
|
||||||
|
const newConfig: BackendConfigType = {
|
||||||
|
...config,
|
||||||
|
apps: [
|
||||||
|
...config.apps.map((app) => ({
|
||||||
|
...app,
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
field: property.field,
|
||||||
|
type: property.type,
|
||||||
|
value: property.value !== undefined ? property.value : previousProperty?.value,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
// Save the body in the /data/config folder with the slug as filename
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
path.join('data/configs', `${slug}.json`),
|
path.join('data/configs', `${slug}.json`),
|
||||||
JSON.stringify(body, null, 2),
|
JSON.stringify(newConfig, null, 2),
|
||||||
'utf8'
|
'utf8'
|
||||||
);
|
);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
|
|||||||
Reference in New Issue
Block a user