Skip to content

Commit

Permalink
feat: adapter-commons: add allowsMulti(method) to AdapterService (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam authored and daffl committed Jul 3, 2019
1 parent b5ee7e2 commit e688851
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
32 changes: 23 additions & 9 deletions packages/adapter-commons/lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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`));
}

Expand All @@ -75,15 +89,15 @@ 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`));
}

return callMethod(this, '_patch', id, data, params);
}

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`));
}

Expand Down
58 changes: 58 additions & 0 deletions packages/adapter-commons/test/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

0 comments on commit e688851

Please sign in to comment.