Browse Source

Add helpers for stream API

http2
Vsevolod Strukchinsky 10 years ago
parent
commit
00a9129b85
  1. 12
      index.js
  2. 2
      readme.md
  3. 2
      test/test-stream.js

12
index.js

@ -260,14 +260,16 @@ function got(url, opts, cb) {
return asPromise(opts);
}
[
var helpers = [
'get',
'post',
'put',
'patch',
'head',
'delete'
].forEach(function (el) {
];
helpers.forEach(function (el) {
got[el] = function (url, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
@ -282,4 +284,10 @@ got.stream = function (url, opts) {
return asStream(normalizeArguments(url, opts));
};
helpers.forEach(function (el) {
got.stream[el] = function (url, opts) {
return got.stream(url, objectAssign({}, opts, {method: el.toUpperCase()}));
};
});
module.exports = got;

2
readme.md

@ -46,7 +46,7 @@ got('todomvc.com')
got.stream('todomvc.com').pipe(fs.createWriteStream('index.html'));
// For POST, PUT and PATCH methods got.stream returns a WritableStream
fs.createReadStream('index.html').pipe(got.stream('todomvc.com', {method: 'POST'}));
fs.createReadStream('index.html').pipe(got.stream.post('todomvc.com'));
```
### API

2
test/test-stream.js

@ -44,7 +44,7 @@ test('return readable stream', function (t) {
test('return writeable stream', function (t) {
t.plan(1);
got.stream(s.url + '/post', {method: 'POST'})
got.stream.post(s.url + '/post')
.on('data', function (data) {
t.equal(data.toString(), 'wow');
})

Loading…
Cancel
Save