Updating block definitions to use pure JSON schema so that it can be used for easy validation.

This commit is contained in:
mike12345567 2020-09-15 15:52:38 +01:00
parent ba125b5987
commit 43afb9af1b
3 changed files with 235 additions and 162 deletions

View File

@ -8,33 +8,44 @@ const ACTION = {
icon: "ri-save-3-fill", icon: "ri-save-3-fill",
description: "Save a record to your database", description: "Save a record to your database",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
record: { schema: {
type: "record", inputs: {
label: "The record to be written", properties: {
default: {}, record: {
type: "object",
customType: "record",
title: "The record to be written",
default: {},
},
model: {
type: "object",
customType: "model",
title: "Table",
},
},
required: ["record", "model"],
}, },
model: { outputs: {
type: "model", properties: {
label: "Table", response: {
}, type: "object",
}, description: "The response from the table",
outputs: { },
response: { success: {
type: "object", type: "boolean",
label: "The response from the table", description: "Whether the action was successful",
}, },
success: { id: {
type: "boolean", type: "string",
label: "Whether the action was successful", description: "The identifier of the new record",
}, },
id: { revision: {
type: "string", type: "string",
label: "The identifier of the new record", description: "The revision of the new record",
}, },
revision: { },
type: "string", required: ["success", "id", "revision"],
label: "The revision of the new record",
}, },
}, },
}, },
@ -44,24 +55,33 @@ const ACTION = {
name: "Delete Record", name: "Delete Record",
tagline: "Delete a {{inputs.record.model.name}} record", tagline: "Delete a {{inputs.record.model.name}} record",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
id: { schema: {
type: "string", inputs: {
label: "Record ID", properties: {
id: {
type: "string",
title: "Record ID",
},
revision: {
type: "string",
title: "Record Revision",
},
},
required: ["id", "revision"],
}, },
revision: { outputs: {
type: "string", properties: {
label: "Record Revision", response: {
}, type: "object",
}, description: "The response from the table",
outputs: { },
response: { success: {
type: "object", type: "boolean",
label: "The response from the table", description: "Whether the action was successful",
}, },
success: { },
type: "boolean", required: ["success"],
label: "Whether the action was successful",
}, },
}, },
}, },
@ -71,38 +91,48 @@ const ACTION = {
icon: "ri-user-add-fill", icon: "ri-user-add-fill",
name: "Create User", name: "Create User",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
username: { schema: {
type: "string", inputs: {
label: "Username", properties: {
username: {
type: "string",
title: "Username",
},
password: {
type: "string",
customType: "password",
title: "Password",
},
accessLevelId: {
type: "string",
title: "Access Level",
default: accessLevels.POWERUSER_LEVEL_ID,
enum: accessLevels.ACCESS_LEVELS,
},
},
required: ["username", "password", "accessLevelId"],
}, },
password: { outputs: {
type: "password", properties: {
label: "Password", id: {
}, type: "string",
accessLevelId: { description: "The identifier of the new user",
type: "string", },
label: "Access Level", revision: {
default: accessLevels.POWERUSER_LEVEL_ID, type: "string",
options: accessLevels.ACCESS_LEVELS, description: "The revision of the new user",
}, },
}, response: {
outputs: { type: "object",
id: { description: "The response from the user table",
type: "string", },
label: "The identifier of the new user", success: {
}, type: "boolean",
revision: { description: "Whether the action was successful",
type: "string", },
label: "The revision of the new user", },
}, required: ["id", "revision", "success"],
response: {
type: "object",
label: "The response from the user table",
},
success: {
type: "boolean",
label: "Whether the action was successful",
}, },
}, },
}, },
@ -112,32 +142,42 @@ const ACTION = {
icon: "ri-mail-open-fill", icon: "ri-mail-open-fill",
name: "Send Email", name: "Send Email",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
to: { schema: {
type: "string", inputs: {
label: "Send To", properties: {
to: {
type: "string",
title: "Send To",
},
from: {
type: "string",
title: "Send From",
},
subject: {
type: "string",
title: "Email Subject",
},
contents: {
type: "string",
title: "Email Contents",
},
},
required: ["to", "from", "subject", "contents"],
}, },
from: { outputs: {
type: "string", properties: {
label: "Send From", success: {
}, type: "boolean",
subject: { description: "Whether the email was sent",
type: "string", },
label: "Email Subject", response: {
}, type: "object",
contents: { description:
type: "string", "A response from the email client, this may be an error",
label: "Email Contents", },
}, },
}, required: ["success"],
outputs: {
success: {
type: "boolean",
label: "Whether the email was sent",
},
response: {
type: "object",
label: "A response from the email client, this may be an error",
}, },
}, },
}, },
@ -150,26 +190,33 @@ const LOGIC = {
icon: "ri-git-branch-line", icon: "ri-git-branch-line",
description: "Filter any workflows which do not meet certain conditions", description: "Filter any workflows which do not meet certain conditions",
type: "LOGIC", type: "LOGIC",
inputs: { inputs: {},
filter: { schema: {
type: "string", inputs: {
label: "Reference Value", filter: {
type: "string",
title: "Reference Value",
},
condition: {
type: "string",
title: "Condition",
enum: conditions,
default: "equals",
},
value: {
type: "string",
title: "Comparison Value",
},
required: ["filter", "condition", "value"],
}, },
condition: { outputs: {
type: "string", properties: {
label: "Condition", success: {
options: conditions, type: "boolean",
default: "equals", description: "Whether the logic block passed",
}, },
value: { },
type: "string", required: ["success"],
label: "Comparison Value",
},
},
outputs: {
success: {
type: "boolean",
label: "Whether the logic block passed",
}, },
}, },
}, },
@ -178,10 +225,16 @@ const LOGIC = {
icon: "ri-time-fill", icon: "ri-time-fill",
tagline: "Delay for {{inputs.time}} milliseconds", tagline: "Delay for {{inputs.time}} milliseconds",
description: "Delay the workflow until an amount of time has passed", description: "Delay the workflow until an amount of time has passed",
inputs: { inputs: {},
time: { schema: {
type: "number", inputs: {
label: "Delay in milliseconds", properties: {
time: {
type: "number",
title: "Delay in milliseconds",
},
},
required: ["time"],
}, },
}, },
type: "LOGIC", type: "LOGIC",
@ -195,16 +248,27 @@ const TRIGGER = {
icon: "ri-save-line", icon: "ri-save-line",
tagline: "Record is added to {{inputs.model.name}}", tagline: "Record is added to {{inputs.model.name}}",
description: "Fired when a record is saved to your database", description: "Fired when a record is saved to your database",
inputs: { inputs: {},
model: { schema: {
type: "model", inputs: {
label: "Table", properties: {
model: {
type: "object",
customType: "model",
title: "Table",
},
},
required: ["model"],
}, },
}, outputs: {
outputs: { properties: {
record: { record: {
type: "record", type: "object",
label: "The new record that was saved", customType: "record",
description: "The new record that was saved",
},
},
required: ["record"],
}, },
}, },
type: "TRIGGER", type: "TRIGGER",
@ -215,16 +279,27 @@ const TRIGGER = {
icon: "ri-delete-bin-line", icon: "ri-delete-bin-line",
tagline: "Record is deleted from {{inputs.model.name}}", tagline: "Record is deleted from {{inputs.model.name}}",
description: "Fired when a record is deleted from your database", description: "Fired when a record is deleted from your database",
inputs: { inputs: {},
model: { schema: {
type: "model", inputs: {
label: "Table", properties: {
model: {
type: "object",
customType: "model",
title: "Table",
},
},
required: ["model"],
}, },
}, outputs: {
outputs: { properties: {
record: { record: {
type: "record", type: "object",
label: "The record that was deleted", customType: "record",
description: "The record that was deleted",
},
},
required: ["record"],
}, },
}, },
type: "TRIGGER", type: "TRIGGER",

View File

@ -5,11 +5,11 @@ const sgMail = require("@sendgrid/mail")
sgMail.setApiKey(process.env.SENDGRID_API_KEY) sgMail.setApiKey(process.env.SENDGRID_API_KEY)
let BUILTIN_ACTIONS = { let BUILTIN_ACTIONS = {
CREATE_USER: async function({ args, context }) { CREATE_USER: async function(inputs) {
const { username, password, accessLevelId } = args const { username, password, accessLevelId } = inputs
const ctx = { const ctx = {
user: { user: {
instanceId: context.instanceId, instanceId: inputs.instanceId,
}, },
request: { request: {
body: { username, password, accessLevelId }, body: { username, password, accessLevelId },
@ -32,18 +32,16 @@ let BUILTIN_ACTIONS = {
} }
} }
}, },
SAVE_RECORD: async function({ args, context }) { SAVE_RECORD: async function(inputs) {
const { model, ...record } = args.record
const ctx = { const ctx = {
params: { params: {
instanceId: context.instanceId, instanceId: inputs.instanceId,
modelId: model._id, modelId: inputs.model._id,
}, },
request: { request: {
body: record, body: inputs.record,
}, },
user: { instanceId: context.instanceId }, user: { instanceId: inputs.instanceId },
} }
try { try {
@ -62,12 +60,12 @@ let BUILTIN_ACTIONS = {
} }
} }
}, },
SEND_EMAIL: async function({ args }) { SEND_EMAIL: async function(inputs) {
const msg = { const msg = {
to: args.to, to: inputs.to,
from: args.from, from: inputs.from,
subject: args.subject, subject: inputs.subject,
text: args.text, text: inputs.text,
} }
try { try {
@ -83,8 +81,8 @@ let BUILTIN_ACTIONS = {
} }
} }
}, },
DELETE_RECORD: async function({ args, context }) { DELETE_RECORD: async function(inputs) {
const { model, ...record } = args.record const { model, ...record } = inputs.record
// TODO: better logging of when actions are missed due to missing parameters // TODO: better logging of when actions are missed due to missing parameters
if (record.recordId == null || record.revId == null) { if (record.recordId == null || record.revId == null) {
return return
@ -95,7 +93,7 @@ let BUILTIN_ACTIONS = {
recordId: record.recordId, recordId: record.recordId,
revId: record.revId, revId: record.revId,
}, },
user: { instanceId: context.instanceId }, user: { instanceId: inputs.instanceId },
} }
try { try {

View File

@ -1,12 +1,12 @@
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)) const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
let LOGIC = { let LOGIC = {
DELAY: async function delay({ args }) { DELAY: async function delay(inputs) {
await wait(args.time) await wait(inputs.time)
}, },
FILTER: async function filter({ args }) { FILTER: async function filter(inputs) {
const { field, condition, value } = args const { field, condition, value } = inputs
switch (condition) { switch (condition) {
case "equals": case "equals":
if (field !== value) return if (field !== value) return