2021-07-09 03:49:18 +02:00
|
|
|
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) => {
|
2021-07-11 06:16:21 +02:00
|
|
|
const password = await generatePassword(config.length);
|
2021-07-09 03:49:18 +02:00
|
|
|
const valueSetPath = msg.to || config.setTo || "payload";
|
2021-07-10 10:33:07 +02:00
|
|
|
msg = yutolity.setValue(msg, valueSetPath, password);
|
2021-07-09 03:49:18 +02:00
|
|
|
send(msg);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|