|
3 | 3 | var util = require('util'); |
4 | 4 | var path = require('path'); |
5 | 5 |
|
6 | | -module.exports = function (options) { |
| 6 | +module.exports = function (source, options) { |
7 | 7 | var opts = util._extend({ |
8 | 8 | indent: ' ' |
9 | 9 | }, options); |
10 | 10 |
|
| 11 | + var includeFS = false; |
11 | 12 | var code = ['var unirest = require("unirest");', null]; |
12 | | - if(this.source.cookies.length){ |
| 13 | + |
| 14 | + code.push(util.format('var req = unirest("%s", "%s");', source.method, source.url)); |
| 15 | + code.push(null); |
| 16 | + |
| 17 | + if (source.cookies.length) { |
13 | 18 | code.push('var CookieJar = unirest.jar();'); |
14 | | - var cookies = {}; |
15 | | - var url = this.source.url; |
16 | | - this.source.cookies.forEach(function(cookie){ |
17 | | - code.push(util.format('CookieJar.add("%s=%s","%s")',encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)); |
18 | | - }) |
| 19 | + |
| 20 | + source.cookies.forEach(function (cookie) { |
| 21 | + code.push(util.format('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url)); |
| 22 | + }); |
| 23 | + |
| 24 | + code.push('req.jar(CookieJar);'); |
19 | 25 | code.push(null); |
20 | 26 | } |
21 | | - code.push(util.format('unirest.%s("%s")', this.source.method.toLowerCase(), this.source.url)); |
22 | | - if (Object.keys(this.source.queryObj).length) { |
23 | | - code.push(opts.indent + util.format('.query(%s)',JSON.stringify(this.source.queryObj))) |
| 27 | + |
| 28 | + if (Object.keys(source.queryObj).length) { |
| 29 | + code.push(util.format('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent))); |
| 30 | + code.push(null); |
24 | 31 | } |
25 | | - if(this.source.cookies.length){ |
26 | | - code.push(opts.indent + '.jar(CookieJar)') |
| 32 | + |
| 33 | + if (Object.keys(source.headersObj).length) { |
| 34 | + code.push(util.format('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent))); |
| 35 | + code.push(null); |
27 | 36 | } |
28 | | - if (Object.keys(this.source.headersObj).length) { |
29 | | - var self = this; |
30 | | - var newHeadersObj = Object.keys(this.source.headersObj).reduce(function(finalHeader, headerName){ |
31 | | - if(headerName !== 'Content-Type'){ |
32 | | - finalHeader[headerName] = self.source.headersObj[headerName]; |
| 37 | + |
| 38 | + switch (source.postData.mimeType) { |
| 39 | + case 'application/x-www-form-urlencoded': |
| 40 | + if (source.postData.paramsObj) { |
| 41 | + code.push(util.format('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))); |
| 42 | + } |
| 43 | + break; |
| 44 | + |
| 45 | + case 'application/json': |
| 46 | + if (source.postData.jsonObj) { |
| 47 | + code.push('req.type("json");'); |
| 48 | + code.push(util.format('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))); |
| 49 | + } |
| 50 | + break; |
| 51 | + |
| 52 | + case 'multipart/form-data': |
| 53 | + var multipart = []; |
| 54 | + |
| 55 | + source.postData.params.forEach(function (param) { |
| 56 | + var part = {}; |
| 57 | + |
| 58 | + if (param.fileName && !param.value) { |
| 59 | + includeFS = true; |
| 60 | + |
| 61 | + part.body = 'fs.createReadStream("' + param.fileName + '")'; |
| 62 | + } else if (param.value) { |
| 63 | + part.body = param.value; |
| 64 | + } |
| 65 | + |
| 66 | + if (part.body) { |
| 67 | + if (param.contentType) { |
| 68 | + part['content-type'] = param.contentType; |
| 69 | + } |
| 70 | + |
| 71 | + multipart.push(part); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + code.push(util.format('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent))); |
| 76 | + break; |
| 77 | + |
| 78 | + default: |
| 79 | + if (source.postData.text) { |
| 80 | + code.push(opts.indent + util.format('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))); |
33 | 81 | } |
34 | | - return finalHeader; |
35 | | - }, {}) |
36 | | - if(Object.keys(newHeadersObj).length){ |
37 | | - code.push(opts.indent + util.format('.headers(%s)',JSON.stringify(newHeadersObj))); |
38 | | - } |
39 | | - } |
40 | | - if(this.source.postData.mimeType){ |
41 | | - if(this.source.postData.mimeType !== 'application/octet-stream'){ |
42 | | - code.push(opts.indent + util.format('.type("%s")',this.source.postData.mimeType)) |
43 | | - } |
44 | | - if(this.source.postData.mimeType === 'multipart/form-data'){ |
45 | | - // not sure how unirest handles multipart |
46 | | - } |
47 | 82 | } |
48 | | - if(this.source.postData.paramsObj.length){ |
49 | | - code.push(opts.indent + util.format('.send(%s)', this.source.postData.paramsObj)); |
| 83 | + |
| 84 | + if (includeFS) { |
| 85 | + code.unshift('var fs = require("fs");'); |
50 | 86 | } |
51 | | - code.push(opts.indent + '.end(function(response){') |
52 | | - code.push(opts.indent + opts.indent + 'if (response.error) throw new Error(response.error);'); |
| 87 | + |
| 88 | + code.push(null); |
| 89 | + code.push('req.end(function (res) {'); |
| 90 | + code.push(opts.indent + 'if (res.error) throw new Error(res.error);'); |
53 | 91 | code.push(null); |
54 | | - code.push(opts.indent+opts.indent+ 'console.log(response.body);'); |
55 | | - code.push(opts.indent+'});'); |
| 92 | + code.push(opts.indent + 'console.log(res.body);'); |
| 93 | + code.push('});'); |
56 | 94 | code.push(null); |
57 | 95 |
|
58 | | - return code.join('\n'); |
| 96 | + return code.join('\n').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")'); |
59 | 97 | }; |
60 | 98 |
|
61 | 99 | module.exports.info = { |
62 | 100 | key: 'unirest', |
63 | 101 | title: 'Unirest', |
64 | | - link: 'https://github.com/request/request', |
65 | | - description: 'Simplified HTTP request client.' |
| 102 | + link: 'http://unirest.io/', |
| 103 | + description: 'Lightweight HTTP Request Client Library' |
66 | 104 | }; |
0 commit comments