Calibrate
Micro library for providing uniform json output for RESTful APIs, with error handling.
Feel free to raise an issue or contact me on twitter if you have any questions @johnbrett_. Beginners, feature requests and bug reports are welcomed.
Please star if you using this module so I know where to focus my time spent on open source work.
Usage:
const Calibrate = require('calibrate')
Calibrate(data [, meta])
{
"statusCode": ...,
"data": ...,
"meta": ...
}
{
"statusCode": ...,
"error": ...,
"message": ...
}
Calibrate.response(data [, meta])
Calibrate.error(data)
Calibrate.decorate
Example in Hapijs:
'use strict';
const Hapi = require('hapi');
const Calibrate = require('calibrate');
const server = new Hapi.Server({ port: 3000 });
(async () => await server.register({ plugin: Calibrate.hapi, options: { onResponse: false } }))();
server.route([
{
method: 'GET',
path: '/user/{id}',
handler: function (request, h) {
const promise = User.findById(request.params.id)
.then(Calibrate.response)
.catch(Calibrate.error);
return h.response(promise);
}
},
{
method: 'GET',
path: '/team/{id}',
handler: function (request, h) {
Team.findById(request.params.id, (err, team) => {
if (err) {
return h.response(Calibrate.error(err));
}
return h.response(Calibrate.response(team));
});
}
},
{
method: 'GET',
path: '/team/{id}',
handler: function (request, h) {
Team.findById(request.params.id, (err, team) => {
if(err) {
return h.calibrate(err);
}
return h.calibrate(team);
});
}
}
]);
(async () => await server.start())();
});
License MIT @ John Brett