Skip to content

Commit

Permalink
fix(wobe): add hook on each handler not only on last children (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
coratgerl authored Jul 24, 2024
1 parent 2774bec commit 777639a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions packages/wobe/src/router/RadixTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,47 @@ describe('RadixTree', () => {
).toBe(1)
})

it('should add a hook with path * on beforeHandler with multiple route', () => {
const radixTree = new RadixTree()

radixTree.addRoute('GET', '/a/simple/route', () =>
Promise.resolve(),
)

radixTree.addRoute('GET', '/a/simple', () => Promise.resolve())

radixTree.addHook(
'beforeHandler',
'*',
() => Promise.resolve(),
'GET',
)

expect(
radixTree.root.children[0].children[0].children[0].name,
).toBe('/route')
expect(
radixTree.root.children[0].children[0].children[0].method,
).toBe('GET')
expect(
radixTree.root.children[0].children[0].children[0].handler,
).toBeDefined()

expect(
radixTree.root.children[0].children[0].beforeHandlerHook
?.length,
).toBe(1)

expect(
radixTree.root.children[0].children[0].children[0]
.beforeHandlerHook?.length,
).toBe(1)
expect(
radixTree.root.children[0].children[0].children[0]
.afterHandlerHook,
).toBeUndefined()
})

it('should add a hook with path * on beforeHandler', () => {
const radixTree = new RadixTree()

Expand Down
2 changes: 1 addition & 1 deletion packages/wobe/src/router/RadixTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class RadixTree {
const child = node.children[i]

if (
child.children.length === 0 &&
child.handler &&
(method === child.method || method === 'ALL')
)
this._addHookToNode(child, hook, handler)
Expand Down

0 comments on commit 777639a

Please sign in to comment.