Skip to content

Commit

Permalink
standard && update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jun 18, 2019
1 parent 8121d7e commit 3a5b364
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 205 deletions.
59 changes: 0 additions & 59 deletions .jshintrc

This file was deleted.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ The `options` object is optional and is passed on to hyperquest. One option is o

The callback is called with up to 3 arguments. If there is an error there will only be an error argument in the first position, otherwise it will be `null`. The second argument will contain the deserialised object obtained from the server and the third argument will be the response object itself if you need to fetch headers or other metadata.

**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances.

### jsonist.post(url, data, [ options, ] callback)

Send a POST request to `url`, writing JSON serialised data to the request, and return the callback with an error or JSON deserialised data (if any).
Expand All @@ -71,18 +73,24 @@ The `data` parameter can also be a readable stream that will get `.pipe()`'d to

The `options` object is optional and is passed on to hyperquest.

**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances.

### jsonist.put(url, data, [ options, ] callback)

Same as `jsonist.post()` but for when that extra character is too much to type or you have to use someone's overloaded API. `'method'` is set to `'PUT'`.

*Note: in each of the requests you can provide an optional `'hyperquest'` parameter in your options if you want to really customise the http chain (see [this](https://github.com/hyperquest))*

**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances.

### jsonist.delete(url, [ options, ] callback)

Send a DELETE request to `url` and return the callback with an error or JSON deserialised data.

Otherwise works the same as GET.

**Returns** the underlying hyperquest stream for this request. Can be safely ignored in most circumstances.

## Error handling and bad JSON responses

Server errors (i.e. response codes >= 300) are handled as standard responses. You can get the status code from the response object which is the third argument to the standard callback if you need to handle error responses in a different way.
Expand Down
94 changes: 45 additions & 49 deletions jsonist.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
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)
}

HttpError.prototype = Object.create(SyntaxError.prototype)
HttpError.prototype.constructor = HttpError


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

return handle(request)

function handle (request) {
if (options.followRedirects) {
request.on('response', function (response) {
request.on('response', (response) => {
redirect = isRedirect(request.request, response) && response.headers.location
})
}

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)
redirect = null
return handle(request)
}

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

try {
ret = JSON.parse(data.toString())
Expand All @@ -55,7 +55,7 @@ function collector (uri, options, callback) {
err.data = data
err.response = request.response

return callback(err);
return callback(err)
}

callback(null, ret, request.response)
Expand All @@ -65,63 +65,59 @@ function collector (uri, options, callback) {
}
}


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, {})

if (typeof options.method != 'string')
options.method = method

if (typeof options.headers != 'object')
options.headers = {}
options = defaultOptions
}

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'
}

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
}

return data ? dataHandler : handler
}


function makeRequest (uri, options) {
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
// since https://github.com/substack/hyperquest/commit/9b130e101
return makeMethod('DELETE', false)(uri, options, callback).end()
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
"url": "https://github.com/rvagg/jsonist/issues"
},
"dependencies": {
"bl": "~1.2.0",
"hyperquest": "~2.1.2",
"bl": "~3.0.0",
"hyperquest": "~2.1.3",
"json-stringify-safe": "~5.0.1",
"xtend": "~4.0.1"
},
"homepage": "https://github.com/rvagg/jsonist",
"devDependencies": {
"after": "~0.8.2",
"faucet": "~0.0.1",
"tape": "~4.6.3"
"tape": "~4.10.2"
}
}

0 comments on commit 3a5b364

Please sign in to comment.