@selfhelp/shared 1.27.0 → 1.28.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.
@@ -0,0 +1,132 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Live-sync contract (task `2026-11-20-live-sync-and-edit-lock`, contracts C1 to C9).
7
+ *
8
+ * The backend publishes a `changed` Mercure event that carries names and ids
9
+ * only, never content. Clients match the tags against the `sync.depends_on`
10
+ * list the server put into each page payload and refetch what matches. The
11
+ * server decides who gets a message; a client never decides who may see a
12
+ * change.
13
+ */
14
+
15
+ /** Mercure event name of a change message (C1). */
16
+ export const SYNC_EVENT_NAME = 'changed' as const;
17
+
18
+ /** Highest change-message version this package understands (C1). Clients ignore a higher `v`. */
19
+ export const SYNC_MESSAGE_VERSION = 1 as const;
20
+
21
+ /**
22
+ * Tags a change message may carry (C1). A client ignores tags it does not know.
23
+ *
24
+ * | Tag | Meaning |
25
+ * | --- | --- |
26
+ * | `acl` | Access of this audience changed (re-bootstrap the stream, refetch user data) |
27
+ * | `navigation` | A menu or the visible page set changed |
28
+ * | `global_values` | Global values changed |
29
+ * | `page:<id>` | The published page changed |
30
+ * | `page_draft:<id>` | The draft of the page changed (editors) |
31
+ * | `page_lock:<id>` | The edit lock of the page changed (editors) |
32
+ * | `table:<id>` | A record of the data table changed |
33
+ * | `section:<id>` | A section was saved in the admin |
34
+ * | `asset:<id>` | An asset changed |
35
+ */
36
+ export type TSyncTag =
37
+ | 'acl'
38
+ | 'navigation'
39
+ | 'global_values'
40
+ | `page:${number}`
41
+ | `page_draft:${number}`
42
+ | `page_lock:${number}`
43
+ | `table:${number}`
44
+ | `section:${number}`
45
+ | `asset:${number}`;
46
+
47
+ /** Mirror of the backend tag pattern (C1). Anchored, so the whole tag must match. */
48
+ export const SYNC_TAG_PATTERN =
49
+ /^(acl|navigation|global_values|page:\d+|page_draft:\d+|page_lock:\d+|table:\d+|section:\d+|asset:\d+)$/;
50
+
51
+ /** Data of one `changed` event (C1). */
52
+ export interface ISyncChangeMessage {
53
+ /** Message version. Always 1 for this package. */
54
+ v: typeof SYNC_MESSAGE_VERSION;
55
+ /** Unique, non-empty list of known tags. */
56
+ tags: TSyncTag[];
57
+ }
58
+
59
+ /**
60
+ * The `sync` block of every page payload, published and draft (C6).
61
+ *
62
+ * `depends_on` lists `page:<id>` (`page_draft:<id>` for a draft), `table:<id>`
63
+ * for every data table the page reads, and `global_values`. `valid_until` is
64
+ * the earliest moment (UTC, ISO 8601) the result can change because time
65
+ * passes, or `null`. The client refetches once at `valid_until` plus a random
66
+ * delay while the page is open.
67
+ */
68
+ export interface ISyncBlock {
69
+ depends_on: string[];
70
+ valid_until: string | null;
71
+ }
72
+
73
+ /**
74
+ * Data of `GET /auth/events` (C4). For a guest `topic`, `impersonationTopic`
75
+ * and `systemUpdateTopic` are `null` and `syncTopics` is the public topic only.
76
+ * The token covers exactly the listed topics (plus plugin topics).
77
+ */
78
+ export interface IAuthEventsBootstrap {
79
+ hubUrl: string;
80
+ topic: string | null;
81
+ impersonationTopic: string | null;
82
+ systemUpdateTopic: string | null;
83
+ /** Sync topics to subscribe to: `[user, each group, each role, public]`, or `[public]` for a guest. */
84
+ syncTopics: string[];
85
+ pluginTopics: string[];
86
+ /** `null` when `transport=cookie` was requested. */
87
+ token: string | null;
88
+ expiresIn: number;
89
+ }
90
+
91
+ /** Error codes of the page draft version and lock checks (C8, C9). */
92
+ export const PAGE_VERSION_REQUIRED_ERROR = 'page_version_required' as const;
93
+ export const STALE_PAGE_VERSION_ERROR = 'stale_page_version' as const;
94
+ export const PAGE_LOCKED_ERROR = 'page_locked' as const;
95
+
96
+ /**
97
+ * Draft version of a page (C8). `GET /admin/pages/{page_id}` returns it in its
98
+ * data (and in the `X-Page-Version` header); admin page types extend this so
99
+ * the editor can send it back with every draft write.
100
+ */
101
+ export interface IPageDraftVersioned {
102
+ /** Integer from 1; raised by one on every successful draft write. */
103
+ draft_version: number;
104
+ }
105
+
106
+ /** Data of `409 stale_page_version` (C8). */
107
+ export interface IStalePageVersionData {
108
+ page_id: number;
109
+ current_version: number;
110
+ }
111
+
112
+ /** Holder of a page edit lock (C9). */
113
+ export interface IPageLockHolder {
114
+ id: number;
115
+ name: string;
116
+ }
117
+
118
+ /**
119
+ * Status of a page edit lock (C9). Per caller (`mine`) and never cached.
120
+ * `holder`, `expires_at` and `idle_expires_at` are `null` when `held` is false.
121
+ * The data of `409 page_locked` is this object.
122
+ */
123
+ export interface IPageLockStatus {
124
+ page_id: number;
125
+ held: boolean;
126
+ mine: boolean;
127
+ holder: IPageLockHolder | null;
128
+ /** The lease ends here unless renewed (120 s after the last renewal). */
129
+ expires_at: string | null;
130
+ /** The lock ends here unless the holder writes (15 minutes after the last draft write). */
131
+ idle_expires_at: string | null;
132
+ }
package/src/types/auth.ts CHANGED
@@ -34,6 +34,12 @@ export interface IUserData {
34
34
  roles?: string[];
35
35
  groups?: string[];
36
36
  acl_version?: string | number | null;
37
+ /**
38
+ * Access stamp (live sync, C5): the same value the backend sends in the
39
+ * `X-Acl-Stamp` header of every response to this user. A client that sees
40
+ * a header value different from this one invalidates its user data.
41
+ */
42
+ acl_stamp?: string;
37
43
  }
38
44
 
39
45
  export interface IJwtPayload {
@@ -4,6 +4,7 @@ SPDX-License-Identifier: MPL-2.0
4
4
  */
5
5
  import type { IContentField } from './styles/base';
6
6
  import type { TStyleName } from './styles/unknown';
7
+ import type { ISyncBlock } from '../sync/types';
7
8
 
8
9
  /**
9
10
  * Comprehensive page type system, single source of truth for both apps.
@@ -125,6 +126,13 @@ export interface IPageContent {
125
126
  matched_url_pattern?: string | null;
126
127
  /** The canonical active route pattern for the page, for canonical-link generation. Resolve responses only. */
127
128
  canonical_url?: string | null;
129
+ /**
130
+ * Live-sync block (C6): the tags this payload depends on and the moment it
131
+ * can change because time passes. Emitted by the backend on published and
132
+ * draft page payloads; optional here because the type is also used for
133
+ * payloads built elsewhere (preview, admin).
134
+ */
135
+ sync?: ISyncBlock;
128
136
  sections: IPageSectionWithFields[];
129
137
  }
130
138