I'm writing a wrapper around node-request:
var RequestWrapper = function(client, method, type, uuid, body, callback) {
callback = callback || body || uuid
// Let's get this looking a little cleaner!
body = (typeof body === 'object') ? body : uuid
body = (typeof body === 'object') ? body : undefined
var uri = urljoin(config.baseUrl, type, (typeof uuid === 'string') ? uuid : "")
var params = request.initParams(uri, {
body: body
}, callback)
params.method = method
params.json = true
request(params, function(error, response) {
response = new UsergridResponse(response)
params.callback(error, response)
})
}
module.exports = RequestWrapper
I'm supporting four RESTful methods, with optional arguments like so:
wrapper(client, 'GET', 'pets', cb) // returns an array of pets
wrapper(client, 'GET', 'pets', '<uuid>', cb) // returns a specific pet
wrapper(client, 'PUT', 'pets', '<uuid>', cb) // updates the specified pet
wrapper(client, 'POST', 'pets', { body: {} }, cb) // creates a new pet
wrapper(client, 'DELETE', 'pets', '<uuid>', cb) // deletes the specified pet
My RequestWrapper works like it is, but I really dislike the way I'm checking for optional body
param and optional uuid param. I managed to get callback
all pretty looking -- is there a way I can do this for body
too?