From ea73fd827111ca19457758df18e4be8d9593aaa7 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Sun, 16 Dec 2018 16:22:07 -0800 Subject: [PATCH] fix: Add whitelist and filter support to common adapter service --- packages/adapter-commons/lib/service.js | 9 +++++++-- packages/adapter-commons/test/service.test.js | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/adapter-commons/lib/service.js b/packages/adapter-commons/lib/service.js index c2dddcd03b..446163cf47 100644 --- a/packages/adapter-commons/lib/service.js +++ b/packages/adapter-commons/lib/service.js @@ -34,11 +34,16 @@ module.exports = class AdapterService { return this.options.events; } - filterQuery (params = {}, options = {}) { + filterQuery (params = {}, opts = {}) { const paginate = typeof params.paginate !== 'undefined' ? params.paginate : this.options.paginate; const { query = {} } = params; - const result = filterQuery(query, Object.assign({ paginate }, options)); + const options = Object.assign({ + operators: this.options.whitelist || [], + filters: this.options.filters, + paginate + }, opts); + const result = filterQuery(query, options); return Object.assign(result, { paginate }); } diff --git a/packages/adapter-commons/test/service.test.js b/packages/adapter-commons/test/service.test.js index 9fd37400db..abf18d2df8 100644 --- a/packages/adapter-commons/test/service.test.js +++ b/packages/adapter-commons/test/service.test.js @@ -100,8 +100,10 @@ describe('@feathersjs/adapter-commons/service', () => { }); }); - it('getFilters', () => { - const service = new CustomService(); + it('filterQuery', () => { + const service = new CustomService({ + whitelist: [ '$something' ] + }); const filtered = service.filterQuery({ query: { $limit: 10, test: 'me' } }); @@ -111,5 +113,15 @@ describe('@feathersjs/adapter-commons/service', () => { filters: { $limit: 10 }, query: { test: 'me' } }); + + const withWhitelisted = service.filterQuery({ + query: { $limit: 10, $something: 'else' } + }); + + assert.deepStrictEqual(withWhitelisted, { + paginate: {}, + filters: { $limit: 10 }, + query: { $something: 'else' } + }); }); });