Skip to content
Myr

fix(dev): preserve original handler in [composedHandler]. (#1873)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(dev): preserve original handler in [composedHandler]. * chore: denoify * refactor(dev): better variable names

authored by Taku Amano · committed by GitHub · parent 7a86bb0 · Browse files · 5 files+76 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+76 added−22 removed

  • tests:+18 added−0 removed
  • other:+58 added−22 removed

5 files+76 added−22 removed

modified:deno_dist/helper/dev/index.ts+17 added-6 removed
modified:deno_dist/hono-base.ts+12 added-5 removed
21 unmodified lines
22 RouterRoute,23} from './types.ts'24import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url.ts'2526export const COMPOSED_HANDLER = Symbol('composedHandler')2728type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE2930function defineDynamicClass(): {31 new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {
127 unmodified lines
159 return subApp160 }161162 app.routes.map((r) => {161 const handler =162 app.errorHandler === errorHandler163 ? r.handler164 : async (c: Context, next: Next) =>165 (await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res163 let handler164 if (app.errorHandler === errorHandler) {165 handler = r.handler166 } else {167 handler = async (c: Context, next: Next) =>168 (await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res169 // eslint-disable-next-line @typescript-eslint/no-explicit-any170 ;(handler as any)[COMPOSED_HANDLER] = r.handler171 }172173 subApp.addRoute(r.method, r.path, handler)174 })175 return this176 }
210 unmodified lines
modified:src/helper/dev/index.test.ts+18 added
modified:src/helper/dev/index.ts+17 added-6 removed
1import type { Hono } from '../../hono'2import { COMPOSED_HANDLER } from '../../hono-base'3import type { Env, RouterRoute } from '../../types'45interface ShowRoutesOptions {6 verbose?: boolean
9 unmodified lines
16const isMiddleware = (handler: Function) => handler.length > 117const handlerName = (handler: Function) => {18 return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')19}20const findTargetHandler = (handler: Function): Function => {21 // eslint-disable-next-line @typescript-eslint/no-explicit-any22 return (handler as any)[COMPOSED_HANDLER]23 ? // eslint-disable-next-line @typescript-eslint/no-explicit-any24 findTargetHandler((handler as any)[COMPOSED_HANDLER])25 : handler26}2728export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {29 return hono.routes.map(({ path, method, handler }: RouterRoute) => {30 const targetHandler = findTargetHandler(handler)21 return hono.routes.map(({ path, method, handler }: RouterRoute) => ({22 path,23 method,24 name: handlerName(handler),25 isMiddleware: isMiddleware(handler),26 }))31 return {32 path,33 method,34 name: handlerName(targetHandler),35 isMiddleware: isMiddleware(targetHandler),36 }37 })38}3940export const showRoutes = <E extends Env>(hono: Hono<E>, opts?: ShowRoutesOptions) => {41 const routeData: Record<string, RouteData[]> = {}
32 unmodified lines
modified:src/hono-base.ts+12 added-5 removed
21 unmodified lines
22 RouterRoute,23} from './types'24import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'2526export const COMPOSED_HANDLER = Symbol('composedHandler')2728type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE2930function defineDynamicClass(): {31 new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {
127 unmodified lines
159 return subApp160 }161162 app.routes.map((r) => {161 const handler =162 app.errorHandler === errorHandler163 ? r.handler164 : async (c: Context, next: Next) =>165 (await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res163 let handler164 if (app.errorHandler === errorHandler) {165 handler = r.handler166 } else {167 handler = async (c: Context, next: Next) =>168 (await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res169 // eslint-disable-next-line @typescript-eslint/no-explicit-any170 ;(handler as any)[COMPOSED_HANDLER] = r.handler171 }172173 subApp.addRoute(r.method, r.path, handler)174 })175 return this176 }
210 unmodified lines