@selfhelp/shared 1.3.0 → 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 (58) hide show
  1. package/CHANGELOG.md +341 -341
  2. package/LICENSE +381 -381
  3. package/README.md +57 -57
  4. package/package.json +102 -102
  5. package/src/api/__tests__/endpoints.test.ts +45 -45
  6. package/src/api/index.ts +5 -5
  7. package/src/assets/__tests__/resolveAssetUrl.test.ts +61 -61
  8. package/src/assets/index.ts +5 -5
  9. package/src/assets/resolveAssetUrl.ts +44 -44
  10. package/src/cms-classes/__tests__/classify.test.ts +63 -63
  11. package/src/cms-classes/allow-list.ts +158 -158
  12. package/src/cms-classes/classify.ts +90 -90
  13. package/src/cms-classes/index.ts +7 -7
  14. package/src/cms-classes/remap.ts +44 -44
  15. package/src/condition/__tests__/evaluate.test.ts +54 -54
  16. package/src/condition/evaluate.ts +121 -121
  17. package/src/condition/index.ts +5 -5
  18. package/src/index.ts +25 -25
  19. package/src/interpolation/__tests__/replaceCalcedValues.test.ts +41 -41
  20. package/src/interpolation/index.ts +5 -5
  21. package/src/interpolation/replaceCalcedValues.ts +27 -27
  22. package/src/plugin-sdk/define.ts +89 -89
  23. package/src/plugin-sdk/index.ts +103 -103
  24. package/src/plugin-sdk/lock.ts +124 -124
  25. package/src/plugin-sdk/manifest.ts +307 -307
  26. package/src/plugin-sdk/realtime.ts +277 -277
  27. package/src/plugin-sdk/registry.ts +105 -105
  28. package/src/plugin-sdk/runtime-shim.ts +140 -140
  29. package/src/plugin-sdk/types.ts +315 -315
  30. package/src/plugin-sdk/version.ts +209 -209
  31. package/src/registry/__tests__/registry.test.ts +135 -135
  32. package/src/registry/index.ts +94 -94
  33. package/src/registry/styles.registry.ts +295 -295
  34. package/src/testing/__tests__/testing-kit.test.ts +228 -228
  35. package/src/testing/index.ts +483 -483
  36. package/src/theme/index.ts +6 -6
  37. package/src/theme/tailwind.ts +73 -73
  38. package/src/theme/tokens.ts +177 -177
  39. package/src/types/api/envelope.ts +34 -34
  40. package/src/types/api/forms.ts +76 -76
  41. package/src/types/api/index.ts +9 -9
  42. package/src/types/api/language.ts +12 -12
  43. package/src/types/api/page.ts +9 -9
  44. package/src/types/index.ts +17 -17
  45. package/src/types/mantine/common.ts +142 -142
  46. package/src/types/pages.ts +107 -107
  47. package/src/types/styles/auth.ts +184 -184
  48. package/src/types/styles/base.ts +72 -72
  49. package/src/types/styles/composite.ts +131 -131
  50. package/src/types/styles/forms.ts +439 -439
  51. package/src/types/styles/interactive.ts +165 -165
  52. package/src/types/styles/layout.ts +184 -184
  53. package/src/types/styles/media.ts +68 -68
  54. package/src/types/styles/typography.ts +118 -118
  55. package/src/types/styles/unknown.ts +220 -220
  56. package/src/utils/__tests__/transformPageData.test.ts +69 -69
  57. package/src/utils/spacing.ts +64 -64
  58. package/src/utils/transformPageData.ts +99 -99
