@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

Readme

Keywords

Package Sidebar

Install

npm i @mhio/koa-api-handle

Weekly Downloads

10

Version

0.12.1

License

MIT

Unpacked Size

29.8 kB

Total Files

13

Last publish

Collaborators

  • mhio