mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-02 13:33:40 +01:00
Private widget options API
This commit is contained in:
parent
8e2ff61f1c
commit
7c39cd8960
@ -4,7 +4,7 @@ import path from "path";
|
||||
|
||||
import yaml from "js-yaml";
|
||||
|
||||
import checkAndCopyConfig from "utils/config/config";
|
||||
import checkAndCopyConfig, { sanitizePrivateOptions } from "utils/config/config";
|
||||
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/config/service-helpers";
|
||||
|
||||
export async function bookmarksResponse() {
|
||||
@ -38,9 +38,12 @@ export async function widgetsResponse() {
|
||||
if (!widgets) return [];
|
||||
|
||||
// map easy to write YAML objects into easy to consume JS arrays
|
||||
const widgetsArray = widgets.map((group) => ({
|
||||
const widgetsArray = widgets.map((group, index) => ({
|
||||
type: Object.keys(group)[0],
|
||||
options: { ...group[Object.keys(group)[0]] },
|
||||
options: {
|
||||
index,
|
||||
...sanitizePrivateOptions(group[Object.keys(group)[0]])
|
||||
},
|
||||
}));
|
||||
|
||||
return widgetsArray;
|
||||
|
@ -34,3 +34,16 @@ export function getSettings() {
|
||||
const fileContents = readFileSync(settingsYaml, "utf8");
|
||||
return yaml.load(fileContents);
|
||||
}
|
||||
|
||||
export function sanitizePrivateOptions(options, privateOnly = false) {
|
||||
const privateOptions = ["url", "username", "password", "key"];
|
||||
const sanitizedOptions = {};
|
||||
Object.keys(options).forEach((key) => {
|
||||
if (!privateOnly && !privateOptions.includes(key)) {
|
||||
sanitizedOptions[key] = options[key];
|
||||
} else if (privateOnly && privateOptions.includes(key)) {
|
||||
sanitizedOptions[key] = options[key];
|
||||
}
|
||||
});
|
||||
return sanitizedOptions;
|
||||
}
|
@ -5,7 +5,7 @@ import yaml from "js-yaml";
|
||||
import Docker from "dockerode";
|
||||
import * as shvl from "shvl";
|
||||
|
||||
import checkAndCopyConfig from "utils/config/config";
|
||||
import checkAndCopyConfig, { sanitizePrivateOptions } from "utils/config/config";
|
||||
import getDockerArguments from "utils/config/docker";
|
||||
|
||||
export async function servicesFromConfig() {
|
||||
@ -166,3 +166,22 @@ export default async function getServiceWidget(group, service) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function getPrivateWidgetOptions(type, index) {
|
||||
checkAndCopyConfig("widgets.yaml");
|
||||
|
||||
const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
|
||||
const fileContents = await fs.readFile(widgetsYaml, "utf8");
|
||||
const widgets = yaml.load(fileContents);
|
||||
|
||||
if (!widgets) return [];
|
||||
|
||||
const privateOptions = widgets.map((group, widgetIndex) => ({
|
||||
type: Object.keys(group)[0],
|
||||
index: widgetIndex,
|
||||
options: sanitizePrivateOptions(group[Object.keys(group)[0]], true),
|
||||
}));
|
||||
|
||||
return (type !== undefined && index !== undefined) ? privateOptions.find(o => o.type === type && o.index === parseInt(index, 10))?.options : privateOptions;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user