8000 fix(core): Run after all hooks first, and then after method hooks by KidkArolis · Pull Request #3004 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions packages/feathers/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type HookStore = {
before: { [method: string]: HookFunction[] }
after: { [method: string]: HookFunction[] }
error: { [method: string]: HookFunction[] }
collected: { [method: string]: AroundHookFunction[] }
collected: { [method: string]: AroundHookFunction[] },
collectedAll: { before?: AroundHookFunction[], after?: AroundHookFunction[] }
}

type HookEnabled = { __hooks: HookStore }
Expand Down Expand Up @@ -55,13 +56,14 @@ export function convertHookData(input: any) {
}

export function collectHooks(target: HookEnabled, method: string) {
const { collected, around } = target.__hooks
const { collected, collectedAll, around } = target.__hooks

return [
...(around.all || []),
...(around[method] || []),
...(collected.all || []),
...(collected[method] || [])
...(collectedAll.before || []),
...(collected[method] || []),
...(collectedAll.after || []),
] as AroundHookFunction[]
}

Expand All @@ -72,7 +74,8 @@ export function enableHooks(object: any) {
before: {},
after: {},
error: {},
collected: {}
collected: {},
collectedAll: {},
}

Object.defineProperty(object, '__hooks', {
Expand Down Expand Up @@ -101,14 +104,31 @@ export function enableHooks(object: any) {

storeHooks.push(...mapHooks)

if (store.before[method] || store.after[method] || store.error[method]) {
const collected = collect({
before: store.before[method] || [],
after: store.after[method] || [],
error: store.error[method] || []
})

store.collected[method] = [collected]
if (method === 'all') {
if (store.before[method] || store.error[method]) {
const beforeAll = collect({
before: store.before[method] || [],
error: store.error[method] || []
})
store.collectedAll.before = [beforeAll]
}

if (store.after[method]) {
const afterAll = collect({
after: store.after[method] || [],
})
store.collectedAll.after = [afterAll]
}
} else {
if (store.before[method] || store.after[method] || store.error[method]) {
const collected = collect({
before: store.before[method] || [],
after: store.after[method] || [],
error: store.error[method] || []
})

store.collected[method] = [collected]
}
}
})
)
Expand Down
35 changes: 35 additions & 0 deletions packages/feathers/test/hooks/after.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,39 @@ describe('`after` hooks', () => {
test: 43
})
})

it('.after all and method specific hooks run in the correct order (#3002)', async () => {
const app = feathers().use('/dummy', {
async get(id: any) {
return { id, items: [] as any }
}
})
const service = app.service('dummy')

service.hooks({
after: {
all(context) {
context.result.items.push('first')

return context
},
get: [
function (context) {
context.result.items.push('second')

return context
},
function (context) {
context.result.items.push('third')

return context
}
]
}
})

const data = await service.get(10)

assert.deepStrictEqual(data.items, ['first', 'second', 'third'])
})
})
0