simple-get
Advanced tools
Comparing version 2.2.5 to 2.3.0
35
index.js
module.exports = simpleGet | ||
var extend = require('xtend') | ||
var concat = require('simple-concat') | ||
var http = require('http') | ||
var https = require('https') | ||
var once = require('once') | ||
var querystring = require('querystring') | ||
var unzipResponse = require('unzip-response') // excluded from browser build | ||
@@ -11,3 +12,3 @@ var url = require('url') | ||
function simpleGet (opts, cb) { | ||
opts = typeof opts === 'string' ? { url: opts } : extend(opts) | ||
opts = typeof opts === 'string' ? {url: opts} : Object.assign({}, opts) | ||
cb = once(cb) | ||
@@ -19,11 +20,15 @@ | ||
var body = opts.json ? JSON.stringify(opts.body) : opts.body | ||
opts.body = undefined | ||
if (body && !opts.method) opts.method = 'POST' | ||
if (opts.method) opts.method = opts.method.toUpperCase() | ||
var body | ||
if (opts.form) body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form) | ||
if (opts.body) body = opts.json ? JSON.stringify(opts.body) : opts.body | ||
if (opts.json) opts.headers.accept = 'application/json' | ||
if (opts.json && body) opts.headers['content-type'] = 'application/json' | ||
if (body) opts.headers['Content-Length'] = Buffer.byteLength(body) | ||
if (opts.form) opts.headers['content-type'] = 'application/x-www-form-urlencoded' | ||
if (body) opts.headers['content-length'] = Buffer.byteLength(body) | ||
delete opts.body; delete opts.form | ||
if (body && !opts.method) opts.method = 'POST' | ||
if (opts.method) opts.method = opts.method.toUpperCase() | ||
// Request gzip/deflate | ||
@@ -35,3 +40,3 @@ var customAcceptEncoding = Object.keys(opts.headers).some(function (h) { | ||
// Support http: and https: urls | ||
// Support http/https urls | ||
var protocol = opts.protocol === 'https:' ? https : http | ||
@@ -60,11 +65,7 @@ var req = protocol.request(opts, function (res) { | ||
module.exports.concat = function (opts, cb) { | ||
simpleGet.concat = function (opts, cb) { | ||
return simpleGet(opts, function (err, res) { | ||
if (err) return cb(err) | ||
var chunks = [] | ||
res.on('data', function (chunk) { | ||
chunks.push(chunk) | ||
}) | ||
res.on('end', function () { | ||
var data = Buffer.concat(chunks) | ||
concat(res, function (err, data) { | ||
if (err) return cb(err) | ||
if (opts.json) { | ||
@@ -83,4 +84,4 @@ try { | ||
;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(function (method) { | ||
module.exports[method] = function (opts, cb) { | ||
if (typeof opts === 'string') opts = { url: opts } | ||
simpleGet[method] = function (opts, cb) { | ||
if (typeof opts === 'string') opts = {url: opts} | ||
opts.method = method.toUpperCase() | ||
@@ -87,0 +88,0 @@ return simpleGet(opts, cb) |
{ | ||
"name": "simple-get", | ||
"description": "Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines.", | ||
"version": "2.2.5", | ||
"version": "2.3.0", | ||
"author": { | ||
@@ -18,7 +18,6 @@ "name": "Feross Aboukhadijeh", | ||
"once": "^1.3.1", | ||
"unzip-response": "^1.0.0", | ||
"xtend": "^4.0.0" | ||
"simple-concat": "^1.0.0", | ||
"unzip-response": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"concat-stream": "^1.4.7", | ||
"self-signed-https": "^1.0.5", | ||
@@ -25,0 +24,0 @@ "standard": "*", |
@@ -31,2 +31,4 @@ # simple-get [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] | ||
Note, all these examples also work in the browser with [browserify](http://browserify.org/). | ||
### simple GET request | ||
@@ -81,3 +83,2 @@ | ||
var get = require('simple-get') | ||
var concat = require('concat-stream') | ||
@@ -103,6 +104,6 @@ get({ | ||
res.pipe(concat(function (data) { | ||
// `data` is the decoded response, after it's been gunzipped or inflated | ||
res.on('data', function (chunk) { | ||
// `chunk` is the decoded response, after it's been gunzipped or inflated | ||
// (if applicable) | ||
console.log('got the response: ' + data) | ||
console.log('got a chunk of the response: ' + chunk) | ||
})) | ||
@@ -113,3 +114,3 @@ | ||
### JSON Handling | ||
### JSON | ||
@@ -135,4 +136,21 @@ You can serialize/deserialize request and response with JSON: | ||
### Forms | ||
You can send `application/x-www-form-urlencoded` form data: | ||
```js | ||
var get = require('simple-get') | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://example.com', | ||
form: { | ||
key: 'value' | ||
} | ||
} | ||
get.concat(opts, function (err, res, data) {}) | ||
``` | ||
## license | ||
MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). |
@@ -1,4 +0,5 @@ | ||
var concat = require('concat-stream') | ||
var concat = require('simple-concat') | ||
var get = require('../') | ||
var http = require('http') | ||
var get = require('../') | ||
var querystring = require('querystring') | ||
var selfSignedHttps = require('self-signed-https') | ||
@@ -13,3 +14,3 @@ var str = require('string-to-stream') | ||
test('simple get', function (t) { | ||
t.plan(4) | ||
t.plan(5) | ||
@@ -27,6 +28,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -37,3 +39,3 @@ }) | ||
test('basic auth', function (t) { | ||
t.plan(4) | ||
t.plan(5) | ||
@@ -51,6 +53,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -61,3 +64,3 @@ }) | ||
test('follow redirects (up to 10)', function (t) { | ||
t.plan(13) | ||
t.plan(14) | ||
@@ -84,6 +87,7 @@ var num = 1 | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -163,3 +167,3 @@ }) | ||
test('gzip response', function (t) { | ||
t.plan(3) | ||
t.plan(4) | ||
@@ -177,6 +181,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) // statusCode still works on gunzip stream | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -187,3 +192,3 @@ }) | ||
test('deflate response', function (t) { | ||
t.plan(3) | ||
t.plan(4) | ||
@@ -201,6 +206,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) // statusCode still works on inflate stream | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -211,3 +217,3 @@ }) | ||
test('https', function (t) { | ||
t.plan(4) | ||
t.plan(5) | ||
@@ -225,6 +231,7 @@ var server = selfSignedHttps(function (req, res) { | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -235,3 +242,3 @@ }) | ||
test('redirect https to http', function (t) { | ||
t.plan(5) | ||
t.plan(6) | ||
@@ -261,7 +268,8 @@ var httpPort = null | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
httpsServer.close() | ||
httpServer.close() | ||
})) | ||
}) | ||
}) | ||
@@ -273,3 +281,3 @@ }) | ||
test('redirect http to https', function (t) { | ||
t.plan(5) | ||
t.plan(6) | ||
@@ -299,7 +307,8 @@ var httpsPort = null | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
httpsServer.close() | ||
httpServer.close() | ||
})) | ||
}) | ||
}) | ||
@@ -311,3 +320,3 @@ }) | ||
test('post (text body)', function (t) { | ||
t.plan(4) | ||
t.plan(5) | ||
@@ -329,6 +338,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'this is the body') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -339,3 +349,3 @@ }) | ||
test('post (utf-8 text body)', function (t) { | ||
t.plan(4) | ||
t.plan(5) | ||
@@ -357,6 +367,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'jedan dva tri četiri') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -367,3 +378,3 @@ }) | ||
test('post (buffer body)', function (t) { | ||
t.plan(4) | ||
t.plan(5) | ||
@@ -385,6 +396,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'this is the body') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -436,3 +448,3 @@ }) | ||
test('simple get json', function (t) { | ||
t.plan(5) | ||
t.plan(6) | ||
@@ -455,6 +467,7 @@ var server = http.createServer(function (req, res) { | ||
t.equal(res.statusCode, 200) | ||
res.pipe(concat(function (data) { | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), '{"message":"response"}') | ||
server.close() | ||
})) | ||
}) | ||
}) | ||
@@ -535,2 +548,60 @@ }) | ||
test('post (form, object)', function (t) { | ||
t.plan(5) | ||
var formData = { | ||
foo: 'bar' | ||
} | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://localhost:' + port, | ||
form: formData | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(querystring.parse(data.toString()), formData) | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('post (form, querystring)', function (t) { | ||
t.plan(5) | ||
var formData = 'foo=bar' | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://localhost:' + port, | ||
form: formData | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.toString(), formData) | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('HEAD request', function (t) { | ||
@@ -537,0 +608,0 @@ t.plan(3) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23308
4
611
152
+ Addedsimple-concat@^1.0.0
+ Addedsimple-concat@1.0.1(transitive)
+ Addedunzip-response@2.0.1(transitive)
- Removedxtend@^4.0.0
- Removedunzip-response@1.0.2(transitive)
- Removedxtend@4.0.2(transitive)
Updatedunzip-response@^2.0.1