@thebes/cadmus 0.2.1 → 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/cms/index.cjs +458 -3
- package/dist/cms/index.cjs.map +1 -1
- package/dist/cms/index.d.cts +2 -2
- package/dist/cms/index.d.ts +2 -2
- package/dist/cms/index.js +451 -5
- package/dist/cms/index.js.map +1 -1
- package/dist/email/index.cjs +1 -1
- package/dist/email/index.js +1 -1
- package/dist/{errors-CW6Lz0AQ.cjs → errors-BhoibM6Z.cjs} +24 -1
- package/dist/{errors-CW6Lz0AQ.cjs.map → errors-BhoibM6Z.cjs.map} +1 -1
- package/dist/{errors-mZIqZJO4.js → errors-C8SqkFjl.js} +19 -2
- package/dist/{errors-mZIqZJO4.js.map → errors-C8SqkFjl.js.map} +1 -1
- package/dist/hono/index.cjs +6 -1
- package/dist/hono/index.cjs.map +1 -1
- package/dist/hono/index.d.cts +1 -1
- package/dist/hono/index.d.cts.map +1 -1
- package/dist/hono/index.d.ts +1 -1
- package/dist/hono/index.d.ts.map +1 -1
- package/dist/hono/index.js +6 -1
- package/dist/hono/index.js.map +1 -1
- package/dist/{index-BUrCSGVb.d.ts → index-BRZrCTsN.d.cts} +641 -145
- package/dist/index-BRZrCTsN.d.cts.map +1 -0
- package/dist/{index-BUrCSGVb.d.cts → index-BRZrCTsN.d.ts} +641 -145
- package/dist/index-BRZrCTsN.d.ts.map +1 -0
- package/dist/index.cjs +11 -1
- package/dist/index.d.cts +2 -88
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +2 -88
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/queues/index.cjs +1 -1
- package/dist/queues/index.js +1 -1
- package/dist/rate-limit/index.cjs +1 -1
- package/dist/rate-limit/index.js +1 -1
- package/dist/session/index.cjs +1 -1
- package/dist/session/index.js +1 -1
- package/dist/storage/index.cjs +1 -1
- package/dist/storage/index.js +1 -1
- package/package.json +8 -8
- package/dist/index-BUrCSGVb.d.cts.map +0 -1
- package/dist/index-BUrCSGVb.d.ts.map +0 -1
package/dist/cms/index.cjs
CHANGED
|
@@ -1,8 +1,65 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_errors = require("../errors-
|
|
2
|
+
const require_errors = require("../errors-BhoibM6Z.cjs");
|
|
3
3
|
const require_queues_index = require("../queues/index.cjs");
|
|
4
4
|
let drizzle_orm_sqlite_core = require("drizzle-orm/sqlite-core");
|
|
5
5
|
let drizzle_orm = require("drizzle-orm");
|
|
6
|
+
//#region src/cms/blocks.ts
|
|
7
|
+
/**
|
|
8
|
+
* Create a block renderer registry. Seed it with an initial `type → renderer`
|
|
9
|
+
* map and/or an `options.fallback` for unknown types.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const registry = createBlockRegistry<StringBlockRenderer>({
|
|
13
|
+
* divider: () => "<hr>",
|
|
14
|
+
* });
|
|
15
|
+
* registry.register("hero", (b) => `<h1>${b.heading}</h1>`);
|
|
16
|
+
* renderBlocksToString(blocks, registry);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function createBlockRegistry(initial = {}, options = {}) {
|
|
20
|
+
const renderers = new Map(Object.entries(initial));
|
|
21
|
+
let fallback = options.fallback;
|
|
22
|
+
const registry = {
|
|
23
|
+
register(type, renderer) {
|
|
24
|
+
renderers.set(type, renderer);
|
|
25
|
+
return registry;
|
|
26
|
+
},
|
|
27
|
+
registerMany(map) {
|
|
28
|
+
for (const [type, renderer] of Object.entries(map)) renderers.set(type, renderer);
|
|
29
|
+
return registry;
|
|
30
|
+
},
|
|
31
|
+
get(type) {
|
|
32
|
+
return renderers.get(type);
|
|
33
|
+
},
|
|
34
|
+
has(type) {
|
|
35
|
+
return renderers.has(type);
|
|
36
|
+
},
|
|
37
|
+
types() {
|
|
38
|
+
return [...renderers.keys()];
|
|
39
|
+
},
|
|
40
|
+
setFallback(renderer) {
|
|
41
|
+
fallback = renderer;
|
|
42
|
+
return registry;
|
|
43
|
+
},
|
|
44
|
+
resolve(type) {
|
|
45
|
+
return renderers.get(type) ?? fallback;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return registry;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Render an array of blocks to a single HTML string via a registry of
|
|
52
|
+
* {@link StringBlockRenderer}s. Blocks whose type resolves to no renderer
|
|
53
|
+
* (and no fallback) contribute the empty string — the same forgiving
|
|
54
|
+
* behavior the old hand-rolled `switch` had for unknown types.
|
|
55
|
+
*/
|
|
56
|
+
function renderBlocksToString(blocks, registry) {
|
|
57
|
+
return blocks.map((block) => {
|
|
58
|
+
const renderer = registry.resolve(block.type);
|
|
59
|
+
return renderer ? renderer(block) : "";
|
|
60
|
+
}).join("");
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
6
63
|
//#region src/cms/types.ts
|
|
7
64
|
/**
|
|
8
65
|
* Expands every `group` field in `fields` into its flattened equivalents
|
|
@@ -250,6 +307,289 @@ function defineCmsConfig(config) {
|
|
|
250
307
|
return resolved;
|
|
251
308
|
}
|
|
252
309
|
//#endregion
|
|
310
|
+
//#region src/cms/validation.ts
|
|
311
|
+
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
312
|
+
const SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
313
|
+
/**
|
|
314
|
+
* Immutable, chainable rule builder — the value a field's `validation`
|
|
315
|
+
* function receives and returns. Build a `Rule` with the module-level
|
|
316
|
+
* {@link rule} factory, or accept the one passed to your `validation`
|
|
317
|
+
* callback.
|
|
318
|
+
*/
|
|
319
|
+
var Rule = class Rule {
|
|
320
|
+
checks;
|
|
321
|
+
constructor(checks = []) {
|
|
322
|
+
this.checks = checks;
|
|
323
|
+
}
|
|
324
|
+
add(check) {
|
|
325
|
+
return new Rule([...this.checks, check]);
|
|
326
|
+
}
|
|
327
|
+
/** Override the message of the most recently added check. */
|
|
328
|
+
error(message) {
|
|
329
|
+
return this.withLast({
|
|
330
|
+
message,
|
|
331
|
+
severity: "error"
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Demote the most recently added check to a warning (non-blocking),
|
|
336
|
+
* optionally with a message. Sanity's `Rule.warning()` analogue.
|
|
337
|
+
*/
|
|
338
|
+
warning(message) {
|
|
339
|
+
return this.withLast({
|
|
340
|
+
severity: "warning",
|
|
341
|
+
...message ? { message } : {}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
withLast(patch) {
|
|
345
|
+
if (this.checks.length === 0) return this;
|
|
346
|
+
const next = this.checks.slice();
|
|
347
|
+
next[next.length - 1] = {
|
|
348
|
+
...next[next.length - 1],
|
|
349
|
+
...patch
|
|
350
|
+
};
|
|
351
|
+
return new Rule(next);
|
|
352
|
+
}
|
|
353
|
+
required() {
|
|
354
|
+
return this.add({ kind: "required" });
|
|
355
|
+
}
|
|
356
|
+
/** Minimum string length / array length / numeric value. */
|
|
357
|
+
min(n) {
|
|
358
|
+
return this.add({
|
|
359
|
+
kind: "min",
|
|
360
|
+
n
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/** Maximum string length / array length / numeric value. */
|
|
364
|
+
max(n) {
|
|
365
|
+
return this.add({
|
|
366
|
+
kind: "max",
|
|
367
|
+
n
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
/** Exact string/array length. */
|
|
371
|
+
length(n) {
|
|
372
|
+
return this.add({
|
|
373
|
+
kind: "length",
|
|
374
|
+
n
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
regex(re, label = "match the required format") {
|
|
378
|
+
return this.add({
|
|
379
|
+
kind: "regex",
|
|
380
|
+
re,
|
|
381
|
+
label
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
email() {
|
|
385
|
+
return this.add({
|
|
386
|
+
kind: "regex",
|
|
387
|
+
re: EMAIL_RE,
|
|
388
|
+
label: "be a valid email"
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
/** Lowercase kebab-case slug format. Pair with `.unique()` for slugs. */
|
|
392
|
+
slug() {
|
|
393
|
+
return this.add({
|
|
394
|
+
kind: "regex",
|
|
395
|
+
re: SLUG_RE,
|
|
396
|
+
label: "be a lowercase, hyphen-separated slug"
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
integer() {
|
|
400
|
+
return this.add({ kind: "integer" });
|
|
401
|
+
}
|
|
402
|
+
positive() {
|
|
403
|
+
return this.add({ kind: "positive" });
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Value must be unique across the collection (DB-backed; skipped in a
|
|
407
|
+
* pure client-side pass). A first-class rule rather than the hand-rolled
|
|
408
|
+
* column `unique` flag, so the failure is a clear field message instead of
|
|
409
|
+
* a raw UNIQUE-constraint write error.
|
|
410
|
+
*/
|
|
411
|
+
unique() {
|
|
412
|
+
return this.add({ kind: "unique" });
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* For a `relationship` field: the referenced id must exist in the related
|
|
416
|
+
* collection (DB-backed; skipped client-side).
|
|
417
|
+
*/
|
|
418
|
+
reference() {
|
|
419
|
+
return this.add({ kind: "reference" });
|
|
420
|
+
}
|
|
421
|
+
custom(fn) {
|
|
422
|
+
return this.add({
|
|
423
|
+
kind: "custom",
|
|
424
|
+
fn
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
/** Internal: the accumulated checks, read by {@link validateDocument}. */
|
|
428
|
+
toChecks() {
|
|
429
|
+
return this.checks;
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
/** Fresh, empty rule — the root of a chain. */
|
|
433
|
+
function rule() {
|
|
434
|
+
return new Rule();
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Identity helper mirroring Sanity's `defineField` — returns the field
|
|
438
|
+
* config unchanged but gives editors autocomplete and a single, greppable
|
|
439
|
+
* call site for field definitions. Optional: a plain object literal is still
|
|
440
|
+
* a valid field.
|
|
441
|
+
*/
|
|
442
|
+
function defineField(field) {
|
|
443
|
+
return field;
|
|
444
|
+
}
|
|
445
|
+
function resolveChecks(field) {
|
|
446
|
+
if (!field.validation) return [];
|
|
447
|
+
const built = field.validation(new Rule());
|
|
448
|
+
return (Array.isArray(built) ? built : [built]).flatMap((r) => r.toChecks());
|
|
449
|
+
}
|
|
450
|
+
function isEmpty(value) {
|
|
451
|
+
return value === void 0 || value === null || typeof value === "string" && value.length === 0;
|
|
452
|
+
}
|
|
453
|
+
function sizeOf(value) {
|
|
454
|
+
if (typeof value === "string") return {
|
|
455
|
+
size: value.length,
|
|
456
|
+
unit: "character"
|
|
457
|
+
};
|
|
458
|
+
if (Array.isArray(value)) return {
|
|
459
|
+
size: value.length,
|
|
460
|
+
unit: "item"
|
|
461
|
+
};
|
|
462
|
+
if (typeof value === "number") return {
|
|
463
|
+
size: value,
|
|
464
|
+
unit: ""
|
|
465
|
+
};
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Evaluate every field's validation rules against `doc`, returning all
|
|
470
|
+
* violations (both errors and warnings). `doc` is the nested document; field
|
|
471
|
+
* values are read from its flattened form so group subfields validate too.
|
|
472
|
+
*/
|
|
473
|
+
async function validateDocument(config, doc, options) {
|
|
474
|
+
const flatFields = flattenFields(config.fields);
|
|
475
|
+
const flatDoc = flattenDocShallow(config, doc);
|
|
476
|
+
const violations = [];
|
|
477
|
+
for (const [path, field] of Object.entries(flatFields)) {
|
|
478
|
+
if (options.onlyFields && !options.onlyFields.has(path)) continue;
|
|
479
|
+
const checks = resolveChecks(field);
|
|
480
|
+
if (checks.length === 0) continue;
|
|
481
|
+
const value = flatDoc[path];
|
|
482
|
+
const ctx = {
|
|
483
|
+
document: doc,
|
|
484
|
+
path,
|
|
485
|
+
operation: options.operation,
|
|
486
|
+
...options.id !== void 0 ? { id: options.id } : {}
|
|
487
|
+
};
|
|
488
|
+
for (const check of checks) {
|
|
489
|
+
const violation = await evaluateCheck(check, value, field, ctx, options);
|
|
490
|
+
if (violation) violations.push(violation);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return violations;
|
|
494
|
+
}
|
|
495
|
+
function flattenDocShallow(config, doc) {
|
|
496
|
+
return Object.values(config.fields).some((f) => f.type === "group") ? flattenDoc(config.fields, doc) : doc;
|
|
497
|
+
}
|
|
498
|
+
async function evaluateCheck(check, value, field, ctx, options) {
|
|
499
|
+
const fail = (defaultMessage) => ({
|
|
500
|
+
path: ctx.path,
|
|
501
|
+
message: check.message ?? `${ctx.path} must ${defaultMessage}`,
|
|
502
|
+
severity: check.severity ?? "error"
|
|
503
|
+
});
|
|
504
|
+
switch (check.kind) {
|
|
505
|
+
case "required": return isEmpty(value) ? fail("not be empty") : null;
|
|
506
|
+
case "min": {
|
|
507
|
+
if (isEmpty(value)) return null;
|
|
508
|
+
const s = sizeOf(value);
|
|
509
|
+
if (s && s.size < check.n) return fail(s.unit ? `have at least ${check.n} ${s.unit}${check.n === 1 ? "" : "s"}` : `be at least ${check.n}`);
|
|
510
|
+
return null;
|
|
511
|
+
}
|
|
512
|
+
case "max": {
|
|
513
|
+
if (isEmpty(value)) return null;
|
|
514
|
+
const s = sizeOf(value);
|
|
515
|
+
if (s && s.size > check.n) return fail(s.unit ? `have at most ${check.n} ${s.unit}${check.n === 1 ? "" : "s"}` : `be at most ${check.n}`);
|
|
516
|
+
return null;
|
|
517
|
+
}
|
|
518
|
+
case "length": {
|
|
519
|
+
if (isEmpty(value)) return null;
|
|
520
|
+
const s = sizeOf(value);
|
|
521
|
+
if (s?.unit && s.size !== check.n) return fail(`be exactly ${check.n} ${s.unit}${check.n === 1 ? "" : "s"}`);
|
|
522
|
+
return null;
|
|
523
|
+
}
|
|
524
|
+
case "regex":
|
|
525
|
+
if (isEmpty(value)) return null;
|
|
526
|
+
if (typeof value !== "string" || !check.re.test(value)) return fail(check.label);
|
|
527
|
+
return null;
|
|
528
|
+
case "integer":
|
|
529
|
+
if (isEmpty(value)) return null;
|
|
530
|
+
return typeof value === "number" && Number.isInteger(value) ? null : fail("be an integer");
|
|
531
|
+
case "positive":
|
|
532
|
+
if (isEmpty(value)) return null;
|
|
533
|
+
return typeof value === "number" && value > 0 ? null : fail("be a positive number");
|
|
534
|
+
case "unique": return evaluateUnique(value, ctx, options, check);
|
|
535
|
+
case "reference": return evaluateReference(value, field, ctx, options, check);
|
|
536
|
+
case "custom": {
|
|
537
|
+
const result = await check.fn(value, ctx);
|
|
538
|
+
if (result === true || result === void 0) return null;
|
|
539
|
+
if (result === false) return fail("be valid");
|
|
540
|
+
if (typeof result === "string") return {
|
|
541
|
+
path: ctx.path,
|
|
542
|
+
message: result,
|
|
543
|
+
severity: check.severity ?? "error"
|
|
544
|
+
};
|
|
545
|
+
return {
|
|
546
|
+
path: ctx.path,
|
|
547
|
+
message: result.message,
|
|
548
|
+
severity: result.severity ?? check.severity ?? "error"
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
async function evaluateUnique(value, ctx, options, check) {
|
|
554
|
+
if (isEmpty(value) || !options.db || !options.table) return null;
|
|
555
|
+
const column = options.table[ctx.path];
|
|
556
|
+
if (!column) return null;
|
|
557
|
+
const where = ctx.id !== void 0 ? (0, drizzle_orm.and)((0, drizzle_orm.eq)(column, value), (0, drizzle_orm.ne)(options.table.id, ctx.id)) : (0, drizzle_orm.eq)(column, value);
|
|
558
|
+
if ((await options.db.select({ id: options.table.id }).from(options.table).where(where).limit(1)).length > 0) return {
|
|
559
|
+
path: ctx.path,
|
|
560
|
+
message: check.message ?? `${ctx.path} "${String(value)}" is already taken`,
|
|
561
|
+
severity: check.severity ?? "error"
|
|
562
|
+
};
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
async function evaluateReference(value, field, ctx, options, check) {
|
|
566
|
+
if (isEmpty(value) || !options.db || !options.registry) return null;
|
|
567
|
+
if (field.type !== "relationship") return null;
|
|
568
|
+
const target = options.registry.tables[field.relationTo];
|
|
569
|
+
if (!target) return null;
|
|
570
|
+
if ((await options.db.select({ id: target.id }).from(target).where((0, drizzle_orm.eq)(target.id, value)).limit(1)).length === 0) return {
|
|
571
|
+
path: ctx.path,
|
|
572
|
+
message: check.message ?? `${ctx.path} references a "${field.relationTo}" that does not exist`,
|
|
573
|
+
severity: check.severity ?? "error"
|
|
574
|
+
};
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Run {@link validateDocument} and throw {@link CadmusValidationError} if any
|
|
579
|
+
* `"error"`-severity violations are found. Warnings are returned (never
|
|
580
|
+
* thrown) so a caller can still surface them. The thrown error's message is
|
|
581
|
+
* a readable, joined summary of every blocking violation.
|
|
582
|
+
*/
|
|
583
|
+
async function assertValid(config, doc, options) {
|
|
584
|
+
const violations = await validateDocument(config, doc, options);
|
|
585
|
+
const errors = violations.filter((v) => v.severity === "error");
|
|
586
|
+
if (errors.length > 0) {
|
|
587
|
+
const summary = errors.map((v) => v.message).join("; ");
|
|
588
|
+
throw new require_errors.CadmusValidationError(`Validation failed for collection "${config.slug}": ${summary}`, violations);
|
|
589
|
+
}
|
|
590
|
+
return violations;
|
|
591
|
+
}
|
|
592
|
+
//#endregion
|
|
253
593
|
//#region src/cms/localApi.ts
|
|
254
594
|
function validateRequiredFields(config, input) {
|
|
255
595
|
for (const [key, field] of Object.entries(flattenFields(config.fields))) {
|
|
@@ -430,9 +770,16 @@ function createLocalApi(db, table, config, registry) {
|
|
|
430
770
|
},
|
|
431
771
|
async create(context, input) {
|
|
432
772
|
await checkAccess(config, "create", context);
|
|
433
|
-
const
|
|
773
|
+
const data = await runBeforeChange(config, input);
|
|
774
|
+
const flatData = toFlatDoc(data);
|
|
434
775
|
validateRequiredFields(config, flatData);
|
|
435
776
|
rejectUnknownFields(config, flatData);
|
|
777
|
+
await assertValid(config, data, {
|
|
778
|
+
operation: "create",
|
|
779
|
+
db,
|
|
780
|
+
table,
|
|
781
|
+
registry
|
|
782
|
+
});
|
|
436
783
|
let row;
|
|
437
784
|
try {
|
|
438
785
|
const [inserted] = await db.insert(table).values(flatData).returning();
|
|
@@ -447,8 +794,17 @@ function createLocalApi(db, table, config, registry) {
|
|
|
447
794
|
},
|
|
448
795
|
async update(context, id, input) {
|
|
449
796
|
await checkAccess(config, "update", context);
|
|
450
|
-
const
|
|
797
|
+
const data = await runBeforeChange(config, input);
|
|
798
|
+
const flatData = toFlatDoc(data);
|
|
451
799
|
rejectUnknownFields(config, flatData);
|
|
800
|
+
await assertValid(config, data, {
|
|
801
|
+
operation: "update",
|
|
802
|
+
id,
|
|
803
|
+
onlyFields: new Set(Object.keys(flatData)),
|
|
804
|
+
db,
|
|
805
|
+
table,
|
|
806
|
+
registry
|
|
807
|
+
});
|
|
452
808
|
let row;
|
|
453
809
|
try {
|
|
454
810
|
const [updated] = await db.update(table).set(flatData).where((0, drizzle_orm.eq)(idColumn, id)).returning();
|
|
@@ -511,6 +867,13 @@ function createVersionedLocalApi(db, table, versionsTable, config, registry) {
|
|
|
511
867
|
validateRequiredFields(config, data);
|
|
512
868
|
rejectUnknownFields(config, data);
|
|
513
869
|
const parentId = versionRecord.parentId;
|
|
870
|
+
await assertValid(config, data, {
|
|
871
|
+
operation: "update",
|
|
872
|
+
id: parentId,
|
|
873
|
+
db,
|
|
874
|
+
table,
|
|
875
|
+
registry
|
|
876
|
+
});
|
|
514
877
|
let doc;
|
|
515
878
|
try {
|
|
516
879
|
const [row] = await db.update(table).set({
|
|
@@ -660,6 +1023,89 @@ function generateSchemaSource(config) {
|
|
|
660
1023
|
].join("\n");
|
|
661
1024
|
}
|
|
662
1025
|
//#endregion
|
|
1026
|
+
//#region src/cms/structure.ts
|
|
1027
|
+
/**
|
|
1028
|
+
* Cadmea's Structure Builder — the framework half of issue #12.
|
|
1029
|
+
*
|
|
1030
|
+
* Adopts Sanity's `sanity/structure` idea (pattern, not code): **decouple
|
|
1031
|
+
* the admin nav from the raw collection list.** Instead of mapping every
|
|
1032
|
+
* `config.collections` entry to an `/admin/<slug>` link — which surfaces
|
|
1033
|
+
* system/log tables as editable links and produces dead links — the sidebar
|
|
1034
|
+
* renders from an explicit, grouped structure derived here from each
|
|
1035
|
+
* collection's `admin` hints (see {@link CollectionAdminConfig}) plus
|
|
1036
|
+
* optional per-slug overrides supplied at the call site.
|
|
1037
|
+
*
|
|
1038
|
+
* Pure data in / pure data out: no SolidJS, no DOM, no server imports — so
|
|
1039
|
+
* it's safe to import from a client studio component (e.g. the site's
|
|
1040
|
+
* `PanelNav`) and trivially testable.
|
|
1041
|
+
*/
|
|
1042
|
+
/** Default group heading for collections that don't declare `admin.group`. */
|
|
1043
|
+
const DEFAULT_STUDIO_GROUP = "Content";
|
|
1044
|
+
function capitalize(value) {
|
|
1045
|
+
return value.length === 0 ? value : value[0].toUpperCase() + value.slice(1);
|
|
1046
|
+
}
|
|
1047
|
+
function resolveAdmin(collection, overrides) {
|
|
1048
|
+
return {
|
|
1049
|
+
...collection.admin,
|
|
1050
|
+
...overrides?.[collection.slug]
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Build the studio sidebar structure from a resolved CMS config.
|
|
1055
|
+
*
|
|
1056
|
+
* - Hidden collections (`admin.hidden`) are dropped entirely.
|
|
1057
|
+
* - Each remaining collection is placed in its `admin.group` (or
|
|
1058
|
+
* {@link DEFAULT_STUDIO_GROUP}).
|
|
1059
|
+
* - Within a group, items sort by `admin.order` (ascending; unset sorts
|
|
1060
|
+
* after set), then by their original position in `config.collections` —
|
|
1061
|
+
* so config order is the stable tiebreaker.
|
|
1062
|
+
* - Groups render in `options.groupOrder` first, then first-appearance
|
|
1063
|
+
* order for the rest.
|
|
1064
|
+
*
|
|
1065
|
+
* The input is expected to be the *resolved* config (post-plugins), since
|
|
1066
|
+
* that's what carries plugin-injected collections like `products`.
|
|
1067
|
+
*/
|
|
1068
|
+
function buildStudioStructure(config, options = {}) {
|
|
1069
|
+
const basePath = options.basePath ?? "/admin";
|
|
1070
|
+
const groupOrder = options.groupOrder ?? [];
|
|
1071
|
+
const ranked = config.collections.map((collection, index) => ({
|
|
1072
|
+
collection,
|
|
1073
|
+
index,
|
|
1074
|
+
admin: resolveAdmin(collection, options.overrides)
|
|
1075
|
+
}));
|
|
1076
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1077
|
+
const appearance = [];
|
|
1078
|
+
for (const { collection, admin } of ranked) {
|
|
1079
|
+
if (admin.hidden) continue;
|
|
1080
|
+
const title = admin.group ?? "Content";
|
|
1081
|
+
if (!groups.has(title)) {
|
|
1082
|
+
groups.set(title, []);
|
|
1083
|
+
appearance.push(title);
|
|
1084
|
+
}
|
|
1085
|
+
groups.get(title).push({
|
|
1086
|
+
slug: collection.slug,
|
|
1087
|
+
label: admin.label ?? capitalize(collection.slug),
|
|
1088
|
+
href: `${basePath}/${collection.slug}`,
|
|
1089
|
+
readOnly: admin.readOnly ?? false,
|
|
1090
|
+
singleton: admin.singleton ?? false,
|
|
1091
|
+
...admin.icon ? { icon: admin.icon } : {}
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
const meta = new Map(ranked.map(({ collection, index, admin }) => [collection.slug, {
|
|
1095
|
+
order: admin.order ?? Number.POSITIVE_INFINITY,
|
|
1096
|
+
index
|
|
1097
|
+
}]));
|
|
1098
|
+
for (const items of groups.values()) items.sort((a, b) => {
|
|
1099
|
+
const ma = meta.get(a.slug);
|
|
1100
|
+
const mb = meta.get(b.slug);
|
|
1101
|
+
return ma.order - mb.order || ma.index - mb.index;
|
|
1102
|
+
});
|
|
1103
|
+
return [...groupOrder.filter((title) => groups.has(title)), ...appearance.filter((title) => !groupOrder.includes(title))].map((title) => ({
|
|
1104
|
+
title,
|
|
1105
|
+
items: groups.get(title)
|
|
1106
|
+
}));
|
|
1107
|
+
}
|
|
1108
|
+
//#endregion
|
|
663
1109
|
//#region src/cms/webhooks.ts
|
|
664
1110
|
/**
|
|
665
1111
|
* Builds an `afterChange` hook that enqueues a `WebhookMessage` for every
|
|
@@ -739,17 +1185,23 @@ async function deliverWebhookMessage(message) {
|
|
|
739
1185
|
if (!response.ok) throw new require_errors.CadmusQueueError(`Webhook delivery to "${message.url}" returned status ${response.status}`);
|
|
740
1186
|
}
|
|
741
1187
|
//#endregion
|
|
1188
|
+
exports.DEFAULT_STUDIO_GROUP = DEFAULT_STUDIO_GROUP;
|
|
1189
|
+
exports.Rule = Rule;
|
|
1190
|
+
exports.assertValid = assertValid;
|
|
1191
|
+
exports.buildStudioStructure = buildStudioStructure;
|
|
742
1192
|
exports.can = can;
|
|
743
1193
|
exports.cmsConfigToSchema = cmsConfigToSchema;
|
|
744
1194
|
exports.collectionSearchTableName = collectionSearchTableName;
|
|
745
1195
|
exports.collectionSearchTableSQL = collectionSearchTableSQL;
|
|
746
1196
|
exports.collectionToTable = collectionToTable;
|
|
747
1197
|
exports.collectionVersionsTable = collectionVersionsTable;
|
|
1198
|
+
exports.createBlockRegistry = createBlockRegistry;
|
|
748
1199
|
exports.createLocalApi = createLocalApi;
|
|
749
1200
|
exports.createVersionedLocalApi = createVersionedLocalApi;
|
|
750
1201
|
exports.createWebhookHook = createWebhookHook;
|
|
751
1202
|
exports.defineCmsConfig = defineCmsConfig;
|
|
752
1203
|
exports.defineCollection = defineCollection;
|
|
1204
|
+
exports.defineField = defineField;
|
|
753
1205
|
exports.deliverWebhookMessage = deliverWebhookMessage;
|
|
754
1206
|
exports.extractSearchText = extractSearchText;
|
|
755
1207
|
exports.flattenDoc = flattenDoc;
|
|
@@ -759,5 +1211,8 @@ exports.getCollectionsMeta = getCollectionsMeta;
|
|
|
759
1211
|
exports.getRegisteredApi = getRegisteredApi;
|
|
760
1212
|
exports.nestDoc = nestDoc;
|
|
761
1213
|
exports.relationshipJoinTables = relationshipJoinTables;
|
|
1214
|
+
exports.renderBlocksToString = renderBlocksToString;
|
|
1215
|
+
exports.rule = rule;
|
|
1216
|
+
exports.validateDocument = validateDocument;
|
|
762
1217
|
|
|
763
1218
|
//# sourceMappingURL=index.cjs.map
|