2020-05-08 12:54:27 +00:00
|
|
|
import { HTTP_METHODS, POST_BODY_FORMATS } from "constants/ApiEditorConstants";
|
|
|
|
|
import _ from "lodash";
|
2019-12-23 12:12:58 +00:00
|
|
|
|
2020-05-08 12:54:27 +00:00
|
|
|
export const transformRestAction = (data: any): any => {
|
2019-12-23 12:12:58 +00:00
|
|
|
let action = { ...data };
|
2020-05-08 12:54:27 +00:00
|
|
|
// GET actions should not save body
|
|
|
|
|
if (action.actionConfiguration.httpMethod === HTTP_METHODS[0]) {
|
2020-05-07 07:56:37 +00:00
|
|
|
delete action.actionConfiguration.body;
|
|
|
|
|
}
|
2020-05-08 12:54:27 +00:00
|
|
|
// Paths should not have query params
|
2020-01-08 09:19:00 +00:00
|
|
|
if (
|
2020-05-08 12:54:27 +00:00
|
|
|
action.actionConfiguration.queryParameters &&
|
|
|
|
|
action.actionConfiguration.queryParameters.length
|
2020-01-08 09:19:00 +00:00
|
|
|
) {
|
2020-05-08 12:54:27 +00:00
|
|
|
const path = action.actionConfiguration.path;
|
2019-12-23 12:12:58 +00:00
|
|
|
if (path && path.indexOf("?") > -1) {
|
|
|
|
|
action = {
|
2020-05-08 12:54:27 +00:00
|
|
|
...action,
|
2019-12-23 12:12:58 +00:00
|
|
|
actionConfiguration: {
|
2020-05-08 12:54:27 +00:00
|
|
|
...action.actionConfiguration,
|
2019-12-23 12:12:58 +00:00
|
|
|
path: path.substr(0, path.indexOf("?")),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-08 12:54:27 +00:00
|
|
|
// 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
|
|
|
|
|
) {
|
|
|
|
|
const contentTypeHeader = _.find(action.actionConfiguration.headers, {
|
|
|
|
|
key: "content-type",
|
|
|
|
|
});
|
|
|
|
|
if (contentTypeHeader) {
|
|
|
|
|
contentType = contentTypeHeader.value;
|
2020-05-07 10:42:17 +00:00
|
|
|
}
|
2020-05-07 07:56:37 +00:00
|
|
|
}
|
2020-05-08 12:54:27 +00:00
|
|
|
let formatIndex = 2;
|
|
|
|
|
if (POST_BODY_FORMATS.includes(contentType)) {
|
|
|
|
|
formatIndex = POST_BODY_FORMATS.indexOf(contentType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let body = action.actionConfiguration.body[formatIndex] || undefined;
|
|
|
|
|
if (!_.isString(body)) body = JSON.stringify(body);
|
|
|
|
|
action = {
|
|
|
|
|
...action,
|
|
|
|
|
actionConfiguration: {
|
|
|
|
|
...action.actionConfiguration,
|
|
|
|
|
body,
|
|
|
|
|
},
|
|
|
|
|
};
|
2020-04-14 12:34:14 +00:00
|
|
|
}
|
2020-05-08 12:54:27 +00:00
|
|
|
|
2019-12-23 12:12:58 +00:00
|
|
|
return action;
|
|
|
|
|
};
|