PromucFlow_constructor/app/client/src/utils/ApiPaneUtils.tsx
Ayangade Adeoluwa 0d33ef1bc9
fix: Return undefined for nonexistent action headers (#18724)
This issue occurs when users switch the datasource of an API action, and
the actionConfiguration headers is empty. This PR adds conditional
checks to prevent an error from happening.

Fixes #18682

Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video

- Bug fix (non-breaking change which fixes an issue)

### Test Plan
> Add Testsmith test cases links that relate to this PR

### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)


## Checklist:
### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
2023-01-18 15:31:08 +05:30

95 lines
2.9 KiB
TypeScript

import { CONTENT_TYPE_HEADER_KEY } from "constants/ApiEditorConstants/CommonApiConstants";
import {
getDynamicStringSegments,
isDynamicValue,
} from "./DynamicBindingUtils";
/**
* This function updates the header at a given index.
* If headerIndexToUpdate is -1, i.e the header we are looking for is not present, then
* update the header key-value pair in the first non-empty row
* @param headers
* @param headerIndexToUpdate
* @returns
*/
export const getIndextoUpdate = (headers: any, headerIndexToUpdate: number) => {
const firstEmptyHeaderRowIndex: number = headers?.findIndex(
(element: { key: string; value: string }) =>
element && element.key === "" && element.value === "",
);
const newHeaderIndex =
firstEmptyHeaderRowIndex > -1 ? firstEmptyHeaderRowIndex : headers.length;
const indexToUpdate =
headerIndexToUpdate > -1 ? headerIndexToUpdate : newHeaderIndex;
return indexToUpdate;
};
export const queryParamsRegEx = /([\s\S]*?)(\?(?![^{]*})[\s\S]*)?$/;
export function parseUrlForQueryParams(url: string) {
const padQueryParams = { key: "", value: "" };
let params = Array(2).fill(padQueryParams);
const dynamicValuesDetected: string[] = [];
const matchGroup = url.match(queryParamsRegEx) || [];
const parsedUrlWithQueryParams = matchGroup[2] || "";
const dynamicStringSegments = getDynamicStringSegments(
parsedUrlWithQueryParams,
);
const templateStringSegments = dynamicStringSegments.map((segment) => {
if (isDynamicValue(segment)) {
dynamicValuesDetected.push(segment);
return "~";
}
return segment;
});
if (parsedUrlWithQueryParams.indexOf("?") > -1) {
const paramsString = templateStringSegments
.join("")
.slice(parsedUrlWithQueryParams.indexOf("?") + 1);
const paramsWithDynamicValues = paramsString.split("&").map((p) => {
const firstEqualPos = p.indexOf("=");
const keyValue =
firstEqualPos > -1
? [p.substring(0, firstEqualPos), p.substring(firstEqualPos + 1)]
: [];
return { key: keyValue[0] || "", value: keyValue[1] || "" };
});
params = paramsWithDynamicValues.map((queryParam) => {
if (queryParam.value.includes("~")) {
const newVal = queryParam?.value?.replace(
/~/,
dynamicValuesDetected[0],
);
// remove the first index from detected dynamic values.
dynamicValuesDetected.shift();
return { key: queryParam.key, value: newVal };
}
return queryParam;
});
}
return params;
}
/**
*
* @param headers Array of key value pairs
* @returns string body type of API request
*/
export function getContentTypeHeaderValue(
headers: Array<{ key: string; value: string }>,
): string {
return (
headers.find(
(h: { key: string; value: string }) =>
h.key?.trim().toLowerCase() === CONTENT_TYPE_HEADER_KEY,
)?.value || ""
);
}