Files
SnowLuma/.pnpm-store/v11/files/b2/c5d15b0130747636c2c136aa346e449b8408cf68d56a0f6152b6aa5cede8fb1e41e5137b4d95802bed18741096ac2f4cad037815cc10571c3ae8c1e9982e45
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

42 lines
1.2 KiB
Plaintext

Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region src/middleware/pretty-json/index.ts
const jsonContentTypeRegex = /^application\/(?:[a-z0-9._-]+\+)?json(?=$|[;\s])/i;
/**
* Pretty JSON Middleware for Hono.
*
* @see {@link https://hono.dev/docs/middleware/builtin/pretty-json}
*
* @param options - The options for the pretty JSON middleware.
* @returns {MiddlewareHandler} The middleware handler function.
*
* @example
* ```ts
* const app = new Hono()
*
* app.use(prettyJSON()) // With options: prettyJSON({ space: 4 })
* app.get('/', (c) => {
* return c.json({ message: 'Hono!' })
* })
* ```
*/
const prettyJSON = (options) => {
const targetQuery = options?.query ?? "pretty";
return async function prettyJSON(c, next) {
const pretty = options?.force || c.req.query(targetQuery) || c.req.query(targetQuery) === "";
await next();
const contentType = c.res.headers.get("Content-Type");
if (pretty && contentType && jsonContentTypeRegex.test(contentType)) {
let obj;
try {
obj = await c.res.clone().json();
} catch {
return;
}
c.res = new Response(JSON.stringify(obj, null, options?.space ?? 2), c.res);
c.res.headers.delete("Content-Length");
}
};
};
//#endregion
exports.prettyJSON = prettyJSON;