From 00a9129b856aff74ffe0286780754265947bc2cd Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Thu, 16 Jul 2015 13:32:35 +0500 Subject: [PATCH] Add helpers for stream API --- index.js | 12 ++++++++++-- readme.md | 2 +- test/test-stream.js | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ff4e194..44f453a 100644 --- a/index.js +++ b/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; diff --git a/readme.md b/readme.md index 41803a3..8a72383 100644 --- a/readme.md +++ b/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 diff --git a/test/test-stream.js b/test/test-stream.js index c11a17b..5cf57ac 100644 --- a/test/test-stream.js +++ b/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'); })