Skip to content

Commit 775430d

Browse files
authored
Merge pull request #77 from postmanlabs/release/v1.8.0
Release version v1.8.0
2 parents ed495e2 + b319e18 commit 775430d

File tree

5 files changed

+58
-98
lines changed

5 files changed

+58
-98
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## [Unreleased]
44

5+
## [v1.8.0] - 2024-01-17
6+
7+
### Changed
8+
9+
- Fix for - [#12349](https://github.com/postmanlabs/postman-app-support/issues/12349) Fixed issue where GraphQL requests were failing to send correct data.
10+
- Fixed various TypeErrors that were occurring frequently for users.
11+
512
## [v1.7.1] - 2023-07-17
613

714
## [v1.7.0] - 2023-06-27
@@ -115,7 +122,9 @@ Newer releases follow the [Keep a Changelog](https://keepachangelog.com) format.
115122
- Conforming to the internal Postman plugin interface
116123
- Fixes for Github issues - 4770,3623,3135,4018,5737,5286, among others
117124

118-
[Unreleased]: https://github.com/postmanlabs/curl-to-postman/compare/v1.7.1...HEAD
125+
[Unreleased]: https://github.com/postmanlabs/curl-to-postman/compare/v1.8.0...HEAD
126+
127+
[v1.8.0]: https://github.com/postmanlabs/curl-to-postman/compare/v1.7.1...v1.8.0
119128

120129
[v1.7.1]: https://github.com/postmanlabs/curl-to-postman/compare/v1.7.0...v1.7.1
121130

package-lock.json

Lines changed: 13 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "curl-to-postmanv2",
3-
"version": "1.7.1",
3+
"version": "1.8.0",
44
"description": "Convert a given CURL command to a Postman request",
55
"main": "index.js",
66
"com_postman_plugin": {
@@ -13,7 +13,7 @@
1313
},
1414
"dependencies": {
1515
"commander": "2.20.3",
16-
"lodash": "^4.17.11",
16+
"lodash": "4.17.21",
1717
"shell-quote": "1.6.1",
1818
"uuid": "3.2.1",
1919
"valid-url": "^1.0.9"

src/lib.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,26 @@ var program,
105105
var validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'COPY', 'HEAD',
106106
'OPTIONS', 'LINK', 'UNLINK', 'PURGE', 'LOCK', 'UNLOCK', 'PROPFIND'],
107107
singleWordXMethod,
108-
singleWordMethodPrefix = '-X';
109-
if (validMethods.indexOf(curlObj.request.toUpperCase()) === -1) {
108+
singleWordMethodPrefix = '-X',
109+
reqMethod = _.toUpper(curlObj.request);
110+
111+
if (validMethods.indexOf(reqMethod) === -1) {
110112

111113
// no valid method
112114
// -XPOST might have been used
113115
// try the POST part again
114-
singleWordXMethod = _.find(curlObj.rawArgs, function (arg) { return arg.startsWith(singleWordMethodPrefix); });
116+
singleWordXMethod = _.find(curlObj.rawArgs, function (arg) {
117+
return typeof arg === 'string' && arg.startsWith(singleWordMethodPrefix);
118+
});
119+
115120
if (singleWordXMethod) {
116121
// try to re-set curlObj.request to the newly extracted method
117122
curlObj.request = singleWordXMethod.substring(singleWordMethodPrefix.length);
118123
}
119124

120-
if (validMethods.indexOf(curlObj.request.toUpperCase()) === -1) {
125+
reqMethod = _.toUpper(curlObj.request);
126+
127+
if (validMethods.indexOf(reqMethod) === -1) {
121128
// the method is still not valid
122129
throw new UserError(USER_ERRORS.METHOD_NOT_SUPPORTED`${curlObj.request}`);
123130
}
@@ -218,7 +225,7 @@ var program,
218225
let authObject;
219226

220227
// It is a valid cURL to have only username, in that case keep password empty
221-
const userParts = curlObj.user.split(':') || [];
228+
const userParts = (typeof curlObj.user === 'string' && curlObj.user.split(':')) || [];
222229
if (userParts.length === 1) {
223230
userParts[1] = '';
224231
}
@@ -529,6 +536,11 @@ var program,
529536
// multipart/form-data; boundary=----7dd322351017c; ...
530537
m = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
531538

539+
// if correct boundary match is not found, keep formdata fields empty
540+
if (!m || typeof data !== 'string') {
541+
return parsedFormData;
542+
}
543+
532544
// \r\n is part of the boundary.
533545
boundary = '\r\n--' + (m[1] || m[2]);
534546

@@ -639,7 +651,7 @@ var program,
639651
this.headerPairs = {};
640652

641653
// if method is not given in the curl command
642-
if (!curlObj.request) {
654+
if (typeof curlObj.request !== 'string' || !curlObj.request) {
643655
curlObj.request = this.getRequestMethod(curlObj);
644656
isMethodGuessed = true;
645657
}
@@ -705,6 +717,7 @@ var program,
705717
identifyGraphqlRequest: function (dataString, contentType) {
706718
try {
707719
const rawDataObj = _.attempt(JSON.parse, this.escapeJson(dataString));
720+
708721
if (contentType === 'application/json' && rawDataObj && !_.isError(rawDataObj)) {
709722
if (!_.has(rawDataObj, 'query') || !_.isString(rawDataObj.query)) {
710723
return { result: false };
@@ -721,17 +734,14 @@ var program,
721734
if (!_.isString(rawDataObj.operationName)) {
722735
return { result: false };
723736
}
737+
delete rawDataObj.operationName;
724738
}
725-
else {
726-
rawDataObj.operationName = '';
727-
}
728-
if (_.keys(rawDataObj).length === 3) {
739+
if (_.keys(rawDataObj).length === 2) {
729740
const graphqlVariables = JSON.stringify(rawDataObj.variables, null, 2);
730741
return {
731742
result: true,
732743
graphql: {
733744
query: rawDataObj.query,
734-
operationName: rawDataObj.operationName,
735745
variables: graphqlVariables === '{}' ? '' : graphqlVariables
736746
}
737747
};

test/conversion.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,18 @@ describe('Curl converter should', function() {
743743
done();
744744
});
745745

746+
it('[Github #12349]: should correctly convert graphql queries without operationName', function(done) {
747+
var result = Converter.convertCurlToRequest(`curl --location 'https://spacex-production.up.railway.app' \\
748+
--header 'Content-Type: application/json' \\
749+
--data '{"query":"query getCompanyData {\r\n company {\r\n ceo\r\n }\r\n}","variables":{}}'`);
750+
751+
expect(result.body).to.have.property('mode', 'graphql');
752+
expect(result.body.graphql.query).to.eql('query getCompanyData {\r\n company {\r\n ceo\r\n }\r\n}');
753+
expect(result.body.graphql.variables).to.eql('');
754+
expect(result.body.graphql).to.not.have.property('operationName');
755+
done();
756+
});
757+
746758
describe('[Github #8843]: It should recognize non-apostrophed ("...") url with multi-param', function() {
747759
it('in case where there is multiple params with & in between in the url (https)', function(done) {
748760
convert({

0 commit comments

Comments
 (0)