@pramen/cms 0.0.49 → 0.0.50

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { HandlerContext, Policy, FileRef, BootstrapFn, JsonValue } from "@pramen/server";
1
+ import type { HandlerContext, Policy, FileRef, BootstrapFn, JsonValue, SchemaDef } from "@pramen/server";
2
2
  import type { EnvBag } from "@pramen/server";
3
3
  /** A field in a block type's (or content type's) field schema. Recursive: a `repeater`
4
4
  * or `group` nests `fields`. Mirrors WollyCMS's FieldDefinition. */
@@ -72,11 +72,35 @@ export interface DefaultBlockDefinition {
72
72
  blockTypeSlug: string;
73
73
  fields?: FieldValues;
74
74
  }
75
- /** A rich-text value — a serialized editor document (or a plain string). */
76
- export type RichText = string | {
75
+ /**
76
+ * A rich-text document the editor's structured JSON (a ProseMirror/TipTap doc tree).
77
+ *
78
+ * **Not an HTML string.** HTML never enters storage: the write path validates this tree
79
+ * against a node/mark allow-list (`normalizeRichText`) and the render side maps node types
80
+ * to components, so there is no `set:html` / `dangerouslySetInnerHTML` anywhere in the
81
+ * chain and nothing to scrub. The one attribute that can still carry script is a `link`
82
+ * mark's `href`, so that one IS checked — see `isSafeHref`.
83
+ */
84
+ export interface RichTextDoc {
85
+ type: "doc";
86
+ content?: RichTextNode[];
87
+ }
88
+ /** One node in a {@link RichTextDoc}. A leaf carries `text` (plus optional inline `marks`);
89
+ * a container carries `content`. */
90
+ export interface RichTextNode {
77
91
  type: string;
78
- content?: unknown[];
79
- };
92
+ content?: RichTextNode[];
93
+ text?: string;
94
+ marks?: RichTextMark[];
95
+ attrs?: Record<string, JsonValue>;
96
+ }
97
+ /** An inline mark on a text node — bold, link, highlight, … */
98
+ export interface RichTextMark {
99
+ type: string;
100
+ attrs?: Record<string, JsonValue>;
101
+ }
102
+ /** The value of a `richtext` field. */
103
+ export type RichText = RichTextDoc;
80
104
  /** Map one FieldDefinition (as a const literal) to the TS type of its RENDERED value.
81
105
  * Media resolves to `ResolvedMedia` (the assemble-time shape a component receives). */
82
106
  export type FieldTsType<D extends FieldDefinition> = D["type"] extends "text" | "textarea" | "url" | "select" | "date" | "datetime" | "publish" | "slug" ? string : D["type"] extends "richtext" ? RichText : D["type"] extends "number" ? number : D["type"] extends "boolean" ? boolean : D["type"] extends "media" ? ResolvedMedia | null : D["type"] extends "group" ? InferBlockFields<NonNullable<D["fields"]>> : D["type"] extends "repeater" ? InferBlockFields<NonNullable<D["fields"]>>[] : unknown;
@@ -267,6 +291,11 @@ export declare const cmsSchema: {
267
291
  } & {
268
292
  readonly default: false;
269
293
  };
