0.3.4-canary.0 • Published 5 years ago

koa-custom-response v0.3.4-canary.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

koa-custom-response

Coverage Status travis-ci MIT

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:

    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:

    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})paramsaction
okdata: object or stringset http status to 200, return json (default: {})
createddata: object or stringset http status to 201, return json (default: {})
noContentno paramsset http status to 204, return null
serverErrordata: object or stringset http status to 500, return json (default: {})
notFounddata: object or stringset http status to 404, return json (default: {})
forbiddendata: object or stringset http status to 403, return json (default: {})
badRequestdata: object or stringset http status to 400, return json (default: {})
catcherr: Error, data: object or stringset http status to 501, return error
replycode: number, data: object or stringset 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

LICENSE

MIT

0.3.4-canary.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago