/
index.js
27 lines (24 loc) · 878 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var request = require('request')
, u = require('ubelt')
module.exports = function jsonRest (defaults) {
// var _request = request.defaults(defaults)
return function (opts, callback) {
opts = u.deepMerge(defaults, opts)
if(opts.path) {
opts.url = opts.url + (opts.path ? opts.path : '')
}
if(opts.auth) {
opts.headers = opts.headers || {}
opts.headers['Authorization'] = 'Basic ' + new Buffer(opts.auth).toString('base64')
}
console.error(opts.method || 'GET', opts.url)
return request(opts, function (err, res, body) {
if(err) return callback (err)
if('object' == typeof body)
return callback(res.statusCode >= 400 ? body : err, body)
var json
try { json = JSON.parse(body) } catch (_err) { return callback(_err, body) }
callback(res.statusCode >= 400 ? json: err, json)
})
}
}