Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form data processor #26

Merged
merged 3 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 87 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions templates/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20-alpine
FROM node:16-alpine

RUN apk --no-cache add \
python3 \
Expand All @@ -19,4 +19,4 @@ RUN chown -R node:node .

USER node

ENTRYPOINT ["npm", "start"]
ENTRYPOINT ["npm", "start"]
11 changes: 7 additions & 4 deletions templates/lib/actions/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

const Swagger = require("swagger-client");
const spec = require("../spec.json");
const { mapFieldNames, getMetadata } = require("../utils/helpers");
const { mapFieldNames, getMetadata, mapFormDataBody } = require("../utils/helpers");
const componentJson = require("../../component.json");

function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenData) {
const isVerbose = process.env.debug || cfg.verbose;
async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenData) {

this.logger.info("Incoming message %j", msg);
this.logger.info("Config %j", cfg);
Expand All @@ -31,8 +30,12 @@ function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenData) {
const specPath = spec.paths[pathName];
const specPathParameters = specPath[method].parameters ? specPath[method].parameters.map(({ name }) => name) : [];

const body = msg.data;
let body = msg.data;
mapFieldNames(body);
if (requestContentType === "multipart/form-data") {
this.logger.info("requestContentType multipart/form-data is defined");
body = await mapFormDataBody.call(this, action, body);
}

let parameters = {};
for (let param of specPathParameters) {
Expand Down
54 changes: 54 additions & 0 deletions templates/lib/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
const dayjs = require("dayjs");
const lodashGet = require("lodash.get");
const fs = require("fs");
const FormDataNode = require("formdata-node");
const path = require("path");
const axios = require("axios");
const { File } = FormDataNode;

const getFileName = (fileUrl) => {
const parsedUrl = new URL(fileUrl);
const filePath = parsedUrl.pathname;
const title = path.basename(filePath);
if (!title){
throw new Error("Cannot find filename in provided url");
}
return title;
};

const downloadFileFromUrl = async (fileUrl) => {
const title = getFileName(fileUrl);
const response = await axios({
method: "GET",
url: fileUrl,
responseType: "arraybuffer"
});
return new File([response.data], title);
};

const getInputMetadataSchema = (metadataPath) => {
const inputMetadata = fs.readFileSync(metadataPath, "utf-8");
return JSON.parse(inputMetadata).properties.requestBody.properties;
};

const mapFormDataBody = async function(action, body) {
this.logger.info("Going to import Input Metadata Schema...");
let inputMetadataSchema = getInputMetadataSchema(action.metadata.in);
this.logger.info("Input Metadata Schema: %j", inputMetadataSchema);
for (const key of Object.keys(body)) {
this.logger.info("Body property '%s' has type: %s", key, inputMetadataSchema[key].type);
if (inputMetadataSchema[key].type === "string" && inputMetadataSchema[key].format && inputMetadataSchema[key].format === "binary") {
this.logger.info("For body property '%s' detected 'binary' format. Going to download binary data from provided URL: %s", key, body[key]);
let fileUrl;
try{
fileUrl = new URL(body[key]);
} catch (e) {
this.logger.error("Body property '%s' has binary format and require valid URL as value, but has %s", key, body[key]);
}
this.logger.info("Going to download File from provided URL...");
const fileContent = await downloadFileFromUrl(fileUrl);
this.logger.info("File was successfully downloaded");
body[key] = fileContent;
}
}
return body;
};

function compareDate(a, b) {
return dayjs(a).isAfter(b);
Expand Down Expand Up @@ -67,4 +120,5 @@ module.exports = {
getMetadata,
dataAndSnapshot,
getElementDataFromResponse,
mapFormDataBody
};
Loading