Skip to content
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

Adds additional option to nodejs-request to generate code gor SDK generator #282

Open
wants to merge 5 commits into
base: feature/sdk-generation
Choose a base branch
from
Open
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
Next Next commit
Adds option for additional snippets for SDK generator
 - Adds option SDKGEN_enabled
 - Adds addition snippets (if SDKGEN_enabled is true)
 - Adds test for extra snippets
 - Updates README for nodejs-request with new option
someshkoli committed Jun 7, 2020
commit 9b6ddc86ef6b5c3cbbcd76af88177811da720c22
1 change: 1 addition & 0 deletions codegens/nodejs-request/README.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ Convert function will take three parameters
* `trimRequestBody` : Trim request body fields
* `followRedirect` : Boolean denoting whether to redirect a request
* `ES6_enabled` : Boolean denoting whether to generate snippet with ES6 features
* `SDKGEN_enabled` : Boolean denoting wether to add checkpoints and other snippets required for postman collection code generator

* `callback`- callback function with first parameter as error and second parameter as string for code snippet

34 changes: 27 additions & 7 deletions codegens/nodejs-request/lib/request.js
Original file line number Diff line number Diff line change
@@ -13,14 +13,17 @@ var _ = require('./lodash'),
* @returns {String} - nodejs(request) code snippet for given request object
*/
function makeSnippet (request, indentString, options) {
var snippet,
var snippet = '',
optionsArray = [],
isFormDataFile = false;
// These checkpoints are usefull to extract required content from the generated snippet
// These checkpoints are placed at different blocks of snippet further
snippet += options.SDKGEN_enabled ? '// ---> IMPORTS BEGIN <---\n' : '';
if (options.ES6_enabled) {
snippet = 'const ';
snippet += 'const ';
}
else {
snippet = 'var ';
snippet += 'var ';
}
snippet += 'request = require(\'request\');\n';
if (request.body && request.body.mode === 'formdata') {
@@ -39,6 +42,9 @@ function makeSnippet (request, indentString, options) {
}
snippet += 'fs = require(\'fs\');\n';
}
snippet += options.SDKGEN_enabled ? '// ---> IMPORTS ENDS <---\n' : '';

snippet += options.SDKGEN_enabled ? '// ---> CONFIG BEGIN <---\n' : '';
if (options.ES6_enabled) {
snippet += 'let ';
}
@@ -48,8 +54,8 @@ function makeSnippet (request, indentString, options) {
snippet += 'options = {\n';

/**
* creating string to represent options object using optionArray.join()
* example:
* creating string to represent options object using optionArray.join()
* example:
* options: {
* method: 'GET',
* url: 'www.google.com',
@@ -88,17 +94,24 @@ function makeSnippet (request, indentString, options) {
}
snippet += optionsArray.join(',\n') + '\n';
snippet += '};\n';
snippet += options.SDKGEN_enabled ? '// ---> CONFIG ENDS <---\n' : '';

snippet += options.SDKGEN_enabled ? '// ---> REQUEST STARTS <---\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 += indentString;
snippet += options.SDKGEN_enabled ? '' : 'if (error) throw new Error(error);\n';
snippet += indentString;
snippet += options.SDKGEN_enabled ? 'callback(error, response);\n' : 'console.log(response.body);\n';
snippet += '});\n';
snippet += options.SDKGEN_enabled ? '// ---> REQUEST ENDS <---\n' : '';
console.log(snippet);
console.log();
return snippet;
}

@@ -152,6 +165,13 @@ function getOptions () {
type: 'boolean',
default: false,
description: 'Modifies code snippet to incorporate ES6 (EcmaScript) features'
},
{
name: 'Codegen for SDK generator',
id: 'SDKGEN_enabled',
type: 'boolean',
default: false,
description: 'Adds checkpoints and snippets changes to be used for postman collection code generator'
}
];
}
27 changes: 27 additions & 0 deletions codegens/nodejs-request/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
@@ -376,6 +376,33 @@ describe('nodejs-request convert function', function () {
});
});

it('should return snippets with checkpoints and snippets for sdkgenerator', function () {
var request = new sdk.Request({
'method': 'GET',
'header': [],
'url': {
'raw': 'https://google.com',
'protocol': 'https',
'host': [
'google',
'com'
]
}
});
convert(request, { SDKGEN_enabled: true}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('// ---> IMPORTS BEGINS <---\n');
expect(snippet).to.include('// ---> IMPORTS ENDS <---\n');
expect(snippet).to.include('// ---> CONFIG BEGINS <---\n');
expect(snippet).to.include('// ---> CONFIG ENDS <---\n');
expect(snippet).to.include('// ---> REQUEST BEGINS <---\n');
expect(snippet).to.include('// ---> REQUEST ENDS <---\n');
});
});

describe('getOptions function', function () {

it('should return an array of specific options', function () {
9 changes: 8 additions & 1 deletion test/codegen/structure.test.js
Original file line number Diff line number Diff line change
@@ -67,6 +67,12 @@ const expectedOptions = {
type: 'boolean',
default: false,
description: 'Modifies code snippet to incorporate ES6 (EcmaScript) features'
},
SDKGEN_enables: {
name: 'Codegen for SDK generator',
type: 'boolean',
default: false,
description: 'Adds checkpoints and snippets changes to be used for postman collection code generator'
}
},
// Standard array of ids that should be used for options ids. Any new option should be updated here.
@@ -83,7 +89,8 @@ const expectedOptions = {
'lineContinuationCharacter',
'protocol',
'useMimeType',
'ES6_enabled'
'ES6_enabled',
'SDKGEN_enabled'
],
CODEGEN_ABS_PATH = `./codegens/${codegen}`;
describe('Code-gen repository ' + codegen, function () {