nomadro/packages/sdk/src/openapi.ts
Eric ffa456afdf Ship social join profile + SeaweedFS avatars, notify/coffee, and .com audience stack.
Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 02:25:05 +08:00

148 lines
4.4 KiB
TypeScript

import {
NOMADRO_TOOLS,
PUBLIC_API_PREFIX,
PUBLIC_API_VERSION,
} from "./tools";
/** OpenAPI 3.1 document for Nomadro public surface. */
export function buildOpenApiDocument(opts?: {
servers?: { url: string; description?: string }[];
}) {
const paths: Record<string, unknown> = {};
for (const tool of NOMADRO_TOOLS) {
const pathKey = `${PUBLIC_API_PREFIX}${tool.path.replace(/\{(\w+)\}/g, "{$1}")}`;
const op = {
operationId: tool.name,
summary: tool.description,
tags: [tool.path.split("/")[1] || "platform"],
parameters: [] as unknown[],
requestBody: undefined as unknown,
responses: {
"200": {
description: "OK",
content: {
"application/json": {
schema: { type: "object", additionalProperties: true },
},
},
},
},
};
const props = (tool.inputSchema.properties || {}) as Record<
string,
{ type?: string; description?: string; enum?: string[] }
>;
const required = new Set(
(tool.inputSchema.required as string[] | undefined) || [],
);
const pathParams = [...tool.path.matchAll(/\{(\w+)\}/g)].map((m) => m[1]);
for (const [key, schema] of Object.entries(props)) {
if (pathParams.includes(key)) {
op.parameters.push({
name: key,
in: "path",
required: true,
schema: { type: schema.type || "string" },
description: schema.description,
});
} else if (tool.method === "GET") {
op.parameters.push({
name: key,
in: "query",
required: required.has(key),
schema: {
type: schema.type || "string",
...(schema.enum ? { enum: schema.enum } : {}),
},
description: schema.description,
});
}
}
if (tool.method === "POST") {
const bodyProps: Record<string, unknown> = {};
const bodyRequired: string[] = [];
for (const [key, schema] of Object.entries(props)) {
if (pathParams.includes(key)) continue;
bodyProps[key] = schema;
if (required.has(key)) bodyRequired.push(key);
}
op.requestBody = {
required: true,
content: {
"application/json": {
schema: {
type: "object",
properties: bodyProps,
required: bodyRequired,
},
},
},
};
}
const existing = (paths[pathKey] as Record<string, unknown>) || {};
existing[tool.method.toLowerCase()] = op;
paths[pathKey] = existing;
}
paths[`${PUBLIC_API_PREFIX}/openapi.json`] = {
get: {
operationId: "getOpenApi",
summary: "This OpenAPI document",
tags: ["platform"],
responses: {
"200": {
description: "OpenAPI 3.1 JSON",
content: { "application/json": { schema: { type: "object" } } },
},
},
},
};
paths[`${PUBLIC_API_PREFIX}/tools`] = {
get: {
operationId: "listTools",
summary: "List MCP/SDK tool descriptors (same as NOMADRO_TOOLS)",
tags: ["platform"],
responses: {
"200": {
description: "Tool list",
content: { "application/json": { schema: { type: "object" } } },
},
},
},
};
return {
openapi: "3.1.0",
info: {
title: "Nomadro Public API",
version: PUBLIC_API_VERSION,
description:
"Standardized Nomadro surface for apps, agents (MCP), and skills. Interactive docs: https://nomadro.com/developers . Core deploy target: nomadro.com (US). .cn is an independent China mirror after cn-audience validation.",
contact: { url: "https://nomadro.com/developers" },
},
servers: opts?.servers || [
{ url: "https://nomadro.com", description: "Production .com (US, primary)" },
{ url: "https://nomadro.cn", description: "China .cn (independent)" },
{ url: "http://localhost:3000", description: "Local Next portal" },
],
paths,
components: {
securitySchemes: {
ApiKey: {
type: "apiKey",
in: "header",
name: "X-Nomadro-Key",
description: "Optional. Required only when NOMADRO_PUBLIC_API_KEY is set on server.",
},
SocialGuest: {
type: "apiKey",
in: "header",
name: "X-Social-Guest-Id",
description: "Guest id for social write ops (apply / trips).",
},
},
},
};
}