Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-formatter

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

koa-formatter

A response body formatter for Koa

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

koa-formatter

A response body formatter for Koa.

This middleware can be used to format the JSON result of API calls.

Features

  • Custom format function
  • Handle aggregation of errors
  • Flowtype definition

Installation

NPM

$ npm install koa-formatter

Usage

const Koa = require('koa');
const formatter = require('koa-formatter');

const app = new Koa();

app.use(formatter());
app.use(formatter({ formatter: function(ctx, errors) {
  ctx.body = (errors.length ? 'some errors occurred' : 'ok');
}}))

Formatter

The library comes with a built-in formatter.

app.use(formatter({ formatter: formatter.defaultFormatter() }));
app.use(function(ctx) {
  ctx.throw('simple error');
});
{
  "ok": 0,
  "status": 500,
  "errors": [ "simple error" ]
}
app.use(formatter({ formatter: formatter.defaultFormatter() }));
app.use(function(ctx) {
  ctx.result = [ 'John' ];
});
{
  "ok": 1,
  "status": 200,
  "result": [
    "John"
  ]
}

Licences

MIT

Keywords

FAQs

Package last updated on 29 Aug 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