|
3 | 3 | var util = require('util'); |
4 | 4 | var reducer = require('.,/../../reducer'); |
5 | 5 |
|
6 | | -module.exports = function (options) { |
| 6 | +module.exports = function(options) { |
7 | 7 | var opts = util._extend({ |
8 | 8 | indent: ' ' |
9 | 9 | }, options); |
10 | 10 |
|
11 | 11 | var code = []; |
12 | 12 |
|
13 | 13 | var reqOpts = { |
14 | | - method: this.source.method, |
15 | | - hostname: this.source.uriObj.hostname, |
16 | | - port: this.source.uriObj.port, |
17 | | - path: this.source.uriObj.path, |
| 14 | + url: this.source.fullUrl, |
18 | 15 | headers: this.source.headersObj |
19 | 16 | }; |
| 17 | + var fsReplace = []; |
| 18 | + if (this.source.headersObj['Content-Type'] === 'application/x-www-form-urlencoded') { |
| 19 | + reqOpts.formData = this.source.postData.params.reduce(function(finalObj, thisParam) { |
| 20 | + finalObj[thisParam.name] = thisParam.value; |
| 21 | + return finalObj; |
| 22 | + }, {}); |
| 23 | + } |
| 24 | + else if (this.source.headersObj['Content-Type'] === 'application/json') { |
| 25 | + if (this.source.postData.params) { |
| 26 | + reqOpts.body = JSON.stringify( |
| 27 | + this.source.postData.params.reduce(function(finalObj, thisParam) { |
| 28 | + finalObj[thisParam.name] = thisParam.value; |
| 29 | + return finalObj; |
| 30 | + }, {}) |
| 31 | + ); |
| 32 | + } |
| 33 | + reqOpts.json = true; |
| 34 | + } |
| 35 | + else if(this.source.headersObj["Content-Type"] === "multipart/form-data"){ |
| 36 | + this.source.postData.params.forEach(function(param){ |
| 37 | + reqOpts.formData = reqOpts.formData || {}; |
| 38 | + reqOpts.formData[param.name] = {}; |
| 39 | + if (!param.value.length){ |
| 40 | + param.value = 'fs.createReadStream(\''+param.fileName+'\')'; |
| 41 | + fsReplace.push(param.value); |
| 42 | + reqOpts.formData[param.name].value =param.value; |
| 43 | + if(param.fileName.indexOf('/')>-1){ |
| 44 | + param.fileName = param.fileName.split('/'); |
| 45 | + param.fileName = param.fileName[param.fileName.length-1]; |
| 46 | + } |
| 47 | + else if(param.fileName.indexOf('\\')>-1){ |
| 48 | + param.fileName = param.fileName.split('\\'); |
| 49 | + param.fileName = param.fileName[param.fileName.length-1]; |
| 50 | + } |
| 51 | + reqOpts.formData[param.name].options = { |
| 52 | + filename:param.fileName, |
| 53 | + "content-type":param.contentType |
| 54 | + } |
| 55 | + } |
| 56 | + else{ |
| 57 | + reqOpts.formData[param.name] = param.value; |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | + else { |
| 62 | + reqOpts.body = this.source.postData.params; |
| 63 | + } |
| 64 | + var simpleRequest = this.source.bodySize === 0 && this.source.headerSize === 0; |
20 | 65 |
|
21 | 66 | // construct cookies argument |
22 | | - var cookies = this.source.cookies.map(function (cookie) { |
| 67 | + var cookies = this.source.cookies.map(function(cookie) { |
23 | 68 | return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value); |
24 | 69 | }); |
25 | 70 |
|
26 | 71 | if (cookies.length) { |
27 | 72 | reqOpts.headers.Cookie = cookies.join('; '); |
28 | 73 | } |
29 | 74 |
|
30 | | - code.push('var request = require("request");'); |
31 | | - |
32 | | - if (!this.source.postData.text && this.source.postData.params) { |
33 | | - code.push('var querystring = require("querystring");'); |
| 75 | + code.push('var request = require(\'request\');'); |
| 76 | + if(fsReplace.length>0){ |
| 77 | + code.push('var fs = require(\'fs\');'); |
34 | 78 | } |
35 | 79 |
|
36 | 80 | code.push(null); |
37 | 81 |
|
38 | | - code.push(util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent))); |
39 | | - |
40 | | - code.push(null); |
41 | | - |
42 | | - code.push('var req = http.request(options, function (res) {'); |
43 | | - |
44 | | - code.push(opts.indent + 'var chunks = [];'); |
| 82 | + var options = !simpleRequest ? util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)) : null; |
| 83 | + fsReplace.forEach(function(fsToReplace){ |
| 84 | + options = options.replace('"'+fsToReplace+'"',fsToReplace); |
| 85 | + }) |
| 86 | + code.push(options); |
45 | 87 |
|
46 | 88 | code.push(null); |
47 | | - |
48 | | - code.push(opts.indent + 'res.on("data", function (chunk) {'); |
49 | | - code.push(opts.indent + opts.indent + 'chunks.push(chunk);'); |
50 | | - code.push(opts.indent + '});'); |
| 89 | + var requestLine = 'request.'; |
| 90 | + requestLine += this.source.method === 'DELETE' ? 'del(' : this.source.method.toLowerCase() + '('; |
| 91 | + requestLine += simpleRequest ? '\'' + this.source.fullUrl + '\'' : 'options'; |
| 92 | + requestLine += ', function(error, response, body){'; |
| 93 | + code.push(requestLine); |
| 94 | + code.push(opts.indent + 'if(error) throw new Error(error);'); |
51 | 95 |
|
52 | 96 | code.push(null); |
53 | 97 |
|
54 | | - code.push(opts.indent + 'res.on("end", function () {'); |
55 | | - code.push(opts.indent + opts.indent + 'var body = Buffer.concat(chunks);'); |
56 | | - code.push(opts.indent + '});'); |
57 | 98 | code.push('});'); |
58 | 99 |
|
59 | 100 | code.push(null); |
60 | 101 |
|
61 | | - if (this.source.postData.text) { |
62 | | - code.push(util.format('req.write(%s);', JSON.stringify(this.source.postData.text))); |
63 | | - } |
64 | | - |
65 | | - if (!this.source.postData.text && this.source.postData.params) { |
66 | | - var postData = this.source.postData.params.reduce(reducer, {}); |
67 | | - |
68 | | - code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData))); |
69 | | - code.push(util.format('req.write(postData);')); |
70 | | - } |
71 | | - |
72 | | - code.push('req.end();'); |
73 | | - |
74 | 102 | return code.join('\n'); |
75 | 103 | }; |
76 | 104 |
|
77 | 105 | module.exports.info = { |
78 | | - key: 'native', |
79 | | - title: 'HTTP', |
80 | | - link: 'http://nodejs.org/api/http.html#http_http_request_options_callback', |
81 | | - description: 'Node.js native HTTP interface' |
| 106 | + key: 'request', |
| 107 | + title: 'request', |
| 108 | + link: 'https://github.com/request/request', |
| 109 | + description: 'Simplified HTTP request client' |
82 | 110 | }; |
0 commit comments