From 1483895159ab3c0cf014cbc7939bb0d7eebcebab Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 7 Dec 2014 23:08:56 +0700 Subject: [PATCH] implement shortcut methods for all the HTTP verbs --- index.js | 27 +++++++++++++++------------ package.json | 2 +- readme.md | 10 ++++++---- test.js | 18 ++++++++---------- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index e9c742a..49fe816 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ var assign = require('object-assign'); var read = require('read-all-stream'); var timeout = require('timed-out'); -function got (url, opts, cb) { +function got(url, opts, cb) { if (typeof opts === 'function') { // if `cb` has been specified but `opts` has not cb = opts; @@ -123,16 +123,19 @@ function got (url, opts, cb) { return proxy; } -got.post = function (url, opts, cb) { - opts = opts || {}; - opts.method = 'POST'; - return got(url, opts, cb); -}; - -got.put = function (url, opts, cb) { - opts = opts || {}; - opts.method = 'PUT'; - return got(url, opts, cb); -}; +[ + '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; diff --git a/package.json b/package.json index dad94ae..5edc465 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=0.10.0" }, "scripts": { - "test": "mocha" + "test": "mocha --timeout 50000" }, "files": [ "index.js" diff --git a/readme.md b/readme.md index 96d8f7b..5a93f64 100644 --- a/readme.md +++ b/readme.md @@ -89,13 +89,15 @@ The data you requested. The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage). +#### got.get(url, [options], [callback]) #### got.post(url, [options], [callback]) - -Sets options.method to POST and makes a request. - #### 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. -Sets options.method to PUT and makes a request. ## Related diff --git a/test.js b/test.js index b2f9697..9af8883 100644 --- a/test.js +++ b/test.js @@ -1,10 +1,8 @@ /* global describe, it, before, after */ - 'use strict'; - var assert = require('assert'); -var got = require('./'); var http = require('http'); +var got = require('./'); it('should do HTTP request', function (done) { got('http://google.com', function (err, data) { @@ -96,7 +94,7 @@ it('should proxy errors to the stream', function (done) { }); it('should support timeout option', function (done) { - var stream = got('http://sindresorhus.com/', { timeout: 1 }); + var stream = got('http://sindresorhus.com/', {timeout: 1}); stream.on('error', function (error) { assert.strictEqual(error.code, 'ETIMEDOUT'); @@ -104,7 +102,7 @@ it('should support timeout option', function (done) { }); }); -describe('with POST ', function () { +describe('POST', function () { var server; before(function (done) { @@ -119,7 +117,7 @@ describe('with POST ', function () { }); it('should support string as body option', function (done) { - got('http://0.0.0.0:8081', { body: 'string' }, function (err, data) { + got('http://0.0.0.0:8081', {body: 'string'}, function (err, data) { assert.ifError(err); assert.equal(data, 'string'); done(); @@ -127,7 +125,7 @@ describe('with POST ', function () { }); it('should support Buffer as body option', function (done) { - got('http://0.0.0.0:8081', { body: new Buffer('string') }, function (err, data) { + got('http://0.0.0.0:8081', {body: new Buffer('string')}, function (err, data) { assert.ifError(err); assert.equal(data, 'string'); done(); @@ -135,7 +133,7 @@ describe('with POST ', function () { }); it('should take data from options.body in stream mode', function (done) { - got.post('http://0.0.0.0:8081', { body: 'Hello' }) + got.post('http://0.0.0.0:8081', {body: 'Hello'}) .on('error', done) .on('data', function (chunk) { assert.equal(chunk, 'Hello'); @@ -145,12 +143,12 @@ describe('with POST ', function () { it('should throw an error on options.body and write operations', function (done) { assert.throws(function () { - got.post('http://0.0.0.0:8081', { body: 'Hello' }) + got.post('http://0.0.0.0:8081', {body: 'Hello'}) .end('Hello'); }, 'got\'s stream is not writable when options.body is used'); assert.throws(function () { - got.post('http://0.0.0.0:8081', { body: 'Hello' }) + got.post('http://0.0.0.0:8081', {body: 'Hello'}) .write('Hello'); }, 'got\'s stream is not writable when options.body is used');