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 all commits
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
41 changes: 37 additions & 4 deletions codegens/nodejs-request/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ 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 = 'const ';
}
else {
snippet = 'var ';
}
snippet += '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 +31,21 @@ function makeSnippet (request, indentString, options) {
});
}
if (isFormDataFile) {
snippet += 'var fs = require(\'fs\');\n';
if (options.ES6_enabled) {
snippet += 'const ';
}
else {
snippet += 'var ';
}
snippet += 'fs = require(\'fs\');\n';
}
if (options.ES6_enabled) {
snippet += 'let ';
}
else {
snippet += 'var ';
}
snippet += 'var options = {\n';
snippet += 'options = {\n';

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

snippet += 'request(options, function (error, response) { \n';
snippet += 'request(options, ';
if (options.ES6_enabled) {
snippet += '(error, response) => {\n';
}
else {
snippet += '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 +145,13 @@ function getOptions () {
type: 'boolean',
default: false,
description: 'Remove white space and additional lines that may affect the server\'s response'
},
{
name: 'Enable ES6 features',
id: 'ES6_enabled',
type: 'boolean',
default: false,
description: 'Modifies code snippet to incorporate ES6 (EcmaScript) features'
}
];
}
Expand All @@ -133,6 +165,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
13 changes: 13 additions & 0 deletions codegens/nodejs-request/test/newman/newman.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ describe('Convert for different types of request', function () {
};

runNewmanTest(convert, options, testConfig);

describe('Convert for request incorporating ES6 features', function () {
var options = {indentCount: 2, indentType: 'Space', ES6_enabled: true},
testConfig = {
compileScript: null,
runScript: 'node run.js',
fileName: 'run.js',
headerSnippet: '/* eslint-disable */\n'
};

runNewmanTest(convert, options, testConfig);
});

});
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('const 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: 'Enable ES6 features',
type: 'boolean',
default: false,
description: 'Modifies code snippet to incorporate ES6 (EcmaScript) 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