-
Notifications
You must be signed in to change notification settings - Fork 360
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
Merged
Merged
Feat/axios nodejs #232
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f2d40e8
feat(nodejs-axios): Added nodejs axios code generation
9cdcaad
updates readme
r3dsm0k3 acf7c92
better formatting for the header object in the generated snippet
r3dsm0k3 68bbf2e
reverts unrelated changes.
r3dsm0k3 a54e54e
Removes unwanted commented code and additional options
someshkoli d12d44e
Merge branch 'develop' of https://github.com/r3dsm0k3/postman-code-ge…
someshkoli 607c245
Adds ES6_ENABLE option to nodejs-axios codegen
someshkoli 9897296
Adds `transfer-encoding` to headers to delete
someshkoli 93ff66f
Snippet sanitization
someshkoli 79127b6
Snippet Sanitization
someshkoli 2e3ad20
Snippet sanitization(1)
someshkoli 7f3561e
Snippet sanitization(2)
someshkoli 593a716
reverts the collection to the develop branch
r3dsm0k3 53c4964
adds dummy file to the ignore list
r3dsm0k3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,4 +66,5 @@ typings/ | |
|
||
out/ | ||
|
||
newmanResponses.json | ||
newmanResponses.json | ||
dummyFile*.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
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 = !_.isEmpty(body) ? parseRequest.parseBody(body, | ||
options.trimRequestBody, | ||
indent, | ||
request.headers.get('Content-Type'), | ||
options.ES6_enabled) : ''; | ||
snippet += dataSnippet + '\n'; | ||
|
||
configArray.push(indentString + `method: '${request.method.toLowerCase()}'`); | ||
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(`${indentString}...data.getHeaders()`); | ||
} | ||
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'; | ||
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 | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
convert: require('./axios').convert, | ||
getOptions: require('./axios').getOptions | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.