express-middleware-filter-data
This middleware allows to filter an object returned by res.json using a query parameter. Example:
const getFilterMiddleware = ;// "filter" will be the query parameter used for filteringconst filterMiddleware = ; // express middlewareapp;
The client, calling this endpoint can ask to filter the response:
http://localhost:3000/data?filter=total
and it will receive:
total: 123
Filtering expressions
The filtering expressions are very powerful. You can use multiple coma separated expressions. Like:
http://localhost:3000/data?filter=user.products // filters only the content of users.products
http://localhost:3000/data?filter=users[:10]name // first ten users names
The full syntax is explained here: https://github.com/sithmel/obj-sieve or https://github.com/sithmel/obj-path-expression-parser
Errors
If the filter is formally incorrect, it won't be used. You can provide a callback to manage the error (by default it is logged in the console).
const filterMiddleware = ;
Using obj-sieve instance
The middleware uses obj-sieve https://github.com/sithmel/obj-sieve. You can access the sieve instance on res.locals.sieve.