294
+ version: {
295
+ readonly type: "integer";
296
+ } & {
297
+ readonly default: 1;
298
+ };
270
299
  createdAt: {
271
300
  readonly type: "text";
272
301
  } & {
@@ -341,6 +370,11 @@ export declare const cmsSchema: {
341
370
  currentRevisionId: {
342
371
  readonly type: "uuid";
343
372
  };
373
+ deletedAt: {
374
+ readonly type: "text";
375
+ } & {
376
+ readonly index: true;
377
+ };
344
378
  metaTitle: {
345
379
  readonly type: "text";
346
380
  };
@@ -365,6 +399,11 @@ export declare const cmsSchema: {
365
399
  structuredData: {
366
400
  readonly type: "json";
367
401
  };
402
+ version: {
403
+ readonly type: "integer";
404
+ } & {
405
+ readonly default: 1;
406
+ };
368
407
  createdAt: {
369
408
  readonly type: "text";
370
409
  } & {
@@ -518,6 +557,47 @@ export declare const cmsSchema: {
518
557
  readonly defaultExpr: string;
519
558
  };
520
559
  }, Record<string, never>>;
560
+ cms_collection_revisions: import("@pramen/server").EntityDef<{
561
+ id: {
562
+ readonly type: "uuid";
563
+ } & {
564
+ readonly generated: true;
565
+ } & {
566
+ readonly primaryKey: true;
567
+ readonly notNull: true;
568
+ };
569
+ collection: {
570
+ readonly type: "text";
571
+ } & {
572
+ readonly notNull: true;
573
+ } & {
574
+ readonly index: true;
575
+ };
576
+ rowId: {
577
+ readonly type: "text";
578
+ } & {
579
+ readonly notNull: true;
580
+ } & {
581
+ readonly index: true;
582
+ };
583
+ revision: {
584
+ readonly type: "integer";
585
+ } & {
586
+ readonly notNull: true;
587
+ };
588
+ snapshot: {
589
+ readonly type: "json";
590
+ };
591
+ note: {
592
+ readonly type: "text";
593
+ };
594
+ actor: {
595
+ readonly type: "text";
596
+ };
597
+ createdAt: {
598
+ readonly type: "text";
599
+ };
600
+ }, Record<string, never>>;
521
601
  cms_media: import("@pramen/server").EntityDef<{
522
602
  id: {
523
603
  readonly type: "uuid";
@@ -533,6 +613,11 @@ export declare const cmsSchema: {
533
613
  alt: {
534
614
  readonly type: "text";
535
615
  };
616
+ deletedAt: {
617
+ readonly type: "text";
618
+ } & {
619
+ readonly index: true;
620
+ };
536
621
  createdAt: {
537
622
  readonly type: "text";
538
623
  } & {
@@ -545,12 +630,53 @@ export interface ValidateOpts {
545
630
  * writes (addBlock/updateBlock/createPage) pass `false` — a DRAFT block may be incomplete;
546
631
  * required is only mandatory when publishing. Type checks always run. */
547
632
  requireRequired?: boolean;
633
+ /** The row's CURRENTLY STORED field values. A legacy HTML-string `richtext` value is
634
+ * tolerated only when it is byte-identical to the stored one — i.e. the caller echoed
635
+ * back a pre-Portable-Text value it never authored (the editor autosaves the whole bag).
636
+ * Anything else is rejected.
637
+ *
638
+ * This must NOT be a plain boolean. The `xss` sanitizer is gone, and `normalizeFields`
639
+ * passes a tolerated string through untouched, so a blanket "allow strings" would let
640
+ * any caller store arbitrary unsanitized HTML — which every consumer still on the
641
+ * pre-migration `set:html` contract would then execute. */
642
+ legacyBaseline?: FieldValues;
548
643
  }
549
644
  export declare function validateFields(schema: FieldDefinition[] | undefined | null, values: unknown, path?: string, opts?: ValidateOpts): void;
550
- /** Deep-sanitize the richtext fields in a values object against a field schema (recursing
551
- * into group/repeater). Returns a sanitized copy; non-richtext fields pass through. */
552
- export declare function sanitizeFields(schema: FieldDefinition[] | undefined | null, values: FieldValues): FieldValues;
645
+ /** The node/mark vocabulary a `richtext` value may use. A node entry maps a node type to
646
+ * the attribute names kept on it; a mark entry does the same for a mark type. */
647
+ export interface RichTextSchema {
648
+ nodes: Record<string, readonly string[]>;
649
+ marks: Record<string, readonly string[]>;
650
+ /** Highest heading level accepted; anything above is CLAMPED to it, not dropped.
651
+ * Defaults to `MAX_HEADING_LEVEL` (3, the shipped editor's StarterKit config). Raise it
652
+ * if your editor is configured for more — this is the widening the docs promise. */
653
+ maxHeadingLevel?: number;
654
+ }
655
+ /** What the shipped editor can actually produce (TipTap StarterKit + Highlight + TaskList,
656
+ * as configured by @podoba/react's BlockEditor). Pass your own to `normalizeFields` if your
657
+ * editor adds extensions — a node type absent from the schema is dropped on write. */
658
+ /** Highest heading level the shipped editor is configured for (StarterKit levels [1,2,3]). */
659
+ export declare const MAX_HEADING_LEVEL = 3;
660
+ export declare const DEFAULT_RICH_TEXT_SCHEMA: RichTextSchema;
661
+ export { isSafeHref, normalizeHref } from "./href";
662
+ /** How deep a document may nest before the normalizer stops descending. Real editor output
663
+ * is a handful of levels (list > item > paragraph > text); a hand-crafted doc nested tens
664
+ * of thousands deep would otherwise blow the stack INSIDE the DO's storage.transaction().
665
+ * JSON.parse is iterative in V8, so such a payload reaches the normalizer intact. */
666
+ export declare const MAX_RICH_TEXT_DEPTH = 100;
667
+ /** Normalize a rich-text value to a document the renderers can trust. A value that is not
668
+ * a doc at all yields an empty doc — `validateFields` rejects those first, so in handler
669
+ * flow this only ever sees a doc; the fallback is for direct callers. */
670
+ export declare function normalizeRichText(value: unknown, schema?: RichTextSchema): RichTextDoc;
671
+ /** Flatten a rich-text document to plain text — for excerpts, meta descriptions, and search
672
+ * indexing, which want the words without the structure. */
673
+ export declare function richTextToPlainText(value: RichTextDoc | null | undefined): string;
674
+ /** Deep-normalize the richtext fields in a values object against a field schema (recursing
675
+ * into group/repeater). Returns a normalized copy; other field types pass through. */
676
+ export declare function normalizeFields(schema: FieldDefinition[] | undefined | null, values: FieldValues, richTextSchema?: RichTextSchema): FieldValues;
553
677
  export interface RenderedBlock {
678
+ /** The block's optimistic-concurrency token — pass back as `expectedVersion`. */
679
+ version: number;
554
680
  /** The placement id (cms_page_blocks) — stable per position; used for reorder/remove. */
555
681
  id: string;
556
682
  /** The underlying block instance id (cms_blocks) — used to edit the block's content. */
@@ -592,13 +718,18 @@ export interface AssembledPage {
592
718
  metaTitle: string | null;
593
719
  metaDescription: string | null;
594
720
  seo: PageSeo;
721
+ /** Optimistic-concurrency token — pass back as `expectedVersion` on a write. */
722
+ version: number;
595
723
  };
596
724
  regions: Record<string, RenderedBlock[]>;
725
+ /** True when this is a live draft assembled behind a preview grant, rather than the
726
+ * published snapshot — so a frontend can render a "you are viewing a draft" banner. */
727
+ isPreview?: boolean;
597
728
  }
598
729
  /** One authored field value inside a block / collection / page `fields` bag. Stored
599
730
  * as JSON; a `"media"` field is resolved from its stored id to a `ResolvedMedia` at
600
731
  * assemble time, and `group`/`repeater` fields nest further bags. */
601
- export type FieldValue = JsonValue | ResolvedMedia | FieldValues | FieldValue[];
732
+ export type FieldValue = JsonValue | ResolvedMedia | RichTextDoc | FieldValues | FieldValue[];
602
733
  /** A block / collection / page `fields` bag — field name -> authored value. */
603
734
  export interface FieldValues {
604
735
  [field: string]: FieldValue;
@@ -627,6 +758,33 @@ export declare function imageUrl(key: string, opts?: {
627
758
  quality?: number;
628
759
  format?: "auto" | "webp" | "avif";
629
760
  }): string;
761
+ /** What a preview link authorizes: one page, in one tenant, until `exp`. */
762
+ export interface PreviewToken {
763
+ /** tenant */ t: string;
764
+ /** page id — the grant is scoped to this ONE page, never "all drafts" */ p: string;
765
+ /** expiry (epoch seconds) */ exp: number;
766
+ }
767
+ /** Secret preference order. `PREVIEW_SECRET` lets an operator rotate preview links without
768
+ * invalidating every signed file url, but falling back keeps the common case zero-config. */
769
+ export declare const PREVIEW_SECRET_NAMES: readonly ["PREVIEW_SECRET", "FILES_SECRET", "AUTH_SECRET"];
770
+ /** Resolve the preview signing secret, or `undefined` when nothing usable is configured. */
771
+ export declare function previewSecret(env: EnvBag): string | undefined;
772
+ /** The viewer roles for a given handler config — `editorRoles ∪ reviewerRoles`, computed
773
+ * exactly as `createCmsHandlers` computes them.
774
+ *
775
+ * Exported so `cmsRoutes()` cannot drift from `createCmsHandlers()`: pass the SAME options
776
+ * object to both. Configuring the two independently was how the preview route ended up
777
+ * presenting an identity neither the handler gate nor the ACL accepted — and a partial
778
+ * customization still worked, so the failure appeared only for the app that had most
779
+ * carefully renamed its roles. */
780
+ export declare function viewerRolesOf(opts?: CmsHandlerOpts): string[];
781
+ /** The viewer roles for the DEFAULT handler config. */
782
+ export declare const DEFAULT_VIEWER_ROLES: string[];
783
+ /** Where a preview link is redeemed. Spread `cmsRoutes()` into `app.routes` to serve it. */
784
+ export declare const PREVIEW_PATH = "/cms/preview";
785
+ /** Default preview-link lifetime: 1 hour. Long enough to share and open, short enough that
786
+ * a link pasted into a public channel stops working the same afternoon. */
787
+ export declare const DEFAULT_PREVIEW_TTL_SECONDS = 3600;
630
788
  export interface CmsHandlerOpts {
631
789
  /** Roles permitted to call the editor mutations (also enforced by the ACL). Default
632
790
  * `["editor", "admin"]`. */
@@ -638,6 +796,12 @@ export interface CmsHandlerOpts {
638
796
  /** Roles permitted to approve/reject a page in review and publish (the editorial gate).
639
797
  * Default `["reviewer", "admin"]`. */
640
798
  reviewerRoles?: readonly string[];
799
+ /** Preview-link lifetime in seconds. Default 3600 (1 hour). */
800
+ previewTtlSeconds?: number;
801
+ /** The node/mark vocabulary accepted on write. Defaults to `DEFAULT_RICH_TEXT_SCHEMA`
802
+ * (what the shipped editor produces). Widen it if your editor adds TipTap extensions —
803
+ * a node type absent from the schema is DROPPED on write, not rejected. */
804
+ richTextSchema?: RichTextSchema;
641
805
  }
642
806
  /** Build the CMS handler map. Spread into your app's handlers. Editor mutations are
643
807
  * gated both by `auth` (fast 403 before the body) and by the row ACL (cmsPolicies). */
@@ -708,13 +872,30 @@ export declare function createCmsHandlers(opts?: CmsHandlerOpts): {
708
872
  id: string;
709
873
  alt: string | null;
710
874
  }, Record<string, unknown>>;
711
- /** Delete a media row AND its R2 blob. (Automatic orphan sweeping media no longer
712
- * referenced by any block is future work; refs live inside opaque block JSON.) */
875
+ /** Trash a media row. The R2 OBJECT IS KEPTdeleting the bytes here would make
876
+ * `restoreMedia` a lie, and a block still referencing the id would render a dead url
877
+ * with no way back. `purgeMedia` is what drops both — and `listTrash` is how you find
878
+ * the id again, since every ACL-scoped read hides it from here on.
879
+ *
880
+ * (Automatic orphan sweeping — media no longer referenced by any block — is still
881
+ * future work; refs live inside opaque block JSON.) */
713
882
  deleteMedia: import("@pramen/server").Handler<{
714
883
  id: string;
715
884
  }, {
716
885
  ok: boolean;
717
886
  }>;
887
+ restoreMedia: import("@pramen/server").Handler<{
888
+ id: string;
889
+ }, {
890
+ ok: true;
891
+ }>;
892
+ /** Permanently remove trashed media — the row AND the R2 object. Reviewer-gated and
893
+ * irreversible; the blob is gone. */
894
+ purgeMedia: import("@pramen/server").Handler<{
895
+ id: string;
896
+ }, {
897
+ ok: true;
898
+ }>;
718
899
  listContentTypes: import("@pramen/server").Handler<unknown, Record<string, unknown>[]>;
719
900
  getContentType: import("@pramen/server").Handler<{
720
901
  id: string;
@@ -740,13 +921,14 @@ export declare function createCmsHandlers(opts?: CmsHandlerOpts): {
740
921
  * Blocks are edited via addBlock/updateBlock; SEO via updatePageSeo; this covers the
741
922
  * page record itself, which was previously only settable at createPage. A slug/locale
742
923
  * change re-checks (slug, locale) uniqueness (excluding this page); `fields` is validated
743
- * + sanitized against the content type's fieldsSchema, exactly like createPage. */
924
+ * + normalized against the content type's fieldsSchema, exactly like createPage. */
744
925
  updatePage: import("@pramen/server").Handler<{
745
926
  pageId: string;
746
927
  title?: string;
747
928
  slug?: string;
748
929
  locale?: string;
749
930
  fields?: FieldValues;
931
+ expectedVersion?: number;
750
932
  }, {
751
933
  ok: boolean;
752
934
  page: Record<string, unknown>;
@@ -817,6 +999,7 @@ export declare function createCmsHandlers(opts?: CmsHandlerOpts): {
817
999
  blockId: string;
818
1000
  fields?: FieldValues;
819
1001
  title?: string;
1002
+ expectedVersion?: number;
820
1003
  }, Record<string, unknown> | undefined>;
821
1004
  /** Reorder a region: `order` is the page_block ids in their new order. It must cover
822
1005
  * EXACTLY the region's current placements (same set, no dups) — otherwise a partial or
@@ -900,10 +1083,61 @@ export declare function createCmsHandlers(opts?: CmsHandlerOpts): {
900
1083
  ok: boolean;
901
1084
  publishInMs: number;
902
1085
  }>;
1086
+ /** Mint a signed, self-expiring preview link for one page. Editor-gated to MINT —
1087
+ * anyone holding the resulting link can redeem it, which is the point. */
1088
+ signPagePreview: import("@pramen/server").Handler<{
1089
+ pageId: string;
1090
+ expiresIn?: number;
1091
+ }, {
1092
+ url: string;
1093
+ token: string;
1094
+ expiresAt: number;
1095
+ }>;
1096
+ /** Assemble a page's LIVE draft by id. Not the redemption endpoint — that is the public
1097
+ * `GET /cms/preview` route, which verifies the token and then calls this privileged.
1098
+ * Role-gated so it is not an anonymous back door on the /rpc surface. */
1099
+ getPagePreview: import("@pramen/server").Handler<{
1100
+ pageId: string;
1101
+ }, AssembledPage>;
903
1102
  /** Fetch an assembled page by slug (+ locale). Anonymous callers get the published
904
1103
  * snapshot (the ACL scopes `cms_pages` reads to `status = published`). Editors may pass
905
1104
  * `preview: true` to assemble the current DRAFT live from the tables. `locale` defaults
906
1105
  * to the configured default locale; a slug is unique per locale. */
1106
+ deletePage: import("@pramen/server").Handler<{
1107
+ pageId: string;
1108
+ }, {
1109
+ ok: true;
1110
+ deletedAt: string;
1111
+ }>;
1112
+ /** What is currently in the trash — pages AND media. Read with `ctx.db.exec` because
1113
+ * the ACL read scope hides exactly these rows: that is the scope doing its job, not a
1114
+ * hole to patch.
1115
+ *
1116
+ * Media has to be listed here or it becomes UNREACHABLE the moment it is trashed —
1117
+ * `listMedia`/`getMedia` are ACL-scoped, so neither `restoreMedia` nor `purgeMedia`
1118
+ * could ever be called with its id again, while `/media/<key>` kept serving the bytes
1119
+ * (that route streams from R2 with no DB lookup at all). */
1120
+ listTrash: import("@pramen/server").Handler<{
1121
+ limit?: number;
1122
+ }, {
1123
+ pages: Record<string, unknown>[];
1124
+ media: {
1125
+ file: unknown;
1126
+ }[];
1127
+ }>;
1128
+ restorePage: import("@pramen/server").Handler<{
1129
+ pageId: string;
1130
+ }, {
1131
+ ok: true;
1132
+ scheduleCleared: boolean;
1133
+ }>;
1134
+ /** Permanently remove a trashed page and everything hanging off it. Reviewer-gated:
1135
+ * this is the only irreversible operation in the CMS. */
1136
+ purgePage: import("@pramen/server").Handler<{
1137
+ pageId: string;
1138
+ }, {
1139
+ ok: true;
1140
+ }>;
907
1141
  getPage: import("@pramen/server").Handler<{
908
1142
  slug: string;
909
1143
  locale?: string;
@@ -978,13 +1212,30 @@ export declare const cmsHandlers: {
978
1212
  id: string;
979
1213
  alt: string | null;
980
1214
  }, Record<string, unknown>>;
981
- /** Delete a media row AND its R2 blob. (Automatic orphan sweeping media no longer
982
- * referenced by any block is future work; refs live inside opaque block JSON.) */
1215
+ /** Trash a media row. The R2 OBJECT IS KEPTdeleting the bytes here would make
1216
+ * `restoreMedia` a lie, and a block still referencing the id would render a dead url
1217
+ * with no way back. `purgeMedia` is what drops both — and `listTrash` is how you find
1218
+ * the id again, since every ACL-scoped read hides it from here on.
1219
+ *
1220
+ * (Automatic orphan sweeping — media no longer referenced by any block — is still
1221
+ * future work; refs live inside opaque block JSON.) */
983
1222
  deleteMedia: import("@pramen/server").Handler<{
984
1223
  id: string;
985
1224
  }, {
986
1225
  ok: boolean;
987
1226
  }>;
1227
+ restoreMedia: import("@pramen/server").Handler<{
1228
+ id: string;
1229
+ }, {
1230
+ ok: true;
1231
+ }>;
1232
+ /** Permanently remove trashed media — the row AND the R2 object. Reviewer-gated and
1233
+ * irreversible; the blob is gone. */
1234
+ purgeMedia: import("@pramen/server").Handler<{
1235
+ id: string;
1236
+ }, {
1237
+ ok: true;
1238
+ }>;
988
1239
  listContentTypes: import("@pramen/server").Handler<unknown, Record<string, unknown>[]>;
989
1240
  getContentType: import("@pramen/server").Handler<{
990
1241
  id: string;
@@ -1010,13 +1261,14 @@ export declare const cmsHandlers: {
1010
1261
  * Blocks are edited via addBlock/updateBlock; SEO via updatePageSeo; this covers the
1011
1262
  * page record itself, which was previously only settable at createPage. A slug/locale
1012
1263
  * change re-checks (slug, locale) uniqueness (excluding this page); `fields` is validated
1013
- * + sanitized against the content type's fieldsSchema, exactly like createPage. */
1264
+ * + normalized against the content type's fieldsSchema, exactly like createPage. */
1014
1265
  updatePage: import("@pramen/server").Handler<{
1015
1266
  pageId: string;
1016
1267
  title?: string;
1017
1268
  slug?: string;
1018
1269
  locale?: string;
1019
1270
  fields?: FieldValues;
1271
+ expectedVersion?: number;
1020
1272
  }, {
1021
1273
  ok: boolean;
1022
1274
  page: Record<string, unknown>;
@@ -1087,6 +1339,7 @@ export declare const cmsHandlers: {
1087
1339
  blockId: string;
1088
1340
  fields?: FieldValues;
1089
1341
  title?: string;
1342
+ expectedVersion?: number;
1090
1343
  }, Record<string, unknown> | undefined>;
1091
1344
  /** Reorder a region: `order` is the page_block ids in their new order. It must cover
1092
1345
  * EXACTLY the region's current placements (same set, no dups) — otherwise a partial or
@@ -1170,10 +1423,61 @@ export declare const cmsHandlers: {
1170
1423
  ok: boolean;
1171
1424
  publishInMs: number;
1172
1425
  }>;
1426
+ /** Mint a signed, self-expiring preview link for one page. Editor-gated to MINT —
1427
+ * anyone holding the resulting link can redeem it, which is the point. */
1428
+ signPagePreview: import("@pramen/server").Handler<{
1429
+ pageId: string;
1430
+ expiresIn?: number;
1431
+ }, {
1432
+ url: string;
1433
+ token: string;
1434
+ expiresAt: number;
1435
+ }>;
1436
+ /** Assemble a page's LIVE draft by id. Not the redemption endpoint — that is the public
1437
+ * `GET /cms/preview` route, which verifies the token and then calls this privileged.
1438
+ * Role-gated so it is not an anonymous back door on the /rpc surface. */
1439
+ getPagePreview: import("@pramen/server").Handler<{
1440
+ pageId: string;
1441
+ }, AssembledPage>;
1173
1442
  /** Fetch an assembled page by slug (+ locale). Anonymous callers get the published
1174
1443
  * snapshot (the ACL scopes `cms_pages` reads to `status = published`). Editors may pass
1175
1444
  * `preview: true` to assemble the current DRAFT live from the tables. `locale` defaults
1176
1445
  * to the configured default locale; a slug is unique per locale. */
1446
+ deletePage: import("@pramen/server").Handler<{
1447
+ pageId: string;
1448
+ }, {
1449
+ ok: true;
1450
+ deletedAt: string;
1451
+ }>;
1452
+ /** What is currently in the trash — pages AND media. Read with `ctx.db.exec` because
1453
+ * the ACL read scope hides exactly these rows: that is the scope doing its job, not a
1454
+ * hole to patch.
1455
+ *
1456
+ * Media has to be listed here or it becomes UNREACHABLE the moment it is trashed —
1457
+ * `listMedia`/`getMedia` are ACL-scoped, so neither `restoreMedia` nor `purgeMedia`
1458
+ * could ever be called with its id again, while `/media/<key>` kept serving the bytes
1459
+ * (that route streams from R2 with no DB lookup at all). */
1460
+ listTrash: import("@pramen/server").Handler<{
1461
+ limit?: number;
1462
+ }, {
1463
+ pages: Record<string, unknown>[];
1464
+ media: {
1465
+ file: unknown;
1466
+ }[];
1467
+ }>;
1468
+ restorePage: import("@pramen/server").Handler<{
1469
+ pageId: string;
1470
+ }, {
1471
+ ok: true;
1472
+ scheduleCleared: boolean;
1473
+ }>;
1474
+ /** Permanently remove a trashed page and everything hanging off it. Reviewer-gated:
1475
+ * this is the only irreversible operation in the CMS. */
1476
+ purgePage: import("@pramen/server").Handler<{
1477
+ pageId: string;
1478
+ }, {
1479
+ ok: true;
1480
+ }>;
1177
1481
  getPage: import("@pramen/server").Handler<{
1178
1482
  slug: string;
1179
1483
  locale?: string;
@@ -1228,6 +1532,11 @@ export interface CollectionDef {
1228
1532
  column: string;
1229
1533
  dir?: "asc" | "desc";
1230
1534
  };
1535
+ /** Workflow features this collection opts into — see {@link CollectionFeature}. Each is
1536
+ * backed by MANAGED COLUMNS on `entity` that the CMS writes and `fields` may not declare.
1537
+ * Validated against your schema at `createCollectionHandlers` time (which is why that call
1538
+ * needs `{ schema }` once this is set). Absent = a plain CRUD collection, as before. */
1539
+ readonly supports?: readonly CollectionFeature[];
1231
1540
  }
1232
1541
  /** Declare a collection. Spread the results into `createCollectionHandlers` +
1233
1542
  * `collectionPolicies`:
@@ -1260,18 +1569,87 @@ export interface CollectionMeta {
1260
1569
  column: string;
1261
1570
  dir?: "asc" | "desc";
1262
1571
  };
1572
+ /** Workflow features enabled — the editor uses this to decide which affordances to show
1573
+ * (a Publish button, a schedule picker, a revisions tab). Empty = plain CRUD. */
1574
+ supports: readonly CollectionFeature[];
1575
+ }
1576
+ /** A workflow feature a collection can opt into.
1577
+ *
1578
+ * - `drafts` — a managed `status` column (`draft` | `published`) plus `collectionPublish` /
1579
+ * `collectionUnpublish`. Pair with `collectionPublicPolicies` so anonymous reads see
1580
+ * published rows only.
1581
+ *
1582
+ * This gates VISIBILITY, not content. A collection is column-mapped — the public reads the
1583
+ * entity's own columns — so there is nowhere to stage an unpublished VERSION of a live
1584
+ * row: an edit (or a revision restore) on a published row is live immediately. That is the
1585
+ * one place collections do not reach page parity, where `getPage` serves a baked revision
1586
+ * snapshot. Unpublish first if an edit needs review.
1587
+ * - `scheduling` — managed `publishedAt` / `scheduledAt` / `unpublishAt`, `collectionSchedule`,
1588
+ * and the deferred tasks from `createCollectionTasks`. Needs `drafts`.
1589
+ * - `revisions` — a snapshot of the row's prior state on every write, in
1590
+ * `cms_collection_revisions`, with `collectionListRevisions` / `collectionRestoreRevision`.
1591
+ * - `preview` — signed, single-row preview links (`signCollectionPreview`), redeemed at
1592
+ * `COLLECTION_PREVIEW_PATH` by the route `cmsRoutes()` serves. Needs `drafts`. It shows
1593
+ * the row's CURRENT state to whoever holds the link, which for a DRAFT is the unpublished
1594
+ * content and for a published row is what the public already sees (see `drafts` above:
1595
+ * there is no separate staged version to show). */
1596
+ export type CollectionFeature = "drafts" | "scheduling" | "revisions" | "preview";
1597
+ export declare const COLLECTION_FEATURES: readonly CollectionFeature[];
1598
+ /** The columns each feature needs on the collection's entity. The app declares them (they
1599
+ * are its own entity); the CMS writes them and `fields` may not. */
1600
+ export declare const COLLECTION_FEATURE_COLUMNS: Readonly<Record<CollectionFeature, readonly string[]>>;
1601
+ /** The shared revision table for collections (see `cmsSchema`). */
1602
+ export declare const COLLECTION_REVISIONS_TABLE = "cms_collection_revisions";
1603
+ /** The two `status` values a `drafts` collection uses. */
1604
+ export declare const COLLECTION_DRAFT = "draft";
1605
+ export declare const COLLECTION_PUBLISHED = "published";
1606
+ /** Check a collection registry at BOOT: slugs and entities are unique, features are known
1607
+ * and have their prerequisites, every declared field maps to a column that can hold it, and
1608
+ * every managed column exists, has the shape the CMS writes, and is not also an editable
1609
+ * field.
1610
+ *
1611
+ * Called by `createCollectionHandlers`. The point is that a misconfiguration surfaces when
1612
+ * the Worker starts, naming the collection and the column — not as a 500 the first time an
1613
+ * editor presses Publish, months later, on the one collection nobody exercised.
1614
+ *
1615
+ * `schema` is REQUIRED. Every check here reads the target entity, so a registry validated
1616
+ * without one is not validated at all — and the failures it catches (a field name typo, a
1617
+ * richtext field over a TEXT column, a non-PK idField) are exactly as fatal on a collection
1618
+ * that declares no `supports` as on one that declares all four. */
1619
+ export declare function validateCollections(collections: readonly CollectionDef[], schema?: SchemaDef): void;
1620
+ /** A signed grant to preview ONE collection row. Mirrors {@link PreviewToken}. */
1621
+ export interface CollectionPreviewToken {
1622
+ /** tenant */ t: string;
1623
+ /** collection slug */ c: string;
1624
+ /** row id — the grant is scoped to this ONE row, never "all drafts" */ r: string;
1625
+ /** expiry (epoch seconds) */ exp: number;
1626
+ }
1627
+ /** Where a collection preview link is redeemed. Served by `cmsRoutes()`. */
1628
+ export declare const COLLECTION_PREVIEW_PATH = "/cms/preview/collection";
1629
+ /** Outbox task kinds behind `collectionSchedule`. Register the handlers with
1630
+ * `app.tasks = { ...cmsTasks, ...createCollectionTasks(collections) }`. */
1631
+ export declare const TASK_COLLECTION_PUBLISH = "cms:collection:publish";
1632
+ export declare const TASK_COLLECTION_UNPUBLISH = "cms:collection:unpublish";
1633
+ /** Options for `createCollectionHandlers`. */
1634
+ export interface CollectionHandlerOpts extends CmsHandlerOpts {
1635
+ /** Your app's schema (the object you pass to `defineSchema`). REQUIRED: the whole registry
1636
+ * is checked against it at boot by `validateCollections` — managed columns, declared
1637
+ * field ↔ column types, the idField/PK pairing, `orderBy`, the partition — so a
1638
+ * misconfiguration is a startup error naming the collection and the column rather than a
1639
+ * 500 (or a silent wrong answer) on the first call. */
1640
+ schema?: SchemaDef;
1263
1641
  }
1264
1642
  /** Build generic CRUD handlers over the registered collections. Spread into your app's
1265
1643
  * handlers alongside `cmsHandlers`:
1266
1644
  *
1267
- * const handlers = { ...cmsHandlers, ...createCollectionHandlers([lectures]) };
1645
+ * const handlers = { ...cmsHandlers, ...createCollectionHandlers([lectures], { schema }) };
1268
1646
  *
1269
1647
  * Exposes `listCollections` (editor discovery) + `collectionList` / `collectionGet` /
1270
1648
  * `collectionCreate` / `collectionUpdate` / `collectionDelete`, all gated by `editorRoles`
1271
1649
  * (a fast 403 before the body) AND the row ACL (they go through `ctx.db`, so
1272
1650
  * `collectionPolicies` scopes them too). The `collection` param is resolved through the
1273
1651
  * registry — an unknown slug is a 400, never a raw table reference. */
1274
- export declare function createCollectionHandlers(collections: readonly CollectionDef[], opts?: CmsHandlerOpts): {
1652
+ export declare function createCollectionHandlers(collections: readonly CollectionDef[], opts?: CollectionHandlerOpts): {
1275
1653
  /** The registered collections (defaults filled) — editor discovery. Editor-gated so
1276
1654
  * the collection schemas aren't exposed to anonymous callers. */
1277
1655
  listCollections: import("@pramen/server").Handler<unknown, CollectionMeta[]>;
@@ -1299,6 +1677,81 @@ export declare function createCollectionHandlers(collections: readonly Collectio
1299
1677
  }, {
1300
1678
  ok: true;
1301
1679
  }>;
1680
+ /** Move a row live. With `scheduling` this also stamps `publishedAt` (the column the
1681
+ * public read scope compares against `$now()`) and clears `scheduledAt` — which makes
1682
+ * any pending scheduled-publish task a no-op, since its intent token no longer matches.
1683
+ * A pending scheduled UNPUBLISH is deliberately left standing: publishing early does not
1684
+ * cancel a planned takedown. */
1685
+ collectionPublish: import("@pramen/server").Handler<{
1686
+ collection: string;
1687
+ id: string;
1688
+ }, Record<string, unknown>>;
1689
+ /** Take a row back to draft, clearing every schedule. Both tokens are cleared, so a
1690
+ * pending publish AND a pending unpublish both become no-ops — unpublishing is an
1691
+ * explicit "this is not live and nothing is queued to change that". */
1692
+ collectionUnpublish: import("@pramen/server").Handler<{
1693
+ collection: string;
1694
+ id: string;
1695
+ }, Record<string, unknown>>;
1696
+ /** Schedule a future publish, and optionally a later unpublish. Mirrors `schedulePage`,
1697
+ * including the INTENT TOKEN: the row stores the scheduled instants
1698
+ * (`scheduledAt`/`unpublishAt`, ISO), the enqueued task carries a copy, and the task
1699
+ * runs only if the two still match. A reschedule overwrites the token, a manual
1700
+ * publish/unpublish clears it, and a duplicate delivery finds it already cleared — so a
1701
+ * superseded or cancelled schedule is a silent no-op rather than a surprise publish.
1702
+ *
1703
+ * The tasks are enqueued in THIS mutation's transaction (the outbox is transactional),
1704
+ * so a rolled-back schedule never leaves a task behind. They only run if you wired
1705
+ * `createCollectionTasks` into `app.tasks`. */
1706
+ collectionSchedule: import("@pramen/server").Handler<{
1707
+ collection: string;
1708
+ id: string;
1709
+ publishAt: number;
1710
+ unpublishAt?: number | null;
1711
+ }, {
1712
+ unpublishAt?: string | null | undefined;
1713
+ ok: true;
1714
+ scheduledAt: string;
1715
+ }>;
1716
+ /** A row's revision history, newest first. */
1717
+ collectionListRevisions: import("@pramen/server").Handler<{
1718
+ collection: string;
1719
+ id: string;
1720
+ limit?: number;
1721
+ }, {
1722
+ snapshot: Record<string, unknown>;
1723
+ }[]>;
1724
+ /** Restore a row to one of its revisions. The CURRENT state is snapshotted first, so a
1725
+ * restore is itself undoable. */
1726
+ collectionRestoreRevision: import("@pramen/server").Handler<{
1727
+ collection: string;
1728
+ id: string;
1729
+ revisionId: string;
1730
+ }, Record<string, unknown>>;
1731
+ /** Mint a signed link that shows ONE row's unpublished state, to whoever holds it.
1732
+ * Mirrors `signPagePreview` — same secret, same TTL clamp, same D1 refusal, and the
1733
+ * same rule that the row is read through the ACL FIRST: minting a link is granting
1734
+ * access to the row, so a caller who cannot read it must not be able to mint one. */
1735
+ signCollectionPreview: import("@pramen/server").Handler<{
1736
+ collection: string;
1737
+ id: string;
1738
+ expiresIn?: number;
1739
+ }, {
1740
+ url: string;
1741
+ token: string;
1742
+ expiresAt: number;
1743
+ }>;
1744
+ /** Read one row's live (possibly unpublished) state. Not the redemption endpoint — that
1745
+ * is the public `GET /cms/preview/collection` route, which verifies the token and then
1746
+ * calls this privileged. Role-gated so it is not an anonymous back door on /rpc. */
1747
+ getCollectionPreview: import("@pramen/server").Handler<{
1748
+ collection: string;
1749
+ id: string;
1750
+ }, {
1751
+ collection: string;
1752
+ id: string;
1753
+ values: Record<string, unknown>;
1754
+ }>;
1302
1755
  };
1303
1756
  /** ACL fragments granting the editor role full CRUD over each collection's entity. Spread
1304
1757
  * into your editor role next to `cmsPolicies().editor`:
@@ -1309,6 +1762,40 @@ export declare function createCollectionHandlers(collections: readonly Collectio
1309
1762
  * them. Your app may already declare its own policies over the entity (e.g. a public read
1310
1763
  * scope) — these only ADD the editor grant the CMS UI needs. */
1311
1764
  export declare function collectionPolicies(collections: readonly CollectionDef[], opts?: CmsPolicyOpts): Policy[];
1765
+ /** ACL fragments granting ANONYMOUS read of the PUBLISHED rows of every collection that
1766
+ * supports `drafts`. Spread into your public role next to `cmsPolicies().public`:
1767
+ *
1768
+ * role("anonymous", [...cmsPolicies().public, ...collectionPublicPolicies(collections)])
1769
+ *
1770
+ * This is the access boundary, not a UI filter — it is AND-merged into every `ctx.db` read
1771
+ * of the entity, so an unpublished row is invisible to the public API, to relation
1772
+ * traversals and to eager-loads alike, without a single query remembering to filter.
1773
+ *
1774
+ * With `scheduling`, the scope also requires `publishedAt <= $now()`. `status` alone would
1775
+ * not be enough the moment anything writes a future `publishedAt`, and `{ publishedAt:
1776
+ * { isNull: false } }` — the obvious-looking alternative — matches a FUTURE timestamp too,
1777
+ * so a row scheduled for next week would be anonymously readable the moment it was saved.
1778
+ * The comparison is lexicographic over TEXT, which is why every managed timestamp is minted
1779
+ * as ISO-8601 UTC (`isoStamp`), the same shape `$now()` produces.
1780
+ *
1781
+ * Collections WITHOUT `drafts` get nothing here: they have no publish state, so their
1782
+ * public exposure is entirely your app's own policy to write. */
1783
+ export declare function collectionPublicPolicies(collections: readonly CollectionDef[], opts?: CmsPolicyOpts): Policy[];
1784
+ /** Task handlers backing `collectionSchedule`. Register alongside `cmsTasks`:
1785
+ *
1786
+ * const app = { tasks: { ...cmsTasks, ...createCollectionTasks(collections) } };
1787
+ *
1788
+ * WITHOUT THIS WIRING A SCHEDULE NEVER FIRES: `collectionSchedule` still stores the
1789
+ * instants and enqueues the tasks, but the drain finds no handler for their kind, so the
1790
+ * row silently stays a draft. (`cmsTasks` has the same requirement for page scheduling.)
1791
+ *
1792
+ * They run with a privileged, system-scoped ctx off the write path, and each validates its
1793
+ * INTENT TOKEN against the row's current `scheduledAt`/`unpublishAt` before acting — see
1794
+ * `collectionSchedule`. */
1795
+ export declare function createCollectionTasks(collections: readonly CollectionDef[]): {
1796
+ "cms:collection:publish": (ctx: HandlerContext, payload: unknown) => Promise<void>;
1797
+ "cms:collection:unpublish": (ctx: HandlerContext, payload: unknown) => Promise<void>;
1798
+ };
1312
1799
  /** Task handlers backing `schedulePage`. Register via `app.tasks = { ...cmsTasks }`.
1313
1800
  * They run with a privileged, system-scoped ctx off the write path (the outbox drain).
1314
1801
  *
@@ -1355,7 +1842,7 @@ interface CmsRoute {
1355
1842
  path: string;
1356
1843
  handler: (request: Request, env: EnvBag, ctx: RouteCtx) => Promise<Response>;
1357
1844
  }
1358
- /** Turnkey public routes for `GET /sitemap.xml` and `GET /robots.txt`. Spread into
1845
+ /** Turnkey public routes for `GET /sitemap.xml`, `GET /cms/preview` and `GET /robots.txt`. Spread into
1359
1846
  * `app.routes`. The sitemap pulls published pages via `callPrivileged(listPublishedPages)`.
1360
1847
  * `origin` defaults to the request's origin; `pageUrl` customizes the URL shape. */
1361
1848
  export declare function cmsRoutes(opts?: {
@@ -1363,5 +1850,18 @@ export declare function cmsRoutes(opts?: {
1363
1850
  tenant?: string;
1364
1851
  pageUrl?: SitemapOpts["pageUrl"];
1365
1852
  disallow?: string[];
1853
+ /** The SAME options you passed to `createCmsHandlers`. The route derives its identity
1854
+ * from them with `viewerRolesOf`, so the two cannot drift. (`viewerRoles` overrides it
1855
+ * outright if you need to.) */
1856
+ handlers?: CmsHandlerOpts;
1857
+ /** The SAME options you passed to `createCollectionHandlers`, if they differ from
1858
+ * `handlers`. The COLLECTION preview route has its own gate — `getCollectionPreview` is
1859
+ * built by `createCollectionHandlers`, so an app that passes different `editorRoles` /
1860
+ * `reviewerRoles` to the two factories would have the route present the page half's
1861
+ * roles to a handler gated on the collection half's, and every collection preview link
1862
+ * would 404 uniformly (the response is deliberately indistinguishable from "not
1863
+ * found"). Defaults to `handlers`, which is right whenever both got the same options. */
1864
+ collectionHandlers?: CmsHandlerOpts;
1865
+ /** Explicit override for the roles the preview routes present to the DO. */
1866
+ viewerRoles?: readonly string[];
1366
1867
  }): CmsRoute[];
1367
- export {};