fix: updated default content-type and default body format type (#11878)

* fix: updated default content-type and default body format type

* fix: updated the cypress test because the default content-type is changed

* fix: update content type on change of http method and update body format type too

* fix: changed cypress test back to previous
This commit is contained in:
Aman Agarwal 2022-04-12 16:43:11 +05:30 committed by GitHub
parent b2d356bcce
commit 21eb88f847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 17 deletions

View File

@ -61,15 +61,26 @@ export const POST_BODY_FORMAT_OPTIONS: Record<
RAW: "text/plain", RAW: "text/plain",
}; };
export const HTTP_METHODS_DEFAULT_FORMAT_TYPES: Record<HTTP_METHOD, string> = {
GET: POST_BODY_FORMAT_OPTIONS.NONE,
POST: POST_BODY_FORMAT_OPTIONS.JSON,
PUT: POST_BODY_FORMAT_OPTIONS.JSON,
DELETE: POST_BODY_FORMAT_OPTIONS.RAW,
PATCH: POST_BODY_FORMAT_OPTIONS.JSON,
};
const DEFAULT_METHOD_TYPE = HTTP_METHOD.GET;
export const DEFAULT_API_ACTION_CONFIG: ApiActionConfig = { export const DEFAULT_API_ACTION_CONFIG: ApiActionConfig = {
timeoutInMillisecond: DEFAULT_ACTION_TIMEOUT, timeoutInMillisecond: DEFAULT_ACTION_TIMEOUT,
encodeParamsToggle: true, encodeParamsToggle: true,
httpMethod: HTTP_METHOD.GET, httpMethod: DEFAULT_METHOD_TYPE,
headers: EMPTY_KEY_VALUE_PAIRS.slice(), headers: EMPTY_KEY_VALUE_PAIRS.slice(),
queryParameters: EMPTY_KEY_VALUE_PAIRS.slice(), queryParameters: EMPTY_KEY_VALUE_PAIRS.slice(),
body: "", body: "",
bodyFormData: [],
formData: { formData: {
apiContentType: POST_BODY_FORMAT_OPTIONS.NONE, apiContentType: HTTP_METHODS_DEFAULT_FORMAT_TYPES[DEFAULT_METHOD_TYPE],
}, },
pluginSpecifiedTemplates: [ pluginSpecifiedTemplates: [
{ {

View File

@ -23,6 +23,7 @@ import {
CONTENT_TYPE_HEADER_KEY, CONTENT_TYPE_HEADER_KEY,
EMPTY_KEY_VALUE_PAIRS, EMPTY_KEY_VALUE_PAIRS,
HTTP_METHOD, HTTP_METHOD,
HTTP_METHODS_DEFAULT_FORMAT_TYPES,
} from "constants/ApiEditorConstants"; } from "constants/ApiEditorConstants";
import history from "utils/history"; import history from "utils/history";
import { INTEGRATION_EDITOR_MODES, INTEGRATION_TABS } from "constants/routes"; import { INTEGRATION_EDITOR_MODES, INTEGRATION_TABS } from "constants/routes";
@ -154,10 +155,7 @@ function* handleUpdateBodyContentType(
let formData = { ...values.actionConfiguration.formData }; let formData = { ...values.actionConfiguration.formData };
if (formData === undefined) formData = {}; if (formData === undefined) formData = {};
formData["apiContentType"] = formData["apiContentType"] =
title === POST_BODY_FORMAT_OPTIONS.RAW || title === POST_BODY_FORMAT_OPTIONS.NONE ? previousContentType : title;
title === POST_BODY_FORMAT_OPTIONS.NONE
? previousContentType
: title;
yield put( yield put(
change(API_EDITOR_FORM_NAME, "actionConfiguration.formData", formData), change(API_EDITOR_FORM_NAME, "actionConfiguration.formData", formData),
@ -195,8 +193,7 @@ function* handleUpdateBodyContentType(
// this is done to ensure user input isn't cleared off if they switch to raw or none mode. // this is done to ensure user input isn't cleared off if they switch to raw or none mode.
// however if the user types in a new value, we use the updated value (formValueChangeSaga - line 426). // however if the user types in a new value, we use the updated value (formValueChangeSaga - line 426).
if ( if (
(displayFormatValue === POST_BODY_FORMAT_OPTIONS.NONE || displayFormatValue === POST_BODY_FORMAT_OPTIONS.NONE &&
displayFormatValue === POST_BODY_FORMAT_OPTIONS.RAW) &&
indexToUpdate !== -1 indexToUpdate !== -1
) { ) {
headers[indexToUpdate] = { headers[indexToUpdate] = {
@ -233,21 +230,52 @@ function* handleUpdateBodyContentType(
} }
} }
function* initializeExtraFormDataSaga() { function* updateExtraFormDataSaga() {
const formData = yield select(getFormData, API_EDITOR_FORM_NAME); const formData = yield select(getFormData, API_EDITOR_FORM_NAME);
const { values } = formData; const { values } = formData;
// when initializing, check if theres a display format present, if not use Json display format as default. // when initializing, check if theres a display format present, if not use Json display format as default.
const extraFormData = yield select(getDisplayFormat, values.id); const extraFormData = yield select(getDisplayFormat, values.id);
// as a fail safe, if no display format is present, use Raw mode const headers: Array<{ key: string; value: string }> =
const rawApiContentType = extraFormData?.displayFormat?.value get(values, "actionConfiguration.headers") || [];
? extraFormData?.displayFormat?.value const contentTypeValue: string =
: POST_BODY_FORMAT_OPTIONS.RAW; headers.find(
(h: { key: string; value: string }) => h.key === CONTENT_TYPE_HEADER_KEY,
)?.value || "";
let rawApiContentType;
if (!extraFormData) { if (!extraFormData) {
yield call(setHeaderFormat, values.id, rawApiContentType); /*
* Checking if the content-type header exists, if yes then set the body format type one of the three json, multipart or url encoded whichever matches else set raw as default
*/
if (
[
POST_BODY_FORMAT_OPTIONS.JSON,
POST_BODY_FORMAT_OPTIONS.FORM_URLENCODED,
POST_BODY_FORMAT_OPTIONS.MULTIPART_FORM_DATA,
].includes(contentTypeValue)
) {
rawApiContentType = contentTypeValue;
} else {
rawApiContentType = POST_BODY_FORMAT_OPTIONS.RAW;
}
} else {
if (
[
POST_BODY_FORMAT_OPTIONS.JSON,
POST_BODY_FORMAT_OPTIONS.FORM_URLENCODED,
POST_BODY_FORMAT_OPTIONS.MULTIPART_FORM_DATA,
].includes(contentTypeValue)
) {
rawApiContentType = contentTypeValue;
} else {
rawApiContentType = POST_BODY_FORMAT_OPTIONS.RAW;
}
} }
yield call(setHeaderFormat, values.id, rawApiContentType);
} }
function* changeApiSaga( function* changeApiSaga(
@ -263,7 +291,7 @@ function* changeApiSaga(
} else { } else {
yield put(initialize(API_EDITOR_FORM_NAME, action)); yield put(initialize(API_EDITOR_FORM_NAME, action));
yield call(initializeExtraFormDataSaga); yield call(updateExtraFormDataSaga);
if ( if (
action.actionConfiguration && action.actionConfiguration &&
@ -343,6 +371,8 @@ export function* updateFormFields(
values?.actionConfiguration?.formData?.apiContentType || values?.actionConfiguration?.formData?.apiContentType ||
POST_BODY_FORMAT_OPTIONS.JSON; POST_BODY_FORMAT_OPTIONS.JSON;
let extraFormDataToBeChanged = false;
if (field === "actionConfiguration.httpMethod") { if (field === "actionConfiguration.httpMethod") {
const { actionConfiguration } = values; const { actionConfiguration } = values;
if (!actionConfiguration.headers) return; if (!actionConfiguration.headers) return;
@ -355,8 +385,11 @@ export function* updateFormFields(
if (value !== HTTP_METHOD.GET) { if (value !== HTTP_METHOD.GET) {
// if user switches to other methods that is not GET and apiContentType is undefined set default apiContentType to JSON. // if user switches to other methods that is not GET and apiContentType is undefined set default apiContentType to JSON.
if (apiContentType === POST_BODY_FORMAT_OPTIONS.NONE) if (apiContentType === POST_BODY_FORMAT_OPTIONS.NONE) {
apiContentType = POST_BODY_FORMAT_OPTIONS.JSON; apiContentType =
HTTP_METHODS_DEFAULT_FORMAT_TYPES[value as HTTP_METHOD];
extraFormDataToBeChanged = true;
}
const indexToUpdate = getIndextoUpdate( const indexToUpdate = getIndextoUpdate(
actionConfigurationHeaders, actionConfigurationHeaders,
@ -383,6 +416,8 @@ export function* updateFormFields(
actionConfigurationHeaders, actionConfigurationHeaders,
), ),
); );
if (extraFormDataToBeChanged) yield call(updateExtraFormDataSaga);
} }
} }