@sendit-th/koa-decorator
TypeScript icon, indicating that this package has built-in type declarations

2.3.1 • Public • Published

koa-decorator

@route decorator for koa-router

Use

install by:

npm i koa-decorator

then use in code:

import {HttpMethod, route} from 'koa-decorator';

@route('/monitor')
export default class MonitorCtrl{

  @route('/alive', HttpMethod.GET)
  async alive(ctx) {
    ctx.body = {
      data: true,
    };
  }
}

register to router:

import {load} from 'koa-decorator';
const apiRouter = load(path.resolve(__dirname, 'route'));
// const apiRouter = load(path.resolve(__dirname, 'route', '.ts')); // only require .ts files as route
app.use(apiRouter.routes()).use(apiRouter.allowedMethods());

Use auth middleware

import {HttpMethod, route} from 'koa-decorator';

function auth(ctx) {
  if(!ctx.auth){
      ctx.response.status = 401;
  }
}

@route('/monitor')
export default class MonitorCtrl {

  @route('/alive', HttpMethod.ALL, auth)
  async alive(ctx) {
    ctx.body = {
      data: true,
    };
  }
}

Return data direct

The following code has the same effect as above:

@route('/monitor')
export default class MonitorCtrl {

  @route('/alive', HttpMethod.GET, auth)
  async alive() {
    return {
      data: true,
    };
  }
}

Package Sidebar

Install

npm i @sendit-th/koa-decorator

Weekly Downloads

1

Version

2.3.1

License

MIT

Unpacked Size

22.1 kB

Total Files

8

Last publish

Collaborators

  • nv4re
  • true-e-logisctics-admin
  • dream07312
  • kazekun
  • tosbodes
  • boshido
  • jokergn
  • bossbossk20
  • doctoraod