1
0
Fork 0
mirror of synced 2024-10-01 09:38:55 +13:00

Saving progress towards re-working CTX usage in automation steps.

This commit is contained in:
mike12345567 2021-09-09 12:23:52 +01:00
parent 4e294fbcd9
commit c337a855cc
4 changed files with 37 additions and 26 deletions

View file

@ -160,8 +160,6 @@ exports.execute = async function (ctx) {
) )
const integration = new Integration(datasource.config) const integration = new Integration(datasource.config)
console.log(query)
// ctx.body = {}
// call the relevant CRUD method on the integration class // call the relevant CRUD method on the integration class
ctx.body = formatResponse(await integration[query.queryVerb](enrichedQuery)) ctx.body = formatResponse(await integration[query.queryVerb](enrichedQuery))
// cleanup // cleanup

View file

@ -1,4 +1,5 @@
const queryController = require("../../api/controllers/query") const queryController = require("../../api/controllers/query")
const { buildCtx } = require("./utils")
exports.definition = { exports.definition = {
name: "External Data Connector", name: "External Data Connector",
@ -54,25 +55,20 @@ exports.run = async function ({ inputs, appId, emitter }) {
const { queryId, ...rest } = inputs.query const { queryId, ...rest } = inputs.query
const ctx = { const ctx = buildCtx(appId, emitter, {
params: {
queryId,
},
request: {
body: { body: {
parameters: rest, parameters: rest,
}, },
params: {
queryId,
}, },
appId, })
eventEmitter: emitter,
}
await queryController.execute(ctx)
try { try {
await queryController.execute(ctx)
return { return {
response: ctx.body, response: ctx.body,
success: ctx.status === 200, success: true,
} }
} catch (err) { } catch (err) {
return { return {

View file

@ -1,4 +1,5 @@
const scriptController = require("../../api/controllers/script") const scriptController = require("../../api/controllers/script")
const { buildCtx } = require("./utils")
exports.definition = { exports.definition = {
name: "JS Scripting", name: "JS Scripting",
@ -23,8 +24,7 @@ exports.definition = {
properties: { properties: {
value: { value: {
type: "string", type: "string",
description: description: "The result of the return statement",
"The result of the last statement of the executed script.",
}, },
success: { success: {
type: "boolean", type: "boolean",
@ -46,21 +46,17 @@ exports.run = async function ({ inputs, appId, context, emitter }) {
} }
} }
const ctx = { const ctx = buildCtx(appId, emitter, {
request: {
body: { body: {
script: inputs.code, script: inputs.code,
context, context,
}, },
}, })
user: { appId },
eventEmitter: emitter,
}
try { try {
await scriptController.execute(ctx) await scriptController.execute(ctx)
return { return {
success: ctx.status === 200, success: true,
value: ctx.body, value: ctx.body,
} }
} catch (err) { } catch (err) {

View file

@ -16,3 +16,24 @@ exports.getFetchResponse = async fetched => {
} }
return { status, message } return { status, message }
} }
// need to make sure all ctx structures have the
// throw added to them, so that controllers don't
// throw a ctx.throw undefined when error occurs
exports.buildCtx = (appId, emitter, { body, params } = {}) => {
const ctx = {
appId,
user: { appId },
eventEmitter: emitter,
throw: (code, error) => {
throw error
},
}
if (body) {
ctx.request = { body }
}
if (params) {
ctx.params = params
}
return ctx
}