1
- const _ = require ( './ lodash' ) ,
1
+ const _ = require ( 'lodash' ) ,
2
2
parseRequest = require ( './parseRequest' ) ,
3
3
sanitize = require ( './util' ) . sanitize ,
4
4
sanitizeOptions = require ( './util' ) . sanitizeOptions ,
@@ -14,7 +14,7 @@ const _ = require('./lodash'),
14
14
*/
15
15
function makeSnippet ( request , indentString , options ) {
16
16
17
- var snippet = options . ES6_enabled ? 'const' : 'var ',
17
+ let snippet = 'const' ,
18
18
configArray = [ ] ,
19
19
dataSnippet = '' ,
20
20
body ,
@@ -84,12 +84,11 @@ function makeSnippet (request, indentString, options) {
84
84
dataSnippet = ! _ . isEmpty ( body ) ? parseRequest . parseBody ( body ,
85
85
options . trimRequestBody ,
86
86
indentString ,
87
- request . headers . get ( 'Content-Type' ) ,
88
- options . ES6_enabled ) : '' ;
87
+ request . headers . get ( 'Content-Type' ) ) : '' ;
89
88
snippet += dataSnippet + '\n' ;
90
89
91
90
configArray . push ( indentString + `method: '${ request . method . toLowerCase ( ) } '` ) ;
92
- configArray . push ( 'maxBodyLength: Infinity' ) ;
91
+ configArray . push ( indentString + 'maxBodyLength: Infinity' ) ;
93
92
configArray . push ( indentString + `url: '${ sanitize ( request . url . toString ( ) ) } '` ) ;
94
93
95
94
headers = parseRequest . parseHeader ( request , indentString ) ;
@@ -113,7 +112,7 @@ function makeSnippet (request, indentString, options) {
113
112
if ( options . requestTimeout ) {
114
113
configArray . push ( indentString + `timeout: ${ options . requestTimeout } ` ) ;
115
114
}
116
- if ( options . followRedirect === false ) {
115
+ if ( _ . get ( request , 'protocolProfileBehavior.followRedirects' , options . followRedirect ) === false ) {
117
116
// setting the maxRedirects to 0 will disable any redirects.
118
117
// by default, maxRedirects are set to 5
119
118
configArray . push ( indentString + 'maxRedirects: 0' ) ;
@@ -123,33 +122,31 @@ function makeSnippet (request, indentString, options) {
123
122
configArray . push ( indentString + 'data : data' ) ;
124
123
}
125
124
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' ;
134
126
snippet += configArray . join ( ',\n' ) + '\n' ;
135
127
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' ;
139
140
}
140
141
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' ;
146
146
snippet += '.catch((error) => {\n' ;
147
+ snippet += indentString + 'console.log(error);\n' ;
148
+ snippet += '});\n' ;
147
149
}
148
- else {
149
- snippet += '.catch(function (error) {\n' ;
150
- }
151
- snippet += indentString + 'console.log(error);\n' ;
152
- snippet += '});\n' ;
153
150
154
151
return snippet ;
155
152
}
@@ -199,11 +196,11 @@ function getOptions () {
199
196
description : 'Remove white space and additional lines that may affect the server\'s response'
200
197
} ,
201
198
{
202
- name : 'Enable ES6 features ' ,
203
- id : 'ES6_enabled ' ,
199
+ name : 'Use async/await ' ,
200
+ id : 'asyncAwaitEnabled ' ,
204
201
type : 'boolean' ,
205
202
default : false ,
206
- description : 'Modifies code snippet to incorporate ES6 (EcmaScript) features '
203
+ description : 'Modifies code snippet to use async/await '
207
204
}
208
205
] ;
209
206
}
@@ -228,7 +225,7 @@ function convert (request, options, callback) {
228
225
options = sanitizeOptions ( options , getOptions ( ) ) ;
229
226
230
227
// String representing value of indentation required
231
- var indentString ;
228
+ let indentString ;
232
229
233
230
indentString = options . indentType === 'Tab' ? '\t' : ' ' ;
234
231
indentString = indentString . repeat ( options . indentCount ) ;
0 commit comments