Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/unix/koa-custom-response
standard and convenient KOA2 custom responses.
https://github.com/unix/koa-custom-response
Last synced: about 2 months ago
JSON representation
standard and convenient KOA2 custom responses.
- Host: GitHub
- URL: https://github.com/unix/koa-custom-response
- Owner: unix
- License: mit
- Created: 2018-02-11T13:57:42.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-09T16:44:13.000Z (about 2 years ago)
- Last Synced: 2024-05-20T20:34:10.638Z (7 months ago)
- Language: TypeScript
- Homepage:
- Size: 537 KB
- Stars: 9
- Watchers: 4
- Forks: 2
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-koa - koa-custom-response - standard RESTful API for koa (Middleware)
README
## koa-custom-response
[![Coverage Status](https://coveralls.io/repos/github/unix/koa-custom-response/badge.svg?branch=master)](https://coveralls.io/github/DhyanaChina/koa-custom-response?branch=master)
![travis-ci](https://travis-ci.org/unix/koa-custom-response.svg?branch=master)
![MIT](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)standard and convenient custom responses.
focus on simplifying your code logic, reduce your controller. you just need a line of code to return the standard data and status code.
#### Usage
- install: `npm i --save koa-custom-response`
- import and load:
```typescript
import * as CustomResponse from 'koa-custom-response'
// or: const CustomResponse = require('koa-custom-response')// const app = new Koa()
app.use(CustomResponse())
```- use it in your controller:
```typescript
async MyController(ctx) => {
ctx.ok()
}async MyController(ctx) => {
ctx.badRequest()
}async MyController(ctx) => {
try {
// ...
ctx.ok({ hello: hello })
} catch (e) { ctx.catch(e) }
}
```
### API
| Method (ctx.{method_name}) | params | action |
| ------- | ----- | ---- |
| ok | data: object or string | set http status to 200, return json (default: `{}`) |
| created | data: object or string | set http status to 201, return json (default: `{}`) |
| noContent | no params | set http status to 204, return null |
| serverError | data: object or string | set http status to 500, return json (default: `{}`) |
| notFound | data: object or string | set http status to 404, return json (default: `{}`) |
| forbidden | data: object or string | set http status to 403, return json (default: `{}`) |
| badRequest | data: object or string | set http status to 400, return json (default: `{}`) |
| catch | err: Error, data: object or string | set http status to 501, return error |
| reply | code: number, data: object or string | set http status with code |if the parameter is a string, will return `{ message: str }`, like:
server: `ctx.notFound('not found anything')`
client response: `{ message: 'not found anything' }`
### Examples
[example](https://github.com/unix/koa-custom-response/tree/master/examples)
## LICENSE
[MIT](LICENSE)