Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
7,301 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.travis.yml | ||
.nyc_output | ||
test.js | ||
controllers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.