@selfhelp/shared 1.28.0 → 1.30.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,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
+ });
package/src/sync/index.ts CHANGED
@@ -4,3 +4,4 @@ SPDX-License-Identifier: MPL-2.0
4
4
  */
5
5
  export * from './types';
6
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
+ }
package/src/sync/types.ts CHANGED
@@ -92,6 +92,12 @@ export interface IAuthEventsBootstrap {
92
92
  export const PAGE_VERSION_REQUIRED_ERROR = 'page_version_required' as const;
93
93
  export const STALE_PAGE_VERSION_ERROR = 'stale_page_version' as const;
94
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;
95
101
 
96
102
  /**
97
103
  * Draft version of a page (C8). `GET /admin/pages/{page_id}` returns it in its
@@ -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
+ }>;