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 💞
Handy decorators dedicated for Koa, batteris included:
- Class based routes supports (full HTTP method supports, RFC7231).
- JSON Schema based validators (
form,query,path).
Installation
npm install dekoaDecorators
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 { route } 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`) route.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 } }
- assuming we have all the view controllers under
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"] }
validators.js#form(
) const NewAccount = require('./NewAccount.json') @resource('inputs') export default class Input { @form(NewAccount) @post('/') async create(ctx) { ctx.status = Status.CREATED ctx.body = { username: 'test@example.com' } } }
validators.js#query(
) const Account = require('./Account.json') @resource('inputs') export default class Input { @query(Account) @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.