@selfhelp/shared 1.27.0 → 1.29.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.
@@ -103,8 +103,28 @@ export const ENDPOINTS = {
103
103
  VERSION_FILE: (tableName: string, recordId: number, version: number, fieldKey: string): string =>
104
104
  `${API_VERSION_PREFIX}/admin/data/tables/${encodeURIComponent(tableName)}/records/${recordId}/versions/${version}/files/${encodeURIComponent(fieldKey)}`,
105
105
  },
106
+ /**
107
+ * Page edit lock (live sync, C9). Route permission `admin.page.update` plus
108
+ * the update grant on the page. `LOCK` serves three methods: GET (status),
109
+ * POST (acquire or renew) and DELETE (release); `TAKE_OVER` is POST only.
110
+ * Every route answers an `IPageLockStatus`.
111
+ */
112
+ ADMIN_PAGE_LOCK: {
113
+ LOCK: (pageId: number): string => `${API_VERSION_PREFIX}/admin/pages/${pageId}/lock`,
114
+ TAKE_OVER: (pageId: number): string =>
115
+ `${API_VERSION_PREFIX}/admin/pages/${pageId}/lock/take-over`,
116
+ },
106
117
  } as const;
107
118
 
119
+ /** Header on every response to a signed-in caller: the access stamp (live sync, C5). */
120
+ export const HEADER_ACL_STAMP = 'X-Acl-Stamp';
121
+
122
+ /** Request header `X-Page-View: 1`: the only page fetch per visit that counts as a view (C7, F135). */
123
+ export const HEADER_PAGE_VIEW = 'X-Page-View';
124
+
125
+ /** Draft-version header: sent with every page draft write, returned on page reads and writes (C8). */
126
+ export const HEADER_PAGE_VERSION = 'X-Page-Version';
127
+
108
128
  /** Header set the backend reads to switch page-access mode + condition platform var. */
109
129
  export const HEADER_CLIENT_TYPE = 'X-Client-Type';
110
130
  export const CLIENT_TYPE_WEB = 'web';
package/src/index.ts CHANGED
@@ -24,4 +24,5 @@ export * from './cms-classes';
24
24
  export * from './assets';
25
25
  export * from './content';
26
26
  export * from './navigation';
27
+ export * from './sync';
27
28
  export * from './utils';
@@ -0,0 +1,72 @@
1
+ /*
2
+ * SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ * SPDX-License-Identifier: MPL-2.0
4
+ */
5
+
6
+ import { describe, expect, it } from 'vitest';
7
+ import {
8
+ LOCK_IDLE_ENDED_ERROR,
9
+ PAGE_LOCKED_ERROR,
10
+ pageFromPayload,
11
+ type IGetPageResponse,
12
+ type IPageContent,
13
+ type IPagePayload,
14
+ type ISyncBlock,
15
+ } from '../../index';
16
+
17
+ const sync: ISyncBlock = {
18
+ depends_on: ['page:12', 'global_values'],
19
+ valid_until: '2026-11-23T12:00:00Z',
20
+ };
21
+
22
+ const page: IPageContent = {
23
+ id: 12,
24
+ keyword: 'demo',
25
+ url: '/demo',
26
+ parent_page_id: null,
27
+ is_headless: false,
28
+ title: 'Demo',
29
+ sections: [],
30
+ };
31
+
32
+ describe('root exports (P1-A1)', () => {
33
+ it('exports LOCK_IDLE_ENDED_ERROR, IPagePayload and pageFromPayload from the package root', () => {
34
+ expect(LOCK_IDLE_ENDED_ERROR).toBe('lock_idle_ended');
35
+ expect(PAGE_LOCKED_ERROR).toBe('page_locked');
36
+ const payload: IPagePayload = { page, sync };
37
+ expect(pageFromPayload(payload)?.id).toBe(12);
38
+ });
39
+ });
40
+
41
+ describe('pageFromPayload (P1-A2)', () => {
42
+ it('prefers payload.sync over page.sync', () => {
43
+ const onPage: ISyncBlock = { depends_on: ['page:99'], valid_until: null };
44
+ expect(pageFromPayload({ page: { ...page, sync: onPage }, sync })).toEqual({
45
+ ...page,
46
+ sync,
47
+ });
48
+ });
49
+
50
+ it('falls back to page.sync when the payload has no sync', () => {
51
+ expect(pageFromPayload({ page: { ...page, sync } })).toEqual({ ...page, sync });
52
+ });
53
+
54
+ it('returns null when there is no page', () => {
55
+ expect(pageFromPayload({ page: null, sync })).toBeNull();
56
+ expect(pageFromPayload({ sync })).toBeNull();
57
+ expect(pageFromPayload(undefined)).toBeNull();
58
+ expect(pageFromPayload(null)).toBeNull();
59
+ });
60
+
61
+ it('returns the page unchanged when there is no sync at all', () => {
62
+ expect(pageFromPayload({ page })).toBe(page);
63
+ });
64
+ });
65
+
66
+ describe('IGetPageResponse data shape (P1-A3)', () => {
67
+ it('accepts { page, sync } on the response data', () => {
68
+ const data: IGetPageResponse['data'] = { page, sync };
69
+ expect(data.page.id).toBe(12);
70
+ expect(data.sync?.depends_on).toContain('page:12');
71
+ });
72
+ });
@@ -0,0 +1,193 @@
1
+ /*
2
+ * SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ * SPDX-License-Identifier: MPL-2.0
4
+ */
5
+
6
+ import { describe, expect, it } from 'vitest';
7
+ import {
8
+ API_VERSION_PREFIX,
9
+ ENDPOINTS,
10
+ HEADER_ACL_STAMP,
11
+ HEADER_PAGE_VERSION,
12
+ HEADER_PAGE_VIEW,
13
+ SYNC_EVENT_NAME,
14
+ pageDependsOn,
15
+ parseSyncMessage,
16
+ parseSyncMessageJson,
17
+ syncRefetchDelayMs,
18
+ type IPageLockStatus,
19
+ type ISyncBlock,
20
+ } from '../../index';
21
+
22
+ describe('parseSyncMessage (C1)', () => {
23
+ it('accepts the contract example', () => {
24
+ expect(parseSyncMessage({ v: 1, tags: ['page:12', 'navigation'] })).toEqual({
25
+ v: 1,
26
+ tags: ['page:12', 'navigation'],
27
+ });
28
+ });
29
+
30
+ it('accepts every tag kind of the contract', () => {
31
+ const tags = [
32
+ 'acl',
33
+ 'navigation',
34
+ 'global_values',
35
+ 'page:1',
36
+ 'page_draft:2',
37
+ 'page_lock:3',
38
+ 'table:4',
39
+ 'section:5',
40
+ 'asset:6',
41
+ ];
42
+ expect(parseSyncMessage({ v: 1, tags })?.tags).toEqual(tags);
43
+ });
44
+
45
+ it('ignores a message of a higher version', () => {
46
+ expect(parseSyncMessage({ v: 2, tags: ['page:12'] })).toBeNull();
47
+ expect(parseSyncMessage({ v: 0, tags: ['page:12'] })).toBeNull();
48
+ expect(parseSyncMessage({ tags: ['page:12'] })).toBeNull();
49
+ });
50
+
51
+ it('returns null for anything that is not an object with a tag list', () => {
52
+ expect(parseSyncMessage(null)).toBeNull();
53
+ expect(parseSyncMessage(undefined)).toBeNull();
54
+ expect(parseSyncMessage('{"v":1,"tags":["page:1"]}')).toBeNull();
55
+ expect(parseSyncMessage(['page:1'])).toBeNull();
56
+ expect(parseSyncMessage({ v: 1, tags: 'page:1' })).toBeNull();
57
+ expect(parseSyncMessage({ v: 1 })).toBeNull();
58
+ });
59
+
60
+ it('returns null for an empty tag list or one with no known tag', () => {
61
+ expect(parseSyncMessage({ v: 1, tags: [] })).toBeNull();
62
+ expect(parseSyncMessage({ v: 1, tags: ['foo:1', 'page:x'] })).toBeNull();
63
+ });
64
+
65
+ it('drops unknown and malformed tags and keeps the valid ones', () => {
66
+ expect(
67
+ parseSyncMessage({
68
+ v: 1,
69
+ tags: ['foo:1', 'page:12', 'page:', 'page:12x', ' table:3', 7, null, 'table:31'],
70
+ }),
71
+ ).toEqual({ v: 1, tags: ['page:12', 'table:31'] });
72
+ });
73
+
74
+ it('removes duplicate tags', () => {
75
+ expect(parseSyncMessage({ v: 1, tags: ['page:12', 'acl', 'page:12', 'acl'] })?.tags).toEqual([
76
+ 'page:12',
77
+ 'acl',
78
+ ]);
79
+ });
80
+ });
81
+
82
+ describe('parseSyncMessageJson', () => {
83
+ it('reads the raw data of a server-sent event', () => {
84
+ expect(parseSyncMessageJson('{"v":1,"tags":["table:31"]}')).toEqual({ v: 1, tags: ['table:31'] });
85
+ });
86
+
87
+ it('returns null for invalid JSON or an invalid message', () => {
88
+ expect(parseSyncMessageJson('not json')).toBeNull();
89
+ expect(parseSyncMessageJson('')).toBeNull();
90
+ expect(parseSyncMessageJson('{"v":2,"tags":["page:1"]}')).toBeNull();
91
+ });
92
+ });
93
+
94
+ describe('pageDependsOn (C6)', () => {
95
+ const sync: ISyncBlock = {
96
+ depends_on: ['page:12', 'table:31', 'global_values'],
97
+ valid_until: null,
98
+ };
99
+
100
+ it('is true when a tag is listed in depends_on', () => {
101
+ expect(pageDependsOn(sync, ['page:12'])).toBe(true);
102
+ expect(pageDependsOn(sync, ['navigation', 'table:31'])).toBe(true);
103
+ });
104
+
105
+ it('is false for a page, table or draft the payload does not name', () => {
106
+ expect(pageDependsOn(sync, ['page:13'])).toBe(false);
107
+ expect(pageDependsOn(sync, ['table:32'])).toBe(false);
108
+ expect(pageDependsOn(sync, ['page_draft:12'])).toBe(false);
109
+ });
110
+
111
+ it('matches global_values because a page payload lists it', () => {
112
+ expect(pageDependsOn(sync, ['global_values'])).toBe(true);
113
+ expect(pageDependsOn({ depends_on: ['page:1'], valid_until: null }, ['global_values'])).toBe(false);
114
+ });
115
+
116
+ it('never matches acl and navigation: those refresh every page, not a listed one', () => {
117
+ expect(pageDependsOn(sync, ['acl'])).toBe(false);
118
+ expect(pageDependsOn(sync, ['navigation'])).toBe(false);
119
+ });
120
+
121
+ it('matches page_draft only for a draft payload', () => {
122
+ const draft: ISyncBlock = { depends_on: ['page_draft:12', 'table:31'], valid_until: null };
123
+ expect(pageDependsOn(draft, ['page_draft:12'])).toBe(true);
124
+ expect(pageDependsOn(draft, ['page:12'])).toBe(false);
125
+ });
126
+
127
+ it('is false without a sync block or an empty list', () => {
128
+ expect(pageDependsOn(undefined, ['page:12'])).toBe(false);
129
+ expect(pageDependsOn(null, ['page:12'])).toBe(false);
130
+ expect(pageDependsOn({ depends_on: [], valid_until: null }, ['page:12'])).toBe(false);
131
+ expect(pageDependsOn(sync, [])).toBe(false);
132
+ });
133
+ });
134
+
135
+ describe('syncRefetchDelayMs', () => {
136
+ it('stays within 0 to 300 ms for a pushed change', () => {
137
+ expect(syncRefetchDelayMs('changed', () => 0)).toBe(0);
138
+ expect(syncRefetchDelayMs('changed', () => 0.5)).toBe(150);
139
+ expect(syncRefetchDelayMs('changed', () => 0.999999)).toBe(300);
140
+ expect(syncRefetchDelayMs('changed', () => 1)).toBe(300);
141
+ });
142
+
143
+ it('stays within 0 to 2000 ms for valid_until', () => {
144
+ expect(syncRefetchDelayMs('valid_until', () => 0)).toBe(0);
145
+ expect(syncRefetchDelayMs('valid_until', () => 0.5)).toBe(1000);
146
+ expect(syncRefetchDelayMs('valid_until', () => 1)).toBe(2000);
147
+ });
148
+
149
+ it('clamps a misbehaving random source', () => {
150
+ expect(syncRefetchDelayMs('changed', () => -3)).toBe(0);
151
+ expect(syncRefetchDelayMs('changed', () => 7)).toBe(300);
152
+ expect(syncRefetchDelayMs('changed', () => Number.NaN)).toBe(0);
153
+ });
154
+
155
+ it('uses Math.random by default and returns an integer in range', () => {
156
+ for (let i = 0; i < 50; i++) {
157
+ const ms = syncRefetchDelayMs('changed');
158
+ expect(Number.isInteger(ms)).toBe(true);
159
+ expect(ms).toBeGreaterThanOrEqual(0);
160
+ expect(ms).toBeLessThanOrEqual(300);
161
+ }
162
+ });
163
+ });
164
+
165
+ describe('sync constants and endpoints', () => {
166
+ it('names the event and the three headers as the backend does', () => {
167
+ expect(SYNC_EVENT_NAME).toBe('changed');
168
+ expect(HEADER_ACL_STAMP).toBe('X-Acl-Stamp');
169
+ expect(HEADER_PAGE_VIEW).toBe('X-Page-View');
170
+ expect(HEADER_PAGE_VERSION).toBe('X-Page-Version');
171
+ });
172
+
173
+ it('builds the four page lock routes (C9)', () => {
174
+ expect(ENDPOINTS.ADMIN_PAGE_LOCK.LOCK(12)).toBe(`${API_VERSION_PREFIX}/admin/pages/12/lock`);
175
+ expect(ENDPOINTS.ADMIN_PAGE_LOCK.TAKE_OVER(12)).toBe(
176
+ `${API_VERSION_PREFIX}/admin/pages/12/lock/take-over`,
177
+ );
178
+ });
179
+
180
+ it('types the lock status the way the contract example shows it', () => {
181
+ const status: IPageLockStatus = {
182
+ page_id: 12,
183
+ held: true,
184
+ mine: false,
185
+ holder: { id: 7, name: 'Anna Muster' },
186
+ expires_at: '2026-11-20T10:02:00Z',
187
+ idle_expires_at: '2026-11-20T10:15:00Z',
188
+ };
189
+ expect(Object.keys(status).sort()).toEqual(
190
+ ['expires_at', 'held', 'holder', 'idle_expires_at', 'mine', 'page_id'],
191
+ );
192
+ });
193
+ });
@@ -0,0 +1,7 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ export * from './types';
6
+ export * from './sync';
7
+ export * from './page-payload';
@@ -0,0 +1,35 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Unwrap a page API response so the live-sync block travels with the page.
7
+ *
8
+ * The backend answers `{ data: { page, sync } }`: the sync block (contract C6)
9
+ * sits next to the page, not inside it. Clients that cache the page read
10
+ * `page.sync` for tag matching and `valid_until`, so every unwrap goes through
11
+ * {@link pageFromPayload}.
12
+ */
13
+ import type { IPageContent } from '../types/pages';
14
+ import type { ISyncBlock } from './types';
15
+
16
+ /** The `data` of a page response (`/pages/by-keyword`, `/pages/resolve`). */
17
+ export interface IPagePayload {
18
+ page?: IPageContent | null;
19
+ sync?: ISyncBlock | null;
20
+ }
21
+
22
+ /**
23
+ * The page of a response's `data`, carrying the response's `sync` block.
24
+ * Prefers `payload.sync`, falls back to `page.sync`. Returns `null` when there
25
+ * is no page. With no sync at all, returns the page unchanged.
26
+ */
27
+ export function pageFromPayload(payload: IPagePayload | null | undefined): IPageContent | null {
28
+ const page = payload?.page;
29
+ if (!page) {
30
+ return null;
31
+ }
32
+ // An older server (or a payload built elsewhere) may have put the block on the page already.
33
+ const sync = payload.sync ?? page.sync;
34
+ return sync ? { ...page, sync } : page;
35
+ }
@@ -0,0 +1,106 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Small, pure helpers both clients use to read a change message and match it
7
+ * to what is on screen (contracts C1 and C6). They hold no state, touch no
8
+ * network and never decide access: they only compare tags the server sent
9
+ * with tags the server put into the page payload.
10
+ */
11
+ import {
12
+ SYNC_MESSAGE_VERSION,
13
+ SYNC_TAG_PATTERN,
14
+ type ISyncBlock,
15
+ type ISyncChangeMessage,
16
+ type TSyncTag,
17
+ } from './types';
18
+
19
+ /** Largest random delay before a refetch caused by a `changed` message (decision "instant"). */
20
+ export const SYNC_CHANGED_MAX_DELAY_MS = 300;
21
+
22
+ /** Largest random delay before the one refetch at a page's `valid_until`. */
23
+ export const SYNC_VALID_UNTIL_MAX_DELAY_MS = 2000;
24
+
25
+ /** Why a refetch is scheduled: a pushed change, or the page's own `valid_until`. */
26
+ export type TSyncRefetchKind = 'changed' | 'valid_until';
27
+
28
+ function isRecord(value: unknown): value is Record<string, unknown> {
29
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
30
+ }
31
+
32
+ /**
33
+ * Validates the data of a `changed` event (C1).
34
+ *
35
+ * Returns `null` when the data is not an object, `v` is not
36
+ * {@link SYNC_MESSAGE_VERSION} (a higher version is ignored by design), `tags`
37
+ * is not a list, or no known tag is left. Unknown tags are dropped and
38
+ * duplicates are removed, so `{ v: 1, tags: ['page:12', 'foo:1'] }` gives
39
+ * `{ v: 1, tags: ['page:12'] }`. A message that carries only unknown tags
40
+ * returns `null` because there is nothing a client could act on.
41
+ */
42
+ export function parseSyncMessage(data: unknown): ISyncChangeMessage | null {
43
+ if (!isRecord(data) || data.v !== SYNC_MESSAGE_VERSION || !Array.isArray(data.tags)) {
44
+ return null;
45
+ }
46
+
47
+ const tags: TSyncTag[] = [];
48
+ for (const tag of data.tags as unknown[]) {
49
+ if (typeof tag === 'string' && SYNC_TAG_PATTERN.test(tag) && !tags.includes(tag as TSyncTag)) {
50
+ tags.push(tag as TSyncTag);
51
+ }
52
+ }
53
+
54
+ return tags.length === 0 ? null : { v: SYNC_MESSAGE_VERSION, tags };
55
+ }
56
+
57
+ /**
58
+ * Same as {@link parseSyncMessage} for the raw `data` string of a server-sent
59
+ * event. Invalid JSON returns `null`.
60
+ */
61
+ export function parseSyncMessageJson(raw: string): ISyncChangeMessage | null {
62
+ try {
63
+ return parseSyncMessage(JSON.parse(raw) as unknown);
64
+ } catch {
65
+ return null;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * True when a page payload's `sync.depends_on` names any of the message's tags (C6).
71
+ *
72
+ * Only tags that a page payload can list match here: `page:<id>`,
73
+ * `page_draft:<id>`, `table:<id>` and `global_values`. The other tags are not
74
+ * page dependencies and are handled by the client on their own:
75
+ *
76
+ * - `acl` and `navigation` affect every page the caller may see, so the client
77
+ * invalidates all page queries and the navigation (it does not ask each page).
78
+ * - `page_lock:<id>`, `section:<id>` and `asset:<id>` are editor-side tags;
79
+ * the admin screens that show them refetch their own queries.
80
+ *
81
+ * A page without a `sync` block (older server, error payload) never matches.
82
+ */
83
+ export function pageDependsOn(
84
+ sync: ISyncBlock | null | undefined,
85
+ tags: readonly string[],
86
+ ): boolean {
87
+ if (!sync || !Array.isArray(sync.depends_on) || sync.depends_on.length === 0) {
88
+ return false;
89
+ }
90
+ return tags.some((tag) => sync.depends_on.includes(tag));
91
+ }
92
+
93
+ /**
94
+ * Random delay in milliseconds before a refetch: 0 to 300 for a `changed`
95
+ * message, 0 to 2000 for `valid_until`. It spreads many clients over a short
96
+ * time without making a change feel slow. `random` is injectable for tests.
97
+ */
98
+ export function syncRefetchDelayMs(
99
+ kind: TSyncRefetchKind,
100
+ random: () => number = Math.random,
101
+ ): number {
102
+ const max = kind === 'changed' ? SYNC_CHANGED_MAX_DELAY_MS : SYNC_VALID_UNTIL_MAX_DELAY_MS;
103
+ const r = random();
104
+ const unit = Number.isFinite(r) ? Math.min(Math.max(r, 0), 1) : 0;
105
+ return Math.min(max, Math.floor(unit * (max + 1)));
106
+ }
@@ -0,0 +1,138 @@
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
+ * `409` when the caller's own page edit lock ended by idle expiry and they try
97
+ * to renew (`POST .../lock`). Data is {@link IPageLockStatus}. Resume through
98
+ * take-over (`POST .../lock/take-over`), not a plain acquire.
99
+ */
100
+ export const LOCK_IDLE_ENDED_ERROR = 'lock_idle_ended' as const;
101
+
102
+ /**
103
+ * Draft version of a page (C8). `GET /admin/pages/{page_id}` returns it in its
104
+ * data (and in the `X-Page-Version` header); admin page types extend this so
105
+ * the editor can send it back with every draft write.
106
+ */
107
+ export interface IPageDraftVersioned {
108
+ /** Integer from 1; raised by one on every successful draft write. */
109
+ draft_version: number;
110
+ }
111
+
112
+ /** Data of `409 stale_page_version` (C8). */
113
+ export interface IStalePageVersionData {
114
+ page_id: number;
115
+ current_version: number;
116
+ }
117
+
118
+ /** Holder of a page edit lock (C9). */
119
+ export interface IPageLockHolder {
120
+ id: number;
121
+ name: string;
122
+ }
123
+
124
+ /**
125
+ * Status of a page edit lock (C9). Per caller (`mine`) and never cached.
126
+ * `holder`, `expires_at` and `idle_expires_at` are `null` when `held` is false.
127
+ * The data of `409 page_locked` is this object.
128
+ */
129
+ export interface IPageLockStatus {
130
+ page_id: number;
131
+ held: boolean;
132
+ mine: boolean;
133
+ holder: IPageLockHolder | null;
134
+ /** The lease ends here unless renewed (120 s after the last renewal). */
135
+ expires_at: string | null;
136
+ /** The lock ends here unless the holder writes (15 minutes after the last draft write). */
137
+ idle_expires_at: string | null;
138
+ }
@@ -4,8 +4,13 @@ SPDX-License-Identifier: MPL-2.0
4
4
  */
5
5
  import type { IBaseApiResponse } from './envelope';
6
6
  import type { IPageContent, IPageItem } from '../pages';
7
+ import type { ISyncBlock } from '../../sync/types';
7
8
 
8
- export type IGetPageResponse = IBaseApiResponse<{ page: IPageContent }>;
9
+ export type IGetPageResponse = IBaseApiResponse<{
10
+ page: IPageContent;
11
+ /** Live-sync block next to the page (contract C6); optional on older servers. */
12
+ sync?: ISyncBlock | null;
13
+ }>;
9
14
  export type IGetPagesResponse = IBaseApiResponse<IPageItem[]>;
10
15
 
11
16
  /**
@@ -13,4 +18,8 @@ export type IGetPagesResponse = IBaseApiResponse<IPageItem[]>;
13
18
  * envelope to {@link IGetPageResponse}; the resolved `page` additionally carries
14
19
  * `route_params`, `matched_url_pattern`, and `canonical_url`.
15
20
  */
16
- export type IResolvePageResponse = IBaseApiResponse<{ page: IPageContent }>;
21
+ export type IResolvePageResponse = IBaseApiResponse<{
22
+ page: IPageContent;
23
+ /** Live-sync block next to the page (contract C6); optional on older servers. */
24
+ sync?: ISyncBlock | null;
25
+ }>;
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