Skip to content

Commit

Permalink
GET methods with path params are also triggers (#30)
Browse files Browse the repository at this point in the history
* methods with path params are also triggers

* add postfix to the trigger name

* add operationId to callParams

* use operationId in triggers and actions
  • Loading branch information
pnedelko authored Nov 15, 2023
1 parent 9e8b1fd commit ecd64dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
8 changes: 8 additions & 0 deletions lib/utils/schemaAndComponentJsonBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 8 additions & 3 deletions templates/lib/actions/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions templates/lib/lookups/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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");

Expand All @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions templates/lib/triggers/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down

0 comments on commit ecd64dd

Please sign in to comment.