node-red-contrib-password-g.../lib/PasswordGeneratorNode.ts
yuto-yuto 456f4332e3 fix
2021-07-11 09:57:56 +02:00

26 lines
949 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.length);
const valueSetPath = msg.to || config.setTo || "payload";
msg = yutolity.setValue(msg, valueSetPath, password);
send(msg);
done();
});
});
}