ayeepi
Pronounced like the acronym API
Abstraction for standard API responses based on this post by Andrey Petrov
{
"code": 200,
"status": "ok",
"messages": [],
"result": {
"user": {
"id": 123,
"name": "shazow"
}
}
}
Install
$ npm install ayeepi --save
Usage
const ayeepi = ;// ... some restify setup or whatever app
Methods
ok
/** * Sends a 200 `OK` response * * @param * @param * transport messages to the API consumer * @return * * @example * { * "code": 200, * "status": "OK", * "messages": [`Whatever you did worked`] * } * */
error
/** * Sends a 500 `Internal Server Error` response * * @param * @param * transport messages to the API consumer. Error * message(s) should go here * @return * * @example * { * "code": 500, * "status": "Internal Server Error", * "messages": [`something broke!`] * } * */
forbidden
/** * Sends a 403 `Forbidden` response * * @param * @param * transport messages to the API consumer * @return * * @example * { * "code": 403, * "status": "Forbidden", * "messages": [`can't touch this!`] * } * */
notFound
/** * Sends a 404 `Not Found` response * * @param * @param * transport messages to the API consumer * @return * * @example * { * "code": 404, * "status": "Not Found", * "messages": [`couldn't find it`] * } * */
notImplemented
/** * Sends a 501 `Not Implemented` response * * @param * @param * transport messages to the API consumer * @return * * @example * { * "code": 501, * "status": "Not Implemented", * "messages": [`this isn't ready!`] * } * */
unauthorized
/** * Sends a 401 `Unauthorized` response * * @param * @param * transport messages to the API consumer * @return * * @example * { * "code": 403, * "status": "Unauthorized", * "messages": [`I don't know you!`] * } * */
send
/** * Sends a 200 `OK` response with a data payload * * @param * @param * @param * transport messages to the API consumer * @return * * @example * { * "code": 200, * "status": "OK", * "messages": [`Here's the result of the stuff`], * "result": * } * */
Tests
$ npm test
Contributing
Totally welcome. Create a PR, create a passing test, adhere to .jshint rules and if it makes sense - I'm happy to merge it!