|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | var util = require('util'); |
4 | | -var reducer = require('.,/../../reducer'); |
| 4 | +var path = require('path'); |
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 | | - var code = []; |
| 11 | + var code = ['var request = require("request");', null]; |
12 | 12 |
|
13 | 13 | var reqOpts = { |
14 | | - url: this.source.fullUrl, |
15 | | - headers: this.source.headersObj |
| 14 | + method: this.source.method, |
| 15 | + url: this.source.url |
16 | 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 | | - }, {}); |
| 17 | + |
| 18 | + if (Object.keys(this.source.queryObj).length) { |
| 19 | + reqOpts.qs = this.source.queryObj; |
23 | 20 | } |
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; |
| 21 | + |
| 22 | + if (Object.keys(this.source.headersObj).length) { |
| 23 | + reqOpts.headers = this.source.headersObj; |
34 | 24 | } |
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.fileName) { |
40 | | - if (!param.value.length) { |
41 | | - param.value = 'fs.createReadStream(\'' + param.fileName + '\')'; |
42 | | - fsReplace.push(param.value); |
43 | | - } |
44 | | - reqOpts.formData[param.name].value = param.value; |
45 | | - if (param.fileName.indexOf('/') > -1) { |
46 | | - param.fileName = param.fileName.split('/'); |
47 | | - param.fileName = param.fileName[param.fileName.length - 1]; |
48 | | - } |
49 | | - else if (param.fileName.indexOf('\\') > -1) { |
50 | | - param.fileName = param.fileName.split('\\'); |
51 | | - param.fileName = param.fileName[param.fileName.length - 1]; |
| 25 | + |
| 26 | + var includeFS = false; |
| 27 | + |
| 28 | + switch (this.source.postData.mimeType) { |
| 29 | + case 'application/x-www-form-urlencoded': |
| 30 | + reqOpts.form = this.source.postData.paramsObj; |
| 31 | + break; |
| 32 | + |
| 33 | + case 'application/json': |
| 34 | + reqOpts.body = this.source.postData.jsonObj; |
| 35 | + reqOpts.json = true; |
| 36 | + break; |
| 37 | + |
| 38 | + case 'multipart/form-data': |
| 39 | + reqOpts.formData = {}; |
| 40 | + |
| 41 | + this.source.postData.params.forEach(function (param) { |
| 42 | + var attachement = {}; |
| 43 | + |
| 44 | + if (param.value) { |
| 45 | + attachement.value = param.value; |
| 46 | + } else if (param.fileName) { |
| 47 | + includeFS = true; |
| 48 | + attachement.value = 'fs.createReadStream("' + param.fileName + '")'; |
52 | 49 | } |
53 | | - reqOpts.formData[param.name].options = { |
54 | | - filename: param.fileName, |
55 | | - "content-type": param.contentType |
| 50 | + |
| 51 | + if (param.fileName) { |
| 52 | + var base = path.parse(param.fileName).base; |
| 53 | + |
| 54 | + attachement.options = { |
| 55 | + filename: base.length ? base : 'filename', |
| 56 | + contentType: param.contentType ? param.contentType : null |
| 57 | + }; |
56 | 58 | } |
57 | | - } |
58 | | - else { |
59 | | - reqOpts.formData[param.name] = param.value; |
60 | | - } |
61 | | - }) |
62 | | - } |
63 | | - else { |
64 | | - reqOpts.body = this.source.postData.params; |
| 59 | + |
| 60 | + reqOpts.formData[param.name] = attachement; |
| 61 | + }); |
| 62 | + break; |
| 63 | + |
| 64 | + default: |
| 65 | + reqOpts.body = this.source.postData.text; |
65 | 66 | } |
66 | 67 |
|
67 | | - var simpleRequest = |
68 | | - this.source.cookies.length === 0 && |
69 | | - this.source.headers.length === 0 && ( |
70 | | - !this.source.postData.params || |
71 | | - this.source.postData.params.length === 0 |
72 | | - ); |
73 | | - |
74 | 68 | // construct cookies argument |
75 | | - var cookies = this.source.cookies.map(function(cookie) { |
76 | | - return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value); |
77 | | - }); |
| 69 | + if (this.source.cookies.length) { |
| 70 | + reqOpts.jar = 'JAR'; |
78 | 71 |
|
79 | | - if (cookies.length) { |
80 | | - reqOpts.headers.Cookie = cookies.join('; '); |
81 | | - } |
| 72 | + code.push(null); |
| 73 | + code.push('var jar = request.jar();'); |
82 | 74 |
|
83 | | - code.push('var request = require(\'request\');'); |
84 | | - if (fsReplace.length > 0) { |
85 | | - code.push('var fs = require(\'fs\');'); |
86 | | - } |
| 75 | + var url = this.source.url; |
87 | 76 |
|
88 | | - if(!simpleRequest) code.push(null); |
| 77 | + this.source.cookies.map(function (cookie) { |
| 78 | + code.push(util.format('jar.setCookie(request.cookie("%s=%s"), "%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)); |
| 79 | + }); |
| 80 | + code.push(null); |
| 81 | + } |
89 | 82 |
|
90 | | - var options = !simpleRequest ? util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)) : null; |
91 | | - fsReplace.forEach(function(fsToReplace) { |
92 | | - options = options.replace('"' + fsToReplace + '"', fsToReplace); |
93 | | - }) |
94 | 83 |
|
95 | | - if(!simpleRequest) code.push(options); |
| 84 | + if (includeFS) { |
| 85 | + code.push('var fs = require("fs");'); |
| 86 | + } |
96 | 87 |
|
97 | | - code.push(null); |
98 | | - var requestLine = 'request.'; |
99 | | - requestLine += this.source.method === 'DELETE' ? 'del(' : this.source.method.toLowerCase() + '('; |
100 | | - requestLine += simpleRequest ? '\'' + this.source.fullUrl + '\'' : 'options'; |
101 | | - requestLine += ', function(error, response, body){'; |
102 | | - code.push(requestLine); |
103 | | - code.push(opts.indent + 'if(error) throw new Error(error);'); |
| 88 | + var content = JSON.stringify(reqOpts, null, opts.indent) |
| 89 | + .replace('"JAR"', 'jar') |
| 90 | + .replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")'); |
104 | 91 |
|
| 92 | + code.push(util.format('request(%s, %s', content, 'function (error, response, body) {')); |
| 93 | + code.push(opts.indent + 'if (error) throw new Error(error);'); |
105 | 94 | code.push(null); |
106 | | - |
| 95 | + code.push(opts.indent + 'console.log(body);'); |
107 | 96 | code.push('});'); |
108 | | - |
109 | 97 | code.push(null); |
110 | 98 |
|
111 | 99 | return code.join('\n'); |
112 | 100 | }; |
113 | 101 |
|
114 | 102 | module.exports.info = { |
115 | 103 | key: 'request', |
116 | | - title: 'request', |
| 104 | + title: 'Request', |
117 | 105 | link: 'https://github.com/request/request', |
118 | 106 | description: 'Simplified HTTP request client' |
119 | 107 | }; |
0 commit comments