# koa-jsend

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

Latest version **2.0.0** (published 2017-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-jsend
pnpm add koa-jsend
yarn add koa-jsend
bun add koa-jsend
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2017-03-03 |
| First published | 2014-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Samora Dake |
| Maintainers | samora |
| Keywords | jsend, koa, json, api, response |

## Links

- npm: https://www.npmjs.com/package/koa-jsend
- Repository: https://github.com/samora/koa-jsend
- Issues: https://github.com/samora/koa-jsend/issues
- npm.io page: https://npm.io/package/koa-jsend

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2017-03-03
- 0.1.0 — 2014-04-04
- 0.0.1 — 2014-03-08

## README

# KOA JSend
Simple and structured application level JSON responses for your API.
Based on JSend specification ([http://labs.omniti.com/labs/jsend](http://labs.omniti.com/labs/jsend)).

## Installation

```
npm install --save koa-jsend
```

In your [koa](http://koajs.com) app.

```javascript
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.

```javascript
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.

```javascript
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.

```javascript
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_

```javascript
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"
//   }
// }
```


```javascript
app.use(async function (ctx, next){
  ctx.jsend(Error('Something went wrong.'));
});

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


# License

MIT

---
_Source: https://npm.io/package/koa-jsend · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
