diff --git a/packages/server/src/api/controllers/query.js b/packages/server/src/api/controllers/query.js index d584b73e20..737c8c9b01 100644 --- a/packages/server/src/api/controllers/query.js +++ b/packages/server/src/api/controllers/query.js @@ -160,8 +160,6 @@ exports.execute = async function (ctx) { ) const integration = new Integration(datasource.config) - console.log(query) - // ctx.body = {} // call the relevant CRUD method on the integration class ctx.body = formatResponse(await integration[query.queryVerb](enrichedQuery)) // cleanup diff --git a/packages/server/src/automations/steps/executeQuery.js b/packages/server/src/automations/steps/executeQuery.js index f34d994183..1dcb75d0a3 100644 --- a/packages/server/src/automations/steps/executeQuery.js +++ b/packages/server/src/automations/steps/executeQuery.js @@ -1,4 +1,5 @@ const queryController = require("../../api/controllers/query") +const { buildCtx } = require("./utils") exports.definition = { name: "External Data Connector", @@ -54,25 +55,20 @@ exports.run = async function ({ inputs, appId, emitter }) { const { queryId, ...rest } = inputs.query - const ctx = { + const ctx = buildCtx(appId, emitter, { + body: { + parameters: rest, + }, params: { queryId, }, - request: { - body: { - parameters: rest, - }, - }, - appId, - eventEmitter: emitter, - } - - await queryController.execute(ctx) + }) try { + await queryController.execute(ctx) return { response: ctx.body, - success: ctx.status === 200, + success: true, } } catch (err) { return { diff --git a/packages/server/src/automations/steps/executeScript.js b/packages/server/src/automations/steps/executeScript.js index b320e38700..68a1071fd1 100644 --- a/packages/server/src/automations/steps/executeScript.js +++ b/packages/server/src/automations/steps/executeScript.js @@ -1,4 +1,5 @@ const scriptController = require("../../api/controllers/script") +const { buildCtx } = require("./utils") exports.definition = { name: "JS Scripting", @@ -23,8 +24,7 @@ exports.definition = { properties: { value: { type: "string", - description: - "The result of the last statement of the executed script.", + description: "The result of the return statement", }, success: { type: "boolean", @@ -46,21 +46,17 @@ exports.run = async function ({ inputs, appId, context, emitter }) { } } - const ctx = { - request: { - body: { - script: inputs.code, - context, - }, + const ctx = buildCtx(appId, emitter, { + body: { + script: inputs.code, + context, }, - user: { appId }, - eventEmitter: emitter, - } + }) try { await scriptController.execute(ctx) return { - success: ctx.status === 200, + success: true, value: ctx.body, } } catch (err) { diff --git a/packages/server/src/automations/steps/utils.js b/packages/server/src/automations/steps/utils.js index 0429c71ca7..83673d0878 100644 --- a/packages/server/src/automations/steps/utils.js +++ b/packages/server/src/automations/steps/utils.js @@ -16,3 +16,24 @@ exports.getFetchResponse = async fetched => { } 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 +}