-
Notifications
You must be signed in to change notification settings - Fork 358
Feat/axios nodejs #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/axios nodejs #232
Changes from 8 commits
f2d40e8
9cdcaad
acf7c92
68bbf2e
a54e54e
d12d44e
607c245
9897296
93ff66f
79127b6
2e3ad20
7f3561e
593a716
53c4964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
.DS_Store | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# temporarily generated file | ||
run.js | ||
|
||
# Prevent IDE stuff | ||
.idea | ||
.vscode | ||
*.sublime-* | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
.coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
out/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# codegen-nodejs-axios | ||
|
||
> Converts Postman-SDK Request into code snippet for NodeJS-axios. | ||
|
||
#### Prerequisites | ||
To run the module, ensure that you have NodeJS >= v8. A copy of the NodeJS installable can be downloaded from https://nodejs.org/en/download/package-manager. | ||
|
||
## Using the Module | ||
The module will expose an object which will have property `convert` which is the function for converting the Postman-SDK request to nodejs-axios code snippet and `getOptions` function which returns an array of supported options. | ||
|
||
### convert function | ||
Convert function will take three parameters | ||
* `request`- Postman-SDK Request object | ||
|
||
* `options`- options is an object which can have following properties | ||
* `indentType`- String denoting type of indentation for code snippet. eg: 'Space', 'Tab' | ||
* `indentCount`- positiveInteger representing count of indentation required. | ||
* `requestTimeout` : Integer denoting time after which the request will bail out in milli-seconds | ||
* `trimRequestBody` : Trim request body fields | ||
* `followRedirect` : Boolean denoting whether to redirect a request | ||
|
||
* `callback`- callback function with first parameter as error and second parameter as string for code snippet | ||
|
||
##### Example: | ||
```js | ||
var request = new sdk.Request('www.google.com'), //using postman sdk to create request | ||
options = { | ||
indentType: 'Space', | ||
indentCount: 2, | ||
ES6_enabled: true | ||
}; | ||
convert(request, options, function(error, snippet) { | ||
if (error) { | ||
// handle error | ||
} | ||
// handle snippet | ||
}); | ||
``` | ||
|
||
### getOptions function | ||
|
||
This function returns a list of options supported by this codegen. | ||
|
||
#### Example | ||
```js | ||
var options = getOptions(); | ||
|
||
console.log(options); | ||
// output | ||
// [ | ||
// { | ||
// name: 'Set indentation count', | ||
// id: 'indentCount', | ||
// type: 'positiveInteger', | ||
// default: 2, | ||
// description: 'Set the number of indentation characters to add per code level' | ||
// }, | ||
// ... | ||
// ] | ||
``` | ||
|
||
### Guideline for using generated snippet | ||
* Generated snippet requires `axios`, `form-data`, `qs` and `fs` modules. | ||
|
||
* Since Postman-SDK Request object doesn't provide complete path of the file, it needs to be manually inserted in case of uploading a file. | ||
|
||
* This module doesn't support cookies. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
const _ = require('./lodash'), | ||
parseRequest = require('./parseRequest'), | ||
sanitize = require('./util').sanitize, | ||
sanitizeOptions = require('./util').sanitizeOptions, | ||
addFormParam = require('./util').addFormParam; | ||
|
||
/** | ||
* retuns snippet of nodejs(axios) by parsing data from Postman-SDK request object | ||
* | ||
* @param {Object} request - Postman SDK request object | ||
* @param {String} indentString - indentation required for code snippet | ||
* @param {Object} options | ||
* @returns {String} - nodejs(axios) code snippet for given request object | ||
*/ | ||
function makeSnippet (request, indentString, options) { | ||
|
||
var snippet, | ||
configArray = [], | ||
dataSnippet = '', | ||
body, | ||
headers, | ||
indentType = options.indentType === 'Tab' ? '\t' : ' ', | ||
indent = indentType.repeat(options.indentCount), | ||
varDeclare = options.ES6_enabled ? 'const' : 'var'; | ||
|
||
snippet = varDeclare + ' axios = require(\'axios\');\n'; | ||
if (request.body && !request.headers.has('Content-Type')) { | ||
if (request.body.mode === 'file') { | ||
request.addHeader({ | ||
key: 'Content-Type', | ||
value: 'text/plain' | ||
}); | ||
} | ||
else if (request.body.mode === 'graphql') { | ||
request.addHeader({ | ||
key: 'Content-Type', | ||
value: 'application/json' | ||
}); | ||
} | ||
} | ||
|
||
// The following code handles multiple files in the same formdata param. | ||
// It removes the form data params where the src property is an array of filepath strings | ||
// Splits that array into different form data params with src set as a single filepath string | ||
if (request.body && request.body.mode === 'formdata') { | ||
let formdata = request.body.formdata, | ||
formdataArray = []; | ||
formdata.members.forEach((param) => { | ||
let key = param.key, | ||
type = param.type, | ||
disabled = param.disabled, | ||
contentType = param.contentType; | ||
// check if type is file or text | ||
if (type === 'file') { | ||
// if src is not of type string we check for array(multiple files) | ||
if (typeof param.src !== 'string') { | ||
// if src is an array(not empty), iterate over it and add files as separate form fields | ||
if (Array.isArray(param.src) && param.src.length) { | ||
param.src.forEach((filePath) => { | ||
addFormParam(formdataArray, key, param.type, filePath, disabled, contentType); | ||
}); | ||
} | ||
// if src is not an array or string, or is an empty array, add a placeholder for file path(no files case) | ||
else { | ||
addFormParam(formdataArray, key, param.type, '/path/to/file', disabled, contentType); | ||
} | ||
} | ||
// if src is string, directly add the param with src as filepath | ||
else { | ||
addFormParam(formdataArray, key, param.type, param.src, disabled, contentType); | ||
} | ||
} | ||
// if type is text, directly add it to formdata array | ||
else { | ||
addFormParam(formdataArray, key, param.type, param.value, disabled, contentType); | ||
} | ||
}); | ||
|
||
request.body.update({ | ||
mode: 'formdata', | ||
formdata: formdataArray | ||
}); | ||
} | ||
|
||
body = request.body && request.body.toJSON(); | ||
|
||
dataSnippet = parseRequest.parseBody(body, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only add the body if it is non empty object, and the mode that it states has non empty data There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
options.trimRequestBody, | ||
indent, | ||
request.headers.get('Content-Type'), | ||
options.ES6_enabled); | ||
snippet += dataSnippet+'\n'; | ||
|
||
configArray.push(indentString + `'method': '${request.method.toLowerCase()}'`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need of adding single quotes around the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
configArray.push(indentString + `'url': '${sanitize(request.url.toString())}'`); | ||
|
||
headers = parseRequest.parseHeader(request, indentString) | ||
// https://github.com/axios/axios/issues/789#issuecomment-577177492 | ||
if (!_.isEmpty(body) && body.formdata ) { | ||
// we can assume that data object is filled up | ||
headers.push(`\n${indentString}...data.getHeaders()`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't really need to add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the snippet generated when request has some headers, and this formdata header is also added. The indentation looks weird, can you check on it once?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
let headerSnippet = indentString + "'headers': { " | ||
if(headers.length >0 ) { | ||
headerSnippet += "\n" | ||
headerSnippet += indentString+ headers.join(', \n'+indentString) + "\n" | ||
headerSnippet += indentString + '}' | ||
} else { | ||
headerSnippet += '}' | ||
} | ||
|
||
configArray.push(headerSnippet); | ||
|
||
if (options.requestTimeout) { | ||
configArray.push(indentString + `timeout: ${options.requestTimeout}`); | ||
} | ||
if (options.followRedirect === false) { | ||
// setting the maxRedirects to 0 will disable any redirects. | ||
// by default, maxRedirects are set to 5 | ||
configArray.push(indentString + 'maxRedirects: 0'); | ||
} | ||
if (dataSnippet !== '') { | ||
// although just data is enough, whatever :shrug: | ||
configArray.push(indentString +`data : data`) | ||
} | ||
snippet += varDeclare + ' config = {\n'; | ||
snippet += configArray.join(',\n') + '\n'; | ||
snippet += '}\n'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a semicolon and a new line after the closing bracket. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
snippet += 'axios(config)\n'; | ||
snippet += '.then(function (response) {\n'; | ||
snippet += indentString + 'console.log(JSON.stringify(response.data));\n'; | ||
snippet += '})\n'; | ||
snippet += '.catch(function (error) {\n'; | ||
snippet += indentString + 'console.log(error);\n'; | ||
snippet += '});\n'; | ||
|
||
return snippet; | ||
} | ||
|
||
/** | ||
* Used to get the options specific to this codegen | ||
* | ||
* @returns {Array} - Returns an array of option objects | ||
*/ | ||
function getOptions () { | ||
return [ | ||
{ | ||
name: 'Set indentation count', | ||
id: 'indentCount', | ||
type: 'positiveInteger', | ||
default: 2, | ||
description: 'Set the number of indentation characters to add per code level' | ||
}, | ||
{ | ||
name: 'Set indentation type', | ||
id: 'indentType', | ||
type: 'enum', | ||
availableOptions: ['Tab', 'Space'], | ||
default: 'Space', | ||
description: 'Select the character used to indent lines of code' | ||
}, | ||
{ | ||
name: 'Set request timeout', | ||
id: 'requestTimeout', | ||
type: 'positiveInteger', | ||
default: 0, | ||
description: 'Set number of milliseconds the request should wait for a response' + | ||
' before timing out (use 0 for infinity)' | ||
}, | ||
{ | ||
name: 'Follow redirects', | ||
id: 'followRedirect', | ||
type: 'boolean', | ||
default: true, | ||
description: 'Automatically follow HTTP redirects' | ||
}, | ||
{ | ||
name: 'Trim request body fields', | ||
id: 'trimRequestBody', | ||
type: 'boolean', | ||
default: false, | ||
description: 'Remove white space and additional lines that may affect the server\'s response' | ||
}, | ||
{ | ||
name: 'Enable ES6 features', | ||
id: 'ES6_enabled', | ||
type: 'boolean', | ||
default: false, | ||
description: 'Modifies code snippet to incorporate ES6 (EcmaScript) features' | ||
} | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* Converts Postman sdk request object to nodejs axios code snippet | ||
* | ||
* @param {Object} request - postman-SDK request object | ||
* @param {Object} options | ||
* @param {String} options.indentType - type for indentation eg: Space, Tab | ||
* @param {String} options.indentCount - number of spaces or tabs for indentation. | ||
* @param {Boolean} options.followRedirect - whether to enable followredirect | ||
* @param {Boolean} options.trimRequestBody - whether to trim fields in request body or not | ||
* @param {Number} options.requestTimeout : time in milli-seconds after which request will bail out | ||
* @param {Function} callback - callback function with parameters (error, snippet) | ||
*/ | ||
function convert (request, options, callback) { | ||
if (!_.isFunction(callback)) { | ||
throw new Error('NodeJS-Axios-Converter: callback is not valid function'); | ||
} | ||
options = sanitizeOptions(options, getOptions()); | ||
|
||
// String representing value of indentation required | ||
var indentString; | ||
|
||
indentString = options.indentType === 'Tab' ? '\t' : ' '; | ||
indentString = indentString.repeat(options.indentCount); | ||
|
||
return callback(null, makeSnippet(request, indentString, options)); | ||
} | ||
|
||
module.exports = { | ||
convert: convert, | ||
getOptions: getOptions | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
convert: require('./axios').convert, | ||
getOptions: require('./axios').getOptions | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for es_6 enable option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than removing the option, it would be great to have that as an option and add support for it in the code.