Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/json-filter
Middleware allowing the client to filter the response to only what they need, reducing the amount of traffic over the wire.
https://github.com/koajs/json-filter
Last synced: 27 days ago
JSON representation
Middleware allowing the client to filter the response to only what they need, reducing the amount of traffic over the wire.
- Host: GitHub
- URL: https://github.com/koajs/json-filter
- Owner: koajs
- Created: 2014-01-07T06:05:06.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-04-10T08:17:26.000Z (over 6 years ago)
- Last Synced: 2024-04-14T13:08:40.275Z (7 months ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 92
- Watchers: 3
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
- awesome-koa - json-filter - Middleware allowing the client to filter the response to only what they need, reducing the amount of traffic over the wire. (Middleware)
- awesome-koa - koa-json-filter - 该中间件允许客户端只过滤他们需要的响应,减少线路上的流量。 ![](https://img.shields.io/github/stars/koajs/json-filter.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-json-filter.svg?style=flat-square) (仓库 / 中间件)
README
# 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
```Please note that if you're using an earlier version of koa 2 with function generator you need to install the older version `0.0.1`
```
$ npm install [email protected]
```## 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:
```js
const Koa = require('koa');
const filter = require('@koa/json-filter');const app = new Koa();
app.use(filter());
app.use(async ctx => {
ctx.body = {
name: 'tobi',
email: '[email protected]',
packages: 5,
friends: ['abby', 'loki', 'jane']
};
});app.listen(3000);
console.log('app listening on port 3000');
```Response:
```
$ GET /?filter=name
{
"name": "tobi"
}
```### Array responses
Script:
```js
const Koa = require('koa');
const filter = require('@koa/json-filter');const app = new Koa();
app.use(filter());
app.use(async ctx => {
ctx.body = [
{
name: 'tobi',
email: '[email protected]',
packages: 5,
friends: ['abby', 'loki', 'jane']
},
{
name: 'loki',
email: '[email protected]',
packages: 2,
friends: ['loki', 'jane']
},
{
name: 'jane',
email: '[email protected]',
packages: 2,
friends: []
},
{
name: 'ewald',
email: '[email protected]',
packages: 2,
friends: ['tobi']
}
];
});app.listen(3000);
console.log('app listening on port 3000');
```Response:
```
$ GET /?filter=name,email
[
{
"name": "tobi",
"email": "[email protected]"
},
{
"name": "loki",
"email": "[email protected]"
},
{
"name": "jane",
"email": "[email protected]"
},
{
"name": "ewald",
"email": "[email protected]"
}
]
```# License
MIT