index.ts
This file
Path
src/middleware/mount/index.ts
109 lines 路 3.2 KiB 路 TypeScript
Last changed by
feat(mount): introduce Mount Middleware and deprecate `app.mount` (#5221)Yusuke Wadabefore adoptionae595de
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110/** * @module * Mount Helper for Hono. */
import type { Context, ExecutionContext } from '../../context'import { routePath } from '../../helper/route'import type { MiddlewareHandler } from '../../types'
// eslint-disable-next-line @typescript-eslint/no-explicit-anytype ApplicationHandler = (request: Request, ...args: any) => Response | Promise<Response>type MountOptionHandler = (c: Context) => unknowntype MountReplaceRequest = (originalRequest: Request) => Requesttype MountOptions = | MountOptionHandler | { optionHandler?: MountOptionHandler replaceRequest?: MountReplaceRequest | false }
const defaultReplaceRequest = (c: Context): Request => { // e.g. `/another-app/*` (basePath is already merged) -> `/another-app` const pathPrefix = routePath(c).replace(/\/\*$/, '') const url = new URL(c.req.raw.url) url.pathname = c.req.path.slice(pathPrefix.length) || '/' return new Request(url, c.req.raw)}
const defaultGetOptions = (c: Context): unknown[] => { let executionContext: ExecutionContext | undefined = undefined try { executionContext = c.executionCtx } catch {} // Do nothing return [c.env, executionContext]}
/** * `mount()` allows you to mount applications built with other frameworks into your Hono application. * * @see {@link https://hono.dev/docs/api/hono#mount} * * @param {Function} applicationHandler - other Request Handler * @param {MountOptions} [options] - options of `mount()` * @returns {MiddlewareHandler} handler to register with `app.all()` * * @example * ```ts * import { Router as IttyRouter } from 'itty-router' * import { Hono } from 'hono' * import { mount } from 'hono/mount' * // Create itty-router application * const ittyRouter = IttyRouter() * // GET /itty-router/hello * ittyRouter.get('/hello', () => new Response('Hello from itty-router')) * * const app = new Hono() * app.all('/itty-router/*', mount(ittyRouter.handle)) * ``` * * @example * ```ts * const app = new Hono() * // Send the request to another application without modification. * app.all('/app/*', mount(anotherApp, { * replaceRequest: (req) => req, * })) * ``` */export const mount = ( applicationHandler: ApplicationHandler, options?: MountOptions): MiddlewareHandler => { // handle options let replaceRequest: MountReplaceRequest | undefined let optionHandler: MountOptionHandler | undefined if (options) { if (typeof options === 'function') { optionHandler = options } else { optionHandler = options.optionHandler if (options.replaceRequest === false) { replaceRequest = (request) => request } else { replaceRequest = options.replaceRequest } } }
// prepare handlers for request const getOptions: (c: Context) => unknown[] = optionHandler ? (c) => { const options = optionHandler!(c) return Array.isArray(options) ? options : [options] } : defaultGetOptions
return async (c, next) => { const request = replaceRequest ? replaceRequest(c.req.raw) : defaultReplaceRequest(c) const res = await applicationHandler(request, ...getOptions(c))
if (res) { return res }
await next() }}
export type { ApplicationHandler, MountOptionHandler, MountReplaceRequest, MountOptions }