README
Koa Params Extractor
Extract Koa's request parameters
Helps to get parameters (headers, params, query, body) that can be distributed or in different locations in a consistent way
- find in alias location. (ctx.xxx)
- find in req location. (ctx.req.xxx)
- find in request location. (ctx.request.xxx)
Install
$ npm i koa-params-extractor
Usage
const paramsExtractor = require('koa-params-extractor');
router.post('/user', async (ctx) => {
const { id, username, password } = paramsExtractor.extract(ctx);
const response = await signUpUser({ id, username, password });
ctx.body = response;
});
Extract
paramsExtractor.extract(ctx, parameters = [parameter.headers, parameter.params, parameter.query, parameter.body]);
Parameters
function Parameter(
where = new Where(null, true, true, true),
options = { name: null, combineLevel: 0, as: null, index: undefined }
) {
this.where = where;
this.name = options.name;
this.combineLevel = options.combineLevel;
this.as = options.as;
this.index = options.index;
}
function Where(name, koaRequest, nodeRequest, context) {
this.name = name;
this.koaRequest = koaRequest;
this.nodeRequest = nodeRequest;
this.context = context;
}
Parameters defines the information of the parameters to pass.
where
defines where to find the parameter.- If
where
is not an instance ofWhere
, the parameter iswhere
. - If
where
is an instance ofWhere
, the parameter is found inctx
with information fromwhere
.
- If
name
is the name of the parameter.- If
name
exists, the same name is taken from the parameter's location.
- If
combineLevel
is the level at which the imported arguments are to be combined.0
means the imported parameter is the parameter to pass.1
means the imported parameter is a child of the parameter to pass.
as
specifies a name when passing a parameter.index
is index of the parameter to pass.
Where defines where to find the parameter.
name
is the name of the location from which to retrieve the parameter.koaRequest
means to find the location of a parameter inctx.request
.nodeRequest
means to find the location of a parameter inctx.req
.context
means to find the location of a parameter inctx
.
The default value is params, query, header, body, cookies defined.