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
Provenance
- Commit
813484d- Tree
0d45cb4- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
7a86bb0
5 files changed+76 added−22 removed
modified:deno_dist/helper/dev/index.ts+17 added-6 removed
1234569 unmodified lines161718192021222324252627282930212223242526313233343536373839404132 unmodified linesimport type { Hono } from '../../hono.ts'import { COMPOSED_HANDLER } from '../../hono-base.ts'import type { Env, RouterRoute } from '../../types.ts'
interface ShowRoutesOptions { verbose?: boolean9 unmodified linesconst isMiddleware = (handler: Function) => handler.length > 1const 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) => { const targetHandler = findTargetHandler(handler) return hono.routes.map(({ path, method, handler }: RouterRoute) => ({ path, method, name: handlerName(handler), isMiddleware: isMiddleware(handler), })) return { path, method, name: handlerName(targetHandler), isMiddleware: isMiddleware(targetHandler), } })}
export const showRoutes = <E extends Env>(hono: Hono<E>, opts?: ShowRoutesOptions) => { const routeData: Record<string, RouteData[]> = {}32 unmodified linesmodified:deno_dist/hono-base.ts+12 added-5 removed
21 unmodified lines22 RouterRoute,23 } from './types.ts'24 import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url.ts'25 26+export const COMPOSED_HANDLER = Symbol('composedHandler')27+28 type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE29 30 function defineDynamicClass(): {31 new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {127 unmodified lines159 return subApp160 }161 162 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+ }172+173 subApp.addRoute(r.method, r.path, handler)174 })175 return this176 }210 unmodified linesmodified:src/helper/dev/index.test.ts+18 added
33 unmodified lines343536373839404142434445464748495051525354555657585953 unmodified lines33 unmodified lines { path: '/', method: 'OPTIONS', name: '[handler]', isMiddleware: false }, { 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()', () => { let logs: string[] = []53 unmodified linesmodified:src/helper/dev/index.ts+17 added-6 removed
1 import type { Hono } from '../../hono'2+import { COMPOSED_HANDLER } from '../../hono-base'3 import type { Env, RouterRoute } from '../../types'4 5 interface ShowRoutesOptions {6 verbose?: boolean9 unmodified lines16 const isMiddleware = (handler: Function) => handler.length > 117 const handlerName = (handler: Function) => {18 return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')19 }20+const 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+}27 28 export 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 }39 40 export const showRoutes = <E extends Env>(hono: Hono<E>, opts?: ShowRoutesOptions) => {41 const routeData: Record<string, RouteData[]> = {}32 unmodified linesmodified:src/hono-base.ts+12 added-5 removed
21 unmodified lines22 RouterRoute,23 } from './types'24 import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'25 26+export const COMPOSED_HANDLER = Symbol('composedHandler')27+28 type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE29 30 function defineDynamicClass(): {31 new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {127 unmodified lines159 return subApp160 }161 162 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+ }172+173 subApp.addRoute(r.method, r.path, handler)174 })175 return this176 }210 unmodified lines