diff --git a/app/api/openapi/route.ts b/app/api/openapi/route.ts new file mode 100644 index 0000000..ed83600 --- /dev/null +++ b/app/api/openapi/route.ts @@ -0,0 +1,16 @@ +import { NextResponse } from "next/server" +import { buildOpenApiSpec } from "@/app/lib/openapiSpec" + +function baseUrlFromRequest(request: Request) { + const forwardedProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim() + const forwardedHost = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim() + const host = forwardedHost || request.headers.get("host") + if (!host) return "" + + const protocol = forwardedProto || new URL(request.url).protocol.replace(":", "") || "http" + return `${protocol}://${host}` +} + +export async function GET(request: Request) { + return NextResponse.json(buildOpenApiSpec(baseUrlFromRequest(request))) +} diff --git a/app/components/Sidebar.tsx b/app/components/Sidebar.tsx index b5c3cea..27a8c19 100644 --- a/app/components/Sidebar.tsx +++ b/app/components/Sidebar.tsx @@ -317,6 +317,16 @@ export default function Sidebar({ Help + + + Swagger + + + +function methodClass(method: string) { + const normalized = method.toLowerCase() + if (normalized === "get") return "border-blue-500/40 bg-blue-500/10 text-blue-300" + if (normalized === "post") return "border-[var(--nvidia-green)]/50 bg-[var(--nvidia-green)]/10 text-[var(--nvidia-green)]" + return "border-[var(--border-subtle)] bg-[var(--background-tertiary)] text-[var(--foreground-dim)]" +} + +function operationRows(spec: OpenApiSpec) { + return Object.entries(spec.paths).flatMap(([path, methods]) => + Object.entries(methods).map(([method, operation]) => ({ + path, + method, + operation, + })), + ) +} + +export default function SwaggerPage() { + const spec = buildOpenApiSpec() + const rows = operationRows(spec) + + return ( +
+
+
+
+
+

OpenAPI 3.1

+

Swagger

+

+ Controller-node API reference for generating launch kits, autodeploying remote nodes, and managing friendly names. +

+
+
+ + Dashboard + + + OpenAPI JSON + +
+
+
+ +
+
+

Title

+

{spec.info.title}

+
+
+

Version

+

{spec.info.version}

+
+
+

Auth

+

session cookie

+
+
+ +
+
+

Endpoints

+

Expandable operations generated from the local OpenAPI spec.

+
+
+ {rows.map(({ path, method, operation }) => ( +
+ + + + {method} + + {path} + + {operation.summary} + +
+

{operation.description || operation.summary}

+
+
+

Operation

+
{JSON.stringify(operation, null, 2)}
+
+
+

Curl

+
{`${method.toUpperCase()} ${path}\nContent-Type: application/json\nCookie: ${spec.components.securitySchemes.sessionCookie.name}=...`}
+
+
+
+
+ ))} +
+
+ +
+

Schemas

+
+ {Object.entries(spec.components.schemas).map(([name, schema]) => ( +
+ {name} +
{JSON.stringify(schema, null, 2)}
+
+ ))} +
+
+
+
+ ) +} diff --git a/tests/swagger-openapi-check.mjs b/tests/swagger-openapi-check.mjs new file mode 100644 index 0000000..a27dec9 --- /dev/null +++ b/tests/swagger-openapi-check.mjs @@ -0,0 +1,26 @@ +import assert from 'node:assert/strict' +import { readFile } from 'node:fs/promises' +import path from 'node:path' + +const root = process.cwd() + +const [specSource, routeSource, pageSource, sidebarSource] = await Promise.all([ + readFile(path.join(root, 'app/lib/openapiSpec.ts'), 'utf8'), + readFile(path.join(root, 'app/api/openapi/route.ts'), 'utf8'), + readFile(path.join(root, 'app/swagger/page.tsx'), 'utf8'), + readFile(path.join(root, 'app/components/Sidebar.tsx'), 'utf8'), +]) + +assert.match(specSource, /openapi:\s*"3\.1\.0"/, 'OpenAPI spec must declare version 3.1.0') +assert.match(specSource, /\/api\/controller-node\/plan/, 'spec must document controller plan endpoint') +assert.match(specSource, /\/api\/controller-node\/deploy/, 'spec must document controller deploy endpoint') +assert.match(specSource, /\/api\/controller-node\/registry/, 'spec must document controller registry endpoint') +assert.match(specSource, /ControllerDeployRequest/, 'spec must include autodeploy request schema') +assert.match(specSource, /sessionCookie/, 'spec must document session cookie auth') +assert.match(routeSource, /buildOpenApiSpec/, 'OpenAPI API route must return generated spec') +assert.match(pageSource, /OpenAPI JSON/, 'Swagger page must link to the JSON spec') +assert.match(pageSource, /Endpoints/, 'Swagger page must render endpoint docs') +assert.match(pageSource, /Schemas/, 'Swagger page must render schema docs') +assert.match(sidebarSource, /href="\/swagger"/, 'sidebar must link to Swagger page') + +console.log('swagger-openapi-check: PASS swagger/openapi assertions')