@ghentcdh/crouton-core 0.0.1-alpha.49 → 0.0.1-alpha.51

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/index.cjs CHANGED
@@ -97,6 +97,7 @@ __export(index_exports, {
97
97
  buildConditionSchema: () => buildConditionSchema,
98
98
  buildFilter: () => buildFilter,
99
99
  buildFormUiSchema: () => buildFormUiSchema,
100
+ buildResourceJsonSchema: () => buildResourceJsonSchema,
100
101
  buildRule: () => buildRule,
101
102
  buildSort: () => buildSort,
102
103
  buildSortKey: () => buildSortKey,
@@ -106,6 +107,7 @@ __export(index_exports, {
106
107
  buildViewsFromColumnTypes: () => buildViewsFromColumnTypes,
107
108
  buildViewsFromColumns: () => buildViewsFromColumns,
108
109
  buildViewsWithSource: () => buildViewsWithSource,
110
+ clearResourceExtensions: () => clearResourceExtensions,
109
111
  colPosition: () => colPosition,
110
112
  columnForContext: () => columnForContext,
111
113
  columnKey: () => columnKey,
@@ -124,6 +126,8 @@ __export(index_exports, {
124
126
  findColumnDef: () => findColumnDef,
125
127
  findProperty: () => findProperty,
126
128
  fromValueLabel: () => fromValueLabel,
129
+ generateResourceJsonSchema: () => generateResourceJsonSchema,
130
+ getResourceExtensions: () => getResourceExtensions,
127
131
  injectCalculatedColumns: () => injectCalculatedColumns,
128
132
  injectCalculatedColumnsToView: () => injectCalculatedColumnsToView,
129
133
  isArrayColumn: () => isArrayColumn,
@@ -146,6 +150,8 @@ __export(index_exports, {
146
150
  patchFilterProperties: () => patchFilterProperties,
147
151
  pickSharedCellOptions: () => pickSharedCellOptions,
148
152
  refineByKind: () => refineByKind,
153
+ registerResourceExtension: () => registerResourceExtension,
154
+ registerResourceExtensions: () => registerResourceExtensions,
149
155
  resolveDefaultSort: () => resolveDefaultSort,
150
156
  resolveFormField: () => resolveFormField,
151
157
  resolveLanguage: () => resolveLanguage,
@@ -1364,6 +1370,44 @@ var transformAction = (action) => {
1364
1370
  };
1365
1371
  var JsonActionSchema = import_zod17.z.union([JsonProcedureActionSchema, JsonLinkActionSchema]).transform(transformAction);
1366
1372
 
1373
+ // src/lib/resource/extensions.ts
1374
+ var CORE_RESOURCE_KEYS = /* @__PURE__ */ new Set([
1375
+ "$schema",
1376
+ "schemaVersion",
1377
+ "draft",
1378
+ "kind",
1379
+ "id",
1380
+ "name",
1381
+ "route",
1382
+ "model",
1383
+ "tag",
1384
+ "title",
1385
+ "table",
1386
+ "idType",
1387
+ "database",
1388
+ "parent",
1389
+ "sidebar",
1390
+ "display",
1391
+ "security",
1392
+ "operations",
1393
+ "columns",
1394
+ "calculatedColumns",
1395
+ "actions",
1396
+ "tableActions",
1397
+ "modalSize",
1398
+ "include",
1399
+ "extensions"
1400
+ ]);
1401
+ var registry = /* @__PURE__ */ new Map();
1402
+ var registerResourceExtension = (name, schema) => {
1403
+ if (CORE_RESOURCE_KEYS.has(name))
1404
+ throw new Error(`Extension "${name}" shadows a core resource key.`);
1405
+ registry.set(name, schema);
1406
+ };
1407
+ var registerResourceExtensions = (map = {}) => Object.entries(map).forEach(([k, s]) => registerResourceExtension(k, s));
1408
+ var getResourceExtensions = () => registry;
1409
+ var clearResourceExtensions = () => registry.clear();
1410
+
1367
1411
  // src/lib/resource/include.schema.ts
1368
1412
  var import_zod18 = require("zod");
1369
1413
  var JsonIncludeEntrySchema = import_zod18.z.lazy(
@@ -1427,15 +1471,17 @@ var ResourceJsonShape = import_zod19.z.object({
1427
1471
  kind: ResourceKindSchema,
1428
1472
  name: import_zod19.z.string(),
1429
1473
  // required — unique id, used as the frontend form id
1430
- route: import_zod19.z.string(),
1431
- // required — URL segment for generated endpoints
1474
+ route: import_zod19.z.string().optional(),
1475
+ // URL segment for generated endpoints - default id is used
1476
+ id: import_zod19.z.string().optional(),
1477
+ // URL segment for generated endpoints - default id is used
1432
1478
  /**
1433
1479
  * Prisma model name. Required when `kind` is `prisma` (enforced by the
1434
1480
  * refinement on `ResourceJsonSchema`), and must be absent when `kind` is
1435
1481
  * `custom` — there is no Prisma delegate to address.
1436
1482
  */
1437
1483
  model: import_zod19.z.string().optional(),
1438
- tag: import_zod19.z.string(),
1484
+ tag: import_zod19.z.string().optional().default("Crouton"),
1439
1485
  // required — OpenAPI tag
1440
1486
  title: import_zod19.z.string().optional(),
1441
1487
  // no computed default — used as UI display title
@@ -1459,9 +1505,9 @@ var ResourceJsonShape = import_zod19.z.object({
1459
1505
  // default: { mode: 'modal', customComponent: null }
1460
1506
  /** Global security block — applies to every operation unless overridden per-operation. */
1461
1507
  security: SecuritySchema.optional(),
1462
- operations: JsonOperationsSchema,
1463
- // required key — but every sub-field defaults to enabled
1464
- columns: ColumnsSchema.default(ColumnsSchema.parse({})),
1508
+ operations: JsonOperationsSchema.optional(),
1509
+ // default applied in transform: custom → all false, prisma → all true
1510
+ columns: ColumnsSchema.optional().default(ColumnsSchema.parse({})),
1465
1511
  // id-keyed map; omit for a columnless resource
1466
1512
  calculatedColumns: import_zod19.z.array(CalculatedColumnSchema).default([]),
1467
1513
  actions: import_zod19.z.array(JsonActionSchema).default([]),
@@ -1477,7 +1523,12 @@ var ResourceJsonShape = import_zod19.z.object({
1477
1523
  * `{ "relation": "text_author", "include": ["author"] }` →
1478
1524
  * `include: { text_author: { include: { author: true } } }`
1479
1525
  */
1480
- include: import_zod19.z.array(JsonIncludeEntrySchema).default([])
1526
+ include: import_zod19.z.array(JsonIncludeEntrySchema).default([]),
1527
+ /**
1528
+ * Normalized extension blocks lifted from registered top-level keys.
1529
+ * Authors never write this key directly — the transform fills it.
1530
+ */
1531
+ extensions: import_zod19.z.record(import_zod19.z.string(), import_zod19.z.unknown()).optional()
1481
1532
  });
1482
1533
  var refineByKind = (obj, ctx) => {
1483
1534
  if (obj.kind === "custom") {
@@ -1528,18 +1579,70 @@ var refineByKind = (obj, ctx) => {
1528
1579
  });
1529
1580
  }
1530
1581
  };
1531
- var ResourceJsonSchema = ResourceJsonShape.superRefine(
1532
- refineByKind
1533
- ).transform((obj) => {
1534
- const title = obj.title ?? labelFromId(obj.name);
1535
- const schemaVersion = obj.schemaVersion ?? BASELINE_RESOURCE_VERSION;
1536
- return {
1537
- title,
1538
- ...obj,
1539
- schemaVersion,
1540
- columns: normalizeColumns(obj.columns)
1541
- };
1542
- });
1582
+ var buildResourceJsonSchema = () => {
1583
+ const ext = getResourceExtensions();
1584
+ const extShape = Object.fromEntries(
1585
+ [...ext].map(([name, schema]) => [name, schema.optional()])
1586
+ );
1587
+ const shape = ext.size ? ResourceJsonShape.extend(extShape) : ResourceJsonShape;
1588
+ return import_zod19.z.preprocess(
1589
+ (raw) => {
1590
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
1591
+ const obj = raw;
1592
+ if (obj["kind"] === void 0) {
1593
+ return {
1594
+ ...obj,
1595
+ kind: obj["model"] !== void 0 ? "prisma" : "custom"
1596
+ };
1597
+ }
1598
+ }
1599
+ return raw;
1600
+ },
1601
+ shape.superRefine(refineByKind).transform((obj) => {
1602
+ const title = obj.title ?? labelFromId(obj.name);
1603
+ const schemaVersion = obj.schemaVersion ?? BASELINE_RESOURCE_VERSION;
1604
+ const defaultOps = obj.kind === "custom" ? JsonOperationsSchema.parse({ findAll: false, findOne: false, create: false, update: false, patch: false, delete: false }) : JsonOperationsSchema.parse({});
1605
+ const raw = obj;
1606
+ const extensions = {};
1607
+ for (const name of ext.keys()) {
1608
+ if (raw[name] !== void 0) {
1609
+ extensions[name] = raw[name];
1610
+ delete raw[name];
1611
+ }
1612
+ }
1613
+ return {
1614
+ title,
1615
+ ...obj,
1616
+ route: obj.route ?? obj.id ?? obj.name ?? "",
1617
+ schemaVersion,
1618
+ columns: normalizeColumns(obj.columns),
1619
+ operations: obj.operations ?? defaultOps,
1620
+ ...Object.keys(extensions).length && { extensions }
1621
+ };
1622
+ })
1623
+ );
1624
+ };
1625
+ var ResourceJsonSchema = buildResourceJsonSchema();
1626
+ var generateResourceJsonSchema = () => {
1627
+ const ext = getResourceExtensions();
1628
+ const extShape = Object.fromEntries(
1629
+ [...ext].map(([name, schema]) => [name, schema.optional()])
1630
+ );
1631
+ const shape = ext.size ? ResourceJsonShape.extend(extShape) : ResourceJsonShape;
1632
+ try {
1633
+ return import_zod19.z.toJSONSchema(shape, {
1634
+ target: "draft-7",
1635
+ io: "input",
1636
+ unrepresentable: "any"
1637
+ });
1638
+ } catch {
1639
+ return import_zod19.z.toJSONSchema(ResourceJsonShape, {
1640
+ target: "draft-7",
1641
+ io: "input",
1642
+ unrepresentable: "any"
1643
+ });
1644
+ }
1645
+ };
1543
1646
 
1544
1647
  // src/lib/resource/migrations/types.ts
1545
1648
  var MigrationPathError = class extends Error {
@@ -2524,6 +2627,7 @@ var resolveLanguage = (acceptLanguage, supported, defaultLanguage) => {
2524
2627
  buildConditionSchema,
2525
2628
  buildFilter,
2526
2629
  buildFormUiSchema,
2630
+ buildResourceJsonSchema,
2527
2631
  buildRule,
2528
2632
  buildSort,
2529
2633
  buildSortKey,
@@ -2533,6 +2637,7 @@ var resolveLanguage = (acceptLanguage, supported, defaultLanguage) => {
2533
2637
  buildViewsFromColumnTypes,
2534
2638
  buildViewsFromColumns,
2535
2639
  buildViewsWithSource,
2640
+ clearResourceExtensions,
2536
2641
  colPosition,
2537
2642
  columnForContext,
2538
2643
  columnKey,
@@ -2551,6 +2656,8 @@ var resolveLanguage = (acceptLanguage, supported, defaultLanguage) => {
2551
2656
  findColumnDef,
2552
2657
  findProperty,
2553
2658
  fromValueLabel,
2659
+ generateResourceJsonSchema,
2660
+ getResourceExtensions,
2554
2661
  injectCalculatedColumns,
2555
2662
  injectCalculatedColumnsToView,
2556
2663
  isArrayColumn,
@@ -2573,6 +2680,8 @@ var resolveLanguage = (acceptLanguage, supported, defaultLanguage) => {
2573
2680
  patchFilterProperties,
2574
2681
  pickSharedCellOptions,
2575
2682
  refineByKind,
2683
+ registerResourceExtension,
2684
+ registerResourceExtensions,
2576
2685
  resolveDefaultSort,
2577
2686
  resolveFormField,
2578
2687
  resolveLanguage,