Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: feat(middleware/hook): introduce hook middleware #3025

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/middleware/hook/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { Context } from '../../context'
import type { Env, Handler, MiddlewareHandler, Next } from '../../types'

const isWrapped = Symbol()

export type Hook = (
c: Context,
handler: Handler,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handlerContext: Record<string, any>,
traceId: string
) => void
export const hook = <E extends Env = Env>(
options: {
before?: Hook
beforeNext?: Hook
afterNext?: Hook
after?: Hook
} = {}
): MiddlewareHandler => {
function hook(c: Context<E>, next: Next) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
c.set('middleware-hook-trace-id' as any, Math.random().toString(16).slice(2))

// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(c.req.matchResult[0] as unknown as [[any]][]).forEach((routeData) => {
if (routeData[0][0][isWrapped]) {
return
}

const handler = routeData[0][0]
const name = handler.name || ''
routeData[0][0] = {
[name]: function (c: Context, next: Next) {
const handlerContext = Object.create(null)
const traceId = c.get('middleware-hook-trace-id')

if (options.before) {
options.before?.(c, handler, handlerContext, traceId)
}
const internalNext = () => {
options.beforeNext?.(c, handler, handlerContext, traceId)
const res = next()
res.finally(() => options.afterNext?.(c, handler, handlerContext, traceId))
return res
}
const res = handler(c, internalNext)
if (res instanceof Promise) {
res.finally(() => options.after?.(c, handler, handlerContext, traceId))
} else {
options.after?.(c, handler, handlerContext, traceId)
}
return res
},
}[name]
routeData[0][0][isWrapped] = true
})
return next()
}
hook[isWrapped] = true
return hook
}

Check warning on line 62 in src/middleware/hook/index.ts

View check run for this annotation

Codecov / codecov/patch

src/middleware/hook/index.ts#L1-L62

Added lines #L1 - L62 were not covered by tests
4 changes: 4 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@
return this.raw.method
}

get matchResult(): Result<[unknown, RouterRoute]> {
return this.#matchResult
}

Check warning on line 363 in src/request.ts

View check run for this annotation

Codecov / codecov/patch

src/request.ts#L362-L363

Added lines #L362 - L363 were not covered by tests

/**
* `.matchedRoutes()` can return a matched route in the handler
*
Expand Down