2.0.0 • Published 10 years ago

koa-permit v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 years ago

koa-permit

travis-ci npmjs npmjs

A simple param filtering middleware. Inspired by Rails' strong parameters with support for nested filtering.

Installation

$ npm install --save koa-permit koa-bodyparser

import permit from 'koa-permit' // new syntax
const permit = require('koa-permit') // old syntax

Basic Usage

/*
 * app.js
 */

import koa from 'koa'
import router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import permit from 'koa-permit'

const app = koa()

const r = router()
r.post('/users',
  bodyParser(),
  permit('email', 'password'),
  function * () {
    const user = new User(this.request.body) // filtered params
    yield user.save()
    this.status = 201
  }
)

app.use(r.routes())

Nested filtering

/*
 * routes/users.js
 */

import router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import permit from 'koa-permit'

const userParams = [
  'email',
  'password',
  'profile.firstname',
  'profile.lastname',
  'profile.address.street',
  'profile.address.number'
]

export default const r = router()
r.post('/users',
  bodyParser(),
  permit(userParams),
  function * () {
    const user = new User(this.request.body) // filtered params
    yield user.save()
    this.status = 201
  }
)

/*
 * app.js
 */

import koa from 'koa'
import users from 'routes/users'

const app = koa()
app.use(users.routes())

Tests

$ npm test

Issues

Feel free to submit issues and enhancement requests.

Contributing

  1. Fork the repo on GitHub
  2. Commit changes to a branch in your fork
  3. Pull request "upstream" with your changes
  4. Merge changes in to "upstream" repo
1.1.0

10 years ago

2.0.0

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago