* WIP * Refactoring HTTP Method & Content Type to be objects instead of arrays TODO: 1. Set the default content-type for Get request to "None". Currently, it's raw 2. For None content-type, don't send the body field in the API request * Almost working implementation for the None type Currently, the body still gets sent in non-GET requests even if the None tab is selected. * Adding object.freeze to prevent any modifications to HTTP_METHOD_ENUM * WIP: Using enum & const for ts autocomplete * working implementation for NONE type, apiContentType prop added to API actions * WIP * Refactoring HTTP Method & Content Type to be objects instead of arrays TODO: 1. Set the default content-type for Get request to "None". Currently, it's raw 2. For None content-type, don't send the body field in the API request * Almost working implementation for the None type Currently, the body still gets sent in non-GET requests even if the None tab is selected. * Adding object.freeze to prevent any modifications to HTTP_METHOD_ENUM * WIP: Using enum & const for ts autocomplete * working implementation for NONE type, apiContentType prop added to API actions * adds apiContentType to actionConfiguration.formData object * Handling apiContentType property in Rest API formData * change apiContentType when user types content-type value and switches http method * makes api editor as similar as possible to postman, project postman. * Correcting the import in ApiEditorConstants * Resolved all merge conflicts * replay DSL functtionality * removes unneccessary files from worker * Fixes type declarations, naming e.t.c. * fix server side merge conflicts * fix client side merge conflicts * fix failing cypress tests Co-authored-by: Irongade <adeoluayangade@yahoo.com> Co-authored-by: Ayangade Adeoluwa <37867493+Irongade@users.noreply.github.com>
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
import {
|
|
HTTP_METHOD,
|
|
CONTENT_TYPE_HEADER_KEY,
|
|
} from "constants/ApiEditorConstants";
|
|
import { ApiAction } from "entities/Action";
|
|
import isEmpty from "lodash/isEmpty";
|
|
import isString from "lodash/isString";
|
|
import cloneDeep from "lodash/cloneDeep";
|
|
|
|
export const transformRestAction = (data: ApiAction): ApiAction => {
|
|
let action = cloneDeep(data);
|
|
const actionConfigurationHeaders = action.actionConfiguration.headers;
|
|
|
|
const contentTypeHeaderIndex = actionConfigurationHeaders.findIndex(
|
|
(header: { key: string; value: string }) =>
|
|
header?.key?.trim().toLowerCase() === CONTENT_TYPE_HEADER_KEY,
|
|
);
|
|
|
|
// GET actions should not save body if the content-type is set to empty
|
|
// In all other scenarios, GET requests will save & execute the action with
|
|
// the request body
|
|
if (
|
|
action.actionConfiguration.httpMethod === HTTP_METHOD.GET &&
|
|
contentTypeHeaderIndex == -1
|
|
) {
|
|
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.slice(0, path.indexOf("?")),
|
|
},
|
|
};
|
|
}
|
|
}
|
|
// Body should send correct format depending on the content type
|
|
if (action.actionConfiguration.httpMethod !== HTTP_METHOD.GET) {
|
|
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;
|
|
};
|
|
|
|
// Filters empty key-value pairs or key-value-type(Multipart) from form data, headers and query params
|
|
function removeEmptyPairs(keyValueArray: any) {
|
|
if (!keyValueArray || !keyValueArray.length) return keyValueArray;
|
|
return keyValueArray.filter(
|
|
(data: any) =>
|
|
data &&
|
|
(!isEmpty(data.key) || !isEmpty(data.value) || !isEmpty(data.type)),
|
|
);
|
|
}
|