{"version":3,"file":"index.mjs","names":["SchemaRegistry"],"sources":["../../../../../src/astro/routes/api/schema/index.ts"],"sourcesContent":["/**\n * Schema export endpoint for CLI sync\n *\n * GET /_emdash/api/schema - Export full schema (requires schema:read permission)\n *\n * This endpoint is used by:\n * - CLI `emdash types` to fetch schema and generate types\n * - Local dev server to sync schema from remote\n * - CI/CD to detect schema drift\n */\n\nimport type { APIRoute } from \"astro\";\nimport { hashString } from \"@premium-cms/emdash\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiSuccess, handleError, requireDb } from \"#api/error.js\";\nimport { SchemaRegistry } from \"#schema/registry.js\";\nimport { generateTypeScript, uniqueInterfaceNames } from \"#schema/zod-generator.js\";\n\nexport const prerender = false;\n\nexport const GET: APIRoute = async ({ request, locals }) => {\n\tconst { emdash, user } = locals;\n\n\tconst dbErr = requireDb(emdash?.db);\n\tif (dbErr) return dbErr;\n\n\tconst denied = requirePerm(user, \"schema:read\");\n\tif (denied) return denied;\n\n\ttry {\n\t\tconst registry = new SchemaRegistry(emdash.db);\n\n\t\t// Get all collections with their fields\n\t\tconst collections = await registry.listCollections();\n\t\tconst collectionsWithFields = await Promise.all(\n\t\t\tcollections.map(async (c) => {\n\t\t\t\tconst fields = await registry.listFields(c.id);\n\t\t\t\treturn { ...c, fields };\n\t\t\t}),\n\t\t);\n\n\t\t// Check for format parameter\n\t\tconst url = new URL(request.url);\n\t\tconst format = url.searchParams.get(\"format\");\n\n\t\tif (format === \"typescript\") {\n\t\t\t// Generate TypeScript definitions. Singularizing slugs can collapse\n\t\t\t// distinct collections onto the same interface name, so resolve names\n\t\t\t// up front to keep every emitted interface unique (`book` + `books`\n\t\t\t// would otherwise both emit `Book`).\n\t\t\tconst interfaceNames = uniqueInterfaceNames(collectionsWithFields);\n\t\t\tconst types = collectionsWithFields\n\t\t\t\t.map((c) => generateTypeScript(c, interfaceNames.get(c.slug)))\n\t\t\t\t.join(\"\\n\\n\");\n\n\t\t\tconst header = `// Generated by EmDash CLI\n// Do not edit manually - run \\`emdash types\\` to regenerate\n\nimport type { PortableTextBlock } from \"@premium-cms/emdash\";\n\n`;\n\n\t\t\treturn new Response(header + types, {\n\t\t\t\tstatus: 200,\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"text/typescript\",\n\t\t\t\t\t\"Cache-Control\": \"private, no-store\",\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\n\t\t// Default: JSON format\n\t\tconst schemaExport = {\n\t\t\tcollections: collectionsWithFields.map((c) => ({\n\t\t\t\tslug: c.slug,\n\t\t\t\tlabel: c.label,\n\t\t\t\tlabelSingular: c.labelSingular,\n\t\t\t\tdescription: c.description,\n\t\t\t\ticon: c.icon,\n\t\t\t\tsupports: c.supports,\n\t\t\t\thidden: c.hidden,\n\t\t\t\tsortOrder: c.sortOrder,\n\t\t\t\tfields: c.fields.map((f) => ({\n\t\t\t\t\tslug: f.slug,\n\t\t\t\t\tlabel: f.label,\n\t\t\t\t\ttype: f.type,\n\t\t\t\t\trequired: f.required,\n\t\t\t\t\tunique: f.unique,\n\t\t\t\t\tdefaultValue: f.defaultValue,\n\t\t\t\t\tvalidation: f.validation,\n\t\t\t\t\twidget: f.widget,\n\t\t\t\t\toptions: f.options,\n\t\t\t\t})),\n\t\t\t})),\n\t\t};\n\n\t\tconst version = await hashString(JSON.stringify(schemaExport));\n\n\t\tconst response = apiSuccess({\n\t\t\t...schemaExport,\n\t\t\tversion,\n\t\t});\n\t\tresponse.headers.set(\"X-Schema-Version\", version);\n\t\treturn response;\n\t} catch (error) {\n\t\treturn handleError(error, \"Schema export failed\", \"SCHEMA_EXPORT_ERROR\");\n\t}\n};\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,MAAa,YAAY;AAEzB,MAAa,MAAgB,OAAO,EAAE,SAAS,aAAa;CAC3D,MAAM,EAAE,QAAQ,SAAS;CAEzB,MAAM,QAAQ,UAAU,QAAQ,GAAG;AACnC,KAAI,MAAO,QAAO;CAElB,MAAM,SAAS,Y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