|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var util = require('util'); |
| 4 | +var reducer = require('.,/../../reducer'); |
| 5 | + |
| 6 | +module.exports = function(options) { |
| 7 | + var opts = util._extend({ |
| 8 | + indent: ' ' |
| 9 | + }, options); |
| 10 | + |
| 11 | + var code = []; |
| 12 | + |
| 13 | + var reqOpts = { |
| 14 | + url: this.source.fullUrl, |
| 15 | + headers: this.source.headersObj |
| 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.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]; |
| 52 | + } |
| 53 | + reqOpts.formData[param.name].options = { |
| 54 | + filename: param.fileName, |
| 55 | + "content-type": param.contentType |
| 56 | + } |
| 57 | + } |
| 58 | + else { |
| 59 | + reqOpts.formData[param.name] = param.value; |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | + else { |
| 64 | + reqOpts.body = this.source.postData.params; |
| 65 | + } |
| 66 | + |
| 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 | + // construct cookies argument |
| 75 | + var cookies = this.source.cookies.map(function(cookie) { |
| 76 | + return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value); |
| 77 | + }); |
| 78 | + |
| 79 | + if (cookies.length) { |
| 80 | + reqOpts.headers.Cookie = cookies.join('; '); |
| 81 | + } |
| 82 | + |
| 83 | + code.push('var request = require(\'request\');'); |
| 84 | + if (fsReplace.length > 0) { |
| 85 | + code.push('var fs = require(\'fs\');'); |
| 86 | + } |
| 87 | + |
| 88 | + if(!simpleRequest) code.push(null); |
| 89 | + |
| 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 | + |
| 95 | + if(!simpleRequest) code.push(options); |
| 96 | + |
| 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);'); |
| 104 | + |
| 105 | + code.push(null); |
| 106 | + |
| 107 | + code.push('});'); |
| 108 | + |
| 109 | + code.push(null); |
| 110 | + |
| 111 | + return code.join('\n'); |
| 112 | +}; |
| 113 | + |
| 114 | +module.exports.info = { |
| 115 | + key: 'request', |
| 116 | + title: 'request', |
| 117 | + link: 'https://github.com/request/request', |
| 118 | + description: 'Simplified HTTP request client' |
| 119 | +}; |
0 commit comments