Skip to content

Commit a849e50

Browse files
committed
config endpoint must handle functions in module configs
1 parent 61870ae commit a849e50

4 files changed

Lines changed: 74 additions & 3 deletions

File tree

js/main.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,19 @@ const MM = (function () {
475475
const loadConfig = async function () {
476476
try {
477477
const res = await fetch(new URL("config/", `${location.origin}${config.basePath}`));
478-
config = JSON.parse(await res.text());
478+
479+
config = JSON.parse(await res.text(), (key, value) => {
480+
if (typeof value === "string") {
481+
// checks for classic function OR arrow function
482+
const isFunction = value.includes("function") || value.includes("=>");
483+
484+
if (isFunction) {
485+
// eval() often needs brackets around
486+
return eval(`(${value})`);
487+
}
488+
}
489+
return value;
490+
});
479491
} catch (error) {
480492
Log.error("Unable to retrieve config", error);
481493
}

js/server.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,22 @@ function Server (configObj) {
111111
const getStartup = (req, res) => res.send(startUp);
112112

113113
const getConfig = (req, res) => {
114+
let obj;
114115
if (config.hideConfigSecrets) {
115-
res.send(configObj.redactedConf);
116+
obj = configObj.redactedConf;
116117
} else {
117-
res.send(configObj.fullConf);
118+
obj = configObj.fullConf;
118119
}
120+
const jsonString = JSON.stringify(obj, (key, value) => {
121+
if (typeof value === "function") {
122+
return value.toString();
123+
}
124+
return value;
125+
});
126+
res.set("Content-Type", "application/json");
127+
res.send(jsonString);
119128
};
129+
120130
app.get("/config", (req, res) => getConfig(req, res));
121131

122132
app.get("/cors", async (req, res) => await cors(req, res));

tests/configs/config_functions.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^roundToInt2$" }]*/
2+
3+
let config = require(`${process.cwd()}/tests/configs/default.js`).configFactory({
4+
modules: [
5+
{
6+
module: "clock",
7+
position: "middle_center",
8+
config: {
9+
moduleFunctions: {
10+
roundToInt1: (value) => {
11+
try {
12+
return Math.round(parseFloat(value));
13+
} catch {
14+
return value;
15+
}
16+
},
17+
roundToInt2: function (value) {
18+
try {
19+
return Math.round(parseFloat(value));
20+
} catch {
21+
return value;
22+
}
23+
}
24+
}
25+
}
26+
}
27+
]
28+
});
29+
30+
/*************** DO NOT EDIT THE LINE BELOW ***************/
31+
if (typeof module !== "undefined") {
32+
module.exports = config;
33+
}

tests/e2e/config_functions_spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const helpers = require("./helpers/global-setup");
2+
3+
describe("config with module function", () => {
4+
beforeAll(async () => {
5+
await helpers.startApplication("tests/configs/config_functions.js");
6+
});
7+
8+
afterAll(async () => {
9+
await helpers.stopApplication();
10+
});
11+
12+
it("config should resolve module functions", () => {
13+
expect(config.modules[0].config.moduleFunctions.roundToInt1(13.3)).toBe(13);
14+
expect(config.modules[0].config.moduleFunctions.roundToInt2(13.3)).toBe(13);
15+
});
16+
});

0 commit comments

Comments
 (0)