From fe4695036a45ce65c05315f6f766d1fdb47e740b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20H=C3=B6ffler?= Date: Thu, 12 Dec 2024 16:21:24 +0100 Subject: [PATCH] Added a function to put additional parameters into body if schema contains them --- templates/lib/actions/action.js | 7 ++++++- templates/lib/utils/helpers.js | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/templates/lib/actions/action.js b/templates/lib/actions/action.js index afc6f96..a237c7d 100644 --- a/templates/lib/actions/action.js +++ b/templates/lib/actions/action.js @@ -12,7 +12,7 @@ */ const spec = require("../spec.json"); -const { mapFieldNames, getMetadata, mapFormDataBody, executeCall } = require("../utils/helpers"); +const { mapFieldNames, getMetadata, mapFormDataBody, putAdditionalParamsInBody, executeCall } = require("../utils/helpers"); const componentJson = require("../../component.json"); async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenData) { @@ -53,6 +53,11 @@ async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenDa specPathParameters.push(...specPathGeneralParams); let body = msg.data; + + if (cfg && cfg.additionalParameters) { + body = putAdditionalParamsInBody(this, action, body, cfg.additionalParameters) + } + mapFieldNames(body); if (requestContentType === "multipart/form-data") { logger.info("requestContentType multipart/form-data is defined"); diff --git a/templates/lib/utils/helpers.js b/templates/lib/utils/helpers.js index 1695f02..975ee49 100644 --- a/templates/lib/utils/helpers.js +++ b/templates/lib/utils/helpers.js @@ -142,6 +142,19 @@ const mapFormDataBody = async function (action, body) { return body; }; +const putAdditionalParamsInBody = async function (action, body, additionalParameters) { + const newBody = body; + const inputMetadataSchema = getInputMetadataSchema(action.metadata.in); + + for (let property in additionalParameters) { + if (property in inputMetadataSchema && !(property in body)) { + body[property] = additionalParameters[property]; + } + } + + return newBody; +}; + function compareDate(a, b) { return dayjs(a).isAfter(b); } @@ -289,5 +302,7 @@ module.exports = { mapFormDataBody, isMicrosoftJsonDate, executeCall, - getInitialSnapshotValue + getInitialSnapshotValue, + getInputMetadataSchema, + putAdditionalParamsInBody };