mirror of
https://github.com/honojs/hono.git
synced 2024-11-30 11:20:41 +01:00
refactor(reg-exp-router): Routes are returned in the registration order. (#324)
This commit is contained in:
parent
f6c9592609
commit
3179adc1c2
@ -106,6 +106,22 @@ describe('Registration order', () => {
|
||||
expect(res).not.toBeNull()
|
||||
expect(res?.handlers).toEqual(['bar', 'foo'])
|
||||
})
|
||||
|
||||
it('middleware -> handler', async () => {
|
||||
router.add('GET', '*', 'bar')
|
||||
router.add('GET', '/:type/:action', 'foo')
|
||||
const res = router.match('GET', '/posts/123')
|
||||
expect(res).not.toBeNull()
|
||||
expect(res?.handlers).toEqual(['bar', 'foo'])
|
||||
})
|
||||
|
||||
it('handler -> fallback', async () => {
|
||||
router.add('GET', '/:type/:action', 'foo')
|
||||
router.add('GET', '*', 'fallback')
|
||||
const res = router.match('GET', '/posts/123')
|
||||
expect(res).not.toBeNull()
|
||||
expect(res?.handlers).toEqual(['foo', 'fallback'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('Optimization for METHOD_NAME_OF_ALL', () => {
|
||||
|
@ -16,7 +16,6 @@ interface Hint {
|
||||
interface HandlerWithSortIndex<T> {
|
||||
handler: T
|
||||
index: number
|
||||
componentsLength: number
|
||||
}
|
||||
interface Route<T> {
|
||||
method: string
|
||||
@ -110,9 +109,7 @@ function compareRoute<T>(a: Route<T>, b: Route<T>): CompareResult {
|
||||
}
|
||||
|
||||
function compareHandler(a: HandlerWithSortIndex<any>, b: HandlerWithSortIndex<any>) {
|
||||
return a.componentsLength !== b.componentsLength
|
||||
? a.componentsLength - b.componentsLength
|
||||
: a.index - b.index
|
||||
return a.index - b.index
|
||||
}
|
||||
|
||||
function getSortedHandlers<T>(
|
||||
@ -224,7 +221,6 @@ export class RegExpRouter<T> implements Router<T> {
|
||||
const handlerWithSortIndex = {
|
||||
index,
|
||||
handler,
|
||||
componentsLength: hint.components.length || 1,
|
||||
}
|
||||
|
||||
for (let i = 0, len = routes.length; i < len; i++) {
|
||||
@ -416,10 +412,8 @@ export class RegExpRouter<T> implements Router<T> {
|
||||
}
|
||||
|
||||
if (routes[j].hint.components.length < routes[i].hint.components.length) {
|
||||
const componentsLength = routes[j].hint.components.length || 1
|
||||
routes[j].middleware.push(
|
||||
...routes[i].handlers.map((h) => ({
|
||||
componentsLength,
|
||||
index: h.index,
|
||||
handler: h.handler,
|
||||
}))
|
||||
|
Loading…
Reference in New Issue
Block a user