Comparing version 2.1.0 to 2.1.1
@@ -1,11 +0,9 @@ | ||
var url = require('url') | ||
, hyperquest = require('hyperquest') | ||
, bl = require('bl') | ||
, stringify = require('json-stringify-safe') | ||
, xtend = require('xtend') | ||
const url = require('url') | ||
const hyperquest = require('hyperquest') | ||
const bl = require('bl') | ||
const stringify = require('json-stringify-safe') | ||
function HttpError (message) { | ||
SyntaxError.call(this, message) | ||
Error.captureStackTrace(this, arguments.callee) | ||
SyntaxError.call(this, message) | ||
Error.captureStackTrace(this, arguments.callee) | ||
} | ||
@@ -16,7 +14,6 @@ | ||
function collector (uri, options, callback) { | ||
var request = makeRequest(uri, options) | ||
, redirect = null | ||
, redirectCount = 0 | ||
let request = makeRequest(uri, options) | ||
let redirect = null | ||
let redirectCount = 0 | ||
@@ -27,3 +24,3 @@ return handle(request) | ||
if (options.followRedirects) { | ||
request.on('response', function (response) { | ||
request.on('response', (response) => { | ||
redirect = isRedirect(request.request, response) && response.headers.location | ||
@@ -33,6 +30,7 @@ }) | ||
request.pipe(bl(function (err, data) { | ||
request.pipe(bl((err, data) => { | ||
if (redirect) { | ||
if (++redirectCount >= (typeof options.followRedirects == 'number' ? options.followRedirects : 10)) | ||
if (++redirectCount >= (typeof options.followRedirects === 'number' ? options.followRedirects : 10)) { | ||
return callback(new Error('Response was redirected too many times (' + redirectCount + ')')) | ||
} | ||
request = makeRequest(url.resolve(uri, redirect), options) | ||
@@ -43,9 +41,11 @@ redirect = null | ||
if (err) | ||
if (err) { | ||
return callback(err) | ||
} | ||
if (!data.length) | ||
if (!data.length) { | ||
return callback(null, null, request.response) | ||
} | ||
var ret, msg | ||
let ret, msg | ||
@@ -62,3 +62,3 @@ try { | ||
return callback(err); | ||
return callback(err) | ||
} | ||
@@ -73,22 +73,18 @@ | ||
function makeMethod (method, data) { | ||
function handler (uri, options, callback) { | ||
if (typeof options == 'function') { | ||
let defaultOptions = { method, headers: {} } | ||
if (typeof options === 'object') { | ||
options = Object.assign(defaultOptions, options) | ||
} else { | ||
callback = options | ||
options = {} | ||
} else | ||
options = xtend(options, {}) | ||
options = defaultOptions | ||
} | ||
if (typeof options.method != 'string') | ||
options.method = method | ||
if (typeof options.headers != 'object') | ||
options.headers = {} | ||
if (data && typeof options.headers['content-type'] != 'string') | ||
if (data && typeof options.headers['content-type'] !== 'string') { | ||
options.headers['content-type'] = 'application/json' | ||
if (typeof options.headers['accept'] != 'string') | ||
} | ||
if (typeof options.headers['accept'] !== 'string') { | ||
options.headers['accept'] = 'application/json' | ||
} | ||
@@ -99,7 +95,10 @@ return collector(uri, options, callback) | ||
function dataHandler (uri, data, options, callback) { | ||
var request = handler(uri, options, callback) | ||
if (typeof data.pipe == 'function') | ||
let request = handler(uri, options, callback) | ||
if (typeof data.pipe === 'function') { | ||
data.pipe(request) | ||
else | ||
} else { | ||
request.end(stringify(data)) | ||
} | ||
return request | ||
@@ -111,3 +110,2 @@ } | ||
function makeRequest (uri, options) { | ||
@@ -117,18 +115,16 @@ return (options.hyperquest || hyperquest)(uri, options) | ||
function isRedirect (request, response) { | ||
return request.method === 'GET' && | ||
response.headers.location && | ||
( response.statusCode === 301 | ||
|| response.statusCode === 302 | ||
|| response.statusCode === 307 | ||
|| response.statusCode === 308 | ||
) | ||
response.headers.location && | ||
(response.statusCode === 301 || | ||
response.statusCode === 302 || | ||
response.statusCode === 307 || | ||
response.statusCode === 308 | ||
) | ||
} | ||
module.exports.get = makeMethod('GET' , false) | ||
module.exports.post = makeMethod('POST' , true) | ||
module.exports.put = makeMethod('PUT' , true) | ||
module.exports.delete = function deleteHandler (uri, options, callback) { | ||
module.exports.get = makeMethod('GET', false) | ||
module.exports.post = makeMethod('POST', true) | ||
module.exports.put = makeMethod('PUT', true) | ||
module.exports.delete = function deleteHandler (uri, options, callback) { | ||
// behaves half-way between a data posting request and a GET | ||
@@ -135,0 +131,0 @@ // since https://github.com/substack/hyperquest/commit/9b130e101 |
{ | ||
"name": "jsonist", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "A simple wrapper around for dealing with JSON web APIs", | ||
@@ -25,4 +25,4 @@ "main": "jsonist.js", | ||
"dependencies": { | ||
"bl": "~1.2.0", | ||
"hyperquest": "~2.1.2", | ||
"bl": "~3.0.0", | ||
"hyperquest": "~2.1.3", | ||
"json-stringify-safe": "~5.0.1", | ||
@@ -35,4 +35,4 @@ "xtend": "~4.0.1" | ||
"faucet": "~0.0.1", | ||
"tape": "~4.6.3" | ||
"tape": "~4.10.2" | ||
} | ||
} |
@@ -64,2 +64,4 @@ # jsonist | ||
**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances. | ||
### jsonist.post(url, data, [ options, ] callback) | ||
@@ -75,2 +77,4 @@ | ||
**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances. | ||
### jsonist.put(url, data, [ options, ] callback) | ||
@@ -82,2 +86,4 @@ | ||
**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances. | ||
### jsonist.delete(url, [ options, ] callback) | ||
@@ -89,2 +95,4 @@ | ||
**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances. | ||
## Error handling and bad JSON responses | ||
@@ -91,0 +99,0 @@ |
181
test.js
@@ -1,20 +0,18 @@ | ||
const test = require('tape') | ||
, http = require('http') | ||
, fs = require('fs') | ||
, bl = require('bl') | ||
, xtend = require('xtend') | ||
, EE = require('events').EventEmitter | ||
, jsonist = require('./') | ||
, stringify = require('json-stringify-safe') | ||
, after = require('after') | ||
const test = require('tape') | ||
const http = require('http') | ||
const fs = require('fs') | ||
const bl = require('bl') | ||
const xtend = require('xtend') | ||
const EE = require('events').EventEmitter | ||
const jsonist = require('./') | ||
const stringify = require('json-stringify-safe') | ||
const after = require('after') | ||
function testServer (serverResponse, statusCode) { | ||
var ee = new EE() | ||
, server = http.createServer(handler) | ||
const ee = new EE() | ||
const server = http.createServer(handler) | ||
function handler (req, res) { | ||
req.pipe(bl(function (err, data) { | ||
if (err) | ||
return ee.emit('error', err) | ||
if (err) { return ee.emit('error', err) } | ||
@@ -27,3 +25,3 @@ ee.emit('request', req, data.toString()) | ||
res.writeHead( | ||
typeof statusCode == 'number' ? statusCode : 200 | ||
typeof statusCode === 'number' ? statusCode : 200 | ||
, { 'content-type': 'application/json' } | ||
@@ -43,8 +41,7 @@ ) | ||
'get delete'.split(' ').forEach(function (type) { | ||
test(type + ' fetch json doc', function (t) { | ||
'get delete'.split(' ').forEach((type) => { | ||
test(type + ' fetch json doc', (t) => { | ||
t.plan(7) | ||
var testDoc = { a: 'test', doc: true, arr: [ { of: 'things' } ] } | ||
let testDoc = { a: 'test', doc: true, arr: [ { of: 'things' } ] } | ||
@@ -58,3 +55,3 @@ testServer(stringify(testDoc)) | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
@@ -71,3 +68,3 @@ ) | ||
test(type + ' fetch non-json doc', function (t) { | ||
test(type + ' fetch non-json doc', (t) => { | ||
t.plan(7) | ||
@@ -93,3 +90,3 @@ | ||
'post put'.split(' ').forEach(function (type) { | ||
test(type + ' json, no response', function (t) { | ||
test(type + ' json, no response', (t) => { | ||
t.plan(9) | ||
@@ -106,3 +103,3 @@ | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
@@ -121,7 +118,7 @@ ) | ||
test(type + ' json, with response', function (t) { | ||
test(type + ' json, with response', (t) => { | ||
t.plan(9) | ||
var sendDoc = { a: 'test2', doc: true, arr: [ { of: 'things' } ] } | ||
, recvDoc = { recv: 'this', obj: true } | ||
let sendDoc = { a: 'test2', doc: true, arr: [ { of: 'things' } ] } | ||
let recvDoc = { recv: 'this', obj: true } | ||
@@ -135,3 +132,3 @@ testServer(stringify(recvDoc)) | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
@@ -150,12 +147,12 @@ ) | ||
test(type + ' data with no pipe function treated as data', function (t) { | ||
test(type + ' data with no pipe function treated as data', (t) => { | ||
t.plan(9) | ||
var sendDoc = { | ||
a : 'test2' | ||
, doc : true | ||
, arr : [ { of: 'things' } ] | ||
, pipe : 'this is a string and not a function' | ||
} | ||
, recvDoc = { recv: 'this', obj: true } | ||
let sendDoc = { | ||
a: 'test2', | ||
doc: true, | ||
arr: [ { of: 'things' } ], | ||
pipe: 'this is a string and not a function' | ||
} | ||
let recvDoc = { recv: 'this', obj: true } | ||
@@ -169,3 +166,3 @@ testServer(stringify(recvDoc)) | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
@@ -184,17 +181,17 @@ ) | ||
test(type + ' data with pipe function will data.pipe(req)', function (t) { | ||
test(type + ' data with pipe function will data.pipe(req)', (t) => { | ||
t.plan(10) | ||
var sendDoc = { | ||
a : 'test2' | ||
, doc : true | ||
, arr : [ { of: 'things' } ] | ||
} | ||
, stream = { | ||
pipe: function (req) { | ||
t.ok(req, 'request should be set') | ||
req.end(stringify(sendDoc)) | ||
} | ||
} | ||
, recvDoc = { recv: 'this', obj: true } | ||
let sendDoc = { | ||
a: 'test2', | ||
doc: true, | ||
arr: [ { of: 'things' } ] | ||
} | ||
let stream = { | ||
pipe: function (req) { | ||
t.ok(req, 'request should be set') | ||
req.end(stringify(sendDoc)) | ||
} | ||
} | ||
let recvDoc = { recv: 'this', obj: true } | ||
@@ -208,3 +205,3 @@ testServer(stringify(recvDoc)) | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
@@ -223,9 +220,9 @@ ) | ||
test(type + ' data with pipe function and real stream works', function (t) { | ||
test(type + ' data with pipe function and real stream works', (t) => { | ||
t.plan(9) | ||
var file = __dirname + '/package.json' | ||
, content = JSON.parse(fs.readFileSync(file)) | ||
, stream = fs.createReadStream(file) | ||
, recvDoc = { recv: 'this', obj: true } | ||
let file = `${__dirname}/package.json` | ||
let content = JSON.parse(fs.readFileSync(file)) | ||
let stream = fs.createReadStream(file) | ||
let recvDoc = { recv: 'this', obj: true } | ||
@@ -239,3 +236,3 @@ testServer(stringify(recvDoc)) | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
@@ -255,22 +252,21 @@ ) | ||
test('follow redirect', function (t) { | ||
test('follow redirect', (t) => { | ||
t.plan(7) | ||
var expectedResponse = { ok: 'foobar!' } | ||
, server = http.createServer(function (req, res) { | ||
if (req.url == '/') { // 2 requests come in here | ||
t.ok('got /') | ||
res.writeHead(302, { 'location': '/foobar' }) | ||
return res.end() | ||
} | ||
// one comes in here | ||
t.equal(req.url, '/foobar', 'got /foobar') | ||
res.writeHead(200, { 'content-type': 'application/json' }) | ||
res.end(stringify(expectedResponse)) | ||
}) | ||
let expectedResponse = { ok: 'foobar!' } | ||
let server = http.createServer(function (req, res) { | ||
if (req.url === '/') { // 2 requests come in here | ||
t.ok('got /') | ||
res.writeHead(302, { 'location': '/foobar' }) | ||
return res.end() | ||
} | ||
// one comes in here | ||
t.equal(req.url, '/foobar', 'got /foobar') | ||
res.writeHead(200, { 'content-type': 'application/json' }) | ||
res.end(stringify(expectedResponse)) | ||
}) | ||
server.listen(function () { | ||
var port = server.address().port | ||
, done = after(2, function () { server.close() }) | ||
let port = server.address().port | ||
let done = after(2, function () { server.close() }) | ||
@@ -292,23 +288,22 @@ jsonist.get('http://localhost:' + port, function (err, data) { | ||
test('follow redirect limit', function (t) { | ||
test('follow redirect limit', (t) => { | ||
t.plan(6 + 10 + 5 + 10) | ||
var expectedResponse = { ok: 'foobar!' } | ||
, server = http.createServer(function (req, res) { | ||
var m = +req.url.match(/^\/(\d+)/)[1] | ||
if (m < 20) { // 2 requests come in here | ||
t.ok('got /') | ||
res.writeHead(302, { 'location': '/' + (m + 1) }) | ||
return res.end() | ||
} | ||
// one comes in here | ||
t.equal(req.url, '/20', 'got /20') | ||
res.writeHead(200, { 'content-type': 'application/json' }) | ||
res.end(stringify(expectedResponse)) | ||
}) | ||
let expectedResponse = { ok: 'foobar!' } | ||
let server = http.createServer(function (req, res) { | ||
let m = +req.url.match(/^\/(\d+)/)[1] | ||
if (m < 20) { // 2 requests come in here | ||
t.ok('got /') | ||
res.writeHead(302, { 'location': '/' + (m + 1) }) | ||
return res.end() | ||
} | ||
// one comes in here | ||
t.equal(req.url, '/20', 'got /20') | ||
res.writeHead(200, { 'content-type': 'application/json' }) | ||
res.end(stringify(expectedResponse)) | ||
}) | ||
server.listen(function () { | ||
var port = server.address().port | ||
, done = after(3, function () { server.close() }) | ||
let port = server.address().port | ||
let done = after(3, function () { server.close() }) | ||
@@ -335,4 +330,3 @@ jsonist.get('http://localhost:' + port + '/1', { followRedirects: true }, function (err, data) { | ||
test('server error, non-JSON', function (t) { | ||
test('server error, non-JSON', (t) => { | ||
t.plan(7) | ||
@@ -358,4 +352,3 @@ | ||
test('server error, with-JSON', function (t) { | ||
test('server error, with-JSON', (t) => { | ||
t.plan(8) | ||
@@ -373,3 +366,3 @@ | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
@@ -376,0 +369,0 @@ ) |
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
414
109
23158
6
+ Addedbl@3.0.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)
- Removedbl@1.2.3(transitive)
- Removedisarray@1.0.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
Updatedbl@~3.0.0
Updatedhyperquest@~2.1.3