From ecd64dd33d2b15efedd47e78538c53deccc2fed0 Mon Sep 17 00:00:00 2001 From: Pavel Nedelko Date: Wed, 15 Nov 2023 20:39:17 +0100 Subject: [PATCH] GET methods with path params are also triggers (#30) * methods with path params are also triggers * add postfix to the trigger name * add operationId to callParams * use operationId in triggers and actions --- lib/utils/schemaAndComponentJsonBuilder.js | 8 ++++++++ templates/lib/actions/action.js | 11 ++++++++--- templates/lib/lookups/lookup.js | 6 +++--- templates/lib/triggers/trigger.js | 17 +++++++++++------ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/utils/schemaAndComponentJsonBuilder.js b/lib/utils/schemaAndComponentJsonBuilder.js index a4f5ddd..d20a418 100644 --- a/lib/utils/schemaAndComponentJsonBuilder.js +++ b/lib/utils/schemaAndComponentJsonBuilder.js @@ -36,6 +36,7 @@ async function schemaBuilder(api, componentJson, existingNames, outputDir) { title: toText(operation.summary || operation.operationId || operation.description || name), description: toText(operation.description), callParams: { + operationId: operation.operationId, pathName: opPath, method: method, }, @@ -105,6 +106,13 @@ async function schemaBuilder(api, componentJson, existingNames, outputDir) { action.main = filename("lib/actions/action.js"); componentJson.actions[name] = action; } + + // get requests with path params are also triggers + // we need to add a postfix to the trigger name, so that we don't break old components + if (method === "get" && opPath.includes("{")) { + action.main = filename("lib/triggers/trigger.js"); + componentJson.triggers[`${name}_trigger`] = action; + } }); }); } catch (err) { diff --git a/templates/lib/actions/action.js b/templates/lib/actions/action.js index e16006f..fe06761 100644 --- a/templates/lib/actions/action.js +++ b/templates/lib/actions/action.js @@ -35,7 +35,7 @@ async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenDa logger.info("Starting to execute action '%s'", actionFunction); const action = componentJson.actions[actionFunction]; - const { pathName, method, requestContentType } = action.callParams; + const { operationId, pathName, method, requestContentType } = action.callParams; logger.info( "Found spec callParams: 'pathName': %s, 'method': %s, 'requestContentType': %s", pathName, @@ -71,7 +71,7 @@ async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenDa const callParams = { spec: spec, - operationId: actionFunction, + operationId: operationId, pathName: pathName, method: method, parameters: parameters, @@ -96,7 +96,12 @@ async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenDa } catch (e) { if (e instanceof Error && e.response) { const response = e.response; - this.logger.error("API error! Status: '%s', statusText: %s, errorBody: %j", response.status, response.statusText, response.body); + this.logger.error( + "API error! Status: '%s', statusText: %s, errorBody: %j", + response.status, + response.statusText, + response.body + ); } throw e; } diff --git a/templates/lib/lookups/lookup.js b/templates/lib/lookups/lookup.js index 9a81b24..3a3cab7 100644 --- a/templates/lib/lookups/lookup.js +++ b/templates/lib/lookups/lookup.js @@ -12,7 +12,7 @@ const { process: triggerProcess } = require("../triggers/trigger"); const logger = require("@openintegrationhub/ferryman/lib/logging"); /* -* data will have the following format: +* data will have the following format: { operationId => for example getCampaigns parameters => msg (usually empty object) @@ -22,7 +22,7 @@ const logger = require("@openintegrationhub/ferryman/lib/logging"); async function processAction(req, res, _, actionParams) { const { secretId, data } = actionParams; const { ferryman } = req; - const { operationId, parameters, cfg } = data; + const { operationId: functionName, parameters, cfg } = data; logger.info({ params: actionParams }, "Running lookup with params"); @@ -32,7 +32,7 @@ async function processAction(req, res, _, actionParams) { const snapshot = {}, incomingMessageHeaders = {}; - const tokenData = { function: operationId }; + const tokenData = { function: functionName }; // only when the secretId parameter is provided if (secretId) { diff --git a/templates/lib/triggers/trigger.js b/templates/lib/triggers/trigger.js index 7268389..12df237 100644 --- a/templates/lib/triggers/trigger.js +++ b/templates/lib/triggers/trigger.js @@ -45,7 +45,7 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD ); const trigger = componentJson.triggers[triggerFunction]; - const { pathName, method, requestContentType } = trigger.callParams; + const { operationId, pathName, method, requestContentType } = trigger.callParams; logger.info( 'Found spec callParams: "pathName": %s, "method": %s, "requestContentType": %s', pathName, @@ -70,9 +70,9 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD } if (syncParam && snapshot.lastUpdated) { - if (syncParam === '$FILTER'){ - if (!snapshotKey){ - throw new Error('snapshotKey params should be specified!') + if (syncParam === "$FILTER") { + if (!snapshotKey) { + throw new Error("snapshotKey params should be specified!"); } parameters[syncParam] = `${snapshotKey} gt datetime'${snapshot.lastUpdated}'`; } else { @@ -109,7 +109,7 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD let callParams = { spec: spec, - operationId: triggerFunction, + operationId: operationId, pathName: pathName, method: method, parameters: parameters, @@ -134,7 +134,12 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD } catch (e) { if (e instanceof Error && e.response) { const response = e.response; - this.logger.error("API error! Status: '%s', statusText: %s, errorBody: %j", response.status, response.statusText, response.body); + this.logger.error( + "API error! Status: '%s', statusText: %s, errorBody: %j", + response.status, + response.statusText, + response.body + ); } throw e; }