63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { HTTP_METHODS } from "constants/ApiEditorConstants";
|
|
import { ApiAction } from "entities/Action";
|
|
import _ from "lodash";
|
|
|
|
export const transformRestAction = (data: ApiAction): ApiAction => {
|
|
let action = _.cloneDeep(data);
|
|
// // GET actions should not save body
|
|
// if (action.actionConfiguration.httpMethod === HTTP_METHODS[0]) {
|
|
// delete action.actionConfiguration.body;
|
|
// }
|
|
// Paths should not have query params
|
|
if (
|
|
action.actionConfiguration.queryParameters &&
|
|
action.actionConfiguration.queryParameters.length
|
|
) {
|
|
const path = action.actionConfiguration.path;
|
|
if (path && path.indexOf("?") > -1) {
|
|
action = {
|
|
...action,
|
|
actionConfiguration: {
|
|
...action.actionConfiguration,
|
|
path: path.substr(0, path.indexOf("?")),
|
|
},
|
|
};
|
|
}
|
|
}
|
|
// Body should send correct format depending on the content type
|
|
if (action.actionConfiguration.httpMethod !== HTTP_METHODS[0]) {
|
|
let body: any = "";
|
|
if (action.actionConfiguration.body) {
|
|
body = action.actionConfiguration.body || undefined;
|
|
}
|
|
|
|
if (!_.isString(body)) body = JSON.stringify(body);
|
|
action = {
|
|
...action,
|
|
actionConfiguration: {
|
|
...action.actionConfiguration,
|
|
body,
|
|
},
|
|
};
|
|
}
|
|
|
|
action.actionConfiguration.bodyFormData = removeEmptyPairs(
|
|
action.actionConfiguration.bodyFormData,
|
|
);
|
|
action.actionConfiguration.headers = removeEmptyPairs(
|
|
action.actionConfiguration.headers,
|
|
);
|
|
action.actionConfiguration.queryParameters = removeEmptyPairs(
|
|
action.actionConfiguration.queryParameters,
|
|
);
|
|
|
|
return action;
|
|
};
|
|
|
|
function removeEmptyPairs(keyValueArray: any) {
|
|
if (!keyValueArray || !keyValueArray.length) return keyValueArray;
|
|
return keyValueArray.filter(
|
|
(data: any) => !_.isEmpty(data.key) || !_.isEmpty(data.value),
|
|
);
|
|
}
|