@selfhelp/shared 1.2.5 → 1.3.1

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +341 -326
  2. package/LICENSE +381 -381
  3. package/README.md +57 -57
  4. package/dist/index.d.mts +25 -1
  5. package/dist/index.d.ts +25 -1
  6. package/dist/index.js +2 -0
  7. package/dist/index.mjs +2 -0
  8. package/package.json +102 -102
  9. package/src/api/__tests__/endpoints.test.ts +45 -0
  10. package/src/api/endpoints.ts +2 -0
  11. package/src/api/index.ts +5 -5
  12. package/src/assets/__tests__/resolveAssetUrl.test.ts +61 -61
  13. package/src/assets/index.ts +5 -5
  14. package/src/assets/resolveAssetUrl.ts +44 -44
  15. package/src/cms-classes/__tests__/classify.test.ts +63 -63
  16. package/src/cms-classes/allow-list.ts +158 -158
  17. package/src/cms-classes/classify.ts +90 -90
  18. package/src/cms-classes/index.ts +7 -7
  19. package/src/cms-classes/remap.ts +44 -44
  20. package/src/condition/__tests__/evaluate.test.ts +54 -54
  21. package/src/condition/evaluate.ts +121 -121
  22. package/src/condition/index.ts +5 -5
  23. package/src/index.ts +25 -25
  24. package/src/interpolation/__tests__/replaceCalcedValues.test.ts +41 -41
  25. package/src/interpolation/index.ts +5 -5
  26. package/src/interpolation/replaceCalcedValues.ts +27 -27
  27. package/src/plugin-sdk/define.ts +89 -89
  28. package/src/plugin-sdk/index.ts +103 -103
  29. package/src/plugin-sdk/lock.ts +124 -124
  30. package/src/plugin-sdk/manifest.ts +307 -307
  31. package/src/plugin-sdk/realtime.ts +277 -277
  32. package/src/plugin-sdk/registry.ts +105 -105
  33. package/src/plugin-sdk/runtime-shim.ts +140 -140
  34. package/src/plugin-sdk/types.ts +315 -315
  35. package/src/plugin-sdk/version.ts +209 -209
  36. package/src/registry/__tests__/registry.test.ts +135 -135
  37. package/src/registry/index.ts +94 -94
  38. package/src/registry/styles.registry.ts +295 -295
  39. package/src/testing/__tests__/testing-kit.test.ts +228 -228
  40. package/src/testing/index.ts +483 -483
  41. package/src/theme/index.ts +6 -6
  42. package/src/theme/tailwind.ts +73 -73
  43. package/src/theme/tokens.ts +177 -177
  44. package/src/types/api/auth.ts +22 -0
  45. package/src/types/api/envelope.ts +34 -34
  46. package/src/types/api/forms.ts +76 -76
  47. package/src/types/api/index.ts +9 -9
  48. package/src/types/api/language.ts +12 -12
  49. package/src/types/api/page.ts +9 -9
  50. package/src/types/index.ts +17 -17
  51. package/src/types/mantine/common.ts +142 -142
  52. package/src/types/pages.ts +107 -107
  53. package/src/types/styles/auth.ts +184 -184
  54. package/src/types/styles/base.ts +72 -72
  55. package/src/types/styles/composite.ts +131 -131
  56. package/src/types/styles/forms.ts +439 -439
  57. package/src/types/styles/interactive.ts +165 -165
  58. package/src/types/styles/layout.ts +184 -184
  59. package/src/types/styles/media.ts +68 -68
  60. package/src/types/styles/typography.ts +118 -118
  61. package/src/types/styles/unknown.ts +220 -220
  62. package/src/utils/__tests__/transformPageData.test.ts +69 -69
  63. package/src/utils/spacing.ts +64 -64
  64. package/src/utils/transformPageData.ts +99 -99
