KOA JSend
Simple and structured application level JSON responses for your API. Based on JSend specification (http://labs.omniti.com/labs/jsend).
Installation
npm install --save koa-jsend
In your koa app.
var Koa = jsend = ; var app = ; app;
API
Instead of using ctx.body=
to send JSON responses, use one of the functions in ctx
.
ctx.success(data)
- data: Optional response data.
app; // JSON response// {// "status": "success",// "data": {// "name": "Samora",// "sex": "Male",// "nationality": "Ghanaian"// }// }
ctx.fail(errors)
- errors: Required error(s) data.
app; // JSON response// {// "status": "fail",// "data": {// "name": "Name is required."// }// }
ctx.error(message, data, code)
- message: Required error message or
Error
object. - data: Optional error data.
- code: Optional error code.
app; // JSON response// {// "status": "error",// "message": "Something went wrong.",// "code": 8// }
ctx.jsend(err, data, code)
Sends a ctx.success
response if no err
is passed.
Sends a ctx.error
response if err
is passed.
- err: Error object or message string or null.
- data: Optional
- code: Optional
app; // JSON response// {// "status": "success",// "data": {// "name": "Samora",// "sex": "Male",// "nationality": "Ghanaian"// }// }
app; // JSON response// {// "status": "error",// "message": "Something went wrong.",// }
License
MIT