Add type validation for filter

This commit is contained in:
2024-11-15 20:23:48 +01:00
parent d5031b9c1a
commit 51205fbbd2

View File

@@ -3,6 +3,14 @@ import { adapters } from "../adapters";
import { DummyOpenHAB, OpenHAB } from "../openhab";
const createFilter = (filter?: string) => {
if(!filter) {
return (_: Thing) => true;
}
return new Function("$", `const v = ${filter}; if(typeof v !== 'boolean') throw new Error("Filter should be of boolean type"); return v;`) as ((_: Thing) => true);
};
export const importThings = async (config: Config, options: ImportOptions) => {
const OH = options.dryRun ? DummyOpenHAB : OpenHAB;
const openhab = new OH(config.openHAB);
@@ -24,9 +32,7 @@ export const importThings = async (config: Config, options: ImportOptions) => {
const existingThings = await openhab.getThings();
const existingThingsUIDs = existingThings.map(t => t.UID);
const filter: (thing: Thing) => boolean = options.where
? (new Function("$", `return ${options.where}`)) as ((_: Thing) => true)
: (_: Thing) => true;
const filter = createFilter(options.where);
const thingsToImport = things
.filter(t => !existingThingsUIDs.includes(t.UID))