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: {},
schema: {
inputs: { inputs: {
properties: {
record: { record: {
type: "record", type: "object",
label: "The record to be written", customType: "record",
title: "The record to be written",
default: {}, default: {},
}, },
model: { model: {
type: "model", type: "object",
label: "Table", customType: "model",
title: "Table",
}, },
}, },
required: ["record", "model"],
},
outputs: { outputs: {
properties: {
response: { response: {
type: "object", type: "object",
label: "The response from the table", description: "The response from the table",
}, },
success: { success: {
type: "boolean", type: "boolean",
label: "Whether the action was successful", description: "Whether the action was successful",
}, },
id: { id: {
type: "string", type: "string",
label: "The identifier of the new record", description: "The identifier of the new record",
}, },
revision: { revision: {
type: "string", type: "string",
label: "The revision of the new record", description: "The revision of the new record",
},
},
required: ["success", "id", "revision"],
}, },
}, },
}, },
@ -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: {},
schema: {
inputs: { inputs: {
properties: {
id: { id: {
type: "string", type: "string",
label: "Record ID", title: "Record ID",
}, },
revision: { revision: {
type: "string", type: "string",
label: "Record Revision", title: "Record Revision",
}, },
}, },
required: ["id", "revision"],
},
outputs: { outputs: {
properties: {
response: { response: {
type: "object", type: "object",
label: "The response from the table", description: "The response from the table",
}, },
success: { success: {
type: "boolean", type: "boolean",
label: "Whether the action was successful", description: "Whether the action was successful",
},
},
required: ["success"],
}, },
}, },
}, },
@ -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: {},
schema: {
inputs: { inputs: {
properties: {
username: { username: {
type: "string", type: "string",
label: "Username", title: "Username",
}, },
password: { password: {
type: "password", type: "string",
label: "Password", customType: "password",
title: "Password",
}, },
accessLevelId: { accessLevelId: {
type: "string", type: "string",
label: "Access Level", title: "Access Level",
default: accessLevels.POWERUSER_LEVEL_ID, default: accessLevels.POWERUSER_LEVEL_ID,
options: accessLevels.ACCESS_LEVELS, enum: accessLevels.ACCESS_LEVELS,
}, },
}, },
required: ["username", "password", "accessLevelId"],
},
outputs: { outputs: {
properties: {
id: { id: {
type: "string", type: "string",
label: "The identifier of the new user", description: "The identifier of the new user",
}, },
revision: { revision: {
type: "string", type: "string",
label: "The revision of the new user", description: "The revision of the new user",
}, },
response: { response: {
type: "object", type: "object",
label: "The response from the user table", description: "The response from the user table",
}, },
success: { success: {
type: "boolean", type: "boolean",
label: "Whether the action was successful", description: "Whether the action was successful",
},
},
required: ["id", "revision", "success"],
}, },
}, },
}, },
@ -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: {},
schema: {
inputs: { inputs: {
properties: {
to: { to: {
type: "string", type: "string",
label: "Send To", title: "Send To",
}, },
from: { from: {
type: "string", type: "string",
label: "Send From", title: "Send From",
}, },
subject: { subject: {
type: "string", type: "string",
label: "Email Subject", title: "Email Subject",
}, },
contents: { contents: {
type: "string", type: "string",
label: "Email Contents", title: "Email Contents",
}, },
}, },
required: ["to", "from", "subject", "contents"],
},
outputs: { outputs: {
properties: {
success: { success: {
type: "boolean", type: "boolean",
label: "Whether the email was sent", description: "Whether the email was sent",
}, },
response: { response: {
type: "object", type: "object",
label: "A response from the email client, this may be an error", description:
"A response from the email client, this may be an error",
},
},
required: ["success"],
}, },
}, },
}, },
@ -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: {},
schema: {
inputs: { inputs: {
filter: { filter: {
type: "string", type: "string",
label: "Reference Value", title: "Reference Value",
}, },
condition: { condition: {
type: "string", type: "string",
label: "Condition", title: "Condition",
options: conditions, enum: conditions,
default: "equals", default: "equals",
}, },
value: { value: {
type: "string", type: "string",
label: "Comparison Value", title: "Comparison Value",
}, },
required: ["filter", "condition", "value"],
}, },
outputs: { outputs: {
properties: {
success: { success: {
type: "boolean", type: "boolean",
label: "Whether the logic block passed", description: "Whether the logic block passed",
},
},
required: ["success"],
}, },
}, },
}, },
@ -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: {},
schema: {
inputs: { inputs: {
properties: {
time: { time: {
type: "number", type: "number",
label: "Delay in milliseconds", 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: {},
schema: {
inputs: { inputs: {
properties: {
model: { model: {
type: "model", type: "object",
label: "Table", 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: {},
schema: {
inputs: { inputs: {
properties: {
model: { model: {
type: "model", type: "object",
label: "Table", 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