Skip to content

Commit b427e61

Browse files
author
Dhwaneet Bhatt
authored
Merge pull request #677 from postmanlabs/feature/nodejs-axios-use-async-await
Add option to use async/await in NodeJS Axios
2 parents 5ee005f + d1382f7 commit b427e61

File tree

12 files changed

+196
-597
lines changed

12 files changed

+196
-597
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
"object-curly-newline": "off",
230230
"object-curly-spacing": "off",
231231
"object-property-newline": "off",
232-
"one-var": ["error", "always"],
232+
"one-var": ["error", "consecutive"],
233233
"one-var-declaration-per-line": "error",
234234
"operator-assignment": "error",
235235
"operator-linebreak": ["error", "after"],

codegens/nodejs-axios/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Convert function will take three parameters
1818
* `requestTimeout` : Integer denoting time after which the request will bail out in milli-seconds
1919
* `trimRequestBody` : Trim request body fields
2020
* `followRedirect` : Boolean denoting whether to redirect a request
21+
* `asyncAwaitEnabled` : Boolean denoting whether to use async/await syntax
2122

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

@@ -27,7 +28,7 @@ var request = new sdk.Request('www.google.com'), //using postman sdk to create
2728
options = {
2829
indentType: 'Space',
2930
indentCount: 2,
30-
ES6_enabled: true
31+
asyncAwaitEnabled: true
3132
};
3233
convert(request, options, function(error, snippet) {
3334
if (error) {

codegens/nodejs-axios/lib/axios.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
*/
1515
function 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

Comments
 (0)