0.5.2 • Published 5 years ago

express-response-filter v0.5.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Express Response Filter Middleware NPM version

This Express Middleware will allow you to send a subset of a JSON object instead of an entire object from your HTTP services. To do so, your services will begin accepting the ?fields= query-string that, using a simple language, will specify which fields and sub-feelds to keep and which to ignore.

If you've used the Google APIs, provided a ?fields= query-string to get a Partial Response, and wanted to do the same for your own server, now you can do so with this middleware.

Underneath, this middleware uses json-mask. Use it directly without this middleware if you need more flexibility.

Installation

npm install express-response-filter --save

Usage

var express = require('express')
  , responseFilter = require('express-response-filter')
  , app = express()

app.use(responseFilter({
  filter: ["test"],
}))

app.get('/', function (req, res, next) {
  // keyword filter, not influenced by user query input
  req.customFilter = ["id", "password", "token"]; // concat to opt.filters array

  res.json({
      firstName: 'Mohandas'
    , lastName: 'Gandhi'
    , aliases: [{
          firstName: 'Mahatma'
        , lastName: 'Gandhi'
      }, {
          firstName: 'Bapu'
      }]
  })
})

app.listen(4000)

Let's test it:

$ curl 'http://localhost:4000'
{"firstName":"Mohandas","lastName":"Gandhi","aliases":[{"firstName":"Mahatma","lastName":"Gandhi"},{"firstName":"Bapu"}]}

$ # Let's just get the first name
$ curl 'http://localhost:4000?fields=lastName'
{"lastName":"Gandhi"}

$ # Now, let's just get the first names directly as well as from aliases
$ curl 'http://localhost:4000?fields=firstName,aliases(firstName)'
{"firstName":"Mohandas","aliases":[{"firstName":"Mahatma"},{"firstName":"Bapu"}]}

Note: take a look at /example.

Syntax

Look at json-mask for the available syntax of the fields param.

Options

query specifies the query-string to use. Defaults to fields prefix specifies the query-string prefix. filter specifies the filter before json-masked. Array | Function is allowed

app.use(responseFilter({
  query: 'filter'
}))

License

MIT. See LICENSE

Reference To https://github.com/nemtsov/express-partial-response/

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.0

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago