eCourt-note/app/api/swagger/route.ts
Your Name 073f72db7d feat(api): add swagger ui and docx template api
เพิ่มกลุ่ม API docx template สำหรับ wordaddin (WIP):
- POST /api/template/extract ดึงค่า Content Control ทั้งหมดจาก .docx
- POST /api/template/render แทนค่า JSON ลง template คืน .docx ใหม่
- GET /api/template/test ตรวจว่า service มีชีวิต
logic แชร์ที่ app/api/template/lib-tmpl.ts (pizzip+xpath) ไม่แตะ DB

เพิ่มหน้า Swagger UI ที่ GET /api/swagger — spec inline จาก
lib/openapi.ts ครอบ API ทุกกลุ่ม (template + N10), asset self-host
ที่ public/note/swagger-ui/ จาก postinstall scripts/copy-swagger-ui.mjs
(intranet ไม่ใช้ CDN — หาไฟล์ไม่เจอหลังอัปเกรด = exit 1 เหมือน
patch-pdf-worker, Docker build รัน script นี้ด้วยผ่าน web.Dockerfile)

middleware เปิดกว้างให้ api/template, api/swagger, note/swagger-ui

จาก code review: จำกัดไฟล์แนบ 100MB ทั้ง extract/render ก่อนอ่าน
เข้าหน่วยความจำกัน DoS, คืน 400 แทน 500 เมื่อ JSON ผิด format,
sync คำอธิบาย BadRequest ใน openapi spec ให้ตรง behavior ใหม่

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-07 10:16:12 +07:00

44 lines
1.6 KiB
TypeScript

import { NextResponse } from "next/server";
import { openApiSpec } from "@/lib/openapi";
import { withBasePath } from "@/utils/base-path";
// GET /api/swagger — หน้า Swagger UI ของ API ทั้งหมด (กลุ่ม template + N10)
// (asset สำหรับ intranet: postinstall scripts/copy-swagger-ui.mjs คัดลอก
// swagger-ui-dist ไป public/note/swagger-ui/ — pattern เดียวกับ pdf.worker)
// spec inline จาก lib/openapi.ts ตรง ๆ ไม่มี endpoint spec แยก
export const runtime = "nodejs";
// กัน </script> ใน JSON ตัดสคริปต์ขาย (spec เป็นของเราเองทั้งหมด แค่กันกรณี
// ข้อความ example มี < ในอนาคต)
const SPEC_JSON = JSON.stringify(openApiSpec).replace(/</g, "\\u003c");
const HTML = `<!doctype html>
<html lang="th">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Swagger UI — Judge's Note API</title>
<link rel="stylesheet" href="${withBasePath("/swagger-ui/swagger-ui.css")}">
</head>
<body>
<div id="swagger-ui"></div>
<script src="${withBasePath("/swagger-ui/swagger-ui-bundle.js")}"></script>
<script>
window.onload = function () {
window.ui = SwaggerUIBundle({
spec: ${SPEC_JSON},
dom_id: "#swagger-ui",
deepLinking: true,
persistAuthorization: true
});
};
</script>
</body>
</html>`;
export async function GET() {
return new NextResponse(HTML, {
headers: { "content-type": "text/html; charset=utf-8" },
});
}