dekoa
Decorators for Koa
Last updated a year ago by jimzhan .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install dekoa 
SYNC missed versions from official npm registry.

dekoa - Decorators for Koa with :revolving_hearts:

build codecov Standard Version

JavaScript Style Guide Styled with Prettier Commitizen friendly dependencies npm version npm downloads

Handy decorators & middlewares dedicated for Koa, batteris included:

  • Class based routes supports (full HTTP method supports, RFC7231).
  • JSON Schema based validators (via koa-body).
  • Common middleware helpers.

Installation

npm install dekoa

Decorators

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

    • assuming we have all the view controllers under src/resources/.
    // src/server.js
    import Koa from 'koa'
    import glob from 'glob'
    import debug from 'debug'
    import * as dekoa from 'dekoa'
    
    const log = debug('debug')
    const server = new Koa()
    
    // all of the view controllers defined in `src/resources` will be automatically registered.
    const views = glob.sync(`${__dirname}/resources/*.js`)
    dekoa.bind(server, views, { 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 injected 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
      }
    }
    
  • JSON Schema, e.g. NewAccount.json.

    {
      "properties": {
        "username": {
          "type": "string",
          "format": "email",
          "minLength": 5,
          "maxLength": 255
        },
        "password": {
          "type": "string",
          "minLength": 6,
          "maxLength": 20
        }
      },
      "required": ["username", "password"]
    }
    
  • validate incoming form data

    const NewAccount = require('./NewAccount.json')
    
    @resource('inputs')
    export default class Input {
      @post('/', NewAccount)
      async create(ctx) {
        ctx.status = Status.CREATED
        ctx.body = { username: 'test@example.com' }
      }
    }
    
  • validate incoming http query (GET|HEAD|DELETE ONLY)

    const Account = require('./Account.json')
    
    @resource('inputs')
    export default class Input {
      @get('/', Account)
      async find(ctx) {
        ctx.status = Status.OK
        ctx.body = { username: 'test@example.com' }
      }
    }
    

Middlewares

  • XSRF (aka. CSRF) - built on top of CSRF, set for SPA without session dependency via cookie and header. Available options:
    • xsrfCookieName - cookie name for saving XSRF token (default xsrftoken).
    • xsrfHeaderName - http header name for responsing XSRF token, value is same as cookie's one (default X-XSRF-Token).
    • invalidTokenMessage - error message responded for client (default Invalid XSRF Token).
    • invalidTokenStatusCode - error http status code responded for client (default 403).
    • excludedMethods - methods bypass for XSRF token checking (default [ 'GET', 'HEAD', 'OPTIONS' ]).
    • renewPostWrite - whether XSRF token should be renew after each write (default: false).
import Koa from 'koa'
import { middleware } from 'dekoa'

const server = new Koa()
server.use(middleware.XSRF('<my-app-secret>'))
server.listen(port, () => {
  log(`Server started at port: ${port}`)
})

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.

Current Tags

  • 0.12.11                                ...           latest (a year ago)

55 Versions

  • 0.12.11                                ...           a year ago
  • 0.12.10                                ...           2 years ago
  • 0.12.9                                ...           2 years ago
  • 0.12.8                                ...           2 years ago
  • 0.12.7                                ...           2 years ago
  • 0.12.6                                ...           2 years ago
  • 0.12.5                                ...           2 years ago
  • 0.12.4                                ...           2 years ago
  • 0.12.3                                ...           2 years ago
  • 0.12.2                                ...           2 years ago
  • 0.12.1                                ...           2 years ago
  • 0.12.0                                ...           2 years ago
  • 0.11.9                                ...           2 years ago
  • 0.11.8                                ...           2 years ago
  • 0.11.7                                ...           2 years ago
  • 0.11.6                                ...           2 years ago
  • 0.11.5                                ...           2 years ago
  • 0.11.4                                ...           2 years ago
  • 0.11.3                                ...           2 years ago
  • 0.11.2                                ...           2 years ago
  • 0.11.1                                ...           2 years ago
  • 0.11.0                                ...           2 years ago
  • 0.10.1                                ...           2 years ago
  • 0.10.0                                ...           2 years ago
  • 0.9.8                                ...           2 years ago
  • 0.9.7                                ...           2 years ago
  • 0.9.6                                ...           2 years ago
  • 0.9.5                                ...           2 years ago
  • 0.9.4                                ...           2 years ago
  • 0.9.3                                ...           2 years ago
  • 0.9.2                                ...           2 years ago
  • 0.9.1                                ...           2 years ago
  • 0.9.0                                ...           2 years ago
  • 0.8.5                                ...           2 years ago
  • 0.8.4                                ...           2 years ago
  • 0.8.3                                ...           2 years ago
  • 0.8.2                                ...           2 years ago
  • 0.8.1                                ...           2 years ago
  • 0.8.0                                ...           2 years ago
  • 0.7.3                                ...           2 years ago
  • 0.7.2                                ...           2 years ago
  • 0.7.1                                ...           2 years ago
  • 0.7.0                                ...           2 years ago
  • 0.6.2                                ...           2 years ago
  • 0.6.1                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.3.1                                ...           2 years ago
  • 0.5.0                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.8                                ...           2 years ago
  • 0.2.7                                ...           2 years ago
  • 0.2.6                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (14)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |