Files
SnowLuma/.pnpm-store/v11/files/6d/e18b00acb7be64a33e093e80bddfa0b3d3aa49612674ea34eb318f942abdb898afea9d9691462254d6e61e8c69037d9fdef7808ea92225968dde42c25aeab3
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

88 lines
2.8 KiB
Plaintext

//#region src/middleware/trailing-slash/index.ts
/**
* 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'))
* ```
*/
const trimTrailingSlash = (options) => {
return async function trimTrailingSlash(c, next) {
if (options?.alwaysRedirect) {
if ((c.req.method === "GET" || c.req.method === "HEAD") && c.req.path !== "/" && c.req.path.at(-1) === "/") {
const url = new URL(c.req.url);
url.pathname = url.pathname.substring(0, url.pathname.length - 1);
return c.redirect(url.toString(), 301);
}
}
await next();
if (!options?.alwaysRedirect && c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path !== "/" && c.req.path.at(-1) === "/") {
const url = new URL(c.req.url);
url.pathname = url.pathname.substring(0, url.pathname.length - 1);
c.res = c.redirect(url.toString(), 301);
}
};
};
/**
* 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'))
* ```
*/
const appendTrailingSlash = (options) => {
return async function appendTrailingSlash(c, next) {
if (options?.alwaysRedirect) {
if ((c.req.method === "GET" || c.req.method === "HEAD") && c.req.path.at(-1) !== "/" && !options.skip?.(c.req.path)) {
const url = new URL(c.req.url);
url.pathname += "/";
return c.redirect(url.toString(), 301);
}
}
await next();
if (!options?.alwaysRedirect && c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path.at(-1) !== "/" && !options?.skip?.(c.req.path)) {
const url = new URL(c.req.url);
url.pathname += "/";
c.res = c.redirect(url.toString(), 301);
}
};
};
//#endregion
export { appendTrailingSlash, trimTrailingSlash };