@@ -24,7 +24,7 @@ function redirectMode (redirect) {
24
24
* @param {boolean } trim trim body option
25
25
*/
26
26
function parseURLEncodedBody ( body , trim ) {
27
- var bodySnippet = 'var urlencoded = new URLSearchParams();\n' ;
27
+ var bodySnippet = 'const urlencoded = new URLSearchParams();\n' ;
28
28
_ . forEach ( body , function ( data ) {
29
29
if ( ! data . disabled ) {
30
30
bodySnippet += `urlencoded.append("${ sanitize ( data . key , trim ) } ", "${ sanitize ( data . value , trim ) } ");\n` ;
@@ -40,7 +40,7 @@ function parseURLEncodedBody (body, trim) {
40
40
* @param {boolean } trim trim body option
41
41
*/
42
42
function parseFormData ( body , trim ) {
43
- var bodySnippet = 'var formdata = new FormData();\n' ;
43
+ var bodySnippet = 'const formdata = new FormData();\n' ;
44
44
_ . forEach ( body , function ( data ) {
45
45
if ( ! data . disabled ) {
46
46
if ( data . type === 'file' ) {
@@ -65,7 +65,7 @@ function parseFormData (body, trim) {
65
65
* @param {String } indentString Indentation string
66
66
*/
67
67
function parseRawBody ( body , trim , contentType , indentString ) {
68
- var bodySnippet = 'var raw = ' ;
68
+ var bodySnippet = 'const raw = ' ;
69
69
// Match any application type whose underlying structure is json
70
70
// For example application/vnd.api+json
71
71
// All of them have +json as suffix
@@ -101,7 +101,7 @@ function parseGraphQL (body, trim, indentString) {
101
101
catch ( e ) {
102
102
graphqlVariables = { } ;
103
103
}
104
- bodySnippet = 'var graphql = JSON.stringify({\n' ;
104
+ bodySnippet = 'const graphql = JSON.stringify({\n' ;
105
105
bodySnippet += `${ indentString } query: "${ sanitize ( query , trim ) } ",\n` ;
106
106
bodySnippet += `${ indentString } variables: ${ JSON . stringify ( graphqlVariables ) } \n})` ;
107
107
return bodySnippet ;
@@ -113,7 +113,7 @@ function parseGraphQL (body, trim, indentString) {
113
113
* parses binamry file data
114
114
*/
115
115
function parseFileData ( ) {
116
- var bodySnippet = 'var file = "<file contents here>";\n' ;
116
+ var bodySnippet = 'const file = "<file contents here>";\n' ;
117
117
return bodySnippet ;
118
118
}
119
119
@@ -154,7 +154,7 @@ function parseBody (body, trim, indentString, contentType) {
154
154
function parseHeaders ( headers ) {
155
155
var headerSnippet = '' ;
156
156
if ( ! _ . isEmpty ( headers ) ) {
157
- headerSnippet = 'var myHeaders = new Headers();\n' ;
157
+ headerSnippet = 'const myHeaders = new Headers();\n' ;
158
158
headers = _ . reject ( headers , 'disabled' ) ;
159
159
_ . forEach ( headers , function ( header ) {
160
160
headerSnippet += `myHeaders.append("${ sanitize ( header . key , true ) } ", "${ sanitize ( header . value ) } ");\n` ;
@@ -209,6 +209,13 @@ function getOptions () {
209
209
type : 'boolean' ,
210
210
default : false ,
211
211
description : 'Remove white space and additional lines that may affect the server\'s response'
212
+ } ,
213
+ {
214
+ name : 'Use async/await' ,
215
+ id : 'asyncAwaitEnabled' ,
216
+ type : 'boolean' ,
217
+ default : false ,
218
+ description : 'Modifies code snippet to use async/await'
212
219
}
213
220
] ;
214
221
}
@@ -238,7 +245,6 @@ function convert (request, options, callback) {
238
245
headerSnippet = '' ,
239
246
bodySnippet = '' ,
240
247
optionsSnippet = '' ,
241
- timeoutSnippet = '' ,
242
248
fetchSnippet = '' ;
243
249
indent = indent . repeat ( options . indentCount ) ;
244
250
if ( request . body && request . body . mode === 'graphql' && ! request . headers . has ( 'Content-Type' ) ) {
@@ -294,8 +300,12 @@ function convert (request, options, callback) {
294
300
body = request . body && request . body . toJSON ( ) ;
295
301
bodySnippet = parseBody ( body , trim , indent , request . headers . get ( 'Content-Type' ) ) ;
296
302
297
- optionsSnippet = `var requestOptions = {\n${ indent } ` ;
298
- optionsSnippet += `method: '${ request . method } ',\n${ indent } ` ;
303
+ if ( options . requestTimeout > 0 ) {
304
+ codeSnippet += 'const controller = new AbortController();\n' ;
305
+ codeSnippet += `const timerId = setTimeout(() => controller.abort(), ${ options . requestTimeout } );\n` ;
306
+ }
307
+ optionsSnippet = `const requestOptions = {\n${ indent } ` ;
308
+ optionsSnippet += `method: "${ request . method } ",\n${ indent } ` ;
299
309
if ( headerSnippet !== '' ) {
300
310
optionsSnippet += `headers: myHeaders,\n${ indent } ` ;
301
311
codeSnippet += headerSnippet + '\n' ;
@@ -305,30 +315,39 @@ function convert (request, options, callback) {
305
315
optionsSnippet += `body: ${ body . mode } ,\n${ indent } ` ;
306
316
codeSnippet += bodySnippet + '\n' ;
307
317
}
308
- optionsSnippet += `redirect: '${ redirectMode ( options . followRedirect ) } '\n};\n` ;
318
+ if ( options . requestTimeout > 0 ) {
319
+ optionsSnippet += `signal: controller.signal,\n${ indent } ` ;
320
+ }
321
+ optionsSnippet += `redirect: "${ redirectMode ( options . followRedirect ) } "\n};\n` ;
309
322
310
323
codeSnippet += optionsSnippet + '\n' ;
311
324
312
- fetchSnippet = `fetch("${ sanitize ( request . url . toString ( ) ) } ", requestOptions)\n${ indent } ` ;
313
- fetchSnippet += `.then(response => response.text())\n${ indent } ` ;
314
- fetchSnippet += `.then(result => console.log(result))\n${ indent } ` ;
315
- fetchSnippet += '.catch(error => console.log(\'error\', error));' ;
316
-
317
- if ( options . requestTimeout > 0 ) {
318
- timeoutSnippet = `var promise = Promise.race([\n${ indent } ` ;
319
- timeoutSnippet += `fetch('${ request . url . toString ( ) } ', requestOptions)\n${ indent } ${ indent } ` ;
320
- timeoutSnippet += `.then(response => response.text()),\n${ indent } ` ;
321
- timeoutSnippet += `new Promise((resolve, reject) =>\n${ indent } ${ indent } ` ;
322
- timeoutSnippet += `setTimeout(() => reject(new Error('Timeout')), ${ options . requestTimeout } )\n${ indent } ` ;
323
- timeoutSnippet += ')\n]);\n\n' ;
324
- timeoutSnippet += 'promise.then(result => console.log(result)),\n' ;
325
- timeoutSnippet += 'promise.catch(error => console.log(error));' ;
326
- codeSnippet += timeoutSnippet ;
325
+ if ( options . asyncAwaitEnabled ) {
326
+ fetchSnippet += `try {\n${ indent } ` ;
327
+ fetchSnippet += `const response = await fetch("${ sanitize ( request . url . toString ( ) ) } ", requestOptions);\n${ indent } ` ;
328
+ fetchSnippet += `const result = await response.text();\n${ indent } ` ;
329
+ fetchSnippet += 'console.log(result)\n' ;
330
+ fetchSnippet += `} catch (error) {\n${ indent } ` ;
331
+ fetchSnippet += 'console.error(error);\n' ;
332
+ if ( options . requestTimeout > 0 ) {
333
+ fetchSnippet += `} finally {\n${ indent } ` ;
334
+ fetchSnippet += 'clearTimeout(timerId);\n' ;
335
+ }
336
+ fetchSnippet += '};' ;
327
337
}
328
338
else {
329
- codeSnippet += fetchSnippet ;
339
+ fetchSnippet = `fetch("${ sanitize ( request . url . toString ( ) ) } ", requestOptions)\n${ indent } ` ;
340
+ fetchSnippet += `.then((response) => response.text())\n${ indent } ` ;
341
+ fetchSnippet += `.then((result) => console.log(result))\n${ indent } ` ;
342
+ fetchSnippet += '.catch((error) => console.error(error))' ;
343
+ if ( options . requestTimeout > 0 ) {
344
+ fetchSnippet += `\n${ indent } .finally(() => clearTimeout(timerId))` ;
345
+ }
346
+ fetchSnippet += ';' ;
330
347
}
331
348
349
+ codeSnippet += fetchSnippet ;
350
+
332
351
callback ( null , codeSnippet ) ;
333
352
}
334
353
0 commit comments