Skip to content
Snippets Groups Projects
Commit 73273083 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Add dimensions middleware

EXAMPLE USAGE:

* Use it with no parameters to get all the dimensions in the query: `app.get('/', dimensions(), function(req, res, next){})`
* Use it with an array of accepted values: `app.get('/', dimensions(['year', 'location']), function(req, res, next){})`
* Use it globally: `app.use(dimensions())`
parent dceb479c
No related branches found
No related tags found
1 merge request!9Dimensions middleware
Pipeline #
/**
* Dimensions middleware
*
* EXAMPLE:
* Use it with no parameters to get all the dimensions specified
* app.get('/', dimensions(), function(req, res, next){})
*
* Use it with an array of accepted values
* app.get('/', dimensions(['year', 'location']), function(req, res, next){})
*
* Use it globally
* app.use(dimensions())
*/
function intersect(a, b) {
var t
if (b.length > a.length) t = b, b = a, a = t
return a.filter(function (e) {
if (b.indexOf(e) !== -1) return true
})
}
function dimensions(dims) {
return function(req, res, next) {
if(req.query.dims) {
params = req.query.dims.split(",")
// If the dims array exists and is not empty
if(typeof dims !== 'undefined' && dims.length > 0) {
req.dims = intersect(dims, params)
} else {
req.dims = params
}
}
console.log(req.dims)
next()
}
}
module.exports = dimensions
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment