2.0.2 • Published 6 years ago

koa-decorator v2.0.2

Weekly downloads
46
License
-
Repository
-
Last release
6 years ago

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,
    };
  }
}
2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago