1- const _ = require ( './ lodash' ) ,
1+ const _ = require ( 'lodash' ) ,
22 parseRequest = require ( './parseRequest' ) ,
33 sanitize = require ( './util' ) . sanitize ,
44 sanitizeOptions = require ( './util' ) . sanitizeOptions ,
@@ -14,7 +14,7 @@ const _ = require('./lodash'),
1414 */
1515function makeSnippet ( request , indentString , options ) {
1616
17- var snippet = options . ES6_enabled ? 'const' : 'var ',
17+ let snippet = 'const' ,
1818 configArray = [ ] ,
1919 dataSnippet = '' ,
2020 body ,
@@ -84,12 +84,11 @@ function makeSnippet (request, indentString, options) {
8484 dataSnippet = ! _ . isEmpty ( body ) ? parseRequest . parseBody ( body ,
8585 options . trimRequestBody ,
8686 indentString ,
87- request . headers . get ( 'Content-Type' ) ,
88- options . ES6_enabled ) : '' ;
87+ request . headers . get ( 'Content-Type' ) ) : '' ;
8988 snippet += dataSnippet + '\n' ;
9089
9190 configArray . push ( indentString + `method: '${ request . method . toLowerCase ( ) } '` ) ;
92- configArray . push ( 'maxBodyLength: Infinity' ) ;
91+ configArray . push ( indentString + 'maxBodyLength: Infinity' ) ;
9392 configArray . push ( indentString + `url: '${ sanitize ( request . url . toString ( ) ) } '` ) ;
9493
9594 headers = parseRequest . parseHeader ( request , indentString ) ;
@@ -113,7 +112,7 @@ function makeSnippet (request, indentString, options) {
113112 if ( options . requestTimeout ) {
114113 configArray . push ( indentString + `timeout: ${ options . requestTimeout } ` ) ;
115114 }
116- if ( options . followRedirect === false ) {
115+ if ( _ . get ( request , 'protocolProfileBehavior.followRedirects' , options . followRedirect ) === false ) {
117116 // setting the maxRedirects to 0 will disable any redirects.
118117 // by default, maxRedirects are set to 5
119118 configArray . push ( indentString + 'maxRedirects: 0' ) ;
@@ -123,33 +122,31 @@ function makeSnippet (request, indentString, options) {
123122 configArray . push ( indentString + 'data : data' ) ;
124123 }
125124
126- if ( options . ES6_enabled ) {
127- snippet += 'let' ;
128- }
129- else {
130- snippet += 'var' ;
131- }
132-
133- snippet += ' config = {\n' ;
125+ snippet += 'let config = {\n' ;
134126 snippet += configArray . join ( ',\n' ) + '\n' ;
135127 snippet += '};\n\n' ;
136- snippet += 'axios(config)\n' ;
137- if ( options . ES6_enabled ) {
138- snippet += '.then((response) => {\n' ;
128+
129+ if ( options . asyncAwaitEnabled ) {
130+ snippet += 'async function makeRequest() {\n' ;
131+ snippet += indentString + 'try {\n' ;
132+ snippet += indentString . repeat ( 2 ) + 'const response = await axios.request(config);\n' ;
133+ snippet += indentString . repeat ( 2 ) + 'console.log(JSON.stringify(response.data));\n' ;
134+ snippet += indentString + '}\n' ;
135+ snippet += indentString + 'catch (error) {\n' ;
136+ snippet += indentString . repeat ( 2 ) + 'console.log(error);\n' ;
137+ snippet += indentString + '}\n' ;
138+ snippet += '}\n\n' ;
139+ snippet += 'makeRequest();\n' ;
139140 }
140141 else {
141- snippet += '.then(function (response) {\n' ;
142- }
143- snippet += indentString + 'console.log(JSON.stringify(response.data));\n' ;
144- snippet += '})\n' ;
145- if ( options . ES6_enabled ) {
142+ snippet += 'axios.request(config)\n' ;
143+ snippet += '.then((response) => {\n' ;
144+ snippet += indentString + 'console.log(JSON.stringify(response.data));\n' ;
145+ snippet += '})\n' ;
146146 snippet += '.catch((error) => {\n' ;
147+ snippet += indentString + 'console.log(error);\n' ;
148+ snippet += '});\n' ;
147149 }
148- else {
149- snippet += '.catch(function (error) {\n' ;
150- }
151- snippet += indentString + 'console.log(error);\n' ;
152- snippet += '});\n' ;
153150
154151 return snippet ;
155152}
@@ -199,11 +196,11 @@ function getOptions () {
199196 description : 'Remove white space and additional lines that may affect the server\'s response'
200197 } ,
201198 {
202- name : 'Enable ES6 features ' ,
203- id : 'ES6_enabled ' ,
199+ name : 'Use async/await ' ,
200+ id : 'asyncAwaitEnabled ' ,
204201 type : 'boolean' ,
205202 default : false ,
206- description : 'Modifies code snippet to incorporate ES6 (EcmaScript) features '
203+ description : 'Modifies code snippet to use async/await '
207204 }
208205 ] ;
209206}
@@ -228,7 +225,7 @@ function convert (request, options, callback) {
228225 options = sanitizeOptions ( options , getOptions ( ) ) ;
229226
230227 // String representing value of indentation required
231- var indentString ;
228+ let indentString ;
232229
233230 indentString = options . indentType === 'Tab' ? '\t' : ' ' ;
234231 indentString = indentString . repeat ( options . indentCount ) ;
0 commit comments