egg-parameters
v1.1.2
Published
Merge ctx.params, ctx.request.query, ctx.request.body into ctx.params"
Downloads
230
Maintainers
Keywords
Readme
egg-parameters
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;
}
};