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

feat(hono-base): introduce replaceRequest option instead of rewritePath #2873

Merged
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
72 changes: 36 additions & 36 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
RouterRoute,
Schema,
} from './types'
import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'
import { getPath, getPathNoStrict, mergePath } from './utils/url'

export const COMPOSED_HANDLER = Symbol('composedHandler')

Expand Down Expand Up @@ -82,12 +82,12 @@ export type HonoOptions<E extends Env> = {
}

type MountOptionHandler = (c: Context) => unknown
type MountRewritePath = (path: string) => string
type MountReplaceRequest = (originalRequest: Request) => Request
type MountOptions =
| MountOptionHandler
| {
optionHandler?: MountOptionHandler
rewritePath?: MountRewritePath
replaceRequest?: MountReplaceRequest
}

class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> {
Expand Down Expand Up @@ -258,43 +258,43 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>,
options?: MountOptions
): Hono<E, S, BasePath> {
const handler: MiddlewareHandler = async (c, next) => {
let executionContext: ExecutionContext | undefined = undefined
try {
executionContext = c.executionCtx
} catch {} // Do nothing

let rewritePath: MountRewritePath | undefined
let optionHandler: MountOptionHandler | undefined

if (options) {
if (typeof options === 'function') {
optionHandler = options
} else {
rewritePath = options.rewritePath
optionHandler = options.optionHandler
}
// handle options
let replaceRequest: MountReplaceRequest | undefined
let optionHandler: MountOptionHandler | undefined
if (options) {
if (typeof options === 'function') {
optionHandler = options
} else {
optionHandler = options.optionHandler
replaceRequest = options.replaceRequest
}
}

const applicationOptions = optionHandler ? [optionHandler(c)] : [c.env, executionContext]
const queryStrings = getQueryStrings(c.req.url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we don't need getQueryStrings!


const defaultRewritePath = () => {
const mergedPath = mergePath(this._basePath, path)
const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length
return c.req.path.slice(pathPrefixLength) || '/'
// prepare handlers for request
const getOptions: (c: Context) => unknown[] = optionHandler
? (c) => {
const options = optionHandler(c)
return Array.isArray(options) ? options : [options]
}
: (c) => {
let executionContext: ExecutionContext | undefined = undefined
try {
executionContext = c.executionCtx
} catch {} // Do nothing
return [c.env, executionContext]
}
replaceRequest ||= (() => {
const mergedPath = mergePath(this._basePath, path)
const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length
return (request) => {
const url = new URL(request.url)
url.pathname = url.pathname.slice(pathPrefixLength) || '/'
return new Request(url, request)
}
})()

const res = await applicationHandler(
new Request(
new URL(
rewritePath ? rewritePath(c.req.path) : defaultRewritePath() + queryStrings,
c.req.url
),
c.req.raw
),
...applicationOptions
)
const handler: MiddlewareHandler = async (c, next) => {
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c))

if (res) {
return res
Expand Down
2 changes: 1 addition & 1 deletion src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,7 @@ describe('app.mount()', () => {

const app = new Hono()
app.mount('/app', anotherApp, {
rewritePath: (path) => path,
replaceRequest: (req) => req,
})

it('Should return 200 response with the correct path', async () => {
Expand Down