@betttercms/codegen 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +46 -19
- package/dist/cli.js.map +1 -1
- package/dist/index.js +45 -18
- package/dist/index.js.map +1 -1
- package/package.json +5 -1
package/dist/cli.js
CHANGED
|
@@ -37,15 +37,33 @@ async function fetchModels(opts) {
|
|
|
37
37
|
|
|
38
38
|
// src/generate.ts
|
|
39
39
|
var PREAMBLE = `/**
|
|
40
|
-
* Rich-text field value
|
|
41
|
-
*
|
|
40
|
+
* Rich-text field value returned by the Delivery API.
|
|
41
|
+
*
|
|
42
|
+
* - \`format\`/\`value\`: the portable, editor-agnostic payload (Lexical EditorState) \u2014
|
|
43
|
+
* render it with your editor's serializer for full fidelity.
|
|
44
|
+
* - \`html\`: server-rendered, sanitized HTML (computed render-on-write). Present on
|
|
45
|
+
* Delivery reads; the simplest path for non-React consumers \u2014 safe to inject directly
|
|
46
|
+
* (e.g. \`dangerouslySetInnerHTML\`). Optional: legacy/un-normalized values may omit it.
|
|
47
|
+
*
|
|
48
|
+
* The \`{ format, value }\` contract is unchanged; \`html\` is additive.
|
|
42
49
|
*/
|
|
43
|
-
export type RichText = {
|
|
50
|
+
export type RichText = {
|
|
51
|
+
readonly format: string;
|
|
52
|
+
readonly value: unknown;
|
|
53
|
+
readonly html?: string;
|
|
54
|
+
};
|
|
44
55
|
|
|
45
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* Image / media field value as stored and returned verbatim by the Delivery API
|
|
58
|
+
* (server-normalized on write to the canonical shape). \`url\` is always present; an
|
|
59
|
+
* unresolved/external value may carry only \`url\`. \`altText\` is the accessibility text
|
|
60
|
+
* for \`<img alt>\`.
|
|
61
|
+
*/
|
|
46
62
|
export interface BetterCMSImage {
|
|
63
|
+
readonly id?: string;
|
|
47
64
|
readonly url: string;
|
|
48
|
-
readonly
|
|
65
|
+
readonly name?: string;
|
|
66
|
+
readonly altText?: string | null;
|
|
49
67
|
readonly width?: number;
|
|
50
68
|
readonly height?: number;
|
|
51
69
|
}
|
|
@@ -105,30 +123,39 @@ function scalarType(field) {
|
|
|
105
123
|
const inner = itemType === "number" ? "number" : "string";
|
|
106
124
|
return `${inner}[]`;
|
|
107
125
|
}
|
|
108
|
-
case "group":
|
|
109
|
-
case "repeater":
|
|
110
|
-
return "unknown";
|
|
111
126
|
default: {
|
|
112
127
|
const _exhaustive = t;
|
|
113
128
|
return "unknown";
|
|
114
129
|
}
|
|
115
130
|
}
|
|
116
131
|
}
|
|
132
|
+
function arrayZoneType(field, indent) {
|
|
133
|
+
const zones = field.config?.zones;
|
|
134
|
+
const parts = [];
|
|
135
|
+
if (zones?.nonRepeatable?.length) {
|
|
136
|
+
const nested = fieldsToBody(zones.nonRepeatable, indent + " ");
|
|
137
|
+
parts.push(`${indent} readonly nonRepeatable?: {
|
|
138
|
+
${nested}
|
|
139
|
+
${indent} };`);
|
|
140
|
+
}
|
|
141
|
+
if (zones?.repeatable?.fields?.length) {
|
|
142
|
+
const nested = fieldsToBody(zones.repeatable.fields, indent + " ");
|
|
143
|
+
parts.push(`${indent} readonly repeatable?: Array<{
|
|
144
|
+
${nested}
|
|
145
|
+
${indent} }>;`);
|
|
146
|
+
}
|
|
147
|
+
if (parts.length === 0) return "Record<string, unknown>";
|
|
148
|
+
return `{
|
|
149
|
+
${parts.join("\n")}
|
|
150
|
+
${indent}}`;
|
|
151
|
+
}
|
|
117
152
|
function fieldsToBody(fields, indent) {
|
|
118
153
|
const lines = [];
|
|
119
154
|
for (const field of fields) {
|
|
120
155
|
const optional = field.required ? "" : "?";
|
|
121
156
|
let typeExpr;
|
|
122
|
-
if (field.type === "
|
|
123
|
-
|
|
124
|
-
typeExpr = `{
|
|
125
|
-
${nested}
|
|
126
|
-
${indent}}`;
|
|
127
|
-
} else if (field.type === "repeater") {
|
|
128
|
-
const nested = fieldsToBody(field.fields ?? [], indent + " ");
|
|
129
|
-
typeExpr = `Array<{
|
|
130
|
-
${nested}
|
|
131
|
-
${indent}}>`;
|
|
157
|
+
if (field.type === "array" && field.config?.zones) {
|
|
158
|
+
typeExpr = arrayZoneType(field, indent);
|
|
132
159
|
} else {
|
|
133
160
|
typeExpr = scalarType(field);
|
|
134
161
|
}
|
|
@@ -186,7 +213,7 @@ export type BetterCMSModelSlug = keyof BetterCMSSchema;`;
|
|
|
186
213
|
}
|
|
187
214
|
|
|
188
215
|
// src/cli.ts
|
|
189
|
-
var VERSION = "0.
|
|
216
|
+
var VERSION = "0.2.0";
|
|
190
217
|
var DEFAULT_API_URL = "https://api.bettercms.ai/api/v1";
|
|
191
218
|
var DEFAULT_OUT = "bettercms.generated.ts";
|
|
192
219
|
function parseArgs(argv) {
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cli.ts","../src/fetch-models.ts","../src/generate.ts"],"sourcesContent":["/**\n * `bettercms-codegen` — fetch a project's content models and write a typed `.ts` file.\n *\n * Designed for two call sites:\n * 1. A developer in their repo: npx @betttercms/codegen --out src/bettercms.generated.ts\n * 2. The build-time GitHub Action: same command, key from a repo secret.\n *\n * Auth + endpoint come from flags or env (BETTERCMS_API_KEY, BETTERCMS_API_URL).\n */\n\nimport { writeFile, mkdir } from \"node:fs/promises\";\nimport { dirname, resolve } from \"node:path\";\nimport { fetchModels } from \"./fetch-models.js\";\nimport { generateTypes } from \"./generate.js\";\n\nconst VERSION = \"0.1.0\";\nconst DEFAULT_API_URL = \"https://api.bettercms.ai/api/v1\";\nconst DEFAULT_OUT = \"bettercms.generated.ts\";\n\ninterface CliArgs {\n apiUrl: string;\n apiKey: string | undefined;\n out: string;\n help: boolean;\n}\n\nfunction parseArgs(argv: string[]): CliArgs {\n const args: CliArgs = {\n apiUrl: process.env.BETTERCMS_API_URL ?? DEFAULT_API_URL,\n apiKey: process.env.BETTERCMS_API_KEY,\n out: DEFAULT_OUT,\n help: false,\n };\n for (let i = 0; i < argv.length; i++) {\n const arg = argv[i];\n const next = () => argv[++i];\n switch (arg) {\n case \"--api-url\":\n args.apiUrl = next() ?? args.apiUrl;\n break;\n case \"--key\":\n case \"--api-key\":\n args.apiKey = next();\n break;\n case \"--out\":\n case \"-o\":\n args.out = next() ?? args.out;\n break;\n case \"--help\":\n case \"-h\":\n args.help = true;\n break;\n }\n }\n return args;\n}\n\nconst HELP = `bettercms-codegen v${VERSION} — generate TypeScript types from your BetterCMS schema\n\nUsage:\n npx @betttercms/codegen [options]\n\nOptions:\n -o, --out <path> Output file (default: ${DEFAULT_OUT})\n --api-url <url> Management API base (default: ${DEFAULT_API_URL})\n --key <key> Management API key (or set BETTERCMS_API_KEY)\n -h, --help Show this help\n\nEnv:\n BETTERCMS_API_KEY Management-scoped key (content:manage)\n BETTERCMS_API_URL Override the API base\n`;\n\nasync function main(): Promise<void> {\n const args = parseArgs(process.argv.slice(2));\n\n if (args.help) {\n process.stdout.write(HELP);\n return;\n }\n if (!args.apiKey) {\n process.stderr.write(\n \"error: no API key. Pass --key <key> or set BETTERCMS_API_KEY.\\n\",\n );\n process.exit(1);\n }\n\n const models = await fetchModels({ apiUrl: args.apiUrl, apiKey: args.apiKey });\n const source = generateTypes(models, { version: VERSION });\n\n const outPath = resolve(process.cwd(), args.out);\n await mkdir(dirname(outPath), { recursive: true });\n await writeFile(outPath, source, \"utf8\");\n\n process.stdout.write(\n `✓ Generated ${models.length} model type${models.length === 1 ? \"\" : \"s\"} → ${args.out}\\n`,\n );\n}\n\nmain().catch((err: unknown) => {\n process.stderr.write(`error: ${err instanceof Error ? err.message : String(err)}\\n`);\n process.exit(1);\n});\n","/**\n * Fetches content models from the BetterCMS Management API so the CLI can generate\n * types against a live project. Kept dependency-free (plain fetch) so the generated\n * artifact and this fetcher can run anywhere — a GitHub Action, a postinstall, a script.\n */\n\nimport type { GeneratableModel } from \"./generate.js\";\n\nexport interface FetchModelsOptions {\n /** Management API base, e.g. \"https://api.bettercms.ai/api/v1\". */\n apiUrl: string;\n /** A management-scoped key (content:manage) or device-minted token. */\n apiKey: string;\n /** Optional fetch override (testing / custom runtime). */\n fetchImpl?: typeof fetch;\n}\n\ninterface ManagedModelRow {\n slug: string;\n name?: string;\n description?: string | null;\n fields: GeneratableModel[\"fields\"];\n}\n\n/**\n * GET /management/content/models — returns the project's models (the key is\n * project-scoped server-side, so this is exactly the schema for this site).\n */\nexport async function fetchModels(\n opts: FetchModelsOptions,\n): Promise<GeneratableModel[]> {\n const doFetch = opts.fetchImpl ?? globalThis.fetch;\n const base = opts.apiUrl.replace(/\\/+$/, \"\");\n const url = `${base}/management/content/models`;\n\n let res: Response;\n try {\n res = await doFetch(url, {\n // No Content-Type: this is a bodyless GET; the header is incorrect here and\n // strict edge runtimes/proxies may reject it.\n headers: { Authorization: `Bearer ${opts.apiKey}`, Accept: \"application/json\" },\n });\n } catch (err) {\n throw new Error(\n `Could not reach the BetterCMS Management API at ${url}: ${\n err instanceof Error ? err.message : String(err)\n }`,\n );\n }\n\n if (!res.ok) {\n const hint =\n res.status === 401 || res.status === 403\n ? \" — check your management API key (it must have the content:manage scope).\"\n : \"\";\n throw new Error(`Management API returned ${res.status} ${res.statusText}${hint}`);\n }\n\n const body = (await res.json()) as { data?: ManagedModelRow[] };\n const rows = body.data ?? [];\n return rows.map((r) => ({\n slug: r.slug,\n name: r.name,\n description: r.description ?? null,\n fields: r.fields ?? [],\n }));\n}\n","/**\n * @betttercms/codegen — schema → TypeScript generator (the single source of truth).\n *\n * Both the dashboard schema builder and the MCP `create_model`/`add_field` tools write\n * the SAME `content_models.fields` (an array of `ContentModelField`). This generator maps\n * that one array into TypeScript. Because there is exactly one schema representation, the\n * generated types can never drift from the editor or the agent — they are the same source.\n *\n * Pure + deterministic: same models in → identical string out (stable ordering, no clock,\n * no I/O). That makes it trivially testable and safe to commit + diff in a customer repo.\n */\n\nimport type { ContentModelField, ContentModelFieldType } from \"@betttercms/types\";\n\n/** Minimal model shape the generator needs — a subset of the Management API model row. */\nexport interface GeneratableModel {\n /** Machine-safe slug, e.g. \"blog\" or \"case-study\". Used for the schema-map key. */\n slug: string;\n /** Human name, used only for the JSDoc header. */\n name?: string;\n description?: string | null;\n fields: ContentModelField[];\n}\n\nexport interface GenerateOptions {\n /** Generator version stamped into the header (defaults to the package version). */\n version?: string;\n /** Override the banner timestamp source — omitted by default so output is deterministic. */\n bannerComment?: string;\n}\n\n/** Helper types emitted once at the top of every generated file (self-contained, zero-dep). */\nconst PREAMBLE = `/**\n * Rich-text field value — the serialized portable rich-text payload returned by the\n * Delivery API. Treated as opaque; render it with your editor's serializer.\n */\nexport type RichText = { readonly format: string; readonly value: unknown };\n\n/** Image / media field value as resolved by the Delivery API. */\nexport interface BetterCMSImage {\n readonly url: string;\n readonly alt?: string;\n readonly width?: number;\n readonly height?: number;\n}\n\n/**\n * Delivery envelope around a model's typed \\`data\\`. \\`getEntry\\`/\\`listEntries\\` in the\n * Next adapter return this shape, with \\`fields\\` typed by the model.\n */\nexport interface BetterCMSEntry<TFields> {\n readonly slug: string;\n readonly status: \"draft\" | \"published\";\n readonly fields: TFields;\n readonly updatedAt: string;\n}\n`;\n\n/** PascalCase an identifier from a slug: \"case-study\" → \"CaseStudy\". */\nfunction pascalCase(slug: string): string {\n const parts = slug.split(/[-_\\s]+/).filter(Boolean);\n const pascal = parts\n .map((p) => p.charAt(0).toUpperCase() + p.slice(1))\n .join(\"\");\n // Guard against an identifier that starts with a digit (invalid TS type name).\n return /^[0-9]/.test(pascal) ? `Model${pascal}` : pascal || \"Model\";\n}\n\n/**\n * Make a string safe to embed inside a `/** ... */` JSDoc comment. A field label\n * (free-text, author/agent-controlled) could contain `*/` — which closes the comment\n * early and injects the remainder as code — or a newline, which breaks the single-line\n * comment. Both are neutralized here. Without this, hostile content produces non-\n * compiling (or worse, code-injected) output.\n */\nfunction escapeJsDoc(text: string): string {\n return text.replace(/\\*\\//g, \"* /\").replace(/[\\r\\n]+/g, \" \").trim();\n}\n\nconst VALID_IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * Render a field key as a TS property name. Field keys are author/agent-controlled and\n * not guaranteed to be valid identifiers (e.g. \"my-field\", \"1title\", \"\"), so anything\n * that isn't a bare identifier is emitted as a quoted property name — always valid TS.\n */\nfunction propName(key: string): string {\n return VALID_IDENT.test(key) ? key : JSON.stringify(key);\n}\n\n/** A scalar/primitive field maps to a TS type expression (no nesting). */\nfunction scalarType(field: ContentModelField): string {\n const t: ContentModelFieldType = field.type;\n switch (t) {\n case \"text\":\n return \"string\";\n case \"richtext\":\n return \"RichText\";\n case \"image\":\n return \"BetterCMSImage\";\n case \"boolean\":\n return \"boolean\";\n case \"number\":\n return \"number\";\n case \"date\":\n case \"datetime\":\n return \"string\"; // ISO 8601\n case \"select\": {\n const opts = field.options?.filter((o) => typeof o === \"string\") ?? [];\n return opts.length > 0\n ? opts.map((o) => JSON.stringify(o)).join(\" | \")\n : \"string\";\n }\n case \"reference\":\n return \"string\"; // referenced entry id\n case \"multi-reference\":\n return \"string[]\"; // referenced entry ids\n case \"array\": {\n const itemType = (field.config?.itemType as string) ?? \"text\";\n const inner =\n itemType === \"number\" ? \"number\" : \"string\"; // text | date → string\n return `${inner}[]`;\n }\n case \"group\":\n case \"repeater\":\n // Handled by the caller (needs nested-field expansion); never reached.\n return \"unknown\";\n default: {\n // Exhaustiveness guard: if a new field type is added to the union and not\n // mapped here, this line becomes a compile error in the codegen build.\n const _exhaustive: never = t;\n return \"unknown\";\n }\n }\n}\n\n/** Render the body of an object type from a field list, recursing into zones. */\nfunction fieldsToBody(fields: ContentModelField[], indent: string): string {\n const lines: string[] = [];\n for (const field of fields) {\n const optional = field.required ? \"\" : \"?\";\n let typeExpr: string;\n\n if (field.type === \"group\") {\n const nested = fieldsToBody(field.fields ?? [], indent + \" \");\n typeExpr = `{\\n${nested}\\n${indent}}`;\n } else if (field.type === \"repeater\") {\n const nested = fieldsToBody(field.fields ?? [], indent + \" \");\n typeExpr = `Array<{\\n${nested}\\n${indent}}>`;\n } else {\n typeExpr = scalarType(field);\n }\n\n const safeLabel = field.label ? escapeJsDoc(field.label) : \"\";\n if (safeLabel && safeLabel !== field.key) {\n lines.push(`${indent}/** ${safeLabel} */`);\n }\n lines.push(`${indent}readonly ${propName(field.key)}${optional}: ${typeExpr};`);\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Generate a complete `.ts` module from a set of content models.\n * Deterministic: models are sorted by slug; field order is preserved as authored.\n */\nexport function generateTypes(\n models: GeneratableModel[],\n opts: GenerateOptions = {},\n): string {\n const version = opts.version ?? \"0.1.0\";\n // Code-unit sort (NOT localeCompare): locale/ICU-independent so the generated\n // file is byte-identical on every machine — committed output diffs cleanly.\n const sorted = [...models].sort((a, b) =>\n a.slug < b.slug ? -1 : a.slug > b.slug ? 1 : 0,\n );\n\n const header = `// ⚠️ AUTO-GENERATED by @betttercms/codegen v${version} — DO NOT EDIT.\n// Regenerate with: npx @betttercms/codegen\n// Source of truth: your BetterCMS content models (the same schema the dashboard\n// builder and the MCP tools write). Re-run codegen after any schema change.\n${opts.bannerComment ? `// ${opts.bannerComment}\\n` : \"\"}`;\n\n const interfaces: string[] = [];\n const mapEntries: string[] = [];\n // Different slugs can PascalCase to the same base name (e.g. \"case-study\" and\n // \"case_study\" → \"CaseStudy\"). Emitting two identical interfaces would silently\n // declaration-merge into one wrong type, so disambiguate with a numeric suffix.\n const usedNames = new Set<string>();\n\n for (const model of sorted) {\n const base = `${pascalCase(model.slug)}Fields`;\n let typeName = base;\n for (let n = 2; usedNames.has(typeName); n++) typeName = `${base}_${n}`;\n usedNames.add(typeName);\n\n const name = model.name ? escapeJsDoc(model.name) : \"\";\n const desc = model.description ? escapeJsDoc(model.description) : \"\";\n const doc = name\n ? `/**\\n * ${name}${desc ? ` — ${desc}` : \"\"}\\n * Model slug: \\`${model.slug}\\`\\n */\\n`\n : \"\";\n const body = model.fields.length\n ? fieldsToBody(model.fields, \" \")\n : \" // (no fields defined yet)\";\n interfaces.push(`${doc}export interface ${typeName} {\\n${body}\\n}`);\n mapEntries.push(` readonly ${JSON.stringify(model.slug)}: ${typeName};`);\n }\n\n const schemaMap = `/**\n * Registry mapping each model slug to its typed fields. The Next adapter uses this to\n * type \\`getEntry(\"blog\", ...)\\` by slug — autocomplete and exhaustiveness for free.\n */\nexport interface BetterCMSSchema {\n${mapEntries.join(\"\\n\") || \" // (no models defined yet)\"}\n}\n\n/** Union of all model slugs. */\nexport type BetterCMSModelSlug = keyof BetterCMSSchema;`;\n\n return [header, PREAMBLE, interfaces.join(\"\\n\\n\"), schemaMap, \"\"].join(\"\\n\");\n}\n"],"mappings":";;;AAUA,SAAS,WAAW,aAAa;AACjC,SAAS,SAAS,eAAe;;;ACiBjC,eAAsB,YACpB,MAC6B;AAC7B,QAAM,UAAU,KAAK,aAAa,WAAW;AAC7C,QAAM,OAAO,KAAK,OAAO,QAAQ,QAAQ,EAAE;AAC3C,QAAM,MAAM,GAAG,IAAI;AAEnB,MAAI;AACJ,MAAI;AACF,UAAM,MAAM,QAAQ,KAAK;AAAA;AAAA;AAAA,MAGvB,SAAS,EAAE,eAAe,UAAU,KAAK,MAAM,IAAI,QAAQ,mBAAmB;AAAA,IAChF,CAAC;AAAA,EACH,SAAS,KAAK;AACZ,UAAM,IAAI;AAAA,MACR,mDAAmD,GAAG,KACpD,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,CACjD;AAAA,IACF;AAAA,EACF;AAEA,MAAI,CAAC,IAAI,IAAI;AACX,UAAM,OACJ,IAAI,WAAW,OAAO,IAAI,WAAW,MACjC,mFACA;AACN,UAAM,IAAI,MAAM,2BAA2B,IAAI,MAAM,IAAI,IAAI,UAAU,GAAG,IAAI,EAAE;AAAA,EAClF;AAEA,QAAM,OAAQ,MAAM,IAAI,KAAK;AAC7B,QAAM,OAAO,KAAK,QAAQ,CAAC;AAC3B,SAAO,KAAK,IAAI,CAAC,OAAO;AAAA,IACtB,MAAM,EAAE;AAAA,IACR,MAAM,EAAE;AAAA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AS,QAAQ,MAAM;AAEvC,UAAQ,OAAO;AAAA,IACb,oBAAe,OAAO,MAAM,cAAc,OAAO,WAAW,IAAI,KAAK,GAAG,WAAM,KAAK,GAAG;AAAA;AAAA,EACxF;AACF;AAEA,KAAK,EAAE,MAAM,CAAC,QAAiB;AAC7B,UAAQ,OAAO,MAAM,UAAU,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,CAAC;AAAA,CAAI;AACnF,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts","../src/fetch-models.ts","../src/generate.ts"],"sourcesContent":["/**\n * `bettercms-codegen` — fetch a project's content models and write a typed `.ts` file.\n *\n * Designed for two call sites:\n * 1. A developer in their repo: npx @betttercms/codegen --out src/bettercms.generated.ts\n * 2. The build-time GitHub Action: same command, key from a repo secret.\n *\n * Auth + endpoint come from flags or env (BETTERCMS_API_KEY, BETTERCMS_API_URL).\n */\n\nimport { writeFile, mkdir } from \"node:fs/promises\";\nimport { dirname, resolve } from \"node:path\";\nimport { fetchModels } from \"./fetch-models.js\";\nimport { generateTypes } from \"./generate.js\";\n\nconst VERSION = \"0.2.0\";\nconst DEFAULT_API_URL = \"https://api.bettercms.ai/api/v1\";\nconst DEFAULT_OUT = \"bettercms.generated.ts\";\n\ninterface CliArgs {\n apiUrl: string;\n apiKey: string | undefined;\n out: string;\n help: boolean;\n}\n\nfunction parseArgs(argv: string[]): CliArgs {\n const args: CliArgs = {\n apiUrl: process.env.BETTERCMS_API_URL ?? DEFAULT_API_URL,\n apiKey: process.env.BETTERCMS_API_KEY,\n out: DEFAULT_OUT,\n help: false,\n };\n for (let i = 0; i < argv.length; i++) {\n const arg = argv[i];\n const next = () => argv[++i];\n switch (arg) {\n case \"--api-url\":\n args.apiUrl = next() ?? args.apiUrl;\n break;\n case \"--key\":\n case \"--api-key\":\n args.apiKey = next();\n break;\n case \"--out\":\n case \"-o\":\n args.out = next() ?? args.out;\n break;\n case \"--help\":\n case \"-h\":\n args.help = true;\n break;\n }\n }\n return args;\n}\n\nconst HELP = `bettercms-codegen v${VERSION} — generate TypeScript types from your BetterCMS schema\n\nUsage:\n npx @betttercms/codegen [options]\n\nOptions:\n -o, --out <path> Output file (default: ${DEFAULT_OUT})\n --api-url <url> Management API base (default: ${DEFAULT_API_URL})\n --key <key> Management API key (or set BETTERCMS_API_KEY)\n -h, --help Show this help\n\nEnv:\n BETTERCMS_API_KEY Management-scoped key (content:manage)\n BETTERCMS_API_URL Override the API base\n`;\n\nasync function main(): Promise<void> {\n const args = parseArgs(process.argv.slice(2));\n\n if (args.help) {\n process.stdout.write(HELP);\n return;\n }\n if (!args.apiKey) {\n process.stderr.write(\n \"error: no API key. Pass --key <key> or set BETTERCMS_API_KEY.\\n\",\n );\n process.exit(1);\n }\n\n const models = await fetchModels({ apiUrl: args.apiUrl, apiKey: args.apiKey });\n const source = generateTypes(models, { version: VERSION });\n\n const outPath = resolve(process.cwd(), args.out);\n await mkdir(dirname(outPath), { recursive: true });\n await writeFile(outPath, source, \"utf8\");\n\n process.stdout.write(\n `✓ Generated ${models.length} model type${models.length === 1 ? \"\" : \"s\"} → ${args.out}\\n`,\n );\n}\n\nmain().catch((err: unknown) => {\n process.stderr.write(`error: ${err instanceof Error ? err.message : String(err)}\\n`);\n process.exit(1);\n});\n","/**\n * Fetches content models from the BetterCMS Management API so the CLI can generate\n * types against a live project. Kept dependency-free (plain fetch) so the generated\n * artifact and this fetcher can run anywhere — a GitHub Action, a postinstall, a script.\n */\n\nimport type { GeneratableModel } from \"./generate.js\";\n\nexport interface FetchModelsOptions {\n /** Management API base, e.g. \"https://api.bettercms.ai/api/v1\". */\n apiUrl: string;\n /** A management-scoped key (content:manage) or device-minted token. */\n apiKey: string;\n /** Optional fetch override (testing / custom runtime). */\n fetchImpl?: typeof fetch;\n}\n\ninterface ManagedModelRow {\n slug: string;\n name?: string;\n description?: string | null;\n fields: GeneratableModel[\"fields\"];\n}\n\n/**\n * GET /management/content/models — returns the project's models (the key is\n * project-scoped server-side, so this is exactly the schema for this site).\n */\nexport async function fetchModels(\n opts: FetchModelsOptions,\n): Promise<GeneratableModel[]> {\n const doFetch = opts.fetchImpl ?? globalThis.fetch;\n const base = opts.apiUrl.replace(/\\/+$/, \"\");\n const url = `${base}/management/content/models`;\n\n let res: Response;\n try {\n res = await doFetch(url, {\n // No Content-Type: this is a bodyless GET; the header is incorrect here and\n // strict edge runtimes/proxies may reject it.\n headers: { Authorization: `Bearer ${opts.apiKey}`, Accept: \"application/json\" },\n });\n } catch (err) {\n throw new Error(\n `Could not reach the BetterCMS Management API at ${url}: ${\n err instanceof Error ? err.message : String(err)\n }`,\n );\n }\n\n if (!res.ok) {\n const hint =\n res.status === 401 || res.status === 403\n ? \" — check your management API key (it must have the content:manage scope).\"\n : \"\";\n throw new Error(`Management API returned ${res.status} ${res.statusText}${hint}`);\n }\n\n const body = (await res.json()) as { data?: ManagedModelRow[] };\n const rows = body.data ?? [];\n return rows.map((r) => ({\n slug: r.slug,\n name: r.name,\n description: r.description ?? null,\n fields: r.fields ?? [],\n }));\n}\n","/**\n * @betttercms/codegen — schema → TypeScript generator (the single source of truth).\n *\n * Both the dashboard schema builder and the MCP `create_model`/`add_field` tools write\n * the SAME `content_models.fields` (an array of `ContentModelField`). This generator maps\n * that one array into TypeScript. Because there is exactly one schema representation, the\n * generated types can never drift from the editor or the agent — they are the same source.\n *\n * Pure + deterministic: same models in → identical string out (stable ordering, no clock,\n * no I/O). That makes it trivially testable and safe to commit + diff in a customer repo.\n */\n\nimport type { ContentModelField, ContentModelFieldType } from \"@betttercms/types\";\n\n/** Minimal model shape the generator needs — a subset of the Management API model row. */\nexport interface GeneratableModel {\n /** Machine-safe slug, e.g. \"blog\" or \"case-study\". Used for the schema-map key. */\n slug: string;\n /** Human name, used only for the JSDoc header. */\n name?: string;\n description?: string | null;\n fields: ContentModelField[];\n}\n\nexport interface GenerateOptions {\n /** Generator version stamped into the header (defaults to the package version). */\n version?: string;\n /** Override the banner timestamp source — omitted by default so output is deterministic. */\n bannerComment?: string;\n}\n\n/** Helper types emitted once at the top of every generated file (self-contained, zero-dep). */\nconst PREAMBLE = `/**\n * Rich-text field value returned by the Delivery API.\n *\n * - \\`format\\`/\\`value\\`: the portable, editor-agnostic payload (Lexical EditorState) —\n * render it with your editor's serializer for full fidelity.\n * - \\`html\\`: server-rendered, sanitized HTML (computed render-on-write). Present on\n * Delivery reads; the simplest path for non-React consumers — safe to inject directly\n * (e.g. \\`dangerouslySetInnerHTML\\`). Optional: legacy/un-normalized values may omit it.\n *\n * The \\`{ format, value }\\` contract is unchanged; \\`html\\` is additive.\n */\nexport type RichText = {\n readonly format: string;\n readonly value: unknown;\n readonly html?: string;\n};\n\n/**\n * Image / media field value as stored and returned verbatim by the Delivery API\n * (server-normalized on write to the canonical shape). \\`url\\` is always present; an\n * unresolved/external value may carry only \\`url\\`. \\`altText\\` is the accessibility text\n * for \\`<img alt>\\`.\n */\nexport interface BetterCMSImage {\n readonly id?: string;\n readonly url: string;\n readonly name?: string;\n readonly altText?: string | null;\n readonly width?: number;\n readonly height?: number;\n}\n\n/**\n * Delivery envelope around a model's typed \\`data\\`. \\`getEntry\\`/\\`listEntries\\` in the\n * Next adapter return this shape, with \\`fields\\` typed by the model.\n */\nexport interface BetterCMSEntry<TFields> {\n readonly slug: string;\n readonly status: \"draft\" | \"published\";\n readonly fields: TFields;\n readonly updatedAt: string;\n}\n`;\n\n/** PascalCase an identifier from a slug: \"case-study\" → \"CaseStudy\". */\nfunction pascalCase(slug: string): string {\n const parts = slug.split(/[-_\\s]+/).filter(Boolean);\n const pascal = parts\n .map((p) => p.charAt(0).toUpperCase() + p.slice(1))\n .join(\"\");\n // Guard against an identifier that starts with a digit (invalid TS type name).\n return /^[0-9]/.test(pascal) ? `Model${pascal}` : pascal || \"Model\";\n}\n\n/**\n * Make a string safe to embed inside a `/** ... */` JSDoc comment. A field label\n * (free-text, author/agent-controlled) could contain `*/` — which closes the comment\n * early and injects the remainder as code — or a newline, which breaks the single-line\n * comment. Both are neutralized here. Without this, hostile content produces non-\n * compiling (or worse, code-injected) output.\n */\nfunction escapeJsDoc(text: string): string {\n return text.replace(/\\*\\//g, \"* /\").replace(/[\\r\\n]+/g, \" \").trim();\n}\n\nconst VALID_IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * Render a field key as a TS property name. Field keys are author/agent-controlled and\n * not guaranteed to be valid identifiers (e.g. \"my-field\", \"1title\", \"\"), so anything\n * that isn't a bare identifier is emitted as a quoted property name — always valid TS.\n */\nfunction propName(key: string): string {\n return VALID_IDENT.test(key) ? key : JSON.stringify(key);\n}\n\n/** A scalar/primitive field maps to a TS type expression (no nesting). */\nfunction scalarType(field: ContentModelField): string {\n const t: ContentModelFieldType = field.type;\n switch (t) {\n case \"text\":\n return \"string\";\n case \"richtext\":\n return \"RichText\";\n case \"image\":\n return \"BetterCMSImage\";\n case \"boolean\":\n return \"boolean\";\n case \"number\":\n return \"number\";\n case \"date\":\n case \"datetime\":\n return \"string\"; // ISO 8601\n case \"select\": {\n const opts = field.options?.filter((o) => typeof o === \"string\") ?? [];\n return opts.length > 0\n ? opts.map((o) => JSON.stringify(o)).join(\" | \")\n : \"string\";\n }\n case \"reference\":\n return \"string\"; // referenced entry id\n case \"multi-reference\":\n return \"string[]\"; // referenced entry ids\n case \"array\": {\n // Zoned arrays (config.zones) are expanded by fieldsToBody before reaching here;\n // this branch handles only the primitive list form (config.itemType).\n const itemType = field.config?.itemType ?? \"text\";\n const inner =\n itemType === \"number\" ? \"number\" : \"string\"; // text | date → string\n return `${inner}[]`;\n }\n default: {\n // Exhaustiveness guard: if a new field type is added to the union and not\n // mapped here, this line becomes a compile error in the codegen build.\n const _exhaustive: never = t;\n return \"unknown\";\n }\n }\n}\n\n/**\n * Render the TS type for a zoned `array` field: an object with optional\n * `nonRepeatable` (a fixed block) and/or `repeatable` (a list of blocks). Recurses\n * through zone fields, so a zone field that is itself a zoned `array` nests naturally.\n */\nfunction arrayZoneType(field: ContentModelField, indent: string): string {\n const zones = field.config?.zones;\n const parts: string[] = [];\n if (zones?.nonRepeatable?.length) {\n const nested = fieldsToBody(zones.nonRepeatable, indent + \" \");\n parts.push(`${indent} readonly nonRepeatable?: {\\n${nested}\\n${indent} };`);\n }\n if (zones?.repeatable?.fields?.length) {\n const nested = fieldsToBody(zones.repeatable.fields, indent + \" \");\n parts.push(`${indent} readonly repeatable?: Array<{\\n${nested}\\n${indent} }>;`);\n }\n if (parts.length === 0) return \"Record<string, unknown>\"; // zoned array with no fields yet\n return `{\\n${parts.join(\"\\n\")}\\n${indent}}`;\n}\n\n/** Render the body of an object type from a field list, recursing into zones. */\nfunction fieldsToBody(fields: ContentModelField[], indent: string): string {\n const lines: string[] = [];\n for (const field of fields) {\n const optional = field.required ? \"\" : \"?\";\n let typeExpr: string;\n\n if (field.type === \"array\" && field.config?.zones) {\n typeExpr = arrayZoneType(field, indent);\n } else {\n typeExpr = scalarType(field);\n }\n\n const safeLabel = field.label ? escapeJsDoc(field.label) : \"\";\n if (safeLabel && safeLabel !== field.key) {\n lines.push(`${indent}/** ${safeLabel} */`);\n }\n lines.push(`${indent}readonly ${propName(field.key)}${optional}: ${typeExpr};`);\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Generate a complete `.ts` module from a set of content models.\n * Deterministic: models are sorted by slug; field order is preserved as authored.\n */\nexport function generateTypes(\n models: GeneratableModel[],\n opts: GenerateOptions = {},\n): string {\n const version = opts.version ?? \"0.1.0\";\n // Code-unit sort (NOT localeCompare): locale/ICU-independent so the generated\n // file is byte-identical on every machine — committed output diffs cleanly.\n const sorted = [...models].sort((a, b) =>\n a.slug < b.slug ? -1 : a.slug > b.slug ? 1 : 0,\n );\n\n const header = `// ⚠️ AUTO-GENERATED by @betttercms/codegen v${version} — DO NOT EDIT.\n// Regenerate with: npx @betttercms/codegen\n// Source of truth: your BetterCMS content models (the same schema the dashboard\n// builder and the MCP tools write). Re-run codegen after any schema change.\n${opts.bannerComment ? `// ${opts.bannerComment}\\n` : \"\"}`;\n\n const interfaces: string[] = [];\n const mapEntries: string[] = [];\n // Different slugs can PascalCase to the same base name (e.g. \"case-study\" and\n // \"case_study\" → \"CaseStudy\"). Emitting two identical interfaces would silently\n // declaration-merge into one wrong type, so disambiguate with a numeric suffix.\n const usedNames = new Set<string>();\n\n for (const model of sorted) {\n const base = `${pascalCase(model.slug)}Fields`;\n let typeName = base;\n for (let n = 2; usedNames.has(typeName); n++) typeName = `${base}_${n}`;\n usedNames.add(typeName);\n\n const name = model.name ? escapeJsDoc(model.name) : \"\";\n const desc = model.description ? escapeJsDoc(model.description) : \"\";\n const doc = name\n ? `/**\\n * ${name}${desc ? ` — ${desc}` : \"\"}\\n * Model slug: \\`${model.slug}\\`\\n */\\n`\n : \"\";\n const body = model.fields.length\n ? fieldsToBody(model.fields, \" \")\n : \" // (no fields defined yet)\";\n interfaces.push(`${doc}export interface ${typeName} {\\n${body}\\n}`);\n mapEntries.push(` readonly ${JSON.stringify(model.slug)}: ${typeName};`);\n }\n\n const schemaMap = `/**\n * Registry mapping each model slug to its typed fields. The Next adapter uses this to\n * type \\`getEntry(\"blog\", ...)\\` by slug — autocomplete and exhaustiveness for free.\n */\nexport interface BetterCMSSchema {\n${mapEntries.join(\"\\n\") || \" // (no models defined yet)\"}\n}\n\n/** Union of all model slugs. */\nexport type BetterCMSModelSlug = keyof BetterCMSSchema;`;\n\n return [header, PREAMBLE, interfaces.join(\"\\n\\n\"), schemaMap, \"\"].join(\"\\n\");\n}\n"],"mappings":";;;AAUA,SAAS,WAAW,aAAa;AACjC,SAAS,SAAS,eAAe;;;ACiBjC,eAAsB,YACpB,MAC6B;AAC7B,QAAM,UAAU,KAAK,aAAa,WAAW;AAC7C,QAAM,OAAO,KAAK,OAAO,QAAQ,QAAQ,EAAE;AAC3C,QAAM,MAAM,GAAG,IAAI;AAEnB,MAAI;AACJ,MAAI;AACF,UAAM,MAAM,QAAQ,KAAK;AAAA;AAAA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AAA,CAAI;AACnF,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":[]}
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
// src/generate.ts
|
|
2
2
|
var PREAMBLE = `/**
|
|
3
|
-
* Rich-text field value
|
|
4
|
-
*
|
|
3
|
+
* Rich-text field value returned by the Delivery API.
|
|
4
|
+
*
|
|
5
|
+
* - \`format\`/\`value\`: the portable, editor-agnostic payload (Lexical EditorState) \u2014
|
|
6
|
+
* render it with your editor's serializer for full fidelity.
|
|
7
|
+
* - \`html\`: server-rendered, sanitized HTML (computed render-on-write). Present on
|
|
8
|
+
* Delivery reads; the simplest path for non-React consumers \u2014 safe to inject directly
|
|
9
|
+
* (e.g. \`dangerouslySetInnerHTML\`). Optional: legacy/un-normalized values may omit it.
|
|
10
|
+
*
|
|
11
|
+
* The \`{ format, value }\` contract is unchanged; \`html\` is additive.
|
|
5
12
|
*/
|
|
6
|
-
export type RichText = {
|
|
13
|
+
export type RichText = {
|
|
14
|
+
readonly format: string;
|
|
15
|
+
readonly value: unknown;
|
|
16
|
+
readonly html?: string;
|
|
17
|
+
};
|
|
7
18
|
|
|
8
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Image / media field value as stored and returned verbatim by the Delivery API
|
|
21
|
+
* (server-normalized on write to the canonical shape). \`url\` is always present; an
|
|
22
|
+
* unresolved/external value may carry only \`url\`. \`altText\` is the accessibility text
|
|
23
|
+
* for \`<img alt>\`.
|
|
24
|
+
*/
|
|
9
25
|
export interface BetterCMSImage {
|
|
26
|
+
readonly id?: string;
|
|
10
27
|
readonly url: string;
|
|
11
|
-
readonly
|
|
28
|
+
readonly name?: string;
|
|
29
|
+
readonly altText?: string | null;
|
|
12
30
|
readonly width?: number;
|
|
13
31
|
readonly height?: number;
|
|
14
32
|
}
|
|
@@ -68,30 +86,39 @@ function scalarType(field) {
|
|
|
68
86
|
const inner = itemType === "number" ? "number" : "string";
|
|
69
87
|
return `${inner}[]`;
|
|
70
88
|
}
|
|
71
|
-
case "group":
|
|
72
|
-
case "repeater":
|
|
73
|
-
return "unknown";
|
|
74
89
|
default: {
|
|
75
90
|
const _exhaustive = t;
|
|
76
91
|
return "unknown";
|
|
77
92
|
}
|
|
78
93
|
}
|
|
79
94
|
}
|
|
95
|
+
function arrayZoneType(field, indent) {
|
|
96
|
+
const zones = field.config?.zones;
|
|
97
|
+
const parts = [];
|
|
98
|
+
if (zones?.nonRepeatable?.length) {
|
|
99
|
+
const nested = fieldsToBody(zones.nonRepeatable, indent + " ");
|
|
100
|
+
parts.push(`${indent} readonly nonRepeatable?: {
|
|
101
|
+
${nested}
|
|
102
|
+
${indent} };`);
|
|
103
|
+
}
|
|
104
|
+
if (zones?.repeatable?.fields?.length) {
|
|
105
|
+
const nested = fieldsToBody(zones.repeatable.fields, indent + " ");
|
|
106
|
+
parts.push(`${indent} readonly repeatable?: Array<{
|
|
107
|
+
${nested}
|
|
108
|
+
${indent} }>;`);
|
|
109
|
+
}
|
|
110
|
+
if (parts.length === 0) return "Record<string, unknown>";
|
|
111
|
+
return `{
|
|
112
|
+
${parts.join("\n")}
|
|
113
|
+
${indent}}`;
|
|
114
|
+
}
|
|
80
115
|
function fieldsToBody(fields, indent) {
|
|
81
116
|
const lines = [];
|
|
82
117
|
for (const field of fields) {
|
|
83
118
|
const optional = field.required ? "" : "?";
|
|
84
119
|
let typeExpr;
|
|
85
|
-
if (field.type === "
|
|
86
|
-
|
|
87
|
-
typeExpr = `{
|
|
88
|
-
${nested}
|
|
89
|
-
${indent}}`;
|
|
90
|
-
} else if (field.type === "repeater") {
|
|
91
|
-
const nested = fieldsToBody(field.fields ?? [], indent + " ");
|
|
92
|
-
typeExpr = `Array<{
|
|
93
|
-
${nested}
|
|
94
|
-
${indent}}>`;
|
|
120
|
+
if (field.type === "array" && field.config?.zones) {
|
|
121
|
+
typeExpr = arrayZoneType(field, indent);
|
|
95
122
|
} else {
|
|
96
123
|
typeExpr = scalarType(field);
|
|
97
124
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generate.ts","../src/fetch-models.ts"],"sourcesContent":["/**\n * @betttercms/codegen — schema → TypeScript generator (the single source of truth).\n *\n * Both the dashboard schema builder and the MCP `create_model`/`add_field` tools write\n * the SAME `content_models.fields` (an array of `ContentModelField`). This generator maps\n * that one array into TypeScript. Because there is exactly one schema representation, the\n * generated types can never drift from the editor or the agent — they are the same source.\n *\n * Pure + deterministic: same models in → identical string out (stable ordering, no clock,\n * no I/O). That makes it trivially testable and safe to commit + diff in a customer repo.\n */\n\nimport type { ContentModelField, ContentModelFieldType } from \"@betttercms/types\";\n\n/** Minimal model shape the generator needs — a subset of the Management API model row. */\nexport interface GeneratableModel {\n /** Machine-safe slug, e.g. \"blog\" or \"case-study\". Used for the schema-map key. */\n slug: string;\n /** Human name, used only for the JSDoc header. */\n name?: string;\n description?: string | null;\n fields: ContentModelField[];\n}\n\nexport interface GenerateOptions {\n /** Generator version stamped into the header (defaults to the package version). */\n version?: string;\n /** Override the banner timestamp source — omitted by default so output is deterministic. */\n bannerComment?: string;\n}\n\n/** Helper types emitted once at the top of every generated file (self-contained, zero-dep). */\nconst PREAMBLE = `/**\n * Rich-text field value — the serialized portable rich-text payload returned by the\n * Delivery API. Treated as opaque; render it with your editor's serializer.\n */\nexport type RichText = { readonly format: string; readonly value: unknown };\n\n/** Image / media field value as resolved by the Delivery API. */\nexport interface BetterCMSImage {\n readonly url: string;\n readonly alt?: string;\n readonly width?: number;\n readonly height?: number;\n}\n\n/**\n * Delivery envelope around a model's typed \\`data\\`. \\`getEntry\\`/\\`listEntries\\` in the\n * Next adapter return this shape, with \\`fields\\` typed by the model.\n */\nexport interface BetterCMSEntry<TFields> {\n readonly slug: string;\n readonly status: \"draft\" | \"published\";\n readonly fields: TFields;\n readonly updatedAt: string;\n}\n`;\n\n/** PascalCase an identifier from a slug: \"case-study\" → \"CaseStudy\". */\nfunction pascalCase(slug: string): string {\n const parts = slug.split(/[-_\\s]+/).filter(Boolean);\n const pascal = parts\n .map((p) => p.charAt(0).toUpperCase() + p.slice(1))\n .join(\"\");\n // Guard against an identifier that starts with a digit (invalid TS type name).\n return /^[0-9]/.test(pascal) ? `Model${pascal}` : pascal || \"Model\";\n}\n\n/**\n * Make a string safe to embed inside a `/** ... */` JSDoc comment. A field label\n * (free-text, author/agent-controlled) could contain `*/` — which closes the comment\n * early and injects the remainder as code — or a newline, which breaks the single-line\n * comment. Both are neutralized here. Without this, hostile content produces non-\n * compiling (or worse, code-injected) output.\n */\nfunction escapeJsDoc(text: string): string {\n return text.replace(/\\*\\//g, \"* /\").replace(/[\\r\\n]+/g, \" \").trim();\n}\n\nconst VALID_IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * Render a field key as a TS property name. Field keys are author/agent-controlled and\n * not guaranteed to be valid identifiers (e.g. \"my-field\", \"1title\", \"\"), so anything\n * that isn't a bare identifier is emitted as a quoted property name — always valid TS.\n */\nfunction propName(key: string): string {\n return VALID_IDENT.test(key) ? key : JSON.stringify(key);\n}\n\n/** A scalar/primitive field maps to a TS type expression (no nesting). */\nfunction scalarType(field: ContentModelField): string {\n const t: ContentModelFieldType = field.type;\n switch (t) {\n case \"text\":\n return \"string\";\n case \"richtext\":\n return \"RichText\";\n case \"image\":\n return \"BetterCMSImage\";\n case \"boolean\":\n return \"boolean\";\n case \"number\":\n return \"number\";\n case \"date\":\n case \"datetime\":\n return \"string\"; // ISO 8601\n case \"select\": {\n const opts = field.options?.filter((o) => typeof o === \"string\") ?? [];\n return opts.length > 0\n ? opts.map((o) => JSON.stringify(o)).join(\" | \")\n : \"string\";\n }\n case \"reference\":\n return \"string\"; // referenced entry id\n case \"multi-reference\":\n return \"string[]\"; // referenced entry ids\n case \"array\": {\n const itemType = (field.config?.itemType as string) ?? \"text\";\n const inner =\n itemType === \"number\" ? \"number\" : \"string\"; // text | date → string\n return `${inner}[]`;\n }\n case \"group\":\n case \"repeater\":\n // Handled by the caller (needs nested-field expansion); never reached.\n return \"unknown\";\n default: {\n // Exhaustiveness guard: if a new field type is added to the union and not\n // mapped here, this line becomes a compile error in the codegen build.\n const _exhaustive: never = t;\n return \"unknown\";\n }\n }\n}\n\n/** Render the body of an object type from a field list, recursing into zones. */\nfunction fieldsToBody(fields: ContentModelField[], indent: string): string {\n const lines: string[] = [];\n for (const field of fields) {\n const optional = field.required ? \"\" : \"?\";\n let typeExpr: string;\n\n if (field.type === \"group\") {\n const nested = fieldsToBody(field.fields ?? [], indent + \" \");\n typeExpr = `{\\n${nested}\\n${indent}}`;\n } else if (field.type === \"repeater\") {\n const nested = fieldsToBody(field.fields ?? [], indent + \" \");\n typeExpr = `Array<{\\n${nested}\\n${indent}}>`;\n } else {\n typeExpr = scalarType(field);\n }\n\n const safeLabel = field.label ? escapeJsDoc(field.label) : \"\";\n if (safeLabel && safeLabel !== field.key) {\n lines.push(`${indent}/** ${safeLabel} */`);\n }\n lines.push(`${indent}readonly ${propName(field.key)}${optional}: ${typeExpr};`);\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Generate a complete `.ts` module from a set of content models.\n * Deterministic: models are sorted by slug; field order is preserved as authored.\n */\nexport function generateTypes(\n models: GeneratableModel[],\n opts: GenerateOptions = {},\n): string {\n const version = opts.version ?? \"0.1.0\";\n // Code-unit sort (NOT localeCompare): locale/ICU-independent so the generated\n // file is byte-identical on every machine — committed output diffs cleanly.\n const sorted = [...models].sort((a, b) =>\n a.slug < b.slug ? -1 : a.slug > b.slug ? 1 : 0,\n );\n\n const header = `// ⚠️ AUTO-GENERATED by @betttercms/codegen v${version} — DO NOT EDIT.\n// Regenerate with: npx @betttercms/codegen\n// Source of truth: your BetterCMS content models (the same schema the dashboard\n// builder and the MCP tools write). Re-run codegen after any schema change.\n${opts.bannerComment ? `// ${opts.bannerComment}\\n` : \"\"}`;\n\n const interfaces: string[] = [];\n const mapEntries: string[] = [];\n // Different slugs can PascalCase to the same base name (e.g. \"case-study\" and\n // \"case_study\" → \"CaseStudy\"). Emitting two identical interfaces would silently\n // declaration-merge into one wrong type, so disambiguate with a numeric suffix.\n const usedNames = new Set<string>();\n\n for (const model of sorted) {\n const base = `${pascalCase(model.slug)}Fields`;\n let typeName = base;\n for (let n = 2; usedNames.has(typeName); n++) typeName = `${base}_${n}`;\n usedNames.add(typeName);\n\n const name = model.name ? escapeJsDoc(model.name) : \"\";\n const desc = model.description ? escapeJsDoc(model.description) : \"\";\n const doc = name\n ? `/**\\n * ${name}${desc ? ` — ${desc}` : \"\"}\\n * Model slug: \\`${model.slug}\\`\\n */\\n`\n : \"\";\n const body = model.fields.length\n ? fieldsToBody(model.fields, \" \")\n : \" // (no fields defined yet)\";\n interfaces.push(`${doc}export interface ${typeName} {\\n${body}\\n}`);\n mapEntries.push(` readonly ${JSON.stringify(model.slug)}: ${typeName};`);\n }\n\n const schemaMap = `/**\n * Registry mapping each model slug to its typed fields. The Next adapter uses this to\n * type \\`getEntry(\"blog\", ...)\\` by slug — autocomplete and exhaustiveness for free.\n */\nexport interface BetterCMSSchema {\n${mapEntries.join(\"\\n\") || \" // (no models defined yet)\"}\n}\n\n/** Union of all model slugs. */\nexport type BetterCMSModelSlug = keyof BetterCMSSchema;`;\n\n return [header, PREAMBLE, interfaces.join(\"\\n\\n\"), schemaMap, \"\"].join(\"\\n\");\n}\n","/**\n * Fetches content models from the BetterCMS Management API so the CLI can generate\n * types against a live project. Kept dependency-free (plain fetch) so the generated\n * artifact and this fetcher can run anywhere — a GitHub Action, a postinstall, a script.\n */\n\nimport type { GeneratableModel } from \"./generate.js\";\n\nexport interface FetchModelsOptions {\n /** Management API base, e.g. \"https://api.bettercms.ai/api/v1\". */\n apiUrl: string;\n /** A management-scoped key (content:manage) or device-minted token. */\n apiKey: string;\n /** Optional fetch override (testing / custom runtime). */\n fetchImpl?: typeof fetch;\n}\n\ninterface ManagedModelRow {\n slug: string;\n name?: string;\n description?: string | null;\n fields: GeneratableModel[\"fields\"];\n}\n\n/**\n * GET /management/content/models — returns the project's models (the key is\n * project-scoped server-side, so this is exactly the schema for this site).\n */\nexport async function fetchModels(\n opts: FetchModelsOptions,\n): Promise<GeneratableModel[]> {\n const doFetch = opts.fetchImpl ?? globalThis.fetch;\n const base = opts.apiUrl.replace(/\\/+$/, \"\");\n const url = `${base}/management/content/models`;\n\n let res: Response;\n try {\n res = await doFetch(url, {\n // No Content-Type: this is a bodyless GET; the header is incorrect here and\n // strict edge runtimes/proxies may reject it.\n headers: { Authorization: `Bearer ${opts.apiKey}`, Accept: \"application/json\" },\n });\n } catch (err) {\n throw new Error(\n `Could not reach the BetterCMS Management API at ${url}: ${\n err instanceof Error ? err.message : String(err)\n }`,\n );\n }\n\n if (!res.ok) {\n const hint =\n res.status === 401 || res.status === 403\n ? \" — check your management API key (it must have the content:manage scope).\"\n : \"\";\n throw new Error(`Management API returned ${res.status} ${res.statusText}${hint}`);\n }\n\n const body = (await res.json()) as { data?: ManagedModelRow[] };\n const rows = body.data ?? [];\n return rows.map((r) => ({\n slug: r.slug,\n name: r.name,\n description: r.description ?? null,\n fields: r.fields ?? [],\n }));\n}\n"],"mappings":";AAgCA,IAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2BjB,SAAS,WAAW,MAAsB;AACxC,QAAM,QAAQ,KAAK,MAAM,SAAS,EAAE,OAAO,OAAO;AAClD,QAAM,SAAS,MACZ,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,IAAI,EAAE,MAAM,CAAC,CAAC,EACjD,KAAK,EAAE;AAEV,SAAO,SAAS,KAAK,MAAM,IAAI,QAAQ,MAAM,KAAK,UAAU;AAC9D;AASA,SAAS,YAAY,MAAsB;AACzC,SAAO,KAAK,QAAQ,SAAS,KAAK,EAAE,QAAQ,YAAY,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SAAO,KAAK,IAAI,CAAC,OAAO;AAAA,IACtB,MAAM,EAAE;AAAA,IACR,MAAM,EAAE;AAAA,IACR,aAAa,EAAE,eAAe;AAAA,IAC9B,QAAQ,EAAE,UAAU,CAAC;AAAA,EACvB,EAAE;AACJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/generate.ts","../src/fetch-models.ts"],"sourcesContent":["/**\n * @betttercms/codegen — schema → TypeScript generator (the single source of truth).\n *\n * Both the dashboard schema builder and the MCP `create_model`/`add_field` tools write\n * the SAME `content_models.fields` (an array of `ContentModelField`). This generator maps\n * that one array into TypeScript. Because there is exactly one schema representation, the\n * generated types can never drift from the editor or the agent — they are the same source.\n *\n * Pure + deterministic: same models in → identical string out (stable ordering, no clock,\n * no I/O). That makes it trivially testable and safe to commit + diff in a customer repo.\n */\n\nimport type { ContentModelField, ContentModelFieldType } from \"@betttercms/types\";\n\n/** Minimal model shape the generator needs — a subset of the Management API model row. */\nexport interface GeneratableModel {\n /** Machine-safe slug, e.g. \"blog\" or \"case-study\". Used for the schema-map key. */\n slug: string;\n /** Human name, used only for the JSDoc header. */\n name?: string;\n description?: string | null;\n fields: ContentModelField[];\n}\n\nexport interface GenerateOptions {\n /** Generator version stamped into the header (defaults to the package version). */\n version?: string;\n /** Override the banner timestamp source — omitted by default so output is deterministic. */\n bannerComment?: string;\n}\n\n/** Helper types emitted once at the top of every generated file (self-contained, zero-dep). */\nconst PREAMBLE = `/**\n * Rich-text field value returned by the Delivery API.\n *\n * - \\`format\\`/\\`value\\`: the portable, editor-agnostic payload (Lexical EditorState) —\n * render it with your editor's serializer for full fidelity.\n * - \\`html\\`: server-rendered, sanitized HTML (computed render-on-write). Present on\n * Delivery reads; the simplest path for non-React consumers — safe to inject directly\n * (e.g. \\`dangerouslySetInnerHTML\\`). Optional: legacy/un-normalized values may omit it.\n *\n * The \\`{ format, value }\\` contract is unchanged; \\`html\\` is additive.\n */\nexport type RichText = {\n readonly format: string;\n readonly value: unknown;\n readonly html?: string;\n};\n\n/**\n * Image / media field value as stored and returned verbatim by the Delivery API\n * (server-normalized on write to the canonical shape). \\`url\\` is always present; an\n * unresolved/external value may carry only \\`url\\`. \\`altText\\` is the accessibility text\n * for \\`<img alt>\\`.\n */\nexport interface BetterCMSImage {\n readonly id?: string;\n readonly url: string;\n readonly name?: string;\n readonly altText?: string | null;\n readonly width?: number;\n readonly height?: number;\n}\n\n/**\n * Delivery envelope around a model's typed \\`data\\`. \\`getEntry\\`/\\`listEntries\\` in the\n * Next adapter return this shape, with \\`fields\\` typed by the model.\n */\nexport interface BetterCMSEntry<TFields> {\n readonly slug: string;\n readonly status: \"draft\" | \"published\";\n readonly fields: TFields;\n readonly updatedAt: string;\n}\n`;\n\n/** PascalCase an identifier from a slug: \"case-study\" → \"CaseStudy\". */\nfunction pascalCase(slug: string): string {\n const parts = slug.split(/[-_\\s]+/).filter(Boolean);\n const pascal = parts\n .map((p) => p.charAt(0).toUpperCase() + p.slice(1))\n .join(\"\");\n // Guard against an identifier that starts with a digit (invalid TS type name).\n return /^[0-9]/.test(pascal) ? `Model${pascal}` : pascal || \"Model\";\n}\n\n/**\n * Make a string safe to embed inside a `/** ... */` JSDoc comment. A field label\n * (free-text, author/agent-controlled) could contain `*/` — which closes the comment\n * early and injects the remainder as code — or a newline, which breaks the single-line\n * comment. Both are neutralized here. Without this, hostile content produces non-\n * compiling (or worse, code-injected) output.\n */\nfunction escapeJsDoc(text: string): string {\n return text.replace(/\\*\\//g, \"* /\").replace(/[\\r\\n]+/g, \" \").trim();\n}\n\nconst VALID_IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * Render a field key as a TS property name. Field keys are author/agent-controlled and\n * not guaranteed to be valid identifiers (e.g. \"my-field\", \"1title\", \"\"), so anything\n * that isn't a bare identifier is emitted as a quoted property name — always valid TS.\n */\nfunction propName(key: string): string {\n return VALID_IDENT.test(key) ? key : JSON.stringify(key);\n}\n\n/** A scalar/primitive field maps to a TS type expression (no nesting). */\nfunction scalarType(field: ContentModelField): string {\n const t: ContentModelFieldType = field.type;\n switch (t) {\n case \"text\":\n return \"string\";\n case \"richtext\":\n return \"RichText\";\n case \"image\":\n return \"BetterCMSImage\";\n case \"boolean\":\n return \"boolean\";\n case \"number\":\n return \"number\";\n case \"date\":\n case \"datetime\":\n return \"string\"; // ISO 8601\n case \"select\": {\n const opts = field.options?.filter((o) => typeof o === \"string\") ?? [];\n return opts.length > 0\n ? opts.map((o) => JSON.stringify(o)).join(\" | \")\n : \"string\";\n }\n case \"reference\":\n return \"string\"; // referenced entry id\n case \"multi-reference\":\n return \"string[]\"; // referenced entry ids\n case \"array\": {\n // Zoned arrays (config.zones) are expanded by fieldsToBody before reaching here;\n // this branch handles only the primitive list form (config.itemType).\n const itemType = field.config?.itemType ?? \"text\";\n const inner =\n itemType === \"number\" ? \"number\" : \"string\"; // text | date → string\n return `${inner}[]`;\n }\n default: {\n // Exhaustiveness guard: if a new field type is added to the union and not\n // mapped here, this line becomes a compile error in the codegen build.\n const _exhaustive: never = t;\n return \"unknown\";\n }\n }\n}\n\n/**\n * Render the TS type for a zoned `array` field: an object with optional\n * `nonRepeatable` (a fixed block) and/or `repeatable` (a list of blocks). Recurses\n * through zone fields, so a zone field that is itself a zoned `array` nests naturally.\n */\nfunction arrayZoneType(field: ContentModelField, indent: string): string {\n const zones = field.config?.zones;\n const parts: string[] = [];\n if (zones?.nonRepeatable?.length) {\n const nested = fieldsToBody(zones.nonRepeatable, indent + \" \");\n parts.push(`${indent} readonly nonRepeatable?: {\\n${nested}\\n${indent} };`);\n }\n if (zones?.repeatable?.fields?.length) {\n const nested = fieldsToBody(zones.repeatable.fields, indent + \" \");\n parts.push(`${indent} readonly repeatable?: Array<{\\n${nested}\\n${indent} }>;`);\n }\n if (parts.length === 0) return \"Record<string, unknown>\"; // zoned array with no fields yet\n return `{\\n${parts.join(\"\\n\")}\\n${indent}}`;\n}\n\n/** Render the body of an object type from a field list, recursing into zones. */\nfunction fieldsToBody(fields: ContentModelField[], indent: string): string {\n const lines: string[] = [];\n for (const field of fields) {\n const optional = field.required ? \"\" : \"?\";\n let typeExpr: string;\n\n if (field.type === \"array\" && field.config?.zones) {\n typeExpr = arrayZoneType(field, indent);\n } else {\n typeExpr = scalarType(field);\n }\n\n const safeLabel = field.label ? escapeJsDoc(field.label) : \"\";\n if (safeLabel && safeLabel !== field.key) {\n lines.push(`${indent}/** ${safeLabel} */`);\n }\n lines.push(`${indent}readonly ${propName(field.key)}${optional}: ${typeExpr};`);\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Generate a complete `.ts` module from a set of content models.\n * Deterministic: models are sorted by slug; field order is preserved as authored.\n */\nexport function generateTypes(\n models: GeneratableModel[],\n opts: GenerateOptions = {},\n): string {\n const version = opts.version ?? \"0.1.0\";\n // Code-unit sort (NOT localeCompare): locale/ICU-independent so the generated\n // file is byte-identical on every machine — committed output diffs cleanly.\n const sorted = [...models].sort((a, b) =>\n a.slug < b.slug ? -1 : a.slug > b.slug ? 1 : 0,\n );\n\n const header = `// ⚠️ AUTO-GENERATED by @betttercms/codegen v${version} — DO NOT EDIT.\n// Regenerate with: npx @betttercms/codegen\n// Source of truth: your BetterCMS content models (the same schema the dashboard\n// builder and the MCP tools write). Re-run codegen after any schema change.\n${opts.bannerComment ? `// ${opts.bannerComment}\\n` : \"\"}`;\n\n const interfaces: string[] = [];\n const mapEntries: string[] = [];\n // Different slugs can PascalCase to the same base name (e.g. \"case-study\" and\n // \"case_study\" → \"CaseStudy\"). Emitting two identical interfaces would silently\n // declaration-merge into one wrong type, so disambiguate with a numeric suffix.\n const usedNames = new Set<string>();\n\n for (const model of sorted) {\n const base = `${pascalCase(model.slug)}Fields`;\n let typeName = base;\n for (let n = 2; usedNames.has(typeName); n++) typeName = `${base}_${n}`;\n usedNames.add(typeName);\n\n const name = model.name ? escapeJsDoc(model.name) : \"\";\n const desc = model.description ? escapeJsDoc(model.description) : \"\";\n const doc = name\n ? `/**\\n * ${name}${desc ? ` — ${desc}` : \"\"}\\n * Model slug: \\`${model.slug}\\`\\n */\\n`\n : \"\";\n const body = model.fields.length\n ? fieldsToBody(model.fields, \" \")\n : \" // (no fields defined yet)\";\n interfaces.push(`${doc}export interface ${typeName} {\\n${body}\\n}`);\n mapEntries.push(` readonly ${JSON.stringify(model.slug)}: ${typeName};`);\n }\n\n const schemaMap = `/**\n * Registry mapping each model slug to its typed fields. The Next adapter uses this to\n * type \\`getEntry(\"blog\", ...)\\` by slug — autocomplete and exhaustiveness for free.\n */\nexport interface BetterCMSSchema {\n${mapEntries.join(\"\\n\") || \" // (no models defined yet)\"}\n}\n\n/** Union of all model slugs. */\nexport type BetterCMSModelSlug = keyof BetterCMSSchema;`;\n\n return [header, PREAMBLE, interfaces.join(\"\\n\\n\"), schemaMap, \"\"].join(\"\\n\");\n}\n","/**\n * Fetches content models from the BetterCMS Management API so the CLI can generate\n * types against a live project. Kept dependency-free (plain fetch) so the generated\n * artifact and this fetcher can run anywhere — a GitHub Action, a postinstall, a script.\n */\n\nimport type { GeneratableModel } from \"./generate.js\";\n\nexport interface FetchModelsOptions {\n /** Management API base, e.g. \"https://api.bettercms.ai/api/v1\". */\n apiUrl: string;\n /** A management-scoped key (content:manage) or device-minted token. */\n apiKey: string;\n /** Optional fetch override (testing / custom runtime). */\n fetchImpl?: typeof fetch;\n}\n\ninterface ManagedModelRow {\n slug: string;\n name?: string;\n description?: string | null;\n fields: GeneratableModel[\"fields\"];\n}\n\n/**\n * GET /management/content/models — returns the project's models (the key is\n * project-scoped server-side, so this is exactly the schema for this site).\n */\nexport async function fetchModels(\n opts: FetchModelsOptions,\n): Promise<GeneratableModel[]> {\n const doFetch = opts.fetchImpl ?? globalThis.fetch;\n const base = opts.apiUrl.replace(/\\/+$/, \"\");\n const url = `${base}/management/content/models`;\n\n let res: Response;\n try {\n res = await doFetch(url, {\n // No Content-Type: this is a bodyless GET; the header is incorrect here and\n // strict edge runtimes/proxies may reject it.\n headers: { Authorization: `Bearer ${opts.apiKey}`, Accept: \"application/json\" },\n });\n } catch (err) {\n throw new Error(\n `Could not reach the BetterCMS Management API at ${url}: ${\n err instanceof Error ? err.message : String(err)\n }`,\n );\n }\n\n if (!res.ok) {\n const hint =\n res.status === 401 || res.status === 403\n ? \" — check your management API key (it must have the content:manage scope).\"\n : \"\";\n throw new Error(`Management API returned ${res.status} ${res.statusText}${hint}`);\n }\n\n const body = (await res.json()) as { data?: ManagedModelRow[] };\n const rows = body.data ?? [];\n return rows.map((r) => ({\n slug: r.slug,\n name: r.name,\n description: r.description ?? null,\n fields: r.fields ?? [],\n }));\n}\n"],"mappings":";AAgCA,IAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6CjB,SAAS,WAAW,MAAsB;AACxC,QAAM,QAAQ,KAAK,MAAM,SAAS,EAAE,OAAO,OAAO;AAClD,QAAM,SAAS,MACZ,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,IAAI,EAAE,MAAM,CAAC,CAAC,EACjD,KAAK,EAAE;AAEV,SAAO,SAAS,KAAK,MAAM,IAAI,QAAQ,MAAM,KAAK,UAAU;AAC9D;AASA,SAAS,YAAY,MAAsB;AACzC,SAAO,KAAK,QAAQ,SAAS,KAAK,EAAE,QAAQ,YAAY,GAAG,EAAE,KAAK;AACpE;AAEA,IAAM,cAAc;AAOpB,SAAS,SAAS,KAAqB;AACrC,SAAO,YAAY,KAAK,GAAG,IAAI,MAAM,KAAK,UAAU,GAAG;AACzD;AAGA,SAAS,WAAW,OAAkC;AACpD,QAAM,IAA2B,MAAM;AACvC,UAAQ,GAAG;AAAA,IACT,KAAK;AACH,aA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MAAM,IAAI,KAAK;AAC7B,QAAM,OAAO,KAAK,QAAQ,CAAC;AAC3B,SAAO,KAAK,IAAI,CAAC,OAAO;AAAA,IACtB,MAAM,EAAE;AAAA,IACR,MAAM,EAAE;AAAA,IACR,aAAa,EAAE,eAAe;AAAA,IAC9B,QAAQ,EAAE,UAAU,CAAC;AAAA,EACvB,EAAE;AACJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@betttercms/codegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Generate TypeScript types from your BetterCMS content schema — the single source of truth shared by the dashboard builder and the MCP tools.",
|
|
6
6
|
"bin": {
|
|
@@ -34,5 +34,9 @@
|
|
|
34
34
|
"@types/node": "^20",
|
|
35
35
|
"tsup": "^8.5.1",
|
|
36
36
|
"vitest": "^4.1.4"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"registry": "https://registry.npmjs.org/"
|
|
37
41
|
}
|
|
38
42
|
}
|