httpsnippet
Advanced tools
Comparing version 1.0.0-beta.9 to 1.0.0-beta.10
{ | ||
"version": "1.0.0-beta.9", | ||
"version": "1.0.0-beta.10", | ||
"name": "httpsnippet", | ||
@@ -4,0 +4,0 @@ "description": "HTTP Request snippet generator for *most* languages", |
@@ -21,3 +21,3 @@ 'use strict'; | ||
this.source.postData = this.source.postData || {}; | ||
this.source.postData.mimeType = this.source.postData.mimeType || 'application/x-www-form-urlencoded'; | ||
this.source.postData.mimeType = this.source.postData.mimeType || 'application/octet-stream'; | ||
@@ -36,3 +36,24 @@ this.source.bodySize = 0; | ||
this.source.headersObj = {}; | ||
this.source.postData.jsonObj = {}; | ||
this.source.postData.paramsObj = {}; | ||
switch (this.source.postData.mimeType) { | ||
case 'application/x-www-form-urlencoded': | ||
if (!this.source.postData.params) { | ||
this.source.postData.text = ''; | ||
} else { | ||
this.source.postData.paramsObj = this.source.postData.params.reduce(reducer, {}); | ||
// always overwrite | ||
this.source.postData.text = qs.stringify(this.source.postData.paramsObj); | ||
} | ||
break; | ||
case 'application/json': | ||
if (this.source.postData.text) { | ||
this.source.postData.jsonObj = JSON.parse(this.source.postData.text); | ||
} | ||
break; | ||
} | ||
// construct query objects | ||
@@ -39,0 +60,0 @@ if (this.source.queryString && this.source.queryString.length) { |
@@ -56,2 +56,3 @@ 'use strict'; | ||
code.push(opts.indent + opts.indent + 'var body = Buffer.concat(chunks);'); | ||
code.push(opts.indent + opts.indent + 'console.log(body);'); | ||
code.push(opts.indent + '});'); | ||
@@ -66,17 +67,17 @@ code.push('});'); | ||
if (!this.source.postData.text && this.source.postData.params) { | ||
if (this.source.postData.mimeType === 'application/x-www-form-urlencoded') { | ||
var postData = this.source.postData.params.reduce(reducer, {}); | ||
// if (!this.source.postData.text && this.source.postData.params) { | ||
// if (this.source.postData.mimeType === 'application/x-www-form-urlencoded') { | ||
// var postData = this.source.postData.params.reduce(reducer, {}); | ||
code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData))); | ||
code.push(util.format('req.write(postData);')); | ||
} | ||
// code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData))); | ||
// code.push(util.format('req.write(postData);')); | ||
// } | ||
if (this.source.postData.mimeType === 'multipart/form-data') { | ||
postData = this.source.postData.params.reduce(reducer, {}); | ||
// if (this.source.postData.mimeType === 'multipart/form-data') { | ||
// var postData = this.source.postData.params.reduce(reducer, {}); | ||
code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData))); | ||
code.push(util.format('req.write(postData);')); | ||
} | ||
} | ||
// code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData))); | ||
// code.push(util.format('req.write(postData);')); | ||
// } | ||
// } | ||
@@ -83,0 +84,0 @@ code.push('req.end();'); |
'use strict'; | ||
var util = require('util'); | ||
var reducer = require('.,/../../reducer'); | ||
var path = require('path'); | ||
module.exports = function(options) { | ||
module.exports = function (options) { | ||
var opts = util._extend({ | ||
@@ -11,100 +11,88 @@ indent: ' ' | ||
var code = []; | ||
var code = ['var request = require("request");', null]; | ||
var reqOpts = { | ||
url: this.source.fullUrl, | ||
headers: this.source.headersObj | ||
method: this.source.method, | ||
url: this.source.url | ||
}; | ||
var fsReplace = []; | ||
if (this.source.headersObj['Content-Type'] === 'application/x-www-form-urlencoded') { | ||
reqOpts.formData = this.source.postData.params.reduce(function(finalObj, thisParam) { | ||
finalObj[thisParam.name] = thisParam.value; | ||
return finalObj; | ||
}, {}); | ||
if (Object.keys(this.source.queryObj).length) { | ||
reqOpts.qs = this.source.queryObj; | ||
} | ||
else if (this.source.headersObj['Content-Type'] === 'application/json') { | ||
if (this.source.postData.params) { | ||
reqOpts.body = JSON.stringify( | ||
this.source.postData.params.reduce(function(finalObj, thisParam) { | ||
finalObj[thisParam.name] = thisParam.value; | ||
return finalObj; | ||
}, {}) | ||
); | ||
} | ||
reqOpts.json = true; | ||
if (Object.keys(this.source.headersObj).length) { | ||
reqOpts.headers = this.source.headersObj; | ||
} | ||
else if (this.source.headersObj["Content-Type"] === "multipart/form-data") { | ||
this.source.postData.params.forEach(function(param) { | ||
reqOpts.formData = reqOpts.formData || {}; | ||
reqOpts.formData[param.name] = {}; | ||
if (param.fileName) { | ||
if (!param.value.length) { | ||
param.value = 'fs.createReadStream(\'' + param.fileName + '\')'; | ||
fsReplace.push(param.value); | ||
var includeFS = false; | ||
switch (this.source.postData.mimeType) { | ||
case 'application/x-www-form-urlencoded': | ||
reqOpts.form = this.source.postData.paramsObj; | ||
break; | ||
case 'application/json': | ||
reqOpts.body = this.source.postData.jsonObj; | ||
reqOpts.json = true; | ||
break; | ||
case 'multipart/form-data': | ||
reqOpts.formData = {}; | ||
this.source.postData.params.forEach(function (param) { | ||
var attachement = {}; | ||
if (param.value) { | ||
attachement.value = param.value; | ||
} else if (param.fileName) { | ||
includeFS = true; | ||
attachement.value = 'fs.createReadStream("' + param.fileName + '")'; | ||
} | ||
reqOpts.formData[param.name].value = param.value; | ||
if (param.fileName.indexOf('/') > -1) { | ||
param.fileName = param.fileName.split('/'); | ||
param.fileName = param.fileName[param.fileName.length - 1]; | ||
if (param.fileName) { | ||
var base = path.parse(param.fileName).base; | ||
attachement.options = { | ||
filename: base.length ? base : 'filename', | ||
contentType: param.contentType ? param.contentType : null | ||
}; | ||
} | ||
else if (param.fileName.indexOf('\\') > -1) { | ||
param.fileName = param.fileName.split('\\'); | ||
param.fileName = param.fileName[param.fileName.length - 1]; | ||
} | ||
reqOpts.formData[param.name].options = { | ||
filename: param.fileName, | ||
"content-type": param.contentType | ||
} | ||
} | ||
else { | ||
reqOpts.formData[param.name] = param.value; | ||
} | ||
}) | ||
reqOpts.formData[param.name] = attachement; | ||
}); | ||
break; | ||
default: | ||
reqOpts.body = this.source.postData.text; | ||
} | ||
else { | ||
reqOpts.body = this.source.postData.params; | ||
} | ||
var simpleRequest = | ||
this.source.cookies.length === 0 && | ||
this.source.headers.length === 0 && ( | ||
!this.source.postData.params || | ||
this.source.postData.params.length === 0 | ||
); | ||
// construct cookies argument | ||
var cookies = this.source.cookies.map(function(cookie) { | ||
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value); | ||
}); | ||
if (this.source.cookies.length) { | ||
reqOpts.jar = 'JAR'; | ||
if (cookies.length) { | ||
reqOpts.headers.Cookie = cookies.join('; '); | ||
} | ||
code.push(null); | ||
code.push('var jar = request.jar();'); | ||
code.push('var request = require(\'request\');'); | ||
if (fsReplace.length > 0) { | ||
code.push('var fs = require(\'fs\');'); | ||
var url = this.source.url; | ||
this.source.cookies.map(function (cookie) { | ||
code.push(util.format('jar.setCookie(request.cookie("%s=%s"), "%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)); | ||
}); | ||
code.push(null); | ||
} | ||
if(!simpleRequest) code.push(null); | ||
var options = !simpleRequest ? util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)) : null; | ||
fsReplace.forEach(function(fsToReplace) { | ||
options = options.replace('"' + fsToReplace + '"', fsToReplace); | ||
}) | ||
if (includeFS) { | ||
code.push('var fs = require("fs");'); | ||
} | ||
if(!simpleRequest) code.push(options); | ||
var content = JSON.stringify(reqOpts, null, opts.indent) | ||
.replace('"JAR"', 'jar') | ||
.replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")'); | ||
code.push(util.format('request(%s, %s', content, 'function (error, response, body) {')); | ||
code.push(opts.indent + 'if (error) throw new Error(error);'); | ||
code.push(null); | ||
var requestLine = 'request.'; | ||
requestLine += this.source.method === 'DELETE' ? 'del(' : this.source.method.toLowerCase() + '('; | ||
requestLine += simpleRequest ? '\'' + this.source.fullUrl + '\'' : 'options'; | ||
requestLine += ', function(error, response, body){'; | ||
code.push(requestLine); | ||
code.push(opts.indent + 'if(error) throw new Error(error);'); | ||
code.push(null); | ||
code.push(opts.indent + 'console.log(body);'); | ||
code.push('});'); | ||
code.push(null); | ||
@@ -117,5 +105,5 @@ | ||
key: 'request', | ||
title: 'request', | ||
title: 'Request', | ||
link: 'https://github.com/request/request', | ||
description: 'Simplified HTTP request client' | ||
}; |
@@ -39,2 +39,12 @@ 'use strict'; | ||
if (this.source.postData.mimeType === 'multipart/form-data') { | ||
this.source.postData.params.forEach(function (param) { | ||
if (param.value) { | ||
code.push(util.format('--body-data %s', JSON.stringify(param.value))); | ||
} else if (param.fileName) { | ||
code.push(util.format('--body-file "%s"', param.fileName)); | ||
} | ||
}); | ||
} | ||
code.push(opts.short ? '-O' : '--output-document'); | ||
@@ -41,0 +51,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44266
712