@@ -1,209 +1,209 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- /**
6
- * SDK contract version. Plugins declare which API they target in
7
- * `plugin.json` under `pluginApiVersion`. The host installer compares
8
- * declarations against this constant.
9
- *
10
- * Versioning rules:
11
- * - patch (1.0.0 -> 1.0.1) — additive SDK fix, no contract change.
12
- * - minor (1.0.x -> 1.1.0) — additive SDK feature.
13
- * - major (1.x -> 2.0) — breaking SDK change.
14
- *
15
- * Independent from the npm `version` of `@selfhelp/shared`.
16
- */
17
- export const PLUGIN_API_VERSION = '1.2' as const;
18
-
19
- export interface ISemver {
20
- major: number;
21
- minor: number;
22
- patch: number;
23
- prerelease?: string;
24
- }
25
-
26
- const SEMVER_REGEX = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/;
27
-
28
- export function parseSemver(version: string): ISemver {
29
- const match = SEMVER_REGEX.exec(version);
30
- if (!match) {
31
- throw new Error(`[plugin-sdk] not a valid semver: '${version}'.`);
32
- }
33
- return {
34
- major: Number(match[1]),
35
- minor: Number(match[2]),
36
- patch: Number(match[3]),
37
- prerelease: match[4],
38
- };
39
- }
40
-
41
- export function compareSemver(a: string, b: string): number {
42
- const pa = parseSemver(a);
43
- const pb = parseSemver(b);
44
- if (pa.major !== pb.major) return pa.major - pb.major;
45
- if (pa.minor !== pb.minor) return pa.minor - pb.minor;
46
- if (pa.patch !== pb.patch) return pa.patch - pb.patch;
47
- if (pa.prerelease && pb.prerelease) return pa.prerelease.localeCompare(pb.prerelease);
48
- if (pa.prerelease) return -1;
49
- if (pb.prerelease) return 1;
50
- return 0;
51
- }
52
-
53
- /**
54
- * Very small npm-style range matcher supporting `=`, `>=`, `<=`, `>`,
55
- * `<`, `^`, `~`, and the simple multi-clause syntax used by
56
- * `plugin.json` compatibility fields. Not a full semver implementation;
57
- * the backend uses Composer's resolver for actual install-time checks.
58
- */
59
- export function satisfiesSemverRange(version: string, range: string): boolean {
60
- const v = parseSemver(version);
61
- return range
62
- .split('||')
63
- .some((clause) => clause
64
- .trim()
65
- .split(/\s+/)
66
- .every((part) => matchSingle(v, part.trim())));
67
- }
68
-
69
- function matchSingle(v: ISemver, part: string): boolean {
70
- if (!part) return true;
71
- if (part === '*' || part === 'x') return true;
72
- if (part.startsWith('^')) {
73
- const target = parseSemver(part.slice(1));
74
- if (target.major === 0) {
75
- if (target.minor === 0) return v.major === 0 && v.minor === 0 && v.patch === target.patch;
76
- return v.major === 0 && v.minor === target.minor && (v.patch > target.patch || (v.patch === target.patch && !v.prerelease));
77
- }
78
- return v.major === target.major && compareSemverObj(v, target) >= 0;
79
- }
80
- if (part.startsWith('~')) {
81
- const target = parseSemver(part.slice(1));
82
- return v.major === target.major && v.minor === target.minor && v.patch >= target.patch;
83
- }
84
- if (part.startsWith('>=')) return compareSemverObj(v, parseSemver(part.slice(2).trim())) >= 0;
85
- if (part.startsWith('<=')) return compareSemverObj(v, parseSemver(part.slice(2).trim())) <= 0;
86
- if (part.startsWith('>')) return compareSemverObj(v, parseSemver(part.slice(1).trim())) > 0;
87
- if (part.startsWith('<')) return compareSemverObj(v, parseSemver(part.slice(1).trim())) < 0;
88
- if (part.startsWith('=')) return compareSemverObj(v, parseSemver(part.slice(1).trim())) === 0;
89
- return compareSemverObj(v, parseSemver(part)) === 0;
90
- }
91
-
92
- function compareSemverObj(a: ISemver, b: ISemver): number {
93
- if (a.major !== b.major) return a.major - b.major;
94
- if (a.minor !== b.minor) return a.minor - b.minor;
95
- if (a.patch !== b.patch) return a.patch - b.patch;
96
- if (a.prerelease && b.prerelease) return a.prerelease.localeCompare(b.prerelease);
97
- if (a.prerelease) return -1;
98
- if (b.prerelease) return 1;
99
- return 0;
100
- }
101
-
102
- /**
103
- * Non-throwing version of {@see assertPluginApiVersion} — returns true
104
- * when the plugin's declared `pluginApiVersion` is compatible with the
105
- * host SDK. Used by the frontend `PluginRuntime` so an incompatible
106
- * plugin is skipped (and logged) instead of crashing the host shell.
107
- */
108
- export function isPluginApiCompatible(required: string, host: string = PLUGIN_API_VERSION): boolean {
109
- const [reqMajor, reqMinor] = required.split('.').map((p) => Number(p));
110
- const [hostMajor, hostMinor] = host.split('.').map((p) => Number(p));
111
- if (Number.isNaN(reqMajor) || Number.isNaN(reqMinor) || Number.isNaN(hostMajor) || Number.isNaN(hostMinor)) {
112
- return false;
113
- }
114
- if (hostMajor !== reqMajor) return false;
115
- return hostMinor >= reqMinor;
116
- }
117
-
118
- /**
119
- * Throw if the plugin's required `pluginApiVersion` is not compatible
120
- * with this SDK's `PLUGIN_API_VERSION`. SDK promises:
121
- * - same major required.
122
- * - host minor >= plugin's required minor.
123
- */
124
- export function assertPluginApiVersion(required: string): void {
125
- const [reqMajor, reqMinor] = required.split('.').map((p) => Number(p));
126
- const [hostMajor, hostMinor] = PLUGIN_API_VERSION.split('.').map((p) => Number(p));
127
- if (Number.isNaN(reqMajor) || Number.isNaN(reqMinor)) {
128
- throw new Error(`[plugin-sdk] invalid pluginApiVersion '${required}'.`);
129
- }
130
- if (hostMajor !== reqMajor) {
131
- throw new Error(
132
- `[plugin-sdk] incompatible pluginApiVersion: plugin wants '${required}' but host is '${PLUGIN_API_VERSION}'.`,
133
- );
134
- }
135
- if (hostMinor < reqMinor) {
136
- throw new Error(
137
- `[plugin-sdk] host SDK too old for plugin: plugin wants '${required}', host is '${PLUGIN_API_VERSION}'.`,
138
- );
139
- }
140
- }
141
-
142
- /**
143
- * Throw if the running CMS version is not in the plugin's declared
144
- * compatibility range.
145
- */
146
- export function assertCmsCompatibility(cmsVersion: string, range: string): void {
147
- if (!satisfiesSemverRange(cmsVersion, range)) {
148
- throw new Error(
149
- `[plugin-sdk] CMS version '${cmsVersion}' does not satisfy plugin compatibility range '${range}'.`,
150
- );
151
- }
152
- }
153
-
154
- /**
155
- * Enforce the plugin version-semantics rule:
156
- * - patch (1.0.0 -> 1.0.1) -- no DB change, no migration shipped.
157
- * - minor (1.0.x -> 1.1.0) -- always carries a DB change/migration.
158
- * - major (1.x -> 2.x) -- breaking change.
159
- *
160
- * The host installer + plugin CI call this with the lock-file `version`
161
- * and the candidate `version` to verify that what is being shipped
162
- * matches the semver bump.
163
- */
164
- export function assertPluginVersionSemantics(
165
- prevVersion: string | undefined,
166
- nextVersion: string,
167
- options: { hasMigration: boolean },
168
- ): void {
169
- const next = parseSemver(nextVersion);
170
-
171
- if (!prevVersion) {
172
- // first install — any version, but if hasMigration=false and
173
- // it claims to be 0.0.x we cannot really verify the rule.
174
- return;
175
- }
176
-
177
- const prev = parseSemver(prevVersion);
178
-
179
- const bumpedMajor = next.major > prev.major;
180
- const bumpedMinor = next.minor > prev.minor && next.major === prev.major;
181
- const bumpedPatch = next.patch > prev.patch && next.minor === prev.minor && next.major === prev.major;
182
-
183
- if (bumpedMajor) {
184
- // breaking — migration optional but allowed.
185
- return;
186
- }
187
- if (bumpedMinor) {
188
- if (!options.hasMigration) {
189
- throw new Error(
190
- `[plugin-sdk] plugin minor bump (${prevVersion} -> ${nextVersion}) must ship a DB migration.`,
191
- );
192
- }
193
- return;
194
- }
195
- if (bumpedPatch) {
196
- if (options.hasMigration) {
197
- throw new Error(
198
- `[plugin-sdk] plugin patch bump (${prevVersion} -> ${nextVersion}) must not ship a DB migration.`,
199
- );
200
- }
201
- return;
202
- }
203
-
204
- if (compareSemver(nextVersion, prevVersion) <= 0) {
205
- throw new Error(
206
- `[plugin-sdk] plugin version did not increase: prev '${prevVersion}', next '${nextVersion}'.`,
207
- );
208
- }
209
- }
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * SDK contract version. Plugins declare which API they target in
7
+ * `plugin.json` under `pluginApiVersion`. The host installer compares
8
+ * declarations against this constant.
9
+ *
10
+ * Versioning rules:
11
+ * - patch (1.0.0 -> 1.0.1) — additive SDK fix, no contract change.
12
+ * - minor (1.0.x -> 1.1.0) — additive SDK feature.
13
+ * - major (1.x -> 2.0) — breaking SDK change.
14
+ *
15
+ * Independent from the npm `version` of `@selfhelp/shared`.
16
+ */
17
+ export const PLUGIN_API_VERSION = '1.2' as const;
18
+
19
+ export interface ISemver {
20
+ major: number;
21
+ minor: number;
22
+ patch: number;
23
+ prerelease?: string;
24
+ }
25
+
26
+ const SEMVER_REGEX = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/;
27
+
28
+ export function parseSemver(version: string): ISemver {
29
+ const match = SEMVER_REGEX.exec(version);
30
+ if (!match) {
31
+ throw new Error(`[plugin-sdk] not a valid semver: '${version}'.`);
32
+ }
33
+ return {
34
+ major: Number(match[1]),
35
+ minor: Number(match[2]),
36
+ patch: Number(match[3]),
37
+ prerelease: match[4],
38
+ };
39
+ }
40
+
41
+ export function compareSemver(a: string, b: string): number {
42
+ const pa = parseSemver(a);
43
+ const pb = parseSemver(b);
44
+ if (pa.major !== pb.major) return pa.major - pb.major;
45
+ if (pa.minor !== pb.minor) return pa.minor - pb.minor;
46
+ if (pa.patch !== pb.patch) return pa.patch - pb.patch;
47
+ if (pa.prerelease && pb.prerelease) return pa.prerelease.localeCompare(pb.prerelease);
48
+ if (pa.prerelease) return -1;
49
+ if (pb.prerelease) return 1;
50
+ return 0;
51
+ }
52
+
53
+ /**
54
+ * Very small npm-style range matcher supporting `=`, `>=`, `<=`, `>`,
55
+ * `<`, `^`, `~`, and the simple multi-clause syntax used by
56
+ * `plugin.json` compatibility fields. Not a full semver implementation;
57
+ * the backend uses Composer's resolver for actual install-time checks.
58
+ */
59
+ export function satisfiesSemverRange(version: string, range: string): boolean {
60
+ const v = parseSemver(version);
61
+ return range
62
+ .split('||')
63
+ .some((clause) => clause
64
+ .trim()
65
+ .split(/\s+/)
66
+ .every((part) => matchSingle(v, part.trim())));
67
+ }
68
+
69
+ function matchSingle(v: ISemver, part: string): boolean {
70
+ if (!part) return true;
71
+ if (part === '*' || part === 'x') return true;
72
+ if (part.startsWith('^')) {
73
+ const target = parseSemver(part.slice(1));
74
+ if (target.major === 0) {
75
+ if (target.minor === 0) return v.major === 0 && v.minor === 0 && v.patch === target.patch;
76
+ return v.major === 0 && v.minor === target.minor && (v.patch > target.patch || (v.patch === target.patch && !v.prerelease));
77
+ }
78
+ return v.major === target.major && compareSemverObj(v, target) >= 0;
79
+ }
80
+ if (part.startsWith('~')) {
81
+ const target = parseSemver(part.slice(1));
82
+ return v.major === target.major && v.minor === target.minor && v.patch >= target.patch;
83
+ }
84
+ if (part.startsWith('>=')) return compareSemverObj(v, parseSemver(part.slice(2).trim())) >= 0;
85
+ if (part.startsWith('<=')) return compareSemverObj(v, parseSemver(part.slice(2).trim())) <= 0;
86
+ if (part.startsWith('>')) return compareSemverObj(v, parseSemver(part.slice(1).trim())) > 0;
87
+ if (part.startsWith('<')) return compareSemverObj(v, parseSemver(part.slice(1).trim())) < 0;
88
+ if (part.startsWith('=')) return compareSemverObj(v, parseSemver(part.slice(1).trim())) === 0;
89
+ return compareSemverObj(v, parseSemver(part)) === 0;
90
+ }
91
+
92
+ function compareSemverObj(a: ISemver, b: ISemver): number {
93
+ if (a.major !== b.major) return a.major - b.major;
94
+ if (a.minor !== b.minor) return a.minor - b.minor;
95
+ if (a.patch !== b.patch) return a.patch - b.patch;
96
+ if (a.prerelease && b.prerelease) return a.prerelease.localeCompare(b.prerelease);
97
+ if (a.prerelease) return -1;
98
+ if (b.prerelease) return 1;
99
+ return 0;
100
+ }
101
+
102
+ /**
103
+ * Non-throwing version of {@see assertPluginApiVersion} — returns true
104
+ * when the plugin's declared `pluginApiVersion` is compatible with the
105
+ * host SDK. Used by the frontend `PluginRuntime` so an incompatible
106
+ * plugin is skipped (and logged) instead of crashing the host shell.
107
+ */
108
+ export function isPluginApiCompatible(required: string, host: string = PLUGIN_API_VERSION): boolean {
109
+ const [reqMajor, reqMinor] = required.split('.').map((p) => Number(p));
110
+ const [hostMajor, hostMinor] = host.split('.').map((p) => Number(p));
111
+ if (Number.isNaN(reqMajor) || Number.isNaN(reqMinor) || Number.isNaN(hostMajor) || Number.isNaN(hostMinor)) {
112
+ return false;
113
+ }
114
+ if (hostMajor !== reqMajor) return false;
115
+ return hostMinor >= reqMinor;
116
+ }
117
+
118
+ /**
119
+ * Throw if the plugin's required `pluginApiVersion` is not compatible
120
+ * with this SDK's `PLUGIN_API_VERSION`. SDK promises:
121
+ * - same major required.
122
+ * - host minor >= plugin's required minor.
123
+ */
124
+ export function assertPluginApiVersion(required: string): void {
125
+ const [reqMajor, reqMinor] = required.split('.').map((p) => Number(p));
126
+ const [hostMajor, hostMinor] = PLUGIN_API_VERSION.split('.').map((p) => Number(p));
127
+ if (Number.isNaN(reqMajor) || Number.isNaN(reqMinor)) {
128
+ throw new Error(`[plugin-sdk] invalid pluginApiVersion '${required}'.`);
129
+ }
130
+ if (hostMajor !== reqMajor) {
131
+ throw new Error(
132
+ `[plugin-sdk] incompatible pluginApiVersion: plugin wants '${required}' but host is '${PLUGIN_API_VERSION}'.`,
133
+ );
134
+ }
135
+ if (hostMinor < reqMinor) {
136
+ throw new Error(
137
+ `[plugin-sdk] host SDK too old for plugin: plugin wants '${required}', host is '${PLUGIN_API_VERSION}'.`,
138
+ );
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Throw if the running CMS version is not in the plugin's declared
144
+ * compatibility range.
145
+ */
146
+ export function assertCmsCompatibility(cmsVersion: string, range: string): void {
147
+ if (!satisfiesSemverRange(cmsVersion, range)) {
148
+ throw new Error(
149
+ `[plugin-sdk] CMS version '${cmsVersion}' does not satisfy plugin compatibility range '${range}'.`,
150
+ );
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Enforce the plugin version-semantics rule:
156
+ * - patch (1.0.0 -> 1.0.1) -- no DB change, no migration shipped.
157
+ * - minor (1.0.x -> 1.1.0) -- always carries a DB change/migration.
158
+ * - major (1.x -> 2.x) -- breaking change.
159
+ *
160
+ * The host installer + plugin CI call this with the lock-file `version`
161
+ * and the candidate `version` to verify that what is being shipped
162
+ * matches the semver bump.
163
+ */
164
+ export function assertPluginVersionSemantics(
165
+ prevVersion: string | undefined,
166
+ nextVersion: string,
167
+ options: { hasMigration: boolean },
168
+ ): void {
169
+ const next = parseSemver(nextVersion);
170
+
171
+ if (!prevVersion) {
172
+ // first install — any version, but if hasMigration=false and
173
+ // it claims to be 0.0.x we cannot really verify the rule.
174
+ return;
175
+ }
176
+
177
+ const prev = parseSemver(prevVersion);
178
+
179
+ const bumpedMajor = next.major > prev.major;
180
+ const bumpedMinor = next.minor > prev.minor && next.major === prev.major;
181
+ const bumpedPatch = next.patch > prev.patch && next.minor === prev.minor && next.major === prev.major;
182
+
183
+ if (bumpedMajor) {
184
+ // breaking — migration optional but allowed.
185
+ return;
186
+ }
187
+ if (bumpedMinor) {
188
+ if (!options.hasMigration) {
189
+ throw new Error(
190
+ `[plugin-sdk] plugin minor bump (${prevVersion} -> ${nextVersion}) must ship a DB migration.`,
191
+ );
192
+ }
193
+ return;
194
+ }
195
+ if (bumpedPatch) {
196
+ if (options.hasMigration) {
197
+ throw new Error(
198
+ `[plugin-sdk] plugin patch bump (${prevVersion} -> ${nextVersion}) must not ship a DB migration.`,
199
+ );
200
+ }
201
+ return;
202
+ }
203
+
204
+ if (compareSemver(nextVersion, prevVersion) <= 0) {
205
+ throw new Error(
206
+ `[plugin-sdk] plugin version did not increase: prev '${prevVersion}', next '${nextVersion}'.`,
207
+ );
208
+ }
209
+ }