got
Advanced tools
Comparing version 2.1.0 to 2.2.0
49
index.js
@@ -6,3 +6,3 @@ 'use strict'; | ||
var zlib = require('zlib'); | ||
var PassThrough = require('stream').PassThrough; | ||
var duplexify = require('duplexify'); | ||
var assign = require('object-assign'); | ||
@@ -12,3 +12,3 @@ var read = require('read-all-stream'); | ||
module.exports = function (url, opts, cb) { | ||
function got(url, opts, cb) { | ||
if (typeof opts === 'function') { | ||
@@ -30,4 +30,4 @@ // if `cb` has been specified but `opts` has not | ||
if (body && opts.method === undefined) { | ||
opts.method = 'POST'; | ||
if (body) { | ||
opts.method = opts.method || 'POST'; | ||
} | ||
@@ -39,3 +39,3 @@ | ||
if (!cb) { | ||
proxy = new PassThrough(); | ||
proxy = duplexify(); | ||
@@ -93,3 +93,3 @@ // forward errors on the stream | ||
if (proxy) { | ||
res.on('error', proxy.emit.bind(proxy, 'error')).pipe(proxy); | ||
proxy.setReadable(res); | ||
return; | ||
@@ -105,8 +105,20 @@ } | ||
if (!body) { | ||
req.end(); | ||
if (!proxy) { | ||
req.end(body); | ||
return; | ||
} | ||
req.write(body); | ||
if (body) { | ||
proxy.write = function () { | ||
throw new Error('got\'s stream is not writable when options.body is used'); | ||
}; | ||
req.end(body); | ||
return; | ||
} | ||
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') { | ||
proxy.setWritable(req); | ||
return; | ||
} | ||
req.end(); | ||
@@ -118,2 +130,19 @@ }; | ||
return proxy; | ||
}; | ||
} | ||
[ | ||
'get', | ||
'post', | ||
'put', | ||
'patch', | ||
'head', | ||
'delete' | ||
].forEach(function (el) { | ||
got[el] = function (url, opts, cb) { | ||
opts = opts || {}; | ||
opts.method = el.toUpperCase(); | ||
return got(url, opts, cb); | ||
}; | ||
}); | ||
module.exports = got; |
{ | ||
"name": "got", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Simplified HTTP/HTTPS requests", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha --timeout 50000" | ||
}, | ||
@@ -35,2 +35,3 @@ "files": [ | ||
"dependencies": { | ||
"duplexify": "^3.2.0", | ||
"object-assign": "^2.0.0", | ||
@@ -37,0 +38,0 @@ "read-all-stream": "^0.1.0", |
@@ -30,4 +30,8 @@ # got [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) | ||
// Stream mode. | ||
got('http://todomvc.com').pipe(fs.createWriteStream('index.html')); | ||
// For POST and PUT methods got returns WritableStream | ||
fs.createReadStream('index.html').pipe(got.post('http://todomvc.com')); | ||
``` | ||
@@ -67,2 +71,4 @@ | ||
This option and stream mode are mutually exclusive. | ||
##### options.timeout | ||
@@ -88,3 +94,12 @@ | ||
#### got.get(url, [options], [callback]) | ||
#### got.post(url, [options], [callback]) | ||
#### got.put(url, [options], [callback]) | ||
#### got.patch(url, [options], [callback]) | ||
#### got.head(url, [options], [callback]) | ||
#### got.delete(url, [options], [callback]) | ||
Sets `options.method` to the method name and makes a request. | ||
## Related | ||
@@ -91,0 +106,0 @@ |
6110
116
110
4
+ Addedduplexify@^3.2.0
+ Addedcore-util-is@1.0.3(transitive)
+ Addedduplexify@3.7.1(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstream-shift@1.0.3(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)