Skip to content

Added ES6 feature support to NodeJS-Request codegen #180

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

Merged
merged 3 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 33 additions & 4 deletions codegens/nodejs-request/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ var _ = require('./lodash'),
* @returns {String} - nodejs(request) code snippet for given request object
*/
function makeSnippet (request, indentString, options) {
var snippet = 'var request = require(\'request\');\n',
var snippet,
optionsArray = [],
isFormDataFile = false;
if (options.ES6_enabled) {
snippet = 'let request = require(\'request\');\n';
}
else {
snippet = 'var request = require(\'request\');\n';
}
if (request.body && request.body.mode === 'formdata') {
_.forEach(request.body.toJSON().formdata, function (data) {
if (!data.disabled && data.type === 'file') {
Expand All @@ -24,9 +30,19 @@ function makeSnippet (request, indentString, options) {
});
}
if (isFormDataFile) {
snippet += 'var fs = require(\'fs\');\n';
if (options.ES6_enabled) {
snippet += 'let fs = require(\'fs\');\n';
}
else {
snippet += 'var fs = require(\'fs\');\n';
}
}
if (options.ES6_enabled) {
snippet += 'let options = {\n';
}
else {
snippet += 'var options = {\n';
}
snippet += 'var options = {\n';

/**
* creating string to represent options object using optionArray.join()
Expand Down Expand Up @@ -70,7 +86,12 @@ function makeSnippet (request, indentString, options) {
snippet += optionsArray.join(',\n') + '\n';
snippet += '};\n';

snippet += 'request(options, function (error, response) { \n';
if (options.ES6_enabled) {
snippet += 'request(options, (error, response) => {\n';
}
else {
snippet += 'request(options, function (error, response) {\n';
}
snippet += indentString + 'if (error) throw new Error(error);\n';
snippet += indentString + 'console.log(response.body);\n';
snippet += '});\n';
Expand Down Expand Up @@ -120,6 +141,13 @@ function getOptions () {
type: 'boolean',
default: false,
description: 'Remove white space and additional lines that may affect the server\'s response'
},
{
name: 'Generate the code snippet with ES6 features',
id: 'ES6_enabled',
type: 'boolean',
default: false,
description: 'Allows the user to generate code snippet with latest EcmaScript 6 (ES6) features'
}
];
}
Expand All @@ -133,6 +161,7 @@ function getOptions () {
* @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.ES6_enabled - whether to generate snippet with ES6 features
* @param {Number} options.requestTimeout : time in milli-seconds after which request will bail out
* @param {Function} callback - callback function with parameters (error, snippet)
*/
Expand Down
19 changes: 19 additions & 0 deletions codegens/nodejs-request/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ describe('nodejs-request convert function', function () {
});
});

it('should return snippet with ES6 features when ES6_enabled is set to true', function () {
request = new sdk.Request(mainCollection.item[0].request);
options = {
ES6_enabled: true
};
convert(request, options, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
return;
}

expect(snippet).to.be.a('string');
snippetArray = snippet.split('\n');
expect(snippetArray[0]).to.equal('let request = require(\'request\');');
expect(snippetArray).to.include('let options = {');
expect(snippetArray).to.include('request(options, (error, response) => {');
});
});

it('should return snippet with followRedirect property set to ' +
'false for no follow redirect', function () {
request = new sdk.Request(mainCollection.item[0].request);
Expand Down
41 changes: 30 additions & 11 deletions package-lock.json

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

9 changes: 8 additions & 1 deletion test/codegen/structure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const expectedOptions = {
type: 'boolean',
default: false,
description: 'Display the requested data without showing the cURL progress meter or error messages'
},
ES6_enabled:{
name: 'Generate the code snippet with ES6 features',
type: 'boolean',
default: false,
description: 'Allows the user to generate code snippet with latest EcmaScript 6 (ES6) features'
}
},
// Standard array of ids that should be used for options ids. Any new option should be updated here.
Expand All @@ -75,7 +81,8 @@ const expectedOptions = {
'followRedirect',
'lineContinuationCharacter',
'protocol',
'useMimeType'
'useMimeType',
'ES6_enabled'
],
CODEGEN_ABS_PATH = `./codegens/${codegen}`;
describe('Code-gen repository ' + codegen, function () {
Expand Down