Skip to content

Handle GraphQL mimetype in request postBody #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/HARToPostmanCollectionBodyMapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const JSON_MODE = 'raw',
JSON_LANGUAGE_ID = 'json',
HTML_LANGUAGE_ID = 'html',
GRAPHQL_LANGUAGE_ID = 'graphql',
XML_LANGUAGE_ID = 'xml',
PLAIN_TEXT_LANGUAGE_ID = 'text',
SUPPORTED_BODY_TYPES = {
Expand All @@ -11,6 +12,7 @@ const JSON_MODE = 'raw',
textJavascript: 'text/javascript',
applicationJavascript: 'application/javascript',
applicationXml: 'application/xml',
applicationGraphQl: 'application/graphql',
formData: 'multipart/form-data',
urlEncoded: 'application/x-www-form-urlencoded'
},
Expand Down Expand Up @@ -76,6 +78,25 @@ function mapBodyFromJson(harRequest, options) {
};
}

/**
* Maps a har request post data into a postman body when is graphql
* @param {object} harRequest parsed HAR content
* @param {object} options process options
* @returns {object} Postman's body structure
*/
function mapBodyFromGraphQl(harRequest, options) {
let textToReturn = '';
if (harRequest && harRequest.postData) {
textToReturn = parseAndFormat(harRequest.postData.text, options);
}
return {
mode: GRAPHQL_LANGUAGE_ID,
graphql: JSON.parse(harRequest.postData.text),
raw: textToReturn,
options: { raw: { language: PLAIN_TEXT_LANGUAGE_ID } }
};
}

/**
* Maps a har response post data into a postman body when is json
* @param {object} harResponse parsed HAR response content
Expand Down Expand Up @@ -214,7 +235,7 @@ function isSafariUrlEncoded(harRequest) {
*/
function mapBody(harRequest, options) {
let handledBody;
if (harRequest.bodySize > 0) {
if (harRequest.bodySize > 0 || harRequest?.postData?.text) {
const mimeType = harRequest?.postData?.mimeType;
if (isMimeType(mimeType, SUPPORTED_BODY_TYPES.applicationJson)) {
handledBody = mapBodyFromJson(harRequest, options);
Expand All @@ -228,6 +249,9 @@ function mapBody(harRequest, options) {
else if (isMimeType(mimeType, SUPPORTED_BODY_TYPES.textJavascript)) {
handledBody = mapBodyFromUnformattedTypeRequest(harRequest, PLAIN_TEXT_LANGUAGE_ID);
}
else if (isMimeType(mimeType, SUPPORTED_BODY_TYPES.applicationGraphQl)) {
handledBody = mapBodyFromGraphQl(harRequest, PLAIN_TEXT_LANGUAGE_ID);
}
else if (isMimeType(mimeType, SUPPORTED_BODY_TYPES.textPlain)) {
handledBody = mapBodyFromUnformattedTypeRequest(harRequest, PLAIN_TEXT_LANGUAGE_ID);
}
Expand Down
Loading