Files
SnowLuma/.pnpm-store/v11/files/b0/5622844ae69502f10815fc841f12e8b4d6ae97fdb0f78104d162c0016a8b856a83955dddc41219e914c407941f77c37db3edda10d80c882bdcc126e028d894
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

93 lines
2.8 KiB
Plaintext

import { MiddlewareHandler } from "../../types.js";
//#region src/middleware/trailing-slash/index.d.ts
type TrimTrailingSlashOptions = {
/**
* If `true`, the middleware will always redirect requests with a trailing slash
* before executing handlers.
* This is useful for routes with wildcards (`*`).
* If `false` (default), it will only redirect when the route is not found (404).
* @default false
*/
alwaysRedirect?: boolean;
};
/**
* Trailing Slash Middleware for Hono.
*
* @see {@link https://hono.dev/docs/middleware/builtin/trailing-slash}
*
* @param {TrimTrailingSlashOptions} options - The options for the middleware.
* @returns {MiddlewareHandler} The middleware handler function.
*
* @example
* ```ts
* const app = new Hono()
*
* app.use(trimTrailingSlash())
* app.get('/about/me/', (c) => c.text('With Trailing Slash'))
* ```
*
* @example
* ```ts
* // With alwaysRedirect option for wildcard routes
* const app = new Hono()
*
* app.use(trimTrailingSlash({ alwaysRedirect: true }))
* app.get('/my-path/*', (c) => c.text('Wildcard route'))
* ```
*/
export declare const trimTrailingSlash: (options?: TrimTrailingSlashOptions) => MiddlewareHandler;
type AppendTrailingSlashOptions = {
/**
* If `true`, the middleware will always redirect requests without a trailing slash
* before executing handlers.
* This is useful for routes with wildcards (`*`).
* If `false` (default), it will only redirect when the route is not found (404).
* @default false
*/
alwaysRedirect?: boolean;
/**
* A function that determines whether to skip the redirect for a given path.
* If the function returns `true`, the redirect will be skipped.
* @param path - The request path
* @returns `true` to skip the redirect
*
* @example
* ```ts
* // Skip redirect for paths with file extensions
* app.use(appendTrailingSlash({
* alwaysRedirect: true,
* skip: (path) => /\.\w+$/.test(path),
* }))
* ```
*/
skip?: (path: string) => boolean;
};
/**
* Append trailing slash middleware for Hono.
* Append a trailing slash to the URL if it doesn't have one. For example, `/path/to/page` will be redirected to `/path/to/page/`.
*
* @see {@link https://hono.dev/docs/middleware/builtin/trailing-slash}
*
* @param {AppendTrailingSlashOptions} options - The options for the middleware.
* @returns {MiddlewareHandler} The middleware handler function.
*
* @example
* ```ts
* const app = new Hono()
*
* app.use(appendTrailingSlash())
* ```
*
* @example
* ```ts
* // With alwaysRedirect option for wildcard routes
* const app = new Hono()
*
* app.use(appendTrailingSlash({ alwaysRedirect: true }))
* app.get('/my-path/*', (c) => c.text('Wildcard route'))
* ```
*/
export declare const appendTrailingSlash: (options?: AppendTrailingSlashOptions) => MiddlewareHandler;
//#endregion
export {};