Skip to content

Commit

Permalink
feat(hono-base): introduce replaceRequest option instead of `rewrit…
Browse files Browse the repository at this point in the history
…ePath`
  • Loading branch information
usualoma committed May 31, 2024
1 parent 6dcc3f2 commit 486315f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
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)

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

0 comments on commit 486315f

Please sign in to comment.