Skip to content

Commit

Permalink
finish 1.0.0-rc.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Maples7 committed Jul 29, 2018
1 parent 6fc73bf commit 7672eec
Show file tree
Hide file tree
Showing 8 changed files with 7,301 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .npmignore
@@ -0,0 +1,4 @@
.travis.yml
.nyc_output
test.js
controllers
10 changes: 10 additions & 0 deletions .travis.yml
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "7.6"
- "8.11"
- "10.4"
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
cache:
directories:
- node_modules
87 changes: 87 additions & 0 deletions README.md
@@ -1,3 +1,90 @@
# koa-final-response

[![Build Status](https://travis-ci.org/Maples7/koa-final-response.svg?branch=master)](https://travis-ci.org/Maples7/koa-final-response)
[![Coverage Status](https://coveralls.io/repos/github/Maples7/koa-final-response/badge.svg?branch=master)](https://coveralls.io/github/Maples7/koa-final-response?branch=master)
[![npm version](https://badge.fury.io/js/koa-final-response.svg)](https://badge.fury.io/js/koa-final-response)

[![NPM](https://nodei.co/npm/koa-final-response.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/koa-final-response/)

The very outer middleware of Koa to handle every response of every request.

## Usage

### Installation

`yarn add koa-final-response` or `npm install koa-final-response --save`

### Example

```js
const path = require('path');
// Error object, see https://github.com/hapijs/boom
// We recommand to use Boom as the standard object for error responses
const Boom = require('boom');
const Koa = require('koa');
const mountRoutes = require('koa-mount-routes');
const finalResp = require('koa-final-response');

const app = new Koa();
// this middleware should be added before router works
app.use(finalResp({ env: process.env.NODE_ENV || 'development' }));
// mount routes, see https://github.com/Maples7/koa-mount-routes
mountRoutes(app, path.join(__dirname, 'controllers'), {
allowedMethods: {
throw: true,
notImplemented: () => {
throw Boom.notImplemented('HTTP method for this API is not implemented');
},
methodNotAllowed: () => {
throw Boom.methodNotAllowed('HTTP method for this API is not allowed');
}
}
});
app.listen(3000);
```

### Responses

- Normal response

```json
{
"success": true,
"data": .... // what you assign to `ctx.body`
}
```

- Exception response

```json
{
"success": false,
"error": "Method Not Allowed", // HTTP error correlated with HTTP statusCode
"message": "HTTP method for this API is not allowed" // Error message
}
```

- 404 response

```json
{
"success": false,
"error": "Not Found",
"message": "API Not Found"
}
```

### API

```js
app.use(finalResp({
env, // String. you can pass environmental variable such as NODE_ENV to it. if it is `production`, we will not return error details to user but a vague error messege like `An internal server error occurred`. Default value: 'production'
errStatusCodePropertie // Array. it is about where to find HTTP Status Code of response while an error is thrown. We will search a valid number from property of Error Object in order. Default value: ['status', 'statusCode', 'code']
}));
```

You are welcomed to review _test.js_, _controllers_ dir in this project for more information of usage.

## LICENSE

[MIT](LICENSE)
25 changes: 25 additions & 0 deletions controllers/test.js
@@ -0,0 +1,25 @@
const Boom = require('boom');

module.exports = {
'/1': ctx => {
ctx.body = 'A normal response';
},
'/2': ctx => {
throw new Error('throw an original error');
},
'/3': ctx => {
const err = new Error('throw a self-defined error');
err.status = 403;
throw err;
},
'/4': ctx => {
throw Boom.notImplemented('throw a Boom error');
},
'/5': ctx => {
ctx.status = 201;
ctx.body = 'A normal response with another status';
},
'/6': ctx => {
throw Boom.teapot('throw a teapot');
}
};
4 changes: 2 additions & 2 deletions lib/index.js
@@ -1,9 +1,9 @@
const Boom = require('boom');

module.exports = (
module.exports = ({
env = 'production',
errStatusCodePropertie = ['status', 'statusCode', 'code']
) => async (ctx, next) => {
} = {}) => async (ctx, next) => {
try {
await next();
if (ctx.status === 404) {
Expand Down

0 comments on commit 7672eec

Please sign in to comment.