Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhio/node-koa-api-handle
Koa API Handler
https://github.com/mhio/node-koa-api-handle
api http javascript koa nodejs
Last synced: 6 days ago
JSON representation
Koa API Handler
- Host: GitHub
- URL: https://github.com/mhio/node-koa-api-handle
- Owner: mhio
- License: mit
- Created: 2018-03-09T23:23:50.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-09T02:14:03.000Z (12 months ago)
- Last Synced: 2024-12-03T09:12:44.587Z (2 months ago)
- Topics: api, http, javascript, koa, nodejs
- Language: JavaScript
- Size: 924 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
@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](doc/API.md)
```
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