From 417fbac15381483ce25133beca5014b9ac18a075 Mon Sep 17 00:00:00 2001 From: Juri Date: Thu, 1 Jun 2017 18:14:41 +0300 Subject: [PATCH] Changed hooks to use `hook.service` instead of `this` (#18) This fixes the problem that hooks lose context of this when used inside other hooks. Fixes #17: Hook "restrictToOwner" looses context (this) when wrapped --- src/has-role-or-restrict.js | 6 +++--- src/restrict-to-owner.js | 2 +- src/restrict-to-roles.js | 2 +- test/has-role-or-restrict.test.js | 14 ++++++++------ test/restrict-to-owner.test.js | 3 ++- test/restrict-to-roles.test.js | 3 ++- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/has-role-or-restrict.js b/src/has-role-or-restrict.js index b33b914..5fd4441 100644 --- a/src/has-role-or-restrict.js +++ b/src/has-role-or-restrict.js @@ -53,7 +53,7 @@ export default function (options = {}) { return hook; } - return this.find({ query }, params).then(results => { + return hook.service.find({ query }, params).then(results => { if (hook.method === 'get' && Array.isArray(results) && results.length === 1) { hook.result = results[0]; return hook; @@ -103,7 +103,7 @@ export default function (options = {}) { // set on the resource we are requesting. const params = Object.assign({}, hook.params, { provider: undefined }); - this.get(hook.id, params).then(data => { + hook.service.get(hook.id, params).then(data => { if (data.toJSON) { data = data.toJSON(); } else if (data.toObject) { @@ -131,7 +131,7 @@ export default function (options = {}) { return hook; } - return this.find({ query }, params).then(results => { + return hook.service.find({ query }, params).then(results => { if (hook.method === 'get' && Array.isArray(results) && results.length === 1) { hook.result = results[0]; return hook; diff --git a/src/restrict-to-owner.js b/src/restrict-to-owner.js index 9cc6eb8..9b4d7e7 100644 --- a/src/restrict-to-owner.js +++ b/src/restrict-to-owner.js @@ -50,7 +50,7 @@ export default function (options = {}) { // set on the resource we are requesting. const params = Object.assign({}, hook.params, { provider: undefined }); - return this.get(hook.id, params).then(data => { + return hook.service.get(hook.id, params).then(data => { if (data.toJSON) { data = data.toJSON(); } else if (data.toObject) { diff --git a/src/restrict-to-roles.js b/src/restrict-to-roles.js index addacab..5f2a19c 100644 --- a/src/restrict-to-roles.js +++ b/src/restrict-to-roles.js @@ -72,7 +72,7 @@ export default function (options = {}) { // set on the resource we are requesting. const params = Object.assign({}, hook.params, { provider: undefined }); - return this.get(hook.id, params).then(data => { + return hook.service.get(hook.id, params).then(data => { if (data.toJSON) { data = data.toJSON(); } else if (data.toObject) { diff --git a/test/has-role-or-restrict.test.js b/test/has-role-or-restrict.test.js index 6c36281..f7fd92a 100644 --- a/test/has-role-or-restrict.test.js +++ b/test/has-role-or-restrict.test.js @@ -81,9 +81,9 @@ describe('hasRoleOrRestrict', () => { it('should merge the restriction in to the query and call find', () => { let hook = { app: { - service: mockService, get: function () {} }, + service: MockService, method: 'find', type: 'before', params: { @@ -100,9 +100,9 @@ describe('hasRoleOrRestrict', () => { let hook = { id: '525235', app: { - service: MockService, get: function () {} }, + service: MockService, method: 'find', type: 'before', params: { @@ -132,7 +132,8 @@ describe('hasRoleOrRestrict', () => { }, app: { get: function () { return {}; } - } + }, + service: MockService }; }); @@ -171,9 +172,9 @@ describe('hasRoleOrRestrict', () => { it('should merge the restriction in to the query and call find', () => { let hook = { app: { - service: mockService, get: function () {} }, + service: MockService, method: 'find', type: 'before', params: { @@ -191,9 +192,9 @@ describe('hasRoleOrRestrict', () => { id: '525235', method: 'find', app: { - service: MockService, get: function () {} }, + service: MockService, type: 'before', params: { provider: 'rest' @@ -276,7 +277,8 @@ describe('hasRoleOrRestrict', () => { }, app: { get: function () { return {}; } - } + }, + service: MockService }; }); diff --git a/test/restrict-to-owner.test.js b/test/restrict-to-owner.test.js index 9b6a4dc..b0c85c2 100644 --- a/test/restrict-to-owner.test.js +++ b/test/restrict-to-owner.test.js @@ -111,7 +111,8 @@ describe('restrictToOwner', () => { provider: 'rest', user: { _id: '1' } }, - app + app, + service: MockService }; }); diff --git a/test/restrict-to-roles.test.js b/test/restrict-to-roles.test.js index 8e2f9a2..4259895 100644 --- a/test/restrict-to-roles.test.js +++ b/test/restrict-to-roles.test.js @@ -100,7 +100,8 @@ describe('restrictToRoles', () => { }, app: { get: function () { return {}; } - } + }, + service: MockService }; });