@mhio/koa-api-handle

0.12.1 • Public • Published

@mhio/koa-api-handle

A Koa API Handler to do all the request heavy lifting, so you just write logic

Higher level use from: https://github.com/mhio/node-koa-api

Install

yarn add @mhio/koa-api-handle
npm install @mhio/koa-api-handle

Usage

API docs

const Koa = require('koa')
const Router = require('koa-router')
const {KoaApiHandle} = require('@mhio/koa-api-handle')

class MyHandler {
  
  static get error_message(){
    return 'Failure'
  }

  static async ok(ctx){
    return {
      ok: true,
      request_id, ctx.state.request_id,
    }
  }

  static async other(){
    return 'other'
  }

  static async error(){
    throw new Error(this.error_message)
  }

}

const app = new Koa()
const router = new Router()

app.use(KoaApiHandle.error())
app.use(KoaApiHandle.tracking())

router.get('/ok', KoaApiHandle.response(MyHandler, 'ok'))
router.post('/other', KoaApiHandle.response(MyHandler, 'other'))
router.get('/error', KoaApiHandle.response(MyHandler.error.bind(MyError)))

app.use(router.routes())
   .use(router.allowedMethods())

app.use(KoaApiHandle.notFound())

app.listen()

Changes

v0.12.x -- Node 20+, pino for logger

https://github.com/mhio/node-koa-api-handle

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.12.13latest
0.12.0-40testing

Version History

VersionDownloads (Last 7 Days)Published
0.12.13
0.12.00
0.12.0-40
0.12.0-30
0.12.0-20
0.12.0-10
0.11.00
0.10.101
0.10.90
0.10.80
0.10.70
0.10.50
0.10.41
0.10.31
0.10.20
0.10.11
0.10.00
0.10.0-31
0.10.0-21
0.10.0-11
0.9.01
0.9.0-10
0.8.00
0.7.00
0.6.10
0.6.00
0.6.0-20
0.5.00
0.5.0-11
0.4.11
0.3.00
0.2.00

Package Sidebar

Install

npm i @mhio/koa-api-handle

Weekly Downloads

13

Version

0.12.1

License

MIT

Unpacked Size

29.8 kB

Total Files

13

Last publish

Collaborators

  • mhio