PromucFlow_constructor/app/client/src/transformers/RestActionTransformer.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-05-23 05:42:23 +00:00
import {
CONTENT_TYPE,
HTTP_METHODS,
POST_BODY_FORMAT_OPTIONS,
2020-05-23 05:42:23 +00:00
} from "constants/ApiEditorConstants";
import _ from "lodash";
2019-12-23 12:12:58 +00:00
export const transformRestAction = (data: any): any => {
2019-12-23 12:12:58 +00:00
let action = { ...data };
// GET actions should not save body
if (action.actionConfiguration.httpMethod === HTTP_METHODS[0]) {
delete action.actionConfiguration.body;
}
// Paths should not have query params
2020-01-08 09:19:00 +00:00
if (
action.actionConfiguration.queryParameters &&
action.actionConfiguration.queryParameters.length
2020-01-08 09:19:00 +00:00
) {
const path = action.actionConfiguration.path;
2019-12-23 12:12:58 +00:00
if (path && path.indexOf("?") > -1) {
action = {
...action,
2019-12-23 12:12:58 +00:00
actionConfiguration: {
...action.actionConfiguration,
2019-12-23 12:12:58 +00:00
path: path.substr(0, path.indexOf("?")),
},
};
}
}
// Body should send correct format depending on the content type
if (action.actionConfiguration.httpMethod !== HTTP_METHODS[0]) {
let contentType = "raw";
if (
action.actionConfiguration.headers &&
action.actionConfiguration.headers.length
) {
2020-05-23 05:42:23 +00:00
const contentTypeHeader = _.find(
action.actionConfiguration.headers,
header => {
return header.key.toLowerCase() === CONTENT_TYPE;
},
);
if (contentTypeHeader) {
contentType = contentTypeHeader.value;
}
}
let body: any = "";
if (
2020-06-09 06:25:11 +00:00
contentType !== POST_BODY_FORMAT_OPTIONS[1].value &&
contentType !== POST_BODY_FORMAT_OPTIONS[2].value
) {
action.actionConfiguration.bodyFormData = undefined;
if (action.actionConfiguration.body)
body = action.actionConfiguration.body || undefined;
}
if (!_.isString(body)) body = JSON.stringify(body);
action = {
...action,
actionConfiguration: {
...action.actionConfiguration,
body,
},
};
}
2019-12-23 12:12:58 +00:00
return action;
};