From 19af0651b02e6fe1d7887e4103b41767b34508c8 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 5 Oct 2021 16:04:11 -0700 Subject: [PATCH 1/2] fix(core): Allow to return and expand the hook context in basic hooks --- packages/feathers/src/hooks/legacy.ts | 22 ++++++++++--- packages/feathers/test/hooks/hooks.test.ts | 38 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/packages/feathers/src/hooks/legacy.ts b/packages/feathers/src/hooks/legacy.ts index 041ac6b08b..bb92a2245d 100644 --- a/packages/feathers/src/hooks/legacy.ts +++ b/packages/feathers/src/hooks/legacy.ts @@ -7,10 +7,14 @@ export function fromBeforeHook (hook: LegacyHookFunction) { return (context: any, next: any) => { context.type = 'before'; - return Promise.resolve(hook.call(context.self, context)).then(() => { - context.type = null; - return next(); - }); + return Promise.resolve(hook.call(context.self, context)) + .then(ctx => { + if (ctx && ctx !== context) { + Object.assign(context, ctx); + } + context.type = null; + return next(); + }); }; } @@ -19,7 +23,10 @@ export function fromAfterHook (hook: LegacyHookFunction) { return next().then(() => { context.type = 'after'; return hook.call(context.self, context) - }).then(() => { + }).then((ctx: any) => { + if (ctx && ctx !== context) { + Object.assign(context, ctx); + } context.type = null; }); } @@ -38,6 +45,11 @@ export function fromErrorHooks (hooks: LegacyHookFunction[]) { for (const hook of hooks) { promise = promise.then(() => hook.call(context.self, context)) + .then(ctx => { + if (ctx && ctx !== context) { + Object.assign(context, ctx); + } + }) } return promise.then(() => { diff --git a/packages/feathers/test/hooks/hooks.test.ts b/packages/feathers/test/hooks/hooks.test.ts index a6ecd6fb9b..8938a6b9be 100644 --- a/packages/feathers/test/hooks/hooks.test.ts +++ b/packages/feathers/test/hooks/hooks.test.ts @@ -354,4 +354,42 @@ describe('hooks basics', () => { params: {} }); }); + + it('allows to return new context in basic hooks (#2451)', async () => { + const app = feathers().use('/dummy', { + async get () { + return {}; + } + }); + const service = app.service('dummy'); + + service.hooks({ + before: { + get: [ + context => { + return { + ...context, + value: 'something' + }; + }, + context => { + assert.strictEqual(context.value, 'something'); + } + ] + }, + after: { + get: [context => { + context.result = { + value: context.value + } + }] + } + }); + + const data = await service.get(10); + + assert.deepStrictEqual(data, { + value: 'something' + }); + }); }); From 2c3058bab39354b701396b53ab2ea4eb1994e64f Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 5 Oct 2021 16:14:04 -0700 Subject: [PATCH 2/2] Make things more DRY --- packages/feathers/src/hooks/legacy.ts | 36 +++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/feathers/src/hooks/legacy.ts b/packages/feathers/src/hooks/legacy.ts index bb92a2245d..205248943b 100644 --- a/packages/feathers/src/hooks/legacy.ts +++ b/packages/feathers/src/hooks/legacy.ts @@ -2,16 +2,20 @@ import { _ } from '../dependencies'; import { LegacyHookFunction } from '../declarations'; const { each } = _; +const mergeContext = (context: any) => (res: any) => { + if (res && res !== context) { + Object.assign(context, res); + } + return res; +} export function fromBeforeHook (hook: LegacyHookFunction) { return (context: any, next: any) => { context.type = 'before'; return Promise.resolve(hook.call(context.self, context)) - .then(ctx => { - if (ctx && ctx !== context) { - Object.assign(context, ctx); - } + .then(mergeContext(context)) + .then(() => { context.type = null; return next(); }); @@ -20,15 +24,15 @@ export function fromBeforeHook (hook: LegacyHookFunction) { export function fromAfterHook (hook: LegacyHookFunction) { return (context: any, next: any) => { - return next().then(() => { - context.type = 'after'; - return hook.call(context.self, context) - }).then((ctx: any) => { - if (ctx && ctx !== context) { - Object.assign(context, ctx); - } - context.type = null; - }); + return next() + .then(() => { + context.type = 'after'; + return hook.call(context.self, context) + }) + .then(mergeContext(context)) + .then(() => { + context.type = null; + }); } } @@ -45,11 +49,7 @@ export function fromErrorHooks (hooks: LegacyHookFunction[]) { for (const hook of hooks) { promise = promise.then(() => hook.call(context.self, context)) - .then(ctx => { - if (ctx && ctx !== context) { - Object.assign(context, ctx); - } - }) + .then(mergeContext(context)) } return promise.then(() => {