Skip to content
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

[IMPORT-774] Graphql data mode from curl import #67

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a9a5361
IMPORT-774 Checking if the raw data is graphql query
aman-v-singh Jun 20, 2023
dab1d90
Update
aman-v-singh Jun 21, 2023
5e3168a
Update
aman-v-singh Jun 21, 2023
f252247
Making a method to check qraphql query and return graphql Object
aman-v-singh Jun 22, 2023
6daab13
Merge branch 'develop' of github.com:postmanlabs/curl-to-postman into…
aman-v-singh Jun 22, 2023
e85fa49
Return graphql body parameters in correct format
aman-v-singh Jun 23, 2023
becf890
Empty variable object returned as empty string
aman-v-singh Jun 23, 2023
7b1a7f7
fix max-len
aman-v-singh Jun 23, 2023
7012579
Adding unit tests, description and changing variable,function names
aman-v-singh Jun 23, 2023
11b71b0
Revert "Adding unit tests, description and changing variable,function…
aman-v-singh Jun 23, 2023
1ca1072
Adding unit tests, description and changing variable,function names
aman-v-singh Jun 23, 2023
5b4052c
Adding Description of identifyGraphqlRequest function
aman-v-singh Jun 23, 2023
fd33602
[IMPORT-774] Adding 'operationName' check in graphql body
aman-v-singh Jun 23, 2023
fda1d87
[IMPORT-774] checking content type and using regexp matching to ident…
aman-v-singh Jun 23, 2023
825d0ee
[IMPORT-774] application/graphql should be displayed in raw tab as text
aman-v-singh Jun 23, 2023
d6e7601
[IMPORT-774] Improving the regex, and adding condition to check if qu…
aman-v-singh Jun 26, 2023
751f988
escape characters before JSON parse
aman-v-singh Jun 26, 2023
33fd277
remove console.log
aman-v-singh Jun 26, 2023
044ba8a
Update conversion tests
aman-v-singh Jun 26, 2023
d86b40b
Removing regex and object tests
aman-v-singh Jun 26, 2023
7afed8b
Removing condtion
aman-v-singh Jun 27, 2023
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
89 changes: 84 additions & 5 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var program,

curlConverter = {
requestUrl: '',

initialize: function() {
/**
* Collects values from the command line arguments and adds them to the memo array.
Expand Down Expand Up @@ -677,6 +676,77 @@ var program,
}
},

/**
* Escape JSON strings before JSON.parse
*
* @param {string} jsonString - Input JSON string
* @returns {string} - JSON string with escaped characters
*/
escapeJson: function (jsonString) {
// eslint-disable-next-line no-implicit-globals
meta = { // table of character substitutions
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r'
};
return jsonString.replace(/[\t\n\f\r]/g, (char) => {
return meta[char];
});
},

/**
* Identifies whether the input data string is a graphql query or not
*
aman-v-singh marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} dataString - Input data string to check if it is a graphql query
* @param {string} contentType - Content type header value
* @returns {Object} - { result: true, graphql: {Object} } if dataString is a graphql query else { result: false }
*/
identifyGraphqlRequest: function (dataString, contentType) {
try {
const rawDataObj = _.attempt(JSON.parse, this.escapeJson(dataString));
if (contentType === 'application/json' && rawDataObj && !_.isError(rawDataObj)) {
if (!_.has(rawDataObj, 'query') || !_.isString(rawDataObj.query)) {
return { result: false };
}
if (_.has(rawDataObj, 'variables')) {
if (!_.isObject(rawDataObj.variables)) {
return { result: false };
}
}
else {
rawDataObj.variables = {};
}
if (_.has(rawDataObj, 'operationName')) {
if (!_.isString(rawDataObj.operationName)) {
return { result: false };
}
}
else {
rawDataObj.operationName = '';
}
if (_.keys(rawDataObj).length === 3) {
if (_.intersection(_.keys(rawDataObj), ['query', 'variables', 'operationName']).length === 3) {
aman-v-singh marked this conversation as resolved.
Show resolved Hide resolved
const graphqlVariables = JSON.stringify(rawDataObj.variables, null, 2);
return {
result: true,
graphql: {
query: rawDataObj.query,
operationName: rawDataObj.operationName,
VShingala marked this conversation as resolved.
Show resolved Hide resolved
variables: graphqlVariables === '{}' ? '' : graphqlVariables
}
};
}
}
}
return { result: false };
}
catch (e) {
return { result: false };
}
},


convertCurlToRequest: function(curlString, shouldRetry = true) {
try {
this.initialize();
Expand Down Expand Up @@ -748,10 +818,19 @@ var program,
bodyArr.push(this.trimQuotesFromString(dataUrlencode));
bodyArr.push(this.trimQuotesFromString(dataAsciiString));

request.body.mode = 'raw';
request.body.raw = _.join(_.reject(bodyArr, (ele) => {
return !ele;
}), '&');
const rawDataString = _.join(_.reject(bodyArr, (ele) => {
return !ele;
}), '&'),
graphqlRequestData = this.identifyGraphqlRequest(rawDataString, content_type);

if (graphqlRequestData.result) {
request.body.mode = 'graphql';
request.body.graphql = graphqlRequestData.graphql;
}
else {
request.body.mode = 'raw';
request.body.raw = rawDataString;
}

urlData = request.data;
}
Expand Down
11 changes: 11 additions & 0 deletions test/conversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@ describe('Curl converter should', function() {
done();
});

it('[Github #9941]: should correctly identify graphql queries', function(done) {
var result = Converter.convertCurlToRequest(`curl -L 'https://countries.trevorblades.com' \\
-H 'Content-Type: application/json' \\
-d '{"query":"{\r\n countries {\r\n code\r\n name\r\n emoji\r\n }\r\n}","variables":{}}'`);

expect(result.body).to.have.property('mode', 'graphql');
expect(result.body.graphql.query).to.eql('{\r\n countries {\r\n code\r\n name\r\n emoji\r\n }\r\n}');
expect(result.body.graphql.variables).to.eql('');
done();
});

describe('[Github #8843]: It should recognize non-apostrophed ("...") url with multi-param', function() {
it('in case where there is multiple params with & in between in the url (https)', function(done) {
convert({
Expand Down