Wondering what’s next for npm?Check out our public roadmap! »

    koa-router-decorators-async
    TypeScript icon, indicating that this package has built-in type declarations

    0.4.6 • Public • Published

    Koa Router Decorators

    ES7 decorators for koa-router model.

    Installation

    $ npm i koa-router-decorators --save

    Usage

    This library supports ES7 decorators proposal which is supported by babel and typescript. To use it with babel you should enable experimental es7.decorators feature in babel as described here. To use it with typescripts you should enable experimentalDecorators and emitDecoratorMetadata in tsconfig.json

     @route(path, HttpMethod, ...middleware)
     optional middlewares are added before the target method. 
    

    See trust-broker for more examples

    Example

    import {route, HttpMethod} from 'koa-router-decorators';
    import User from '../models/User'
     
    @route('/users')
    export default class UserController {
     
      router:Router;
     
      constructor() {
        return this.router.routes();
      }
      
      @route('/', HttpMethod.GET, isAdmin)
      static *index(next) {
        let query = User.find().skip(0).limit(20);
        let users = yield query.exec();
        let count = yield User.count();
        this.body = {users, count};
      }
     
      @route('/', HttpMethod.POST)
      static *create(next) {
        let newUser =  new User(this.request.body);
        let result;
        try {
          result = yield newUser.save();
        } catch (err) {
          this.throw( 'DB Error: Unable to save', 500);
        }
     
        this.status = 201;
        this.body = result
      }
    }
     
    function *isAdmin(next) {
      if (!this.state.user.roles.includes('admin')) {
        throw new AuthorizationError(AuthorizationError.code.FORBIDDEN, {message: 'insufficient role (admin only)'});
      }
      yield next;
    }

    Annotated routes are applied at the end. may overwrite manual added routes if path/method matches.

    import koa from 'koa';
    import Router from 'koa-router';
    import bodyParser from'koa-bodyparser';
    import UserController from  './controllers/UserController';
     
    rootRouter = new Router({
      prefix: '/api'
    });
     
    app = koa();
    app.use(bodyParser());
    app.use(new AuthController());
    rootRouter.use('/v1', new UserController());
    app
      .use(rootRouter.routes())
      .use(rootRouter.allowedMethods());
    app.listen(3000);

    Development

    You need typescript installed globally

    npm install -g typescript
    npm install -g tslint

    build

    npm run compile  # or just `tsc` 

    test

    npm test # bug :  if you see error: remove "pretest": "tsc -p ./test" from package.json and try again. 

    publish to npm registry

    npm publish

    Maintain

    npm outdated -g --depth=0
    npm outdated  --depth=0
    

    Install

    npm i koa-router-decorators-async

    DownloadsWeekly Downloads

    1

    Version

    0.4.6

    License

    MIT

    Last publish

    Collaborators

    • avatar