egg-parameters

1.1.2 • Public • Published

egg-parameters

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Merge all parameters (ctx.params, ctx.request.query, ctx.request.body) into ctx.params like Rails application.

Configuration

config/plugin.js

exports.parameters = {
  enable: true,
  package: 'egg-parameters',
};

config/config.default.js

exports.parameters = {
  logParameters: true,
  // param names that you want filter in log.
  filterParameters: ['password'],
};

Usage

When you add egg-parameters into your package.json this will enabled by default.

// app/controller/posts.js
module.exports = class {
  /**
   * POST /posts?title=jason&foo=1&user_id=123
   */
  async create() {
    const { ctx } = this;
    const postParam = ctx.params.permit('title', 'body')
 
    // postParam => { title: '', body: '' }
    // postParam.isPermitted() => true
    // :foo, :user_id will be filted
 
    // Now you can use safely for egg-sequelize create param
    const post = await ctx.model.Post.create(postParam);
    ctx.body = post;
  }
};

/egg-parameters/

    Package Sidebar

    Install

    npm i egg-parameters

    Weekly Downloads

    72

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    8.48 kB

    Total Files

    8

    Last publish

    Collaborators

    • akitasummer
    • gxkl
    • gemwuu
    • eggjs-admin
    • fengmk2
    • atian25
    • dead_horse
    • wanghx
    • hyj1991
    • killagu
    • coolme200
    • mansonchor.zzw
    • hubcarl
    • huacnlee