@@ -1,69 +1,69 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- import { describe, it, expect } from 'vitest';
6
- import { transformPageData, transformPagesData } from '../transformPageData';
7
-
8
- describe('transformPageData', () => {
9
- it('maps snake_case backend keys to the consumer page shape', () => {
10
- const result = transformPageData({
11
- id_pages: 7,
12
- keyword: 'qa-home',
13
- url: '/qa-home',
14
- nav_position: 3,
15
- footer_position: null,
16
- is_headless: 1,
17
- is_system: 0,
18
- });
19
-
20
- expect(result.id).toBe(7);
21
- expect(result.id_pages).toBe(7);
22
- expect(result.keyword).toBe('qa-home');
23
- expect(result.navPosition).toBe(3);
24
- expect(result.footerPosition).toBeNull();
25
- expect(result.is_headless).toBe(true);
26
- expect(result.is_system).toBe(false);
27
- });
28
-
29
- it('prefers an explicit camelCase navPosition over the snake_case key', () => {
30
- const result = transformPageData({ id: 1, keyword: 'k', navPosition: 9, nav_position: 1 });
31
- expect(result.navPosition).toBe(9);
32
- });
33
-
34
- it('coerces boolean-like is_headless values (number/string/boolean)', () => {
35
- expect(transformPageData({ id: 1, keyword: 'k', is_headless: '1' }).is_headless).toBe(true);
36
- expect(transformPageData({ id: 1, keyword: 'k', is_headless: true }).is_headless).toBe(true);
37
- expect(transformPageData({ id: 1, keyword: 'k', is_headless: 0 }).is_headless).toBe(false);
38
- });
39
-
40
- it('recursively transforms children', () => {
41
- const result = transformPageData({
42
- id: 1,
43
- keyword: 'parent',
44
- children: [{ id_pages: 2, keyword: 'child', nav_position: 5 }],
45
- });
46
- expect(result.children).toHaveLength(1);
47
- expect(result.children[0].id).toBe(2);
48
- expect(result.children[0].navPosition).toBe(5);
49
- });
50
-
51
- it('defaults missing id/keyword safely', () => {
52
- const result = transformPageData({});
53
- expect(result.id).toBe(0);
54
- expect(result.keyword).toBe('');
55
- expect(result.children).toEqual([]);
56
- });
57
- });
58
-
59
- describe('transformPagesData', () => {
60
- it('transforms a list of pages', () => {
61
- const out = transformPagesData([{ id: 1, keyword: 'a' }, { id: 2, keyword: 'b' }]);
62
- expect(out.map((p) => p.keyword)).toEqual(['a', 'b']);
63
- });
64
-
65
- it('returns an empty array for null/undefined', () => {
66
- expect(transformPagesData(null)).toEqual([]);
67
- expect(transformPagesData(undefined)).toEqual([]);
68
- });
69
- });
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ import { describe, it, expect } from 'vitest';
6
+ import { transformPageData, transformPagesData } from '../transformPageData';
7
+
8
+ describe('transformPageData', () => {
9
+ it('maps snake_case backend keys to the consumer page shape', () => {
10
+ const result = transformPageData({
11
+ id_pages: 7,
12
+ keyword: 'qa-home',
13
+ url: '/qa-home',
14
+ nav_position: 3,
15
+ footer_position: null,
16
+ is_headless: 1,
17
+ is_system: 0,
18
+ });
19
+
20
+ expect(result.id).toBe(7);
21
+ expect(result.id_pages).toBe(7);
22
+ expect(result.keyword).toBe('qa-home');
23
+ expect(result.navPosition).toBe(3);
24
+ expect(result.footerPosition).toBeNull();
25
+ expect(result.is_headless).toBe(true);
26
+ expect(result.is_system).toBe(false);
27
+ });
28
+
29
+ it('prefers an explicit camelCase navPosition over the snake_case key', () => {
30
+ const result = transformPageData({ id: 1, keyword: 'k', navPosition: 9, nav_position: 1 });
31
+ expect(result.navPosition).toBe(9);
32
+ });
33
+
34
+ it('coerces boolean-like is_headless values (number/string/boolean)', () => {
35
+ expect(transformPageData({ id: 1, keyword: 'k', is_headless: '1' }).is_headless).toBe(true);
36
+ expect(transformPageData({ id: 1, keyword: 'k', is_headless: true }).is_headless).toBe(true);
37
+ expect(transformPageData({ id: 1, keyword: 'k', is_headless: 0 }).is_headless).toBe(false);
38
+ });
39
+
40
+ it('recursively transforms children', () => {
41
+ const result = transformPageData({
42
+ id: 1,
43
+ keyword: 'parent',
44
+ children: [{ id_pages: 2, keyword: 'child', nav_position: 5 }],
45
+ });
46
+ expect(result.children).toHaveLength(1);
47
+ expect(result.children[0].id).toBe(2);
48
+ expect(result.children[0].navPosition).toBe(5);
49
+ });
50
+
51
+ it('defaults missing id/keyword safely', () => {
52
+ const result = transformPageData({});
53
+ expect(result.id).toBe(0);
54
+ expect(result.keyword).toBe('');
55
+ expect(result.children).toEqual([]);
56
+ });
57
+ });
58
+
59
+ describe('transformPagesData', () => {
60
+ it('transforms a list of pages', () => {
61
+ const out = transformPagesData([{ id: 1, keyword: 'a' }, { id: 2, keyword: 'b' }]);
62
+ expect(out.map((p) => p.keyword)).toEqual(['a', 'b']);
63
+ });
64
+
65
+ it('returns an empty array for null/undefined', () => {
66
+ expect(transformPagesData(null)).toEqual([]);
67
+ expect(transformPagesData(undefined)).toEqual([]);
68
+ });
69
+ });
@@ -1,64 +1,64 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- /**
6
- * Parse the unified `mantine_spacing_margin_padding` field. The CMS
7
- * stores this as a JSON string with optional keys for each side:
8
- *
9
- * { "mt": "md", "mb": "lg", "ms": "0", "me": "auto",
10
- * "pt": "sm", "pb": "sm", "ps": "md", "pe": "md" }
11
- *
12
- * Empty/null/missing values are filtered out so downstream renderers
13
- * can call e.g. `Object.entries(parsed)` without seeing falsy entries.
14
- */
15
-
16
- export interface ISpacingValue {
17
- mt?: string;
18
- mb?: string;
19
- ms?: string;
20
- me?: string;
21
- pt?: string;
22
- pb?: string;
23
- ps?: string;
24
- pe?: string;
25
- }
26
-
27
- const ALLOWED_KEYS: ReadonlyArray<keyof ISpacingValue> = ['mt', 'mb', 'ms', 'me', 'pt', 'pb', 'ps', 'pe'];
28
-
29
- export function parseSpacing(raw: string | null | undefined): ISpacingValue {
30
- if (!raw) return {};
31
- let parsed: unknown;
32
- try {
33
- parsed = JSON.parse(raw);
34
- } catch {
35
- return {};
36
- }
37
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
38
-
39
- const result: ISpacingValue = {};
40
- const obj = parsed as Record<string, unknown>;
41
- for (const key of ALLOWED_KEYS) {
42
- const value = obj[key];
43
- if (value === undefined || value === null || value === '') continue;
44
- result[key] = String(value);
45
- }
46
- return result;
47
- }
48
-
49
- /**
50
- * Convert spacing values to Tailwind classes via `SPACING_TO_TAILWIND`
51
- * mapping. Numeric values pass through as `mt-{n}` etc.
52
- */
53
- export function spacingToClasses(
54
- spacing: ISpacingValue,
55
- spacingToTailwind: Record<string, string>
56
- ): string[] {
57
- const classes: string[] = [];
58
- for (const [key, raw] of Object.entries(spacing)) {
59
- if (!raw) continue;
60
- const tail = spacingToTailwind[raw] ?? raw;
61
- classes.push(`${key}-${tail}`);
62
- }
63
- return classes;
64
- }
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Parse the unified `mantine_spacing_margin_padding` field. The CMS
7
+ * stores this as a JSON string with optional keys for each side:
8
+ *
9
+ * { "mt": "md", "mb": "lg", "ms": "0", "me": "auto",
10
+ * "pt": "sm", "pb": "sm", "ps": "md", "pe": "md" }
11
+ *
12
+ * Empty/null/missing values are filtered out so downstream renderers
13
+ * can call e.g. `Object.entries(parsed)` without seeing falsy entries.
14
+ */
15
+
16
+ export interface ISpacingValue {
17
+ mt?: string;
18
+ mb?: string;
19
+ ms?: string;
20
+ me?: string;
21
+ pt?: string;
22
+ pb?: string;
23
+ ps?: string;
24
+ pe?: string;
25
+ }
26
+
27
+ const ALLOWED_KEYS: ReadonlyArray<keyof ISpacingValue> = ['mt', 'mb', 'ms', 'me', 'pt', 'pb', 'ps', 'pe'];
28
+
29
+ export function parseSpacing(raw: string | null | undefined): ISpacingValue {
30
+ if (!raw) return {};
31
+ let parsed: unknown;
32
+ try {
33
+ parsed = JSON.parse(raw);
34
+ } catch {
35
+ return {};
36
+ }
37
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
38
+
39
+ const result: ISpacingValue = {};
40
+ const obj = parsed as Record<string, unknown>;
41
+ for (const key of ALLOWED_KEYS) {
42
+ const value = obj[key];
43
+ if (value === undefined || value === null || value === '') continue;
44
+ result[key] = String(value);
45
+ }
46
+ return result;
47
+ }
48
+
49
+ /**
50
+ * Convert spacing values to Tailwind classes via `SPACING_TO_TAILWIND`
51
+ * mapping. Numeric values pass through as `mt-{n}` etc.
52
+ */
53
+ export function spacingToClasses(
54
+ spacing: ISpacingValue,
55
+ spacingToTailwind: Record<string, string>
56
+ ): string[] {
57
+ const classes: string[] = [];
58
+ for (const [key, raw] of Object.entries(spacing)) {
59
+ if (!raw) continue;
60
+ const tail = spacingToTailwind[raw] ?? raw;
61
+ classes.push(`${key}-${tail}`);
62
+ }
63
+ return classes;
64
+ }
@@ -1,99 +1,99 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- /**
6
- * Backend → frontend page-shape transformer.
7
- *
8
- * The Symfony backend projects pages with canonical `snake_case` keys
9
- * (`nav_position`, `footer_position`, `id_pages`, `id_parent_page`,
10
- * `id_page_types`, `id_page_access_types`). The web and mobile apps
11
- * prefer camelCase (`navPosition`, `footerPosition`) at the type-system
12
- * level. This helper bridges the two so consumers don't have to know
13
- * about the snake-case wire format.
14
- *
15
- * It is the single source of truth for the conversion. Web frontend
16
- * and mobile both import it from `@selfhelp/shared`.
17
- *
18
- * NOTE: Pre-release breaking-change cutover (`db_naming_cutover_*`)
19
- * removed all legacy aliases (`parent`, `id_type`,
20
- * `id_pageAccessTypes`). The backend is the single source of canonical
21
- * keys; the transformer no longer accepts the legacy shapes.
22
- */
23
-
24
- import type { IPageItem } from '../types/pages';
25
-
26
- interface IRawPage {
27
- id?: number;
28
- id_pages?: number;
29
- keyword?: string;
30
- url?: string | null;
31
- id_parent_page?: number | null;
32
- parent_page_id?: number | null;
33
- is_headless?: number | boolean | null;
34
- nav_position?: number | null;
35
- navPosition?: number | null;
36
- footer_position?: number | null;
37
- footerPosition?: number | null;
38
- is_system?: number | boolean | null;
39
- is_open_access?: number | boolean | null;
40
- title?: string | null;
41
- description?: string | null;
42
- id_users?: number;
43
- acl_select?: 0 | 1;
44
- acl_insert?: 0 | 1;
45
- acl_update?: 0 | 1;
46
- acl_delete?: 0 | 1;
47
- id_page_types?: number;
48
- id_page_access_types?: number;
49
- children?: IRawPage[];
50
- [key: string]: unknown;
51
- }
52
-
53
- function toBool(value: unknown): boolean {
54
- if (value === null || value === undefined) return false;
55
- if (typeof value === 'boolean') return value;
56
- if (typeof value === 'number') return value === 1;
57
- if (typeof value === 'string') return value === '1' || value === 'true';
58
- return false;
59
- }
60
-
61
- export function transformPageData(apiPage: IRawPage): IPageItem {
62
- const id = apiPage.id ?? apiPage.id_pages ?? 0;
63
- const parentPageId = apiPage.parent_page_id ?? apiPage.id_parent_page ?? null;
64
- return {
65
- id,
66
- keyword: apiPage.keyword ?? '',
67
- url: apiPage.url ?? null,
68
- parent_page_id: parentPageId,
69
- is_headless: toBool(apiPage.is_headless),
70
- navPosition:
71
- apiPage.navPosition !== undefined ? apiPage.navPosition : (apiPage.nav_position ?? null),
72
- footerPosition:
73
- apiPage.footerPosition !== undefined
74
- ? apiPage.footerPosition
75
- : (apiPage.footer_position ?? null),
76
- is_system: apiPage.is_system !== undefined ? toBool(apiPage.is_system) : undefined,
77
- title: apiPage.title ?? null,
78
- description: apiPage.description ?? null,
79
- children: apiPage.children?.map(transformPageData) ?? [],
80
-
81
- id_users: apiPage.id_users,
82
- id_pages: apiPage.id_pages ?? id,
83
- acl_select: apiPage.acl_select,
84
- acl_insert: apiPage.acl_insert,
85
- acl_update: apiPage.acl_update,
86
- acl_delete: apiPage.acl_delete,
87
- id_page_types: apiPage.id_page_types,
88
- id_page_access_types: apiPage.id_page_access_types,
89
- };
90
- }
91
-
92
- /**
93
- * Same as `transformPageData` but for an array. Convenience helper
94
- * because almost every consumer fetches a list of pages.
95
- */
96
- export function transformPagesData(pages: IRawPage[] | null | undefined): IPageItem[] {
97
- if (!pages) return [];
98
- return pages.map(transformPageData);
99
- }
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Backend → frontend page-shape transformer.
7
+ *
8
+ * The Symfony backend projects pages with canonical `snake_case` keys
9
+ * (`nav_position`, `footer_position`, `id_pages`, `id_parent_page`,
10
+ * `id_page_types`, `id_page_access_types`). The web and mobile apps
11
+ * prefer camelCase (`navPosition`, `footerPosition`) at the type-system
12
+ * level. This helper bridges the two so consumers don't have to know
13
+ * about the snake-case wire format.
14
+ *
15
+ * It is the single source of truth for the conversion. Web frontend
16
+ * and mobile both import it from `@selfhelp/shared`.
17
+ *
18
+ * NOTE: Pre-release breaking-change cutover (`db_naming_cutover_*`)
19
+ * removed all legacy aliases (`parent`, `id_type`,
20
+ * `id_pageAccessTypes`). The backend is the single source of canonical
21
+ * keys; the transformer no longer accepts the legacy shapes.
22
+ */
23
+
24
+ import type { IPageItem } from '../types/pages';
25
+
26
+ interface IRawPage {
27
+ id?: number;
28
+ id_pages?: number;
29
+ keyword?: string;
30
+ url?: string | null;
31
+ id_parent_page?: number | null;
32
+ parent_page_id?: number | null;
33
+ is_headless?: number | boolean | null;
34
+ nav_position?: number | null;
35
+ navPosition?: number | null;
36
+ footer_position?: number | null;
37
+ footerPosition?: number | null;
38
+ is_system?: number | boolean | null;
39
+ is_open_access?: number | boolean | null;
40
+ title?: string | null;
41
+ description?: string | null;
42
+ id_users?: number;
43
+ acl_select?: 0 | 1;
44
+ acl_insert?: 0 | 1;
45
+ acl_update?: 0 | 1;
46
+ acl_delete?: 0 | 1;
47
+ id_page_types?: number;
48
+ id_page_access_types?: number;
49
+ children?: IRawPage[];
50
+ [key: string]: unknown;
51
+ }
52
+
53
+ function toBool(value: unknown): boolean {
54
+ if (value === null || value === undefined) return false;
55
+ if (typeof value === 'boolean') return value;
56
+ if (typeof value === 'number') return value === 1;
57
+ if (typeof value === 'string') return value === '1' || value === 'true';
58
+ return false;
59
+ }
60
+
61
+ export function transformPageData(apiPage: IRawPage): IPageItem {
62
+ const id = apiPage.id ?? apiPage.id_pages ?? 0;
63
+ const parentPageId = apiPage.parent_page_id ?? apiPage.id_parent_page ?? null;
64
+ return {
65
+ id,
66
+ keyword: apiPage.keyword ?? '',
67
+ url: apiPage.url ?? null,
68
+ parent_page_id: parentPageId,
69
+ is_headless: toBool(apiPage.is_headless),
70
+ navPosition:
71
+ apiPage.navPosition !== undefined ? apiPage.navPosition : (apiPage.nav_position ?? null),
72
+ footerPosition:
73
+ apiPage.footerPosition !== undefined
74
+ ? apiPage.footerPosition
75
+ : (apiPage.footer_position ?? null),
76
+ is_system: apiPage.is_system !== undefined ? toBool(apiPage.is_system) : undefined,
77
+ title: apiPage.title ?? null,
78
+ description: apiPage.description ?? null,
79
+ children: apiPage.children?.map(transformPageData) ?? [],
80
+
81
+ id_users: apiPage.id_users,
82
+ id_pages: apiPage.id_pages ?? id,
83
+ acl_select: apiPage.acl_select,
84
+ acl_insert: apiPage.acl_insert,
85
+ acl_update: apiPage.acl_update,
86
+ acl_delete: apiPage.acl_delete,
87
+ id_page_types: apiPage.id_page_types,
88
+ id_page_access_types: apiPage.id_page_access_types,
89
+ };
90
+ }
91
+
92
+ /**
93
+ * Same as `transformPageData` but for an array. Convenience helper
94
+ * because almost every consumer fetches a list of pages.
95
+ */
96
+ export function transformPagesData(pages: IRawPage[] | null | undefined): IPageItem[] {
97
+ if (!pages) return [];
98
+ return pages.map(transformPageData);
99
+ }