Add support for enhancemend string configs for credential properties

This commit is contained in:
2024-11-15 20:07:49 +01:00
parent 70b78c6085
commit c2e401eb95
3 changed files with 25 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ import mqtt from "mqtt";
import { Z2MConfig, Thing, Channel, ChannelType, ItemType } from "@types"; import { Z2MConfig, Thing, Channel, ChannelType, ItemType } from "@types";
import { snakecase } from "../utils/string"; import { snakecase } from "../utils/string";
import { Adapter } from "./abstract"; import { Adapter } from "./abstract";
import { enhancedStringConfig } from "../utils/config";
type Device = { type Device = {
definition?: Definition; definition?: Definition;
@@ -140,7 +141,11 @@ export class Z2MAdapter extends Adapter<Z2MConfig> {
const { brokerURL, username, password, clientId, prefix } = this.config; const { brokerURL, username, password, clientId, prefix } = this.config;
return new Promise<Device[]>((resolve, reject) => { return new Promise<Device[]>((resolve, reject) => {
const client = mqtt.connect(brokerURL, { username, password, clientId: clientId || "oh-importer" }); const client = mqtt.connect(brokerURL, {
username: enhancedStringConfig(username),
password: enhancedStringConfig(password),
clientId: clientId || "oh-importer"
});
client.on("message", (_, message) => { client.on("message", (_, message) => {
client.end(); client.end();

View File

@@ -1,5 +1,6 @@
import { OpenHABConfig, Thing } from "@types"; import { OpenHABConfig, Thing } from "@types";
import axios, { AxiosInstance } from "axios"; import axios, { AxiosInstance } from "axios";
import { enhancedStringConfig } from "../utils/config";
export class OpenHAB { export class OpenHAB {
#api: AxiosInstance; #api: AxiosInstance;
@@ -12,7 +13,7 @@ export class OpenHAB {
this.#api = axios.create({ this.#api = axios.create({
baseURL: `${baseURL}/rest`, baseURL: `${baseURL}/rest`,
headers: { headers: {
Authorization: `Bearer ${token}` Authorization: `Bearer ${enhancedStringConfig(token)}`
} }
}); });
} }

17
src/utils/config.ts Normal file
View File

@@ -0,0 +1,17 @@
import { readFileSync } from "fs";
const specialOptions: Record<string, (text: string) => string> = {
$__file: (arg: string) => readFileSync(arg, 'utf8')
};
export const enhancedStringConfig = (value: string) => {
const trimmed = value.trim();
for(const opt of Object.keys(specialOptions)) {
if(trimmed.startsWith(`${opt}:`) && opt in specialOptions) {
return specialOptions[opt](trimmed.slice(opt.length + 1));
}
}
return trimmed;
};