@mhio/koa-api

0.5.0 • Public • Published

@mhio/koa-api

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

Errors and json responses are handled, just return the data or throw the error

Based on @mhio/koa-api-handler and it's message format

Install

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

Usage

API docs

const {KoaApi, KoaApiHandler} = require('@mhio/koa-api')

class MyHandler extends KoaApiHandler {
  static getWhatever(ctx) {
     return 'whatever'
  }
  static createOveralls(ctx) {
     const blah = { truth: false }
     return blah
  }
}

const api = new KoaApi({ routes: MyHandler.routeConfig() })
api.listen().then(srv => console.log(srv))
const {KoaApi} = require('@mhio/koa-api')

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)
  }

}

// Route config for the API
const routes = [
  // Simple function
  [ 'get', '/ok', MyHandler.ok ],

  // Function bound to parent
  [ 'get', '/ok2', MyHandler, 'ok' ],

  // Object setup
  { method: 'post', path: '/other', fn: MyHandler.other },

  // binds `MyHandler` for you
  { method: 'get', path: '/error', handler_object: MyHandler, handler_function: 'error' },
]
const api = new KoaApi({ routes })
api.listen().then(srv => console.log(srv))

Readme

Keywords

Package Sidebar

Install

npm i @mhio/koa-api

Weekly Downloads

0

Version

0.5.0

License

MIT

Unpacked Size

129 kB

Total Files

23

Last publish

Collaborators

  • mhio