Code Monkey home page Code Monkey logo

koa-strong-params's Introduction

koa-strong-params

Build Status NPM version Dependency Status

Rails-style implementation of strong parameters for Koa. The middleware adds the this.params object to the Koa context which returns an object, built from query string and request body data. The returned object has some useful methods allows for data requiring and filtering.

You should consider using koa-qs and koa-bodyparser packages together with koa-strong-params.

Installation

Install the npm package.

npm install koa-strong-params --save

Attach the middleware.

var koa = require('koa');
var params = require('koa-strong-params');
var app = koa();
app.use(params());

Example

var koa = require('koa');
var params = require('koa-strong-params');
var bodyparser = require('koa-bodyparser');
var qs = require('koa-qs')
var app = koa();
qs(app); // required for nested query string objects
app.use(bodyparser()); // required for params to include request body objects
app.use(params());
app.use(function *() {

  // all available params
  this.body = this.params.all();
  // -> { id: '13', name: 'Bob', age: '13', email: '[email protected]', address: { country: 'US', street: '261 West' }}

  // only selected params
  this.body = this.params.only('name', 'age');
  // -> { name: 'Bob', age: '13' }

  // all params except those provided
  this.body = this.params.except('name', 'id', 'address');
  // -> { id: '13', age: '13', email: '[email protected]' }

  // all params of a sub-object
  this.body = this.params.require('address').all();
  // -> { country: 'US', street: '261 West' }

  // only selected params + some merged attributes
  this.body = this.params.merge({ badge: 'coder' }).only('name');
  // -> { name: 'Bob', badge: 'coder' }

});
app.listen(3001);

koa-strong-params's People

Contributors

isakovp avatar xpepermint avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.