Socket
Book a DemoInstallSign in
Socket

koa-jsend

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-jsend

Simple and structured application level JSON responses for Koa based on JSend specification.

2.0.0
latest
Source
npmnpm
Version published
Weekly downloads
45
1400%
Maintainers
1
Weekly downloads
 
Created
Source

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 = require('koa')
  , jsend = require('koa-jsend')
  ;

var app = new Koa();

app.use(jsend());

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.use(async function (ctx, next){
  ctx.success({
    name: 'Samora',
    sex: 'Male',
    nationality: 'Ghanaian'
  });
});

// JSON response
// {
//  "status": "success",
//  "data": {
//     "name": "Samora",
//     "sex": "Male",
//     "nationality": "Ghanaian"
//   }
// }

ctx.fail(errors)

  • errors: Required error(s) data.
app.use(async function (ctx, next){
  ctx.fail({
    name: 'Name is required.'
  });
});

// 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.use(async function (ctx, next){
  ctx.error('Something went wrong.', null, 8);
});

// 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.use(async function (ctx, next){
  ctx.jsend(null, {
    name: 'Samora',
    sex: 'Male',
    nationality: 'Ghanaian'
  });
});

// JSON response
// {
//   "status": "success",
//   "data": {
//     "name": "Samora",
//     "sex": "Male",
//     "nationality": "Ghanaian"
//   }
// }
app.use(async function (ctx, next){
  ctx.jsend(Error('Something went wrong.'));
});

// JSON response
// {
//   "status": "error",
//   "message": "Something went wrong.",
// }

License

MIT

Keywords

jsend

FAQs

Package last updated on 03 Mar 2017

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.