Skip to content

Commit

Permalink
Merge pull request #77 from postmanlabs/release/v1.8.0
Browse files Browse the repository at this point in the history
Release version v1.8.0
  • Loading branch information
VShingala authored Jan 17, 2024
2 parents ed495e2 + b319e18 commit 775430d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 98 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## [Unreleased]

## [v1.8.0] - 2024-01-17

### Changed

- Fix for - [#12349](https://github.com/postmanlabs/postman-app-support/issues/12349) Fixed issue where GraphQL requests were failing to send correct data.
- Fixed various TypeErrors that were occurring frequently for users.

## [v1.7.1] - 2023-07-17

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

[Unreleased]: https://github.com/postmanlabs/curl-to-postman/compare/v1.7.1...HEAD
[Unreleased]: https://github.com/postmanlabs/curl-to-postman/compare/v1.8.0...HEAD

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

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

Expand Down
97 changes: 13 additions & 84 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "curl-to-postmanv2",
"version": "1.7.1",
"version": "1.8.0",
"description": "Convert a given CURL command to a Postman request",
"main": "index.js",
"com_postman_plugin": {
Expand All @@ -13,7 +13,7 @@
},
"dependencies": {
"commander": "2.20.3",
"lodash": "^4.17.11",
"lodash": "4.17.21",
"shell-quote": "1.6.1",
"uuid": "3.2.1",
"valid-url": "^1.0.9"
Expand Down
32 changes: 21 additions & 11 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,26 @@ var program,
var validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'COPY', 'HEAD',
'OPTIONS', 'LINK', 'UNLINK', 'PURGE', 'LOCK', 'UNLOCK', 'PROPFIND'],
singleWordXMethod,
singleWordMethodPrefix = '-X';
if (validMethods.indexOf(curlObj.request.toUpperCase()) === -1) {
singleWordMethodPrefix = '-X',
reqMethod = _.toUpper(curlObj.request);

if (validMethods.indexOf(reqMethod) === -1) {

// no valid method
// -XPOST might have been used
// try the POST part again
singleWordXMethod = _.find(curlObj.rawArgs, function (arg) { return arg.startsWith(singleWordMethodPrefix); });
singleWordXMethod = _.find(curlObj.rawArgs, function (arg) {
return typeof arg === 'string' && arg.startsWith(singleWordMethodPrefix);
});

if (singleWordXMethod) {
// try to re-set curlObj.request to the newly extracted method
curlObj.request = singleWordXMethod.substring(singleWordMethodPrefix.length);
}

if (validMethods.indexOf(curlObj.request.toUpperCase()) === -1) {
reqMethod = _.toUpper(curlObj.request);

if (validMethods.indexOf(reqMethod) === -1) {
// the method is still not valid
throw new UserError(USER_ERRORS.METHOD_NOT_SUPPORTED`${curlObj.request}`);
}
Expand Down Expand Up @@ -218,7 +225,7 @@ var program,
let authObject;

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

// if correct boundary match is not found, keep formdata fields empty
if (!m || typeof data !== 'string') {
return parsedFormData;
}

// \r\n is part of the boundary.
boundary = '\r\n--' + (m[1] || m[2]);

Expand Down Expand Up @@ -639,7 +651,7 @@ var program,
this.headerPairs = {};

// if method is not given in the curl command
if (!curlObj.request) {
if (typeof curlObj.request !== 'string' || !curlObj.request) {
curlObj.request = this.getRequestMethod(curlObj);
isMethodGuessed = true;
}
Expand Down Expand Up @@ -705,6 +717,7 @@ var program,
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 };
Expand All @@ -721,17 +734,14 @@ var program,
if (!_.isString(rawDataObj.operationName)) {
return { result: false };
}
delete rawDataObj.operationName;
}
else {
rawDataObj.operationName = '';
}
if (_.keys(rawDataObj).length === 3) {
if (_.keys(rawDataObj).length === 2) {
const graphqlVariables = JSON.stringify(rawDataObj.variables, null, 2);
return {
result: true,
graphql: {
query: rawDataObj.query,
operationName: rawDataObj.operationName,
variables: graphqlVariables === '{}' ? '' : graphqlVariables
}
};
Expand Down
12 changes: 12 additions & 0 deletions test/conversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,18 @@ describe('Curl converter should', function() {
done();
});

it('[Github #12349]: should correctly convert graphql queries without operationName', function(done) {
var result = Converter.convertCurlToRequest(`curl --location 'https://spacex-production.up.railway.app' \\
--header 'Content-Type: application/json' \\
--data '{"query":"query getCompanyData {\r\n company {\r\n ceo\r\n }\r\n}","variables":{}}'`);

expect(result.body).to.have.property('mode', 'graphql');
expect(result.body.graphql.query).to.eql('query getCompanyData {\r\n company {\r\n ceo\r\n }\r\n}');
expect(result.body.graphql.variables).to.eql('');
expect(result.body.graphql).to.not.have.property('operationName');
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

0 comments on commit 775430d

Please sign in to comment.