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): add replaceRequest option for app.mount #2852

Merged
merged 6 commits into from
Jun 3, 2024
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
76 changes: 56 additions & 20 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'

/**
* Symbol used to mark a composed handler.
Expand Down Expand Up @@ -91,6 +91,15 @@ export type HonoOptions<E extends Env> = {
getPath?: GetPath<E>
}

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

class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> {
get!: HandlerInterface<E, 'get', S, BasePath>
post!: HandlerInterface<E, 'post', S, BasePath>
Expand Down Expand Up @@ -296,7 +305,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
*
* @param {string} path - base Path
* @param {Function} applicationHandler - other Request Handler
* @param {Function | undefined} optionHandler - other Request Handler with Hono Context
* @param {MountOptions} [options] - options of `.mount()`
* @returns {Hono} mounted Hono instance
*
* @example
Expand All @@ -311,31 +320,58 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
* const app = new Hono()
* app.mount('/itty-router', ittyRouter.handle)
* ```
*
* @example
* ```ts
* const app = new Hono()
* // Send the request to another application without modification.
* app.mount('/app', anotherApp, {
* replaceRequest: (req) => req,
* })
* ```
*/
mount(
path: string,
applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>,
optionHandler?: (c: Context) => unknown
options?: MountOptions
): Hono<E, S, BasePath> {
const mergedPath = mergePath(this._basePath, path)
const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length
// 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
}
}

// 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 handler: MiddlewareHandler = async (c, next) => {
let executionContext: ExecutionContext | undefined = undefined
try {
executionContext = c.executionCtx
} catch {} // Do nothing
const options = optionHandler ? optionHandler(c) : [c.env, executionContext]
const optionsArray = Array.isArray(options) ? options : [options]

const queryStrings = getQueryStrings(c.req.url)
const res = await applicationHandler(
new Request(
new URL((c.req.path.slice(pathPrefixLength) || '/') + queryStrings, c.req.url),
c.req.raw
),
...optionsArray
)
const res = await applicationHandler(replaceRequest!(c.req.raw), ...getOptions(c))

if (res) {
return res
Expand Down
21 changes: 21 additions & 0 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,27 @@ describe('app.mount()', () => {
expect(await res.text()).toBe('Not Found from AnotherApp')
})
})

describe('With replaceRequest option', () => {
const anotherApp = (req: Request) => {
const path = getPath(req)
if (path === '/app') {
return new Response(getPath(req))
}
return new Response(null, { status: 404 })
}

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

it('Should return 200 response with the correct path', async () => {
const res = await app.request('/app')
expect(res.status).toBe(200)
expect(await res.text()).toBe('/app')
})
})
})

describe('HEAD method', () => {
Expand Down