@selfhelp/shared 1.25.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.
- package/CHANGELOG.md +44 -0
- package/dist/index.d.mts +193 -10
- package/dist/index.d.ts +193 -10
- package/dist/index.js +93 -6
- package/dist/index.mjs +78 -6
- package/dist/{manifest-8YXAk-4o.d.mts → manifest-BNnCN1Qo.d.mts} +7 -0
- package/dist/{manifest-8YXAk-4o.d.ts → manifest-BNnCN1Qo.d.ts} +7 -0
- package/dist/plugin-sdk/index.d.mts +1 -1
- package/dist/plugin-sdk/index.d.ts +1 -1
- package/dist/testing/index.d.mts +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/api/endpoints.ts +20 -0
- package/src/index.ts +1 -0
- package/src/plugin-sdk/__tests__/manifest.test.ts +30 -0
- package/src/plugin-sdk/manifest.ts +7 -0
- package/src/sync/__tests__/sync.test.ts +193 -0
- package/src/sync/index.ts +6 -0
- package/src/sync/sync.ts +106 -0
- package/src/sync/types.ts +132 -0
- package/src/types/auth.ts +10 -3
- package/src/types/pages.ts +9 -3
- package/src/utils/__tests__/transformPageData.test.ts +10 -0
- package/src/utils/transformPageData.ts +0 -6
|
@@ -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 {
|
|
@@ -104,14 +110,15 @@ export const PERMISSIONS = {
|
|
|
104
110
|
ADMIN_SCHEDULED_JOB_EXECUTE: 'admin.scheduled_job.execute',
|
|
105
111
|
ADMIN_SCHEDULED_JOB_DELETE: 'admin.scheduled_job.delete',
|
|
106
112
|
ADMIN_SCHEDULED_JOB_MANAGE: 'admin.scheduled_job.manage',
|
|
113
|
+
ADMIN_SECTION_READ: 'admin.section.read',
|
|
107
114
|
ADMIN_SECTION_DELETE: 'admin.section.delete',
|
|
108
|
-
|
|
115
|
+
ADMIN_LANGUAGE_READ: 'admin.language.read',
|
|
116
|
+
ADMIN_LANGUAGE_UPDATE: 'admin.language.update',
|
|
109
117
|
ADMIN_CMS_PREFERENCES_READ: 'admin.cms_preferences.read',
|
|
110
|
-
ADMIN_CMS_PREFERENCES_UPDATE: 'admin.cms_preferences.update',
|
|
111
118
|
ADMIN_CACHE_READ: 'admin.cache.read',
|
|
112
119
|
ADMIN_CACHE_CLEAR: 'admin.cache.clear',
|
|
113
120
|
ADMIN_CACHE_MANAGE: 'admin.cache.manage',
|
|
114
|
-
|
|
121
|
+
ADMIN_AUDIT_READ: 'admin.audit.read',
|
|
115
122
|
} as const;
|
|
116
123
|
|
|
117
124
|
export type TPermission = (typeof PERMISSIONS)[keyof typeof PERMISSIONS];
|
package/src/types/pages.ts
CHANGED
|
@@ -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.
|
|
@@ -52,10 +53,8 @@ export interface IPageAccessTypeInfo {
|
|
|
52
53
|
export interface IPageItem extends IBasePageInfo {
|
|
53
54
|
id_users?: number;
|
|
54
55
|
id_pages?: number;
|
|
56
|
+
/** Page ACL: members of a group with this right can see the page and use its forms. */
|
|
55
57
|
acl_select?: 0 | 1;
|
|
56
|
-
acl_insert?: 0 | 1;
|
|
57
|
-
acl_update?: 0 | 1;
|
|
58
|
-
acl_delete?: 0 | 1;
|
|
59
58
|
id_page_types?: number;
|
|
60
59
|
id_page_access_types?: number;
|
|
61
60
|
title?: string | null;
|
|
@@ -127,6 +126,13 @@ export interface IPageContent {
|
|
|
127
126
|
matched_url_pattern?: string | null;
|
|
128
127
|
/** The canonical active route pattern for the page, for canonical-link generation. Resolve responses only. */
|
|
129
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;
|
|
130
136
|
sections: IPageSectionWithFields[];
|
|
131
137
|
}
|
|
132
138
|
|
|
@@ -61,6 +61,16 @@ describe('transformPageData', () => {
|
|
|
61
61
|
expect(result.keyword).toBe('');
|
|
62
62
|
expect(result.children).toEqual([]);
|
|
63
63
|
});
|
|
64
|
+
|
|
65
|
+
it('passes through acl_select only (page ACL is one right)', () => {
|
|
66
|
+
const result = transformPageData({
|
|
67
|
+
id: 1,
|
|
68
|
+
keyword: 'seen',
|
|
69
|
+
acl_select: 1,
|
|
70
|
+
});
|
|
71
|
+
expect(result.acl_select).toBe(1);
|
|
72
|
+
expect(Object.keys(result).filter((k) => k.startsWith('acl_'))).toEqual(['acl_select']);
|
|
73
|
+
});
|
|
64
74
|
});
|
|
65
75
|
|
|
66
76
|
describe('transformPagesData', () => {
|
|
@@ -26,9 +26,6 @@ interface IRawPage {
|
|
|
26
26
|
mobile_icon?: string | null;
|
|
27
27
|
id_users?: number;
|
|
28
28
|
acl_select?: 0 | 1;
|
|
29
|
-
acl_insert?: 0 | 1;
|
|
30
|
-
acl_update?: 0 | 1;
|
|
31
|
-
acl_delete?: 0 | 1;
|
|
32
29
|
id_page_types?: number;
|
|
33
30
|
id_page_access_types?: number;
|
|
34
31
|
children?: IRawPage[];
|
|
@@ -62,9 +59,6 @@ export function transformPageData(apiPage: IRawPage): IPageItem {
|
|
|
62
59
|
id_users: apiPage.id_users,
|
|
63
60
|
id_pages: apiPage.id_pages ?? id,
|
|
64
61
|
acl_select: apiPage.acl_select,
|
|
65
|
-
acl_insert: apiPage.acl_insert,
|
|
66
|
-
acl_update: apiPage.acl_update,
|
|
67
|
-
acl_delete: apiPage.acl_delete,
|
|
68
62
|
id_page_types: apiPage.id_page_types,
|
|
69
63
|
id_page_access_types: apiPage.id_page_access_types,
|
|
70
64
|
};
|