Files
SnowLuma/.pnpm-store/v11/files/be/27616b38af1dbf5cd49a8913ee4b1e49bee96fa54b41103cfd5fa5296b17a117048b1d62bedd6c14b405949ec98be9a717fcc20b5fcdb703ae2653776aee48
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

90 lines
2.9 KiB
Plaintext

Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#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
exports.appendTrailingSlash = appendTrailingSlash;
exports.trimTrailingSlash = trimTrailingSlash;