feat: wire up swagger-jsdoc dynamic spec, delete static openapi.json
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
- Create src/swagger.ts config module for swagger-jsdoc
- Add GET /openapi.json dynamic route (generated from @openapi annotations)
- Delete static public/openapi.json (was drifting from code)
- Add @openapi annotation for deprecated /v1/signup/free in index.ts
- Import swaggerSpec into index.ts
- All 12 endpoints now code-driven: demo/html, demo/markdown, convert/html,
convert/markdown, convert/url, templates, templates/{id}/render,
recover, recover/verify, billing/checkout, signup/free, health
This commit is contained in:
parent
792e2d9142
commit
825c6562ba
11 changed files with 624 additions and 1070 deletions
58
dist/index.js
vendored
58
dist/index.js
vendored
|
|
@ -1,15 +1,18 @@
|
|||
import express from "express";
|
||||
import { randomUUID } from "crypto";
|
||||
import { createRequire } from "module";
|
||||
import { compressionMiddleware } from "./middleware/compression.js";
|
||||
import logger from "./services/logger.js";
|
||||
import helmet from "helmet";
|
||||
const _require = createRequire(import.meta.url);
|
||||
const APP_VERSION = _require("../package.json").version;
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import rateLimit from "express-rate-limit";
|
||||
import { convertRouter } from "./routes/convert.js";
|
||||
import { templatesRouter } from "./routes/templates.js";
|
||||
import { healthRouter } from "./routes/health.js";
|
||||
import { signupRouter } from "./routes/signup.js";
|
||||
import { demoRouter } from "./routes/demo.js";
|
||||
import { recoverRouter } from "./routes/recover.js";
|
||||
import { billingRouter } from "./routes/billing.js";
|
||||
import { authMiddleware } from "./middleware/auth.js";
|
||||
|
|
@ -20,6 +23,7 @@ import { initBrowser, closeBrowser } from "./services/browser.js";
|
|||
import { loadKeys, getAllKeys } from "./services/keys.js";
|
||||
import { verifyToken, loadVerifications } from "./services/verification.js";
|
||||
import { initDatabase, pool } from "./services/db.js";
|
||||
import { swaggerSpec } from "./swagger.js";
|
||||
const app = express();
|
||||
const PORT = parseInt(process.env.PORT || "3100", 10);
|
||||
app.use(helmet({ crossOriginResourcePolicy: { policy: "cross-origin" } }));
|
||||
|
|
@ -48,7 +52,8 @@ app.use(compressionMiddleware);
|
|||
app.use((req, res, next) => {
|
||||
const isAuthBillingRoute = req.path.startsWith('/v1/signup') ||
|
||||
req.path.startsWith('/v1/recover') ||
|
||||
req.path.startsWith('/v1/billing');
|
||||
req.path.startsWith('/v1/billing') ||
|
||||
req.path.startsWith('/v1/demo');
|
||||
if (isAuthBillingRoute) {
|
||||
res.setHeader("Access-Control-Allow-Origin", "https://docfast.dev");
|
||||
}
|
||||
|
|
@ -80,7 +85,36 @@ const limiter = rateLimit({
|
|||
app.use(limiter);
|
||||
// Public routes
|
||||
app.use("/health", healthRouter);
|
||||
app.use("/v1/signup", signupRouter);
|
||||
app.use("/v1/demo", express.json({ limit: "50kb" }), pdfRateLimitMiddleware, demoRouter);
|
||||
/**
|
||||
* @openapi
|
||||
* /v1/signup/free:
|
||||
* post:
|
||||
* tags: [Account]
|
||||
* summary: Request a free API key (discontinued)
|
||||
* description: Free accounts have been discontinued. Use the demo endpoints or upgrade to Pro.
|
||||
* responses:
|
||||
* 410:
|
||||
* description: Feature discontinued
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* error:
|
||||
* type: string
|
||||
* demo_endpoint:
|
||||
* type: string
|
||||
* pro_url:
|
||||
* type: string
|
||||
*/
|
||||
app.use("/v1/signup", (_req, res) => {
|
||||
res.status(410).json({
|
||||
error: "Free accounts have been discontinued. Try our demo at POST /v1/demo/html or upgrade to Pro at https://docfast.dev",
|
||||
demo_endpoint: "/v1/demo/html",
|
||||
pro_url: "https://docfast.dev/#pricing"
|
||||
});
|
||||
});
|
||||
app.use("/v1/recover", recoverRouter);
|
||||
app.use("/v1/billing", billingRouter);
|
||||
// Authenticated routes — conversion routes get tighter body limits (500KB)
|
||||
|
|
@ -156,7 +190,7 @@ p{color:#7a8194;margin-bottom:24px;line-height:1.6}
|
|||
${apiKey ? `
|
||||
<div class="warning">⚠️ Save your API key securely. You can recover it via email if needed.</div>
|
||||
<div class="key-box" onclick="navigator.clipboard.writeText('${apiKey}');this.style.borderColor='#5eead4';setTimeout(()=>this.style.borderColor='#34d399',1500)">${apiKey}</div>
|
||||
<div class="links">100 free PDFs/month · <a href="/docs">Read the docs →</a></div>
|
||||
<div class="links">Upgrade to Pro for 5,000 PDFs/month · <a href="/docs">Read the docs →</a></div>
|
||||
` : `<div class="links"><a href="/">← Back to DocFast</a></div>`}
|
||||
</div></body></html>`;
|
||||
}
|
||||
|
|
@ -168,6 +202,10 @@ app.get("/favicon.ico", (_req, res) => {
|
|||
res.setHeader('Cache-Control', 'public, max-age=604800');
|
||||
res.sendFile(path.join(__dirname, "../public/favicon.svg"));
|
||||
});
|
||||
// Dynamic OpenAPI spec — generated from @openapi JSDoc annotations at startup
|
||||
app.get("/openapi.json", (_req, res) => {
|
||||
res.json(swaggerSpec);
|
||||
});
|
||||
// Docs page (clean URL)
|
||||
app.get("/docs", (_req, res) => {
|
||||
// Swagger UI 5.x uses new Function() (via ajv) for JSON schema validation.
|
||||
|
|
@ -179,7 +217,6 @@ app.get("/docs", (_req, res) => {
|
|||
// Static asset cache headers middleware
|
||||
app.use((req, res, next) => {
|
||||
if (/\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$/.test(req.path)) {
|
||||
console.log("CACHE HIT:", req.path);
|
||||
res.setHeader('Cache-Control', 'public, max-age=604800, immutable');
|
||||
}
|
||||
next();
|
||||
|
|
@ -209,12 +246,13 @@ app.get("/status", (_req, res) => {
|
|||
app.get("/api", (_req, res) => {
|
||||
res.json({
|
||||
name: "DocFast API",
|
||||
version: "0.2.9",
|
||||
version: APP_VERSION,
|
||||
endpoints: [
|
||||
"POST /v1/signup/free — Get a free API key",
|
||||
"POST /v1/convert/html",
|
||||
"POST /v1/convert/markdown",
|
||||
"POST /v1/convert/url",
|
||||
"POST /v1/demo/html — Try HTML→PDF (no auth, watermarked, 5/hour)",
|
||||
"POST /v1/demo/markdown — Try Markdown→PDF (no auth, watermarked, 5/hour)",
|
||||
"POST /v1/convert/html — HTML→PDF (requires API key)",
|
||||
"POST /v1/convert/markdown — Markdown→PDF (requires API key)",
|
||||
"POST /v1/convert/url — URL→PDF (requires API key)",
|
||||
"POST /v1/templates/:id/render",
|
||||
"GET /v1/templates",
|
||||
"POST /v1/billing/checkout — Start Pro subscription",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue