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
feat(mount): introduce Mount Middleware and deprecate `app.mount` (#5221)Yusuke Wadabefore adoptionae595de
lines 1–109why, one step back1/**2 * @module3 * Mount Helper for Hono.4 */56import type { Context, ExecutionContext } from '../../context'7import { routePath } from '../../helper/route'8import type { MiddlewareHandler } from '../../types'910// eslint-disable-next-line @typescript-eslint/no-explicit-any11type ApplicationHandler = (request: Request, ...args: any) => Response | Promise<Response>12type MountOptionHandler = (c: Context) => unknown13type MountReplaceRequest = (originalRequest: Request) => Request14type MountOptions =15 | MountOptionHandler16 | {17 optionHandler?: MountOptionHandler18 replaceRequest?: MountReplaceRequest | false19 }2021const defaultReplaceRequest = (c: Context): Request => {22 // e.g. `/another-app/*` (basePath is already merged) -> `/another-app`23 const pathPrefix = routePath(c).replace(/\/\*$/, '')24 const url = new URL(c.req.raw.url)25 url.pathname = c.req.path.slice(pathPrefix.length) || '/'26 return new Request(url, c.req.raw)27}2829const defaultGetOptions = (c: Context): unknown[] => {30 let executionContext: ExecutionContext | undefined = undefined31 try {32 executionContext = c.executionCtx33 } catch {} // Do nothing34 return [c.env, executionContext]35}3637/**38 * `mount()` allows you to mount applications built with other frameworks into your Hono application.39 *40 * @see {@link https://hono.dev/docs/api/hono#mount}41 *42 * @param {Function} applicationHandler - other Request Handler43 * @param {MountOptions} [options] - options of `mount()`44 * @returns {MiddlewareHandler} handler to register with `app.all()`45 *46 * @example47 * ```ts48 * import { Router as IttyRouter } from 'itty-router'49 * import { Hono } from 'hono'50 * import { mount } from 'hono/mount'51 * // Create itty-router application52 * const ittyRouter = IttyRouter()53 * // GET /itty-router/hello54 * ittyRouter.get('/hello', () => new Response('Hello from itty-router'))55 *56 * const app = new Hono()57 * app.all('/itty-router/*', mount(ittyRouter.handle))58 * ```59 *60 * @example61 * ```ts62 * const app = new Hono()63 * // Send the request to another application without modification.64 * app.all('/app/*', mount(anotherApp, {65 * replaceRequest: (req) => req,66 * }))67 * ```68 */69export const mount = (70 applicationHandler: ApplicationHandler,71 options?: MountOptions72): MiddlewareHandler => {73 // handle options74 let replaceRequest: MountReplaceRequest | undefined75 let optionHandler: MountOptionHandler | undefined76 if (options) {77 if (typeof options === 'function') {78 optionHandler = options79 } else {80 optionHandler = options.optionHandler81 if (options.replaceRequest === false) {82 replaceRequest = (request) => request83 } else {84 replaceRequest = options.replaceRequest85 }86 }87 }8889 // prepare handlers for request90 const getOptions: (c: Context) => unknown[] = optionHandler91 ? (c) => {92 const options = optionHandler!(c)93 return Array.isArray(options) ? options : [options]94 }95 : defaultGetOptions9697 return async (c, next) => {98 const request = replaceRequest ? replaceRequest(c.req.raw) : defaultReplaceRequest(c)99 const res = await applicationHandler(request, ...getOptions(c))100101 if (res) {102 return res103 }104105 await next()106 }107}108109export type { ApplicationHandler, MountOptionHandler, MountReplaceRequest, MountOptions }