cms-renderer 0.3.1 → 0.3.2

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../lib/cms-api.ts"],"sourcesContent":["/**\n * CMS API Client\n *\n * Creates an HTTP-based tRPC client for calling the CMS API.\n * Used in Server Components to fetch routes and blocks from the CMS.\n */\n\nimport type { AppRouter } from '@repo/cms-schema/trpc';\nimport { type CreateTRPCClient, createTRPCClient, httpBatchLink } from '@trpc/client';\nimport superjson from 'superjson';\n\n/** Type alias for the CMS API client */\ntype CmsClient = CreateTRPCClient<AppRouter>;\n\n/**\n * Get the CMS API URL from the provided base URL.\n */\nfunction getCmsApiUrl(cmsUrl: string): string {\n return new URL('/api/trpc', cmsUrl).toString();\n}\n\n/** Unified configuration for all CMS API access */\nexport interface CmsConfig {\n /** CMS API base URL (e.g., 'http://localhost:3000') */\n cmsUrl: string;\n /** API key for authentication */\n apiKey?: string;\n /** Website ID to scope API requests */\n websiteId?: string;\n}\n\n/**\n * Create a custom fetch function that appends API key as query parameter.\n */\nfunction createFetchWithApiKey(apiKey?: string, websiteId?: string) {\n return async (url: URL | RequestInfo, options?: RequestInit): Promise<Response> => {\n let finalUrl = url;\n\n const urlObj = new URL(url.toString());\n if (apiKey) {\n urlObj.searchParams.set('api_key', apiKey);\n }\n if (websiteId) {\n urlObj.searchParams.set('website_id', websiteId);\n }\n if (apiKey || websiteId) {\n finalUrl = urlObj.toString();\n }\n\n\n\n const response = await fetch(finalUrl, options);\n\n return response;\n };\n}\n\n/**\n * Create a tRPC client for the CMS API.\n */\nfunction createCmsClient(options: CmsConfig): CmsClient {\n const url = getCmsApiUrl(options.cmsUrl);\n\n return createTRPCClient<AppRouter>({\n links: [\n httpBatchLink({\n url,\n transformer: superjson,\n fetch: createFetchWithApiKey(options.apiKey, options.websiteId),\n }),\n ],\n });\n}\n\n/**\n * Get a CMS client for the specified CMS URL.\n */\nexport function getCmsClient(options: CmsConfig): CmsClient {\n return createCmsClient(options);\n}\n"],"mappings":";AAQA,SAAgC,kBAAkB,qBAAqB;AACvE,OAAO,eAAe;AAQtB,SAAS,aAAa,QAAwB;AAC5C,SAAO,IAAI,IAAI,aAAa,MAAM,EAAE,SAAS;AAC/C;AAeA,SAAS,sBAAsB,QAAiB,WAAoB;AAClE,SAAO,OAAO,KAAwB,YAA6C;AACjF,QAAI,WAAW;AAEf,UAAM,SAAS,IAAI,IAAI,IAAI,SAAS,CAAC;AACrC,QAAI,QAAQ;AACV,aAAO,aAAa,IAAI,WAAW,MAAM;AAAA,IAC3C;AACA,QAAI,WAAW;AACb,aAAO,aAAa,IAAI,cAAc,SAAS;AAAA,IACjD;AACA,QAAI,UAAU,WAAW;AACvB,iBAAW,OAAO,SAAS;AAAA,IAC7B;AAIA,UAAM,WAAW,MAAM,MAAM,UAAU,OAAO;AAE9C,WAAO;AAAA,EACT;AACF;AAKA,SAAS,gBAAgB,SAA+B;AACtD,QAAM,MAAM,aAAa,QAAQ,MAAM;AAEvC,SAAO,iBAA4B;AAAA,IACjC,OAAO;AAAA,MACL,cAAc;AAAA,QACZ;AAAA,QACA,aAAa;AAAA,QACb,OAAO,sBAAsB,QAAQ,QAAQ,QAAQ,SAAS;AAAA,MAChE,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AACH;AAKO,SAAS,aAAa,SAA+B;AAC1D,SAAO,gBAAgB,OAAO;AAChC;","names":[]}
1
+ {"version":3,"sources":["../../lib/cms-api.ts"],"sourcesContent":["/**\n * CMS API Client\n *\n * Creates an HTTP-based tRPC client for calling the CMS API.\n * Used in Server Components to fetch routes and blocks from the CMS.\n */\n\nimport type { AppRouter } from '@repo/cms-schema/trpc';\nimport { type CreateTRPCClient, createTRPCClient, httpBatchLink } from '@trpc/client';\nimport superjson from 'superjson';\n\n/** Type alias for the CMS API client */\ntype CmsClient = CreateTRPCClient<AppRouter>;\n\n/**\n * Get the CMS API URL from the provided base URL.\n */\nfunction getCmsApiUrl(cmsUrl: string): string {\n return new URL('/api/trpc', cmsUrl).toString();\n}\n\n/** Unified configuration for all CMS API access */\nexport interface CmsConfig {\n /** CMS API base URL (e.g., 'http://localhost:3000') */\n cmsUrl: string;\n /** API key for authentication */\n apiKey?: string;\n /** Website ID to scope API requests */\n websiteId?: string;\n}\n\n/**\n * Create a custom fetch function that appends API key as query parameter.\n */\nfunction createFetchWithApiKey(apiKey?: string, websiteId?: string) {\n return async (url: URL | RequestInfo, options?: RequestInit): Promise<Response> => {\n let finalUrl = url;\n\n const urlObj = new URL(url.toString());\n if (apiKey) {\n urlObj.searchParams.set('api_key', apiKey);\n }\n if (websiteId) {\n urlObj.searchParams.set('website_id', websiteId);\n }\n if (apiKey || websiteId) {\n finalUrl = urlObj.toString();\n }\n\n const response = await fetch(finalUrl, options);\n\n return response;\n };\n}\n\n/**\n * Create a tRPC client for the CMS API.\n */\nfunction createCmsClient(options: CmsConfig): CmsClient {\n const url = getCmsApiUrl(options.cmsUrl);\n\n return createTRPCClient<AppRouter>({\n links: [\n httpBatchLink({\n url,\n transformer: superjson,\n fetch: createFetchWithApiKey(options.apiKey, options.websiteId),\n }),\n ],\n });\n}\n\n/**\n * Get a CMS client for the specified CMS URL.\n */\nexport function getCmsClient(options: CmsConfig): CmsClient {\n return createCmsClient(options);\n}\n"],"mappings":";AAQA,SAAgC,kBAAkB,qBAAqB;AACvE,OAAO,eAAe;AAQtB,SAAS,aAAa,QAAwB;AAC5C,SAAO,IAAI,IAAI,aAAa,MAAM,EAAE,SAAS;AAC/C;AAeA,SAAS,sBAAsB,QAAiB,WAAoB;AAClE,SAAO,OAAO,KAAwB,YAA6C;AACjF,QAAI,WAAW;AAEf,UAAM,SAAS,IAAI,IAAI,IAAI,SAAS,CAAC;AACrC,QAAI,QAAQ;AACV,aAAO,aAAa,IAAI,WAAW,MAAM;AAAA,IAC3C;AACA,QAAI,WAAW;AACb,aAAO,aAAa,IAAI,cAAc,SAAS;AAAA,IACjD;AACA,QAAI,UAAU,WAAW;AACvB,iBAAW,OAAO,SAAS;AAAA,IAC7B;AAEA,UAAM,WAAW,MAAM,MAAM,UAAU,OAAO;AAE9C,WAAO;AAAA,EACT;AACF;AAKA,SAAS,gBAAgB,SAA+B;AACtD,QAAM,MAAM,aAAa,QAAQ,MAAM;AAEvC,SAAO,iBAA4B;AAAA,IACjC,OAAO;AAAA,MACL,cAAc;AAAA,QACZ;AAAA,QACA,aAAa;AAAA,QACb,OAAO,sBAAsB,QAAQ,QAAQ,QAAQ,SAAS;AAAA,MAChE,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AACH;AAKO,SAAS,aAAa,SAA+B;AAC1D,SAAO,gBAAgB,OAAO;AAChC;","names":[]}
@@ -799,6 +799,171 @@ var require_safe_regex2 = __commonJS({
799
799
  import { mkdir, writeFile } from "fs/promises";
800
800
  import { dirname } from "path";
801
801
 
802
+ // ../../packages/cms-schema/src/documents/generate-zod-code.ts
803
+ function generateZodSchemaCode(schemaName, fields) {
804
+ const lines = [];
805
+ lines.push(`import { z } from 'zod';`);
806
+ lines.push("");
807
+ const hasImageField = fields.some((f) => f.type === "image");
808
+ if (hasImageField) {
809
+ appendImageReferenceSchema(lines);
810
+ lines.push("");
811
+ }
812
+ appendSchemaBody(lines, schemaName, fields);
813
+ return lines.join("\n");
814
+ }
815
+ function generateCombinedZodSchemaCode(schemas) {
816
+ const lines = [];
817
+ lines.push("/* eslint-disable */");
818
+ lines.push("/* prettier-ignore */");
819
+ lines.push("/* biome-ignore format: auto-generated */");
820
+ lines.push("// This file is auto-generated by cms-renderer. Do not edit manually.");
821
+ lines.push("");
822
+ lines.push(`import { z } from 'zod';`);
823
+ lines.push("");
824
+ const hasImageField = schemas.some((s) => s.fields.some((f) => f.type === "image"));
825
+ if (hasImageField) {
826
+ appendImageReferenceSchema(lines);
827
+ lines.push("");
828
+ }
829
+ for (const [i, schema] of schemas.entries()) {
830
+ if (i > 0) lines.push("");
831
+ appendSchemaBody(lines, schema.name, schema.fields);
832
+ }
833
+ return lines.join("\n");
834
+ }
835
+ function appendSchemaBody(lines, schemaName, fields) {
836
+ const varName = `${toCamelCase(schemaName)}Schema`;
837
+ lines.push(`export const ${varName} = z.object({`);
838
+ for (const field of fields) {
839
+ const fieldCode = generateFieldCode(field);
840
+ if (field.description) {
841
+ lines.push(` /** ${field.description} */`);
842
+ }
843
+ lines.push(` ${field.name}: ${fieldCode},`);
844
+ }
845
+ lines.push("});");
846
+ lines.push("");
847
+ lines.push(`export type ${toPascalCase(schemaName)} = z.infer<typeof ${varName}>;`);
848
+ lines.push("");
849
+ }
850
+ function appendImageReferenceSchema(lines) {
851
+ lines.push("const ImageReferenceSchema = z.object({");
852
+ lines.push(" alt: z.string().min(1, 'Alt text is required for accessibility'),");
853
+ lines.push(" caption: z.string().max(500).optional(),");
854
+ lines.push(" attribution: z.string().max(255).optional(),");
855
+ lines.push(" _asset: z.object({");
856
+ lines.push(" id: z.string().uuid(),");
857
+ lines.push(" }),");
858
+ lines.push("});");
859
+ }
860
+ function generateFieldCode(field) {
861
+ let code = generateBaseTypeCode(field.type, field.constraints);
862
+ if (!field.required) {
863
+ code += ".optional()";
864
+ }
865
+ return code;
866
+ }
867
+ function generateBaseTypeCode(type, constraints) {
868
+ switch (type) {
869
+ case "string":
870
+ return applyStringConstraintCode("z.string()", constraints);
871
+ case "number":
872
+ return applyNumberConstraintCode("z.number()", constraints);
873
+ case "boolean":
874
+ return "z.boolean()";
875
+ case "image":
876
+ return "ImageReferenceSchema";
877
+ case "date":
878
+ return "z.string().date('Invalid date format. Expected YYYY-MM-DD')";
879
+ case "datetime":
880
+ return "z.string().datetime({ offset: true, message: 'Invalid datetime format. Expected ISO 8601' })";
881
+ case "url":
882
+ return applyStringConstraintCode("z.string().url('Invalid URL format')", constraints);
883
+ case "email":
884
+ return applyStringConstraintCode("z.string().email('Invalid email format')", constraints);
885
+ case "enum": {
886
+ const values = constraints?.enumValues;
887
+ if (!values || values.length === 0) {
888
+ return "z.enum([''])";
889
+ }
890
+ const formatted = values.map((v) => `'${escapeString(v)}'`).join(", ");
891
+ return `z.enum([${formatted}])`;
892
+ }
893
+ case "reference":
894
+ return "z.record(z.string(), z.unknown())";
895
+ case "array": {
896
+ const itemType = constraints?.arrayItemType ?? "string";
897
+ const itemCode = generatePrimitiveCode(itemType);
898
+ return applyArrayConstraintCode(`z.array(${itemCode})`, constraints);
899
+ }
900
+ default:
901
+ return "z.unknown()";
902
+ }
903
+ }
904
+ function generatePrimitiveCode(type) {
905
+ switch (type) {
906
+ case "string":
907
+ return "z.string()";
908
+ case "number":
909
+ return "z.number()";
910
+ case "boolean":
911
+ return "z.boolean()";
912
+ }
913
+ }
914
+ function applyStringConstraintCode(base, constraints) {
915
+ if (!constraints) return base;
916
+ let code = base;
917
+ if (constraints.minLength !== void 0) {
918
+ code += `.min(${constraints.minLength}, 'Must be at least ${constraints.minLength} characters')`;
919
+ }
920
+ if (constraints.maxLength !== void 0) {
921
+ code += `.max(${constraints.maxLength}, 'Must be at most ${constraints.maxLength} characters')`;
922
+ }
923
+ if (constraints.pattern) {
924
+ code += `.regex(/${escapeRegex(constraints.pattern)}/, 'Invalid format')`;
925
+ }
926
+ return code;
927
+ }
928
+ function applyNumberConstraintCode(base, constraints) {
929
+ if (!constraints) return base;
930
+ let code = base;
931
+ if (constraints.integer) {
932
+ code += `.int('Must be an integer')`;
933
+ }
934
+ if (constraints.min !== void 0) {
935
+ code += `.min(${constraints.min}, 'Must be at least ${constraints.min}')`;
936
+ }
937
+ if (constraints.max !== void 0) {
938
+ code += `.max(${constraints.max}, 'Must be at most ${constraints.max}')`;
939
+ }
940
+ return code;
941
+ }
942
+ function applyArrayConstraintCode(base, constraints) {
943
+ if (!constraints) return base;
944
+ let code = base;
945
+ if (constraints.minItems !== void 0) {
946
+ code += `.min(${constraints.minItems}, 'Must have at least ${constraints.minItems} items')`;
947
+ }
948
+ if (constraints.maxItems !== void 0) {
949
+ code += `.max(${constraints.maxItems}, 'Must have at most ${constraints.maxItems} items')`;
950
+ }
951
+ return code;
952
+ }
953
+ function escapeString(str) {
954
+ return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
955
+ }
956
+ function escapeRegex(pattern) {
957
+ return pattern.replace(/\//g, "\\/");
958
+ }
959
+ function toCamelCase(str) {
960
+ return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
961
+ }
962
+ function toPascalCase(str) {
963
+ const camel = toCamelCase(str);
964
+ return camel.charAt(0).toUpperCase() + camel.slice(1);
965
+ }
966
+
802
967
  // ../../packages/cms-schema/src/documents/registry.ts
803
968
  import { z } from "zod";
804
969
  var schemaNameValidator = z.string().min(1, "Schema name must be a non-empty string").refine((name) => name.trim().length > 0, "Schema name must not be only whitespace");
@@ -1114,167 +1279,6 @@ import hash from "object-hash";
1114
1279
  // ../../packages/cms-schema/src/documents/validations/country.ts
1115
1280
  import { TRPCError } from "@trpc/server";
1116
1281
 
1117
- // ../../packages/cms-schema/src/documents/generate-zod-code.ts
1118
- function generateZodSchemaCode(schemaName, fields) {
1119
- const lines = [];
1120
- lines.push(`import { z } from 'zod';`);
1121
- lines.push("");
1122
- const hasImageField = fields.some((f) => f.type === "image");
1123
- if (hasImageField) {
1124
- appendImageReferenceSchema(lines);
1125
- lines.push("");
1126
- }
1127
- appendSchemaBody(lines, schemaName, fields);
1128
- return lines.join("\n");
1129
- }
1130
- function generateCombinedZodSchemaCode(schemas) {
1131
- const lines = [];
1132
- lines.push(`import { z } from 'zod';`);
1133
- lines.push("");
1134
- const hasImageField = schemas.some((s) => s.fields.some((f) => f.type === "image"));
1135
- if (hasImageField) {
1136
- appendImageReferenceSchema(lines);
1137
- lines.push("");
1138
- }
1139
- for (let i = 0; i < schemas.length; i++) {
1140
- const schema = schemas[i];
1141
- if (i > 0) lines.push("");
1142
- appendSchemaBody(lines, schema.name, schema.fields);
1143
- }
1144
- return lines.join("\n");
1145
- }
1146
- function appendSchemaBody(lines, schemaName, fields) {
1147
- const varName = toCamelCase(schemaName) + "Schema";
1148
- lines.push(`export const ${varName} = z.object({`);
1149
- for (const field of fields) {
1150
- const fieldCode = generateFieldCode(field);
1151
- if (field.description) {
1152
- lines.push(` /** ${field.description} */`);
1153
- }
1154
- lines.push(` ${field.name}: ${fieldCode},`);
1155
- }
1156
- lines.push("});");
1157
- lines.push("");
1158
- lines.push(`export type ${toPascalCase(schemaName)} = z.infer<typeof ${varName}>;`);
1159
- lines.push("");
1160
- }
1161
- function appendImageReferenceSchema(lines) {
1162
- lines.push("const ImageReferenceSchema = z.object({");
1163
- lines.push(" alt: z.string().min(1, 'Alt text is required for accessibility'),");
1164
- lines.push(" caption: z.string().max(500).optional(),");
1165
- lines.push(" attribution: z.string().max(255).optional(),");
1166
- lines.push(" _asset: z.object({");
1167
- lines.push(" id: z.string().uuid(),");
1168
- lines.push(" }),");
1169
- lines.push("});");
1170
- }
1171
- function generateFieldCode(field) {
1172
- let code = generateBaseTypeCode(field.type, field.constraints);
1173
- if (!field.required) {
1174
- code += ".optional()";
1175
- }
1176
- return code;
1177
- }
1178
- function generateBaseTypeCode(type, constraints) {
1179
- switch (type) {
1180
- case "string":
1181
- return applyStringConstraintCode("z.string()", constraints);
1182
- case "number":
1183
- return applyNumberConstraintCode("z.number()", constraints);
1184
- case "boolean":
1185
- return "z.boolean()";
1186
- case "image":
1187
- return "ImageReferenceSchema";
1188
- case "date":
1189
- return "z.string().date('Invalid date format. Expected YYYY-MM-DD')";
1190
- case "datetime":
1191
- return "z.string().datetime({ offset: true, message: 'Invalid datetime format. Expected ISO 8601' })";
1192
- case "url":
1193
- return applyStringConstraintCode("z.string().url('Invalid URL format')", constraints);
1194
- case "email":
1195
- return applyStringConstraintCode("z.string().email('Invalid email format')", constraints);
1196
- case "enum": {
1197
- const values = constraints?.enumValues;
1198
- if (!values || values.length === 0) {
1199
- return "z.enum([''])";
1200
- }
1201
- const formatted = values.map((v) => `'${escapeString(v)}'`).join(", ");
1202
- return `z.enum([${formatted}])`;
1203
- }
1204
- case "reference":
1205
- return "z.record(z.string(), z.unknown())";
1206
- case "array": {
1207
- const itemType = constraints?.arrayItemType ?? "string";
1208
- const itemCode = generatePrimitiveCode(itemType);
1209
- return applyArrayConstraintCode(`z.array(${itemCode})`, constraints);
1210
- }
1211
- default:
1212
- return "z.unknown()";
1213
- }
1214
- }
1215
- function generatePrimitiveCode(type) {
1216
- switch (type) {
1217
- case "string":
1218
- return "z.string()";
1219
- case "number":
1220
- return "z.number()";
1221
- case "boolean":
1222
- return "z.boolean()";
1223
- }
1224
- }
1225
- function applyStringConstraintCode(base, constraints) {
1226
- if (!constraints) return base;
1227
- let code = base;
1228
- if (constraints.minLength !== void 0) {
1229
- code += `.min(${constraints.minLength}, 'Must be at least ${constraints.minLength} characters')`;
1230
- }
1231
- if (constraints.maxLength !== void 0) {
1232
- code += `.max(${constraints.maxLength}, 'Must be at most ${constraints.maxLength} characters')`;
1233
- }
1234
- if (constraints.pattern) {
1235
- code += `.regex(/${escapeRegex(constraints.pattern)}/, 'Invalid format')`;
1236
- }
1237
- return code;
1238
- }
1239
- function applyNumberConstraintCode(base, constraints) {
1240
- if (!constraints) return base;
1241
- let code = base;
1242
- if (constraints.integer) {
1243
- code += `.int('Must be an integer')`;
1244
- }
1245
- if (constraints.min !== void 0) {
1246
- code += `.min(${constraints.min}, 'Must be at least ${constraints.min}')`;
1247
- }
1248
- if (constraints.max !== void 0) {
1249
- code += `.max(${constraints.max}, 'Must be at most ${constraints.max}')`;
1250
- }
1251
- return code;
1252
- }
1253
- function applyArrayConstraintCode(base, constraints) {
1254
- if (!constraints) return base;
1255
- let code = base;
1256
- if (constraints.minItems !== void 0) {
1257
- code += `.min(${constraints.minItems}, 'Must have at least ${constraints.minItems} items')`;
1258
- }
1259
- if (constraints.maxItems !== void 0) {
1260
- code += `.max(${constraints.maxItems}, 'Must have at most ${constraints.maxItems} items')`;
1261
- }
1262
- return code;
1263
- }
1264
- function escapeString(str) {
1265
- return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
1266
- }
1267
- function escapeRegex(pattern) {
1268
- return pattern.replace(/\//g, "\\/");
1269
- }
1270
- function toCamelCase(str) {
1271
- return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
1272
- }
1273
- function toPascalCase(str) {
1274
- const camel = toCamelCase(str);
1275
- return camel.charAt(0).toUpperCase() + camel.slice(1);
1276
- }
1277
-
1278
1282
  // lib/cms-api.ts
1279
1283
  import { createTRPCClient, httpBatchLink } from "@trpc/client";
1280
1284
  import superjson from "superjson";