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

84 lines
2.5 KiB
TypeScript
Raw Normal View History

feat: Support body in GET API requests (#7127) * 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>
2022-02-15 11:13:48 +00:00
import {
HTTP_METHOD,
CONTENT_TYPE_HEADER_KEY,
} from "constants/ApiEditorConstants";
import { ApiAction } from "entities/Action";
2021-09-22 16:59:47 +00:00
import isEmpty from "lodash/isEmpty";
import isString from "lodash/isString";
feat: Support body in GET API requests (#7127) * 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>
2022-02-15 11:13:48 +00:00
import cloneDeep from "lodash/cloneDeep";
2019-12-23 12:12:58 +00:00
export const transformRestAction = (data: ApiAction): ApiAction => {
2021-09-22 16:59:47 +00:00
let action = cloneDeep(data);
feat: Support body in GET API requests (#7127) * 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>
2022-02-15 11:13:48 +00:00
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
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,
path: path.slice(0, path.indexOf("?")),
2019-12-23 12:12:58 +00:00
},
};
}
}
// Body should send correct format depending on the content type
feat: Support body in GET API requests (#7127) * 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>
2022-02-15 11:13:48 +00:00
if (action.actionConfiguration.httpMethod !== HTTP_METHOD.GET) {
let body: any = "";
2021-01-12 01:22:31 +00:00
if (action.actionConfiguration.body) {
body = action.actionConfiguration.body || undefined;
2021-01-12 01:22:31 +00:00
}
2021-09-22 16:59:47 +00:00
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,
);
2019-12-23 12:12:58 +00:00
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)),
);
}