The framework I'm creating uses json schemas (draft v7) as the source of truth for configs (defaults), credentials, input and outputs messages. This is the folder structure of a single node.
This is an example of a Node that uses json schemas for configs, credentials, input and output messages
import { Type, Static } from "@sinclair/typebox";
import MessageSchema from "../../core/schemas/message";
import TypedInputSchema from "../../core/schemas/typed-input";
const ConfigsSchema = Type.Object({
name: Type.String({ default: "your-node" }),
myProperty: TypedInputSchema,
myProperty2: TypedInputSchema,
country: Type.String({ default: "brasil" }),
server: Type.Optional(Type.String({ nodeType: "remote-server" })),
});
const CredentialsSchema = Type.Object({
password: Type.Optional(
Type.String({
default: "",
minLength: 8,
maxLength: 20,
pattern: "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]+$",
format: "password",
This file has been truncated. show original
import { Static } from "@sinclair/typebox";
import { node } from "../../../core/server/decorators";
import {
InputDoneFunction,
Node,
SendFunction,
} from "../../../core/server/node";
import {
ConfigsSchema,
CredentialsSchema,
InputMessageSchema,
OutputMessageSchema,
} from "../schemas";
type Configs = Static<typeof ConfigsSchema>;
type Credentials = Static<typeof CredentialsSchema>;
type InputMessage = Static<typeof InputMessageSchema>;
type OutputMessage = Static<typeof OutputMessageSchema>;
@node({
This file has been truncated. show original
This an example of Config Node that use a schema for Config props only
import { node } from "../../../core/server/decorators";
import { Node } from "../../../core/server/node";
import { ConfigsSchema } from "../schemas";
type Configs = Static<typeof ConfigsSchema>;
@node({
validation: {
configs: ConfigsSchema,
},
})
export class RemoteServerConfigNode extends Node<Configs> {
host: string;
// NOTE: run only once when node type is registered
static init() {
console.log("server-node");
}
}
In this framework you can use TypeBox to write your schemas with typechecking or just write an object that complies with json schema draft v7. The only additional keyword I had to add is "nodeType", which allows developers to define the node type of a config node.
Intelisense is based on the types you create from your schemas
Validations run in the server using the same schemas
The client side of your node is now reduced to a simple Vue component
And the registration is much simpler than ever before. You no longer have to redefine defaults or credentials. The source of thruth is also your schemas
If you want to use other node-red node props you can still do it
While writting your node forms you can use the same built in node-red inputs you are used to, but with a much simpler api.
Typed Input
Config Input
Select and MultiSelect Input
Editor Input
To avoid breaking the editor all these inputs are being mounted in the dom using node-red apis (jquery widgets)
1 Like