node-red-contrib-password-g.../lib/PasswordGeneratorNode.ts
2021-07-11 09:57:55 +02:00

27 lines
942 B
TypeScript

import * as nodered from "node-red";
import { generatePassword } from "./PasswordGenerator";
import { PasswordGeneratorNodeDef } from "./PasswordGeneratorNodeDef";
import * as yutolity from "yutolity";
// it can't set to the input event listener now
// using any is workaround but not good
interface PayloadType extends nodered.NodeMessageInFlow {
to: string;
}
export = (RED: nodered.NodeAPI): void => {
RED.nodes.registerType("password-generator",
function (this: nodered.Node, config: PasswordGeneratorNodeDef): void {
RED.nodes.createNode(this, config);
this.on("input", async (msg: any, send, done) => {
const password = await generatePassword(config.size);
const valueSetPath = msg.to || config.setTo || "payload";
yutolity.setValue(msg, valueSetPath, password);
send(msg);
done();
});
});
}