koa-json-filter
Middleware allowing the client to filter the response to only what they need,
reducing the amount of traffic over the wire using the ?filter=foo,bar,baz
querystring parameter.
JSONSelect would also be great for this but I find it's a little too complicated for the average use-case, so this is just a simple key filter.
Installation
$ npm install koa-json-filter
Options
name
querystring param defaulting to "filter"
Filtering customization
You may also set ctx.filter
to an array of names to filter on,
for example by using a header field X-Filter: name,email
.
Example
Object responses
Script:
var filter = ;var koa = ; var app = ; app; app; app;console;
Response:
$ GET /?filter=name
{
"name": "tobi"
}
Array responses
Script:
var filter = ;var koa = ; var app = ; app; app; app;console;
Response:
$ GET /?filter=name,email
[
{
"name": "tobi",
"email": "tobi@segment.io"
},
{
"name": "loki",
"email": "loki@segment.io"
},
{
"name": "jane",
"email": "jane@segment.io"
},
{
"name": "ewald",
"email": "ewald@segment.io"
}
]
License
MIT