JSPM

  • Created
  • Published
  • Downloads 99
  • Score
    100M100P100Q71605F
  • License Apache-2.0

Decorators for Koa

Package Exports

  • dekoa

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dekoa) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

dekoa - Decorators for Koa with 💞

dependencies build npm version npm downloads

Handy decorators dedicated for Koa, batteris included:

  • Class based routes supports.

Installation

npm install dekoa

Decorators

  • route.js#bind(server, pattern, options)

    • assuming we have all the view controllers under src/resources/.

      // src/server.js
      import Koa from 'koa'
      import debug from 'debug';
      import { route } from 'dekoa'
      
      const log = debug('debug');
      const server = new Koa();
      
      // all of the view controllers defined in `src/resources` will be automatically registered.
      route.bind(server, `${__dirname}/resources/*.js`, { prefix: '/v1' });
      
      const port = process.env.PORT || 9394;
      server.listen(port, () => {
        log(`Server started at port: ${port}`);
      });
    • sample view controllers with decorators supports.

      // src/resources/accounts.js
      import Status from 'http-status-codes';
      import { resource, GET, POST } from 'dekoa/route';
      
      @resource('accounts')
      export default class Account {
        @GET('/:id')
        async findById(ctx) {
          const params = ctx.params;
          ctx.status = Status.OK;
          ctx.body = { id: params.id, username: 'test@example.com' };
        }
      
        @POST('/')
        async create(ctx) {
          ctx.status = Status.CREATED;
          ctx.body = { username: 'test@example.com' };
        }
      }
      import Status from 'http-status-codes';
      import { resource, POST } from 'dekoa/route';
      
      // `resource` decorator without prefix will be inject as top level URL.
      @resource
      export default class Auth {
        @POST('/login')
        async login(ctx) {
          ctx.status = Status.RESET_CONTENT;
        }
      
        @POST('/logout')
        async logout(ctx) {
          ctx.status = Status.RESET_CONTENT;
        }
      }