New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ayeepi

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ayeepi

- Abstraction for standard API responses

  • 0.2.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

ayeepi

Build Status

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 = require('ayeepi');
// ... some restify setup or whatever

app.post('/foo', (req, res) => {
  const id = req.body.id;
  let message;
  doSomething(id)
    .then((data) => {
      message = `It worked for ${id}!`;
      ayeepi.ok(res, message);
    })
    .catch((err) => {
      log.error(err.message);
      message = `An error occures while doing that thing for ${id}`;
      ayeepi.error(res, message);
    })
})

Methods

ok

/**
 * Sends a 200 `OK` response
 *
 * @param  {Response} A response object
 * @param  {Array|String} A string or array of strings to be used to
 *                        transport messages to the API consumer
 * @return {Object} The payload that was sent to the client
 *
 * @example
 * {
 *     "code": 200,
 *     "status": "OK",
 *     "messages": [`Whatever you did worked`]
 * }
 *
 */
ok(res, messages)

error

/**
 * Sends a 500 `Internal Server Error` response
 *
 * @param  {Response} A response object
 * @param  {Array|String} A string or array of strings to be used to
 *                        transport messages to the API consumer. Error
 *                        message(s) should go here
 * @return {Object} The payload that was sent to the client
 *
 * @example
 * {
 *     "code": 500,
 *     "status": "Internal Server Error",
 *     "messages": [`something broke!`]
 * }
 *
 */
error(res, messages)

forbidden

/**
 * Sends a 403 `Forbidden` response
 *
 * @param  {Response} A response object
 * @param  {Array|String} A string or array of strings to be used to
 *                        transport messages to the API consumer
 * @return {Object} The payload that was sent to the client
 *
 * @example
 * {
 *     "code": 403,
 *     "status": "Forbidden",
 *     "messages": [`can't touch this!`]
 * }
 *
 */
forbidden(res, messages)

notFound

/**
 * Sends a 404 `Not Found` response
 *
 * @param  {Response} A response object
 * @param  {Array|String} A string or array of strings to be used to
 *                        transport messages to the API consumer
 * @return {Object} The payload that was sent to the client
 *
 * @example
 * {
 *     "code": 404,
 *     "status": "Not Found",
 *     "messages": [`couldn't find it`]
 * }
 *
 */
notFound(res, messages)

notImplemented

/**
 * Sends a 501 `Not Implemented` response
 *
 * @param  {Response} A response object
 * @param  {Array|String} A string or array of strings to be used to
 *                        transport messages to the API consumer
 * @return {Object} The payload that was sent to the client
 *
 * @example
 * {
 *     "code": 501,
 *     "status": "Not Implemented",
 *     "messages": [`this isn't ready!`]
 * }
 *
 */
notImplemented(res, msgs)

unauthorized

/**
 * Sends a 401 `Unauthorized` response
 *
 * @param  {Response} A response object
 * @param  {Array|String} A string or array of strings to be used to
 *                        transport messages to the API consumer
 * @return {Object} The payload that was sent to the client
 *
 * @example
 * {
 *     "code": 403,
 *     "status": "Unauthorized",
 *     "messages": [`I don't know you!`]
 * }
 *
 */
unauthorized(res, msgs)

send

/**
 * Sends a 200 `OK` response with a data payload
 *
 * @param  {Response} A response object
 * @param  {Object} The data to be sent to the client
 * @param  {Array|String} A string or array of strings to be used to
 *                        transport messages to the API consumer
 * @return {Object} The payload that was sent to the client
 *
 * @example
 * {
 *     "code": 200,
 *     "status": "OK",
 *     "messages": [`Here's the result of the stuff`],
 *     "result": {} // your data
 * }
 *
 */
send(res, data, msgs)

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!

FAQs

Package last updated on 06 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc