diff --git a/codegens/nodejs-request/README.md b/codegens/nodejs-request/README.md index ec86322bf..f1994ec10 100644 --- a/codegens/nodejs-request/README.md +++ b/codegens/nodejs-request/README.md @@ -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 diff --git a/codegens/nodejs-request/lib/parseRequest.js b/codegens/nodejs-request/lib/parseRequest.js index 75c99fd8f..3d7d6fac5 100644 --- a/codegens/nodejs-request/lib/parseRequest.js +++ b/codegens/nodejs-request/lib/parseRequest.js @@ -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, diff --git a/codegens/nodejs-request/lib/request.js b/codegens/nodejs-request/lib/request.js index 73d2e5d29..3f94396d5 100644 --- a/codegens/nodejs-request/lib/request.js +++ b/codegens/nodejs-request/lib/request.js @@ -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 '; @@ -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', @@ -88,7 +92,6 @@ function makeSnippet (request, indentString, options) { } snippet += optionsArray.join(',\n') + '\n'; snippet += '};\n'; - snippet += 'request(options, '; if (options.ES6_enabled) { snippet += '(error, response) => {\n'; @@ -96,8 +99,10 @@ function makeSnippet (request, indentString, options) { 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; } @@ -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.' } ]; } diff --git a/codegens/nodejs-request/test/unit/snippet.test.js b/codegens/nodejs-request/test/unit/snippet.test.js index 1d4102e34..c72676206 100644 --- a/codegens/nodejs-request/test/unit/snippet.test.js +++ b/codegens/nodejs-request/test/unit/snippet.test.js @@ -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 () { diff --git a/test/codegen/structure.test.js b/test/codegen/structure.test.js index aaaa3e1ae..ff4a533d6 100644 --- a/test/codegen/structure.test.js +++ b/test/codegen/structure.test.js @@ -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. @@ -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 () {