hapi-query-filters

1.0.1 • Public • Published

Hapi Query Filter

Build Status Coverage Status NPM version Downloads

The purpose of this plugin is to convert query parameters into a single filter object that is accessible via request.query.filter.

For example: ?first_name=John&last_name=Doe would create a request.query that looks like

{
  filter: {
    first_name: 'John',
    last_name: 'Doe'
  }
}

Registering the Plugin

var Hapi = require('hapi');
var server = new Hapi.Server();
 
server.register([
  {
    register: require('hapi-query-filter'),
    options: {
      ignoredKeys: ['count', 'offset'], // Array of query parameters not to convert to filter object
      defaultEnabled: true // if true plugin will be used on all routes
    }
  }
], function (err) {
  // An error will be available here if anything goes wrong
});

Ignoring Keys

You can ignore keys to have them stay at the root level of request.query. A configuration of:

var Hapi = require('hapi');
var server = new Hapi.Server();
 
server.register([
  {
    register: require('hapi-query-filter'),
    options: {
      ignoredKeys: ['count', 'offset'], // Array of query parameters not to convert to filter object
      defaultEnabled: true // if true plugin will be used on all routes
    }
  }
], function (err) {
  // An error will be available here if anything goes wrong
});

Will cause a request like ?first_name=John&last_name=Doe&count=10&offset=0 to create a request.query that looks like:

{
  count: 10,
  offset: 0,
  filter: {
    first_name: 'John', 
    last_name: 'Doe'
  }
}

Enabling at the Route Level

If defaultEnabled: false you will need to enable the plugin an a per-route basis by doing the following:

var Hapi = require('hapi');
var server = new Hapi.Server();
 
server.register([
  {
    register: require('hapi-query-filter')
  }
], function (err) {
  // An error will be available here if anything goes wrong
});
 
server.route({
  method: 'GET',
  path: '/test',
  config: {
    handler: function (request, reply) { ... },
    plugins: {
      queryFilter: {
        enabled: true,
        ignoredKey: ['count', 'offset'], // Array will be concatenated with the ignoredKeys set at register
        params: ['test_param'] // Array of request.params that will be put into filter object
      }
    }
  }
})

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.1
    1
  • 1.0.0
    1

Package Sidebar

Install

npm i hapi-query-filters

Weekly Downloads

2

Version

1.0.1

License

MIT

Last publish

Collaborators

  • tanya.sah
  • hunteryoakum
  • rdimouro7373
  • lobstertroy
  • joshnkoy
  • michel_lob
  • haroutrs
  • kjones_lob
  • eamon-barisone
  • nathanielwaldschmidtlob
  • zach.reed
  • richpodrazalob
  • kencrim
  • jorgelob
  • nick-place-lob
  • andrew.guterres
  • ajorczak
  • hanqingchen-lob
  • juan.frissdekereki
  • mmorgan-lob
  • karankwatra-lob
  • joey-bates-lob
  • luke.birdeau
  • mg-lob
  • vmangwani
  • sachinlob
  • kc-lob
  • nick.perri
  • siddharthpant92
  • bethqiang
  • kplob
  • samkitsheth95
  • erin-doyle
  • rich_seviora_lob
  • jfdavidson
  • meussdorffer
  • shannamurry
  • davidnutting-lob
  • amaan_lob
  • team.platform.lob.com
  • elijah-lob
  • jlowsley-lob
  • barnabygo
  • james.cho
  • douglaje
  • lob-owner
  • graeme.lowe.lob