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
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
1 change: 1 addition & 0 deletions codegens/nodejs-request/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 denote snippet for codegen or sdkgen

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

Expand Down
4 changes: 2 additions & 2 deletions codegens/nodejs-request/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ function parseBody (requestbody, indentString, trimBody, contentType) {
return `body: JSON.stringify(${JSON.stringify(jsonBody)})\n`;
}
catch (error) {
return `body: ${JSON.stringify(requestbody[requestbody.mode])}\n`;
return `body: '${sanitize(requestbody[requestbody.mode])}'\n`;
}
}
return `body: ${JSON.stringify(requestbody[requestbody.mode])}\n`;
return `body: '${sanitize(requestbody[requestbody.mode])}'\n`;
// eslint-disable-next-line no-case-declarations
case 'graphql':
let query = requestbody[requestbody.mode].query,
Expand Down
56 changes: 34 additions & 22 deletions codegens/nodejs-request/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@ 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;
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') {
isFormDataFile = true;
}
});
}
if (isFormDataFile) {
// These checkpoints are usefull to extract required content from the generated snippet
// These checkpoints are placed at different blocks of snippet further
if (!options.SDKGEN_enabled) {
if (options.ES6_enabled) {
snippet += 'const ';
}
else {
snippet += 'var ';
}
snippet += 'fs = require(\'fs\');\n';
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') {
isFormDataFile = true;
}
});
}
if (isFormDataFile) {
if (options.ES6_enabled) {
snippet += 'const ';
}
else {
snippet += 'var ';
}
snippet += 'fs = require(\'fs\');\n';
}
}
if (options.ES6_enabled) {
snippet += 'let ';
Expand All @@ -48,8 +52,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',
Expand Down Expand Up @@ -88,16 +92,17 @@ function makeSnippet (request, indentString, options) {
}
snippet += optionsArray.join(',\n') + '\n';
snippet += '};\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';
return snippet;
}
Expand Down Expand Up @@ -152,6 +157,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: 'Generates snippet for SDK generator.'
}
];
}
Expand Down
24 changes: 24 additions & 0 deletions codegens/nodejs-request/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,30 @@ describe('nodejs-request convert function', function () {
});
});

it('should return snippets without imports', 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).not.to.include('var request = require(\'request\');');
expect(snippet).not.to.include('var fs = require(\'fs\');');
expect(snippet).to.include('callback(error, response);');
});
});

describe('getOptions function', function () {

it('should return an array of specific options', function () {
Expand Down
9 changes: 8 additions & 1 deletion test/codegen/structure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: 'Generates snippet for SDK generator.'
}
},
// Standard array of ids that should be used for options ids. Any new option should be updated here.
Expand All @@ -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 () {
Expand Down