JSPM

  • Created
  • Published
  • Downloads 99
  • Score
    100M100P100Q71569F
  • 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 💞

build npm version npm downloads dependencies

Handy decorators dedicated for Koa, batteris included:

  • Class based routes supports (full HTTP method supports, RFC7231).
  • various validators (form, query etc.)

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';
    
    @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';
    
    // `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;
      }
    }
  • validators.js#form(fields)

    @resource('inputs')
    export default class Input {
      @form({ username: regex.email })
      @post('/')
      async create(ctx) {
        ctx.status = Status.CREATED;
        ctx.body = { username: 'test@example.com' };
      }
    }
  • validators.js#query(fields)

    @resource('inputs')
    export default class Input {
      @query({ username: regex.email })
      @post('/')
      async create(ctx) {
        ctx.status = Status.CREATED;
        ctx.body = { username: 'test@example.com' };
      }
    }

Regular Expression Helpers

  • dekoa.regex.chinese - chinese characters.
  • dekoa.regex.email - email address.
  • dekoa.regex.password - valid password (>= 6 bits, includes at least 1 lower & 1 upper letter, 1 number & 1 special character).
  • dekoa.regex.integer - positive/negative integer.
  • dekoa.regex.number - positive/negative number.
  • dekoa.regex.url - http/ftp/file address.
  • dekoa.regex.ipv4 - IP address version 4.