Skip to content

Commit

Permalink
fix(dev): preserve original handler in [composedHandler]. (#1873)
Browse files Browse the repository at this point in the history
* fix(dev): preserve original handler in [composedHandler].

* chore: denoify

* refactor(dev): better variable names
  • Loading branch information
usualoma authored and yusukebe committed Jan 2, 2024
1 parent 405de0d commit d5d2e23
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 22 deletions.
23 changes: 17 additions & 6 deletions deno_dist/helper/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Hono } from '../../hono.ts'
import { COMPOSED_HANDLER } from '../../hono-base.ts'
import type { Env, RouterRoute } from '../../types.ts'

interface ShowRoutesOptions {
Expand All @@ -16,14 +17,24 @@ const isMiddleware = (handler: Function) => handler.length > 1
const handlerName = (handler: Function) => {
return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')
}
const findTargetHandler = (handler: Function): Function => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (handler as any)[COMPOSED_HANDLER]
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
findTargetHandler((handler as any)[COMPOSED_HANDLER])
: handler
}

export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {
return hono.routes.map(({ path, method, handler }: RouterRoute) => ({
path,
method,
name: handlerName(handler),
isMiddleware: isMiddleware(handler),
}))
return hono.routes.map(({ path, method, handler }: RouterRoute) => {
const targetHandler = findTargetHandler(handler)
return {
path,
method,
name: handlerName(targetHandler),
isMiddleware: isMiddleware(targetHandler),
}
})
}

export const showRoutes = <E extends Env>(hono: Hono<E>, opts?: ShowRoutesOptions) => {
Expand Down
17 changes: 12 additions & 5 deletions deno_dist/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import type {
} from './types.ts'
import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url.ts'

export const COMPOSED_HANDLER = Symbol('composedHandler')

type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE

function defineDynamicClass(): {
Expand Down Expand Up @@ -158,11 +160,16 @@ class Hono<
}

app.routes.map((r) => {
const handler =
app.errorHandler === errorHandler
? r.handler
: async (c: Context, next: Next) =>
(await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res
let handler
if (app.errorHandler === errorHandler) {
handler = r.handler
} else {
handler = async (c: Context, next: Next) =>
(await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(handler as any)[COMPOSED_HANDLER] = r.handler
}

subApp.addRoute(r.method, r.path, handler)
})
return this
Expand Down
18 changes: 18 additions & 0 deletions src/helper/dev/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ describe('inspectRoutes()', () => {
{ path: '/static', method: 'GET', name: '[handler]', isMiddleware: false },
])
})

it('should return [handler] also for sub app', async () => {
const subApp = new Hono()

subApp.get('/', (c) => c.json(0))
subApp.onError((_, c) => c.json(0))

const mainApp = new Hono()
mainApp.route('/', subApp)
expect(inspectRoutes(mainApp)).toEqual([
{
isMiddleware: false,
method: 'GET',
name: '[handler]',
path: '/',
},
])
})
})

describe('showRoutes()', () => {
Expand Down
23 changes: 17 additions & 6 deletions src/helper/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Hono } from '../../hono'
import { COMPOSED_HANDLER } from '../../hono-base'
import type { Env, RouterRoute } from '../../types'

interface ShowRoutesOptions {
Expand All @@ -16,14 +17,24 @@ const isMiddleware = (handler: Function) => handler.length > 1
const handlerName = (handler: Function) => {
return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')
}
const findTargetHandler = (handler: Function): Function => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (handler as any)[COMPOSED_HANDLER]
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
findTargetHandler((handler as any)[COMPOSED_HANDLER])
: handler
}

export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {
return hono.routes.map(({ path, method, handler }: RouterRoute) => ({
path,
method,
name: handlerName(handler),
isMiddleware: isMiddleware(handler),
}))
return hono.routes.map(({ path, method, handler }: RouterRoute) => {
const targetHandler = findTargetHandler(handler)
return {
path,
method,
name: handlerName(targetHandler),
isMiddleware: isMiddleware(targetHandler),
}
})
}

export const showRoutes = <E extends Env>(hono: Hono<E>, opts?: ShowRoutesOptions) => {
Expand Down
17 changes: 12 additions & 5 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import type {
} from './types'
import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'

export const COMPOSED_HANDLER = Symbol('composedHandler')

type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE

function defineDynamicClass(): {
Expand Down Expand Up @@ -158,11 +160,16 @@ class Hono<
}

app.routes.map((r) => {
const handler =
app.errorHandler === errorHandler
? r.handler
: async (c: Context, next: Next) =>
(await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res
let handler
if (app.errorHandler === errorHandler) {
handler = r.handler
} else {
handler = async (c: Context, next: Next) =>
(await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(handler as any)[COMPOSED_HANDLER] = r.handler
}

subApp.addRoute(r.method, r.path, handler)
})
return this
Expand Down

0 comments on commit d5d2e23

Please sign in to comment.