From 7b77236edd09babb461e1a00559be4167a58b9f9 Mon Sep 17 00:00:00 2001 From: Dmitry Maganov Date: Wed, 3 Jul 2019 01:46:44 +0300 Subject: [PATCH] feat: adapter-commons: add `allowsMulti(method)` to AdapterService --- packages/adapter-commons/lib/service.js | 32 +++++++--- packages/adapter-commons/test/service.test.js | 58 +++++++++++++++++++ 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/packages/adapter-commons/lib/service.js b/packages/adapter-commons/lib/service.js index 9e6dfc76c6..efbf192af5 100644 --- a/packages/adapter-commons/lib/service.js +++ b/packages/adapter-commons/lib/service.js @@ -9,12 +9,10 @@ const callMethod = (self, name, ...args) => { return self[name](...args); }; -const checkMulti = (method, option) => { - if (option === true) { - return true; - } - - return Array.isArray(option) ? option.includes(method) : false; +const alwaysMulti = { + find: true, + get: false, + update: false }; module.exports = class AdapterService { @@ -48,6 +46,22 @@ module.exports = class AdapterService { return Object.assign(result, { paginate }); } + allowsMulti (method) { + const always = alwaysMulti[method]; + + if (typeof always !== 'undefined') { + return always; + } + + const option = this.options.multi; + + if (option === true || option === false) { + return option; + } else { + return option.includes(method); + } + } + find (params) { return callMethod(this, '_find', params); } @@ -57,7 +71,7 @@ module.exports = class AdapterService { } create (data, params) { - if (Array.isArray(data) && !checkMulti('create', this.options.multi)) { + if (Array.isArray(data) && !this.allowsMulti('create')) { return Promise.reject(new MethodNotAllowed(`Can not create multiple entries`)); } @@ -75,7 +89,7 @@ module.exports = class AdapterService { } patch (id, data, params) { - if (id === null && !checkMulti('patch', this.options.multi)) { + if (id === null && !this.allowsMulti('patch')) { return Promise.reject(new MethodNotAllowed(`Can not patch multiple entries`)); } @@ -83,7 +97,7 @@ module.exports = class AdapterService { } remove (id, params) { - if (id === null && !checkMulti('remove', this.options.multi)) { + if (id === null && !this.allowsMulti('remove')) { return Promise.reject(new MethodNotAllowed(`Can not remove multiple entries`)); } diff --git a/packages/adapter-commons/test/service.test.js b/packages/adapter-commons/test/service.test.js index 712cc771fb..c46185a4a7 100644 --- a/packages/adapter-commons/test/service.test.js +++ b/packages/adapter-commons/test/service.test.js @@ -133,4 +133,62 @@ describe('@feathersjs/adapter-commons/service', () => { query: { $something: 'else' } }); }); + + it('allowsMulti', () => { + context('with true', () => { + const service = new AdapterService({multi: true}); + + it('does return true for multible methodes', () => { + assert.equal(service.allowsMulti('patch'), true); + }); + + it('does return false for always non-multible methodes', () => { + assert.equal(service.allowsMulti('update'), false); + }); + + it('does return true for unknown methods', () => { + assert.equal(service.allowsMulti('other'), true); + }); + }); + + context('with false', () => { + const service = new AdapterService({multi: false}); + + it('does return false for multible methodes', () => { + assert.equal(service.allowsMulti('remove'), false); + }); + + it('does return true for always multible methodes', () => { + assert.equal(service.allowsMulti('find'), true); + }); + + it('does return false for unknown methods', () => { + assert.equal(service.allowsMulti('other'), false); + }); + }); + + context('with array', () => { + const service = new AdapterService({multi: ['create', 'get', 'other']}); + + it('does return true for specified multible methodes', () => { + assert.equal(service.allowsMulti('create'), true); + }); + + it('does return false for non-specified multible methodes', () => { + assert.equal(service.allowsMulti('patch'), false); + }); + + it('does return false for specified always multible methodes', () => { + assert.equal(service.allowsMulti('get'), false); + }); + + it('does return true for specified unknown methodes', () => { + assert.equal(service.allowsMulti('other'), true); + }); + + it('does return false for non-specified unknown methodes', () => { + assert.equal(service.allowsMulti('another'), false); + }); + }); + }); });