-
Notifications
You must be signed in to change notification settings - Fork 360
Feat/axios nodejs #232
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
Feat/axios nodejs #232
Conversation
Oh I'm excited about this |
any updates? I want this so muh :) |
@r3dsm0k3 can you rebase your branch with the latest develop from postman-code-generators? |
Also, can you post some example snippets here? for GET, POST, etc. |
df45da4
to
9cdcaad
Compare
Sure, based on the tests; ✓ should generate snippet for basicCollection request: Request Headers const axios = require('axios');
const config = {
'method': 'get',
'url': 'https://postman-echo.com/headers',
'headers': {
'my-sample-header': 'Lorem ipsum dolor sit amet',
'not-disabled-header': 'ENABLED'
}
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
}); ✓ should generate snippet for basicCollection request: POST json with raw const axios = require('axios');
const data = JSON.stringify({
query: "{\n body{\n graphql\n }\n}",
variables: {"variable_key":"variable_value"}
})
const config = {
'method': 'post',
'url': 'http://postman-echo.com/post',
'headers': {
'Content-Type': 'application/json'
},
data : data
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
}); ✓ should generate snippet for basicCollection request: DELETE Request const axios = require('axios');
const FormData = require('form-data')
const data = new FormData();
data.append("pl", "\'a\'");
data.append("qu", "\"b\"");
data.append("sa", "d");
data.append("Special", "!@#$%&*()^_+=`~");
data.append("more", ",./\';[]}{\":?><|\\\\");
const config = {
'method': 'post',
'url': 'https://postman-echo.com/post',
'headers': {
...data.getHeaders()
},
data : data
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
}); ✓ should generate snippet for formdataCollection request: POST multipart/form-data with text parameters const axios = require('axios');
const FormData = require('form-data')
const data = new FormData();
const config = {
'method': 'post',
'url': 'https://postman-echo.com/post',
'headers': {
...data.getHeaders()
},
data : data
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
}); |
Tests failing |
@shreys7 I see that all of the axios related tests are passing it is failing at completely unrelated places, although there's no change in those related libraries (same as upstream). Is there anything I'm missing here? |
@shreys7 I've tested this version locally and generated snippets are perfect. Tests are failing due to #246 else everything seems alright. Here's a ss of failed request. All other requests are failing due to same parameter. The the I think the x-amzn... parameter has to be changed from all the variants in order to tests to get passed. |
I did check for snippet generation before. By review, I meant to review the code used to generate the snippets. The code should be in alignment with other languages' code, proper linting, correct code generated for options, etc. |
Okay sure got it ✌️ |
@shreys7 reviewed code thoroughly. All requests are generating according to the options provided. Also I've suggested some changes below. |
codegens/nodejs-axios/README.md
Outdated
* `requestTimeout` : Integer denoting time after which the request will bail out in milli-seconds | ||
* `trimRequestBody` : Trim request body fields | ||
* `followRedirect` : Boolean denoting whether to redirect a request | ||
* `AsyncAwait_enabled` : Boolean denoting whether to generate snippet with async await coding style (WIP) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove async await option from the readme as its not supported by the code generator
options = { | ||
indentType: 'Space', | ||
indentCount: 2, | ||
ES6_enabled: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for es_6 enable option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than removing the option, it would be great to have that as an option and add support for it in the code.
codegens/nodejs-axios/lib/axios.js
Outdated
snippet += indentString + 'console.log(error);\n'; | ||
snippet += '});\n'; | ||
|
||
// if(options.AsyncAwait_enabled === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove these unwanted commented code
codegens/nodejs-axios/lib/axios.js
Outdated
default: false, | ||
description: 'Remove white space and additional lines that may affect the server\'s response' | ||
} | ||
// , |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unwanted commented code
codegens/nodejs-axios/lib/axios.js
Outdated
* @param {String} options.indentCount - number of spaces or tabs for indentation. | ||
* @param {Boolean} options.followRedirect - whether to enable followredirect | ||
* @param {Boolean} options.trimRequestBody - whether to trim fields in request body or not | ||
* @param {Boolean} options.AsyncAwait_enabled : whether to enable async await pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove AsyncAwait_enabled option as it is not there in the code generator
|
||
runNewmanTest(convert, options, testConfig); | ||
|
||
describe('Convert for request incorporating ES6 features', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since your variant does not have this option you can remove this test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment, add the support for ES6 syntax
@r3dsm0k3 Can you address the changes as suggested by @someshkoli ? |
Yeah sure. |
Any updates @r3dsm0k3 ? |
Hey guys, I'm quite occupied these days. unfortunately, I'll only be able to spend time on the weekend. Feel free to take a stab at it if you feel like. Cheers. |
@shreys7 shall I take over this ? |
@someshkoli I've given you access to my repo (check your invitation). If you push to the same branch, it should update the PR automatically. Thanks for the effort 👍 |
Okay cool ✌️ |
sure @someshkoli. Feel free to make the changes. Also, I have fixed the CI, changes up on latest develop. You can rebase with the latest develop, and the CI should pass. |
Great. Will make these changes and puch today ✌️✌️ |
…nerators into feat/axios-nodejs pulls fix for ci fails
@shreys7 can you please check why |
Can you add |
again failed due to csharp @shreys7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added some review comments. The comments are mostly about adding proper indentations, semicolons, sanitization of strings, etc. Tell me if you face any queries regarding the comments 🙂
codegens/nodejs-axios/lib/axios.js
Outdated
} | ||
snippet += varDeclare + ' config = {\n'; | ||
snippet += configArray.join(',\n') + '\n'; | ||
snippet += '}\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a semicolon and a new line after the closing bracket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
codegens/nodejs-axios/lib/axios.js
Outdated
options.ES6_enabled); | ||
snippet += dataSnippet+'\n'; | ||
|
||
configArray.push(indentString + `'method': '${request.method.toLowerCase()}'`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need of adding single quotes around the method
, url
and headers
key. just add
{
method: 'post',
url: 'example.com',
headers: {...}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
codegens/nodejs-axios/lib/util.js
Outdated
return ''; | ||
} | ||
(trim) && (inputString = inputString.trim()); | ||
return inputString.replace(/\\/g, '\\\\').replace(/'/g, '\\\'').replace(/\n/g, '\\n').replace(/"/g, '\\"'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no need to sanitize both single as well as double quotes. The need for sanitization is that when you receive a single quote inside a single quoted string, it needs to be escaped. For example
'ab\'cd'
. But if we have a single quoted string, we can place double quotes inside it, e.g. 'ab"cd'
. Thus, we only need to escape a single one from both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
*/ | ||
function parseFormData (body, trim, ES6_enabled) { | ||
var varDeclare = ES6_enabled ? 'const' : 'var', | ||
bodySnippet = varDeclare + ' FormData = require(\'form-data\')\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a semicolon at the end of the require('form-data') statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
// check if there's file | ||
const fileArray = body.filter(function (item) { return !item.disabled && item.type === 'file'; }); | ||
if (fileArray.length > 0) { | ||
bodySnippet += varDeclare + ' fs = require(\'fs\')\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a semicolon at the end of the require('fs') statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
codegens/nodejs-axios/lib/axios.js
Outdated
// https://github.com/axios/axios/issues/789#issuecomment-577177492 | ||
if (!_.isEmpty(body) && body.formdata ) { | ||
// we can assume that data object is filled up | ||
headers.push(`\n${indentString}...data.getHeaders()`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't really need to add a \n
before the ...data.getHeaders(). I guess headers.join(,\n) will take care of it 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the snippet generated when request has some headers, and this formdata header is also added. The indentation looks weird, can you check on it once?
'headers': {
'my-sample-header': 'Lorem ipsum dolor sit amet',
'TEST': '@#$%^&*()',
'more': ',./\';[]}{\":?><|\\\\',
...data.getHeaders()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
codegens/nodejs-axios/lib/axios.js
Outdated
|
||
body = request.body && request.body.toJSON(); | ||
|
||
dataSnippet = parseRequest.parseBody(body, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only add the body if it is non empty object, and the mode that it states has non empty data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
*/ | ||
function parseURLEncodedBody (body, trim, ES6_enabled) { | ||
var varDeclare = ES6_enabled ? 'const' : 'var', | ||
bodySnippet = varDeclare + ' qs = require(\'qs\')\n', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a semicolon after the require('qs') statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
bodySnippet = varDeclare + ' data = JSON.stringify({\n'; | ||
bodySnippet += `${indentString}query: "${sanitize(query, trim)}",\n`; | ||
bodySnippet += `${indentString}variables: ${JSON.stringify(graphqlVariables)}\n})`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a semicolon at the end of line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
- adds ; where necessary - removes \n where not required - removes double quote sanitization - adds \n where necessary
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`; | ||
} | ||
catch (error) { | ||
bodySnippet += `"${sanitize(body.toString(), trim)}";\n`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the sanitize function, we escape single quotes only. If the body here contains a double quote, it won't get escaped, resulting in a wrong snippet generated. e.g. "this is a sample "body preview"
. Look at the sanitize functions of other snippets to see how we handle the sanitization of quotes.
Merging this PR. Thanks a lot for the contribution @r3dsm0k3 @someshkoli 🙏😄 |
@shreys7 this now available when making docs in postman? Thank you! |
Changes
Tests ✅
Fixes postmanlabs/postman-app-support#3822 ✅
Currently there is no async await style code generation. However, adding it is very trivial.
I've tested for node-axios. Since axios also works on browser, it can also be used on the browser with minimal changes (I guess
formdata
file reading might need to change, but cant be very certain since I didnt test it myself)I could not update the author in the
package.json
since there is a test to make sure it is Postman labs.