@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
package/README.md CHANGED
@@ -1,57 +1,57 @@
1
- <!--
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- -->
5
-
6
- # @selfhelp/shared
7
-
8
- The shared TypeScript foundation for the SelfHelp ecosystem. It is the single source of truth that keeps the web frontend (`sh-selfhelp_frontend`) and mobile app (`sh-selfhelp_mobile`) aligned on:
9
-
10
- - CMS page and style types
11
- - API request/response contracts
12
- - shared theme tokens and a Tailwind preset
13
- - condition and interpolation helpers that mirror backend behavior
14
- - CMS class allow-listing and asset URL utilities
15
-
16
- ## Install
17
-
18
- ```bash
19
- npm install @selfhelp/shared
20
- ```
21
-
22
- For local development against sibling repositories you can use a file dependency:
23
-
24
- ```json
25
- { "dependencies": { "@selfhelp/shared": "file:../sh-selfhelp_shared" } }
26
- ```
27
-
28
- ## Quick usage
29
-
30
- ```ts
31
- import { ENDPOINTS, replaceCalcedValues, resolveAssetUrl } from '@selfhelp/shared';
32
- import type { IPageContent, IUserDataResponse } from '@selfhelp/shared';
33
-
34
- import { STYLE_REGISTRY } from '@selfhelp/shared/registry';
35
- import { THEME_TOKENS } from '@selfhelp/shared/theme';
36
- import tailwindPreset from '@selfhelp/shared/tailwind';
37
- ```
38
-
39
- The package exposes a root export plus the `registry`, `theme`, `tailwind`, `plugin-sdk`, and `testing` subpaths.
40
-
41
- ## Build
42
-
43
- ```bash
44
- npm install
45
- npm run build
46
- ```
47
-
48
- ## Documentation
49
-
50
- Full documentation lives in [docs/](docs/README.md):
51
-
52
- - Public exports, types, and contracts: [docs/reference/exports.md](docs/reference/exports.md)
53
- - Release and versioning: [docs/developer/release-and-versioning.md](docs/developer/release-and-versioning.md)
54
- - Testing and the coverage gate: [docs/developer/testing.md](docs/developer/testing.md)
55
- - Extending the package: [docs/developer/extending.md](docs/developer/extending.md)
56
-
57
- Release notes are in [CHANGELOG.md](CHANGELOG.md). Licensed under [MPL-2.0](LICENSE).
1
+ <!--
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ -->
5
+
6
+ # @selfhelp/shared
7
+
8
+ The shared TypeScript foundation for the SelfHelp ecosystem. It is the single source of truth that keeps the web frontend (`sh-selfhelp_frontend`) and mobile app (`sh-selfhelp_mobile`) aligned on:
9
+
10
+ - CMS page and style types
11
+ - API request/response contracts
12
+ - shared theme tokens and a Tailwind preset
13
+ - condition and interpolation helpers that mirror backend behavior
14
+ - CMS class allow-listing and asset URL utilities
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ npm install @selfhelp/shared
20
+ ```
21
+
22
+ For local development against sibling repositories you can use a file dependency:
23
+
24
+ ```json
25
+ { "dependencies": { "@selfhelp/shared": "file:../sh-selfhelp_shared" } }
26
+ ```
27
+
28
+ ## Quick usage
29
+
30
+ ```ts
31
+ import { ENDPOINTS, replaceCalcedValues, resolveAssetUrl } from '@selfhelp/shared';
32
+ import type { IPageContent, IUserDataResponse } from '@selfhelp/shared';
33
+
34
+ import { STYLE_REGISTRY } from '@selfhelp/shared/registry';
35
+ import { THEME_TOKENS } from '@selfhelp/shared/theme';
36
+ import tailwindPreset from '@selfhelp/shared/tailwind';
37
+ ```
38
+
39
+ The package exposes a root export plus the `registry`, `theme`, `tailwind`, `plugin-sdk`, and `testing` subpaths.
40
+
41
+ ## Build
42
+
43
+ ```bash
44
+ npm install
45
+ npm run build
46
+ ```
47
+
48
+ ## Documentation
49
+
50
+ Full documentation lives in [docs/](docs/README.md):
51
+
52
+ - Public exports, types, and contracts: [docs/reference/exports.md](docs/reference/exports.md)
53
+ - Release and versioning: [docs/developer/release-and-versioning.md](docs/developer/release-and-versioning.md)
54
+ - Testing and the coverage gate: [docs/developer/testing.md](docs/developer/testing.md)
55
+ - Extending the package: [docs/developer/extending.md](docs/developer/extending.md)
56
+
57
+ Release notes are in [CHANGELOG.md](CHANGELOG.md). Licensed under [MPL-2.0](LICENSE).
package/package.json CHANGED
@@ -1,102 +1,102 @@
1
- {
2
- "name": "@selfhelp/shared",
3
- "version": "1.3.0",
4
- "description": "Shared TypeScript package for SelfHelp web frontend and mobile app: style registry, types, API contracts, Mantine theme tokens, condition + interpolation engines, CMS class allow-list.",
5
- "repository": {
6
- "type": "git",
7
- "url": "git+https://github.com/humdek-unibe-ch/sh-selfhelp_shared.git"
8
- },
9
- "homepage": "https://github.com/humdek-unibe-ch/sh-selfhelp_shared#readme",
10
- "bugs": {
11
- "url": "https://github.com/humdek-unibe-ch/sh-selfhelp_shared/issues"
12
- },
13
- "main": "dist/index.js",
14
- "module": "dist/index.mjs",
15
- "types": "dist/index.d.ts",
16
- "exports": {
17
- ".": {
18
- "types": "./dist/index.d.ts",
19
- "import": "./dist/index.mjs",
20
- "require": "./dist/index.js"
21
- },
22
- "./registry": {
23
- "types": "./dist/registry/index.d.ts",
24
- "import": "./dist/registry/index.mjs",
25
- "require": "./dist/registry/index.js"
26
- },
27
- "./theme": {
28
- "types": "./dist/theme/index.d.ts",
29
- "import": "./dist/theme/index.mjs",
30
- "require": "./dist/theme/index.js"
31
- },
32
- "./tailwind": {
33
- "types": "./dist/theme/tailwind.d.ts",
34
- "import": "./dist/theme/tailwind.mjs",
35
- "require": "./dist/theme/tailwind.js"
36
- },
37
- "./plugin-sdk": {
38
- "types": "./dist/plugin-sdk/index.d.ts",
39
- "import": "./dist/plugin-sdk/index.mjs",
40
- "require": "./dist/plugin-sdk/index.js"
41
- },
42
- "./testing": {
43
- "types": "./dist/testing/index.d.ts",
44
- "import": "./dist/testing/index.mjs",
45
- "require": "./dist/testing/index.js"
46
- }
47
- },
48
- "files": [
49
- "dist",
50
- "src",
51
- "README.md",
52
- "CHANGELOG.md"
53
- ],
54
- "scripts": {
55
- "build": "tsup src/index.ts src/registry/index.ts src/theme/index.ts src/theme/tailwind.ts src/plugin-sdk/index.ts src/testing/index.ts --format cjs,esm --dts --clean --no-splitting",
56
- "dev": "tsup src/index.ts src/registry/index.ts src/theme/index.ts src/theme/tailwind.ts src/plugin-sdk/index.ts src/testing/index.ts --format cjs,esm --dts --watch --no-splitting",
57
- "lint": "eslint . --ext .ts",
58
- "typecheck": "tsc --noEmit",
59
- "test": "vitest run --passWithNoTests",
60
- "test:coverage": "vitest run --coverage",
61
- "test:changed": "vitest run --changed --passWithNoTests",
62
- "test:release": "npm run headers:check && npm run typecheck && npm run check:schemas && npm run test:coverage && npm run build",
63
- "check:schemas": "node scripts/check-schema-parity.mjs",
64
- "prepublishOnly": "npm run build",
65
- "headers:check": "license-check-and-add check -f license-check-and-add-config.json",
66
- "headers:add": "license-check-and-add add -f license-check-and-add-config.json",
67
- "headers:remove": "license-check-and-add remove -f license-check-and-add-config.json"
68
- },
69
- "dependencies": {
70
- "json-logic-js": "^2.0.5"
71
- },
72
- "peerDependencies": {
73
- "react": "^18.0.0 || ^19.0.0"
74
- },
75
- "peerDependenciesMeta": {
76
- "react": {
77
- "optional": true
78
- }
79
- },
80
- "devDependencies": {
81
- "@types/json-logic-js": "^2.0.8",
82
- "@types/node": "^22.10.0",
83
- "@types/react": "^19.2.15",
84
- "@typescript-eslint/eslint-plugin": "^8.0.0",
85
- "@typescript-eslint/parser": "^8.0.0",
86
- "@vitest/coverage-istanbul": "^2.1.9",
87
- "eslint": "^9.18.0",
88
- "license-check-and-add": "^4.0.5",
89
- "react": "^19.2.6",
90
- "tsup": "^8.3.5",
91
- "typescript": "^5.6.0",
92
- "vitest": "^2.1.0"
93
- },
94
- "publishConfig": {
95
- "access": "public"
96
- },
97
- "engines": {
98
- "node": ">=18"
99
- },
100
- "sideEffects": false,
101
- "license": "MPL-2.0"
102
- }
1
+ {
2
+ "name": "@selfhelp/shared",
3
+ "version": "1.3.1",
4
+ "description": "Shared TypeScript package for SelfHelp web frontend and mobile app: style registry, types, API contracts, Mantine theme tokens, condition + interpolation engines, CMS class allow-list.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/humdek-unibe-ch/sh-selfhelp_shared.git"
8
+ },
9
+ "homepage": "https://github.com/humdek-unibe-ch/sh-selfhelp_shared#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/humdek-unibe-ch/sh-selfhelp_shared/issues"
12
+ },
13
+ "main": "dist/index.js",
14
+ "module": "dist/index.mjs",
15
+ "types": "dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.js"
21
+ },
22
+ "./registry": {
23
+ "types": "./dist/registry/index.d.ts",
24
+ "import": "./dist/registry/index.mjs",
25
+ "require": "./dist/registry/index.js"
26
+ },
27
+ "./theme": {
28
+ "types": "./dist/theme/index.d.ts",
29
+ "import": "./dist/theme/index.mjs",
30
+ "require": "./dist/theme/index.js"
31
+ },
32
+ "./tailwind": {
33
+ "types": "./dist/theme/tailwind.d.ts",
34
+ "import": "./dist/theme/tailwind.mjs",
35
+ "require": "./dist/theme/tailwind.js"
36
+ },
37
+ "./plugin-sdk": {
38
+ "types": "./dist/plugin-sdk/index.d.ts",
39
+ "import": "./dist/plugin-sdk/index.mjs",
40
+ "require": "./dist/plugin-sdk/index.js"
41
+ },
42
+ "./testing": {
43
+ "types": "./dist/testing/index.d.ts",
44
+ "import": "./dist/testing/index.mjs",
45
+ "require": "./dist/testing/index.js"
46
+ }
47
+ },
48
+ "files": [
49
+ "dist",
50
+ "src",
51
+ "README.md",
52
+ "CHANGELOG.md"
53
+ ],
54
+ "scripts": {
55
+ "build": "tsup src/index.ts src/registry/index.ts src/theme/index.ts src/theme/tailwind.ts src/plugin-sdk/index.ts src/testing/index.ts --format cjs,esm --dts --clean --no-splitting",
56
+ "dev": "tsup src/index.ts src/registry/index.ts src/theme/index.ts src/theme/tailwind.ts src/plugin-sdk/index.ts src/testing/index.ts --format cjs,esm --dts --watch --no-splitting",
57
+ "lint": "eslint . --ext .ts",
58
+ "typecheck": "tsc --noEmit",
59
+ "test": "vitest run --passWithNoTests",
60
+ "test:coverage": "vitest run --coverage",
61
+ "test:changed": "vitest run --changed --passWithNoTests",
62
+ "test:release": "npm run headers:check && npm run typecheck && npm run check:schemas && npm run test:coverage && npm run build",
63
+ "check:schemas": "node scripts/check-schema-parity.mjs",
64
+ "prepublishOnly": "npm run build",
65
+ "headers:check": "license-check-and-add check -f license-check-and-add-config.json",
66
+ "headers:add": "license-check-and-add add -f license-check-and-add-config.json",
67
+ "headers:remove": "license-check-and-add remove -f license-check-and-add-config.json"
68
+ },
69
+ "dependencies": {
70
+ "json-logic-js": "^2.0.5"
71
+ },
72
+ "peerDependencies": {
73
+ "react": "^18.0.0 || ^19.0.0"
74
+ },
75
+ "peerDependenciesMeta": {
76
+ "react": {
77
+ "optional": true
78
+ }
79
+ },
80
+ "devDependencies": {
81
+ "@types/json-logic-js": "^2.0.8",
82
+ "@types/node": "^22.10.0",
83
+ "@types/react": "^19.2.15",
84
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
85
+ "@typescript-eslint/parser": "^8.0.0",
86
+ "@vitest/coverage-istanbul": "^2.1.9",
87
+ "eslint": "^9.18.0",
88
+ "license-check-and-add": "^4.0.5",
89
+ "react": "^19.2.6",
90
+ "tsup": "^8.3.5",
91
+ "typescript": "^5.6.0",
92
+ "vitest": "^2.1.0"
93
+ },
94
+ "publishConfig": {
95
+ "access": "public"
96
+ },
97
+ "engines": {
98
+ "node": ">=18"
99
+ },
100
+ "sideEffects": false,
101
+ "license": "MPL-2.0"
102
+ }
@@ -1,45 +1,45 @@
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 { API_VERSION_PREFIX, ENDPOINTS } from '../endpoints';
8
- import type {
9
- IForgotPasswordRequest,
10
- IResetPasswordRequest,
11
- } from '../../types/api/auth';
12
-
13
- /**
14
- * Contract coverage for the public auth endpoint catalog. The frontend and the
15
- * mobile app both build their password-recovery requests from these constants,
16
- * so a wrong/missing path silently breaks the cross-repo flow (issue #31). The
17
- * backend serves these under `/cms-api/v1/auth/...`.
18
- */
19
- describe('ENDPOINTS.AUTH password recovery', () => {
20
- it('exposes the forgot-password endpoint at the backend path', () => {
21
- expect(ENDPOINTS.AUTH.FORGOT_PASSWORD).toBe(
22
- `${API_VERSION_PREFIX}/auth/forgot-password`,
23
- );
24
- });
25
-
26
- it('exposes the reset-password endpoint at the backend path', () => {
27
- expect(ENDPOINTS.AUTH.RESET_PASSWORD).toBe(
28
- `${API_VERSION_PREFIX}/auth/reset-password`,
29
- );
30
- });
31
-
32
- it('models the request DTOs with the backend-required fields', () => {
33
- const forgot: IForgotPasswordRequest = { email: 'qa.user@selfhelp.test' };
34
- const reset: IResetPasswordRequest = {
35
- id_users: 1,
36
- token: 'qa-token',
37
- password: 'qa-password-123',
38
- };
39
-
40
- expect(Object.keys(forgot)).toEqual(['email']);
41
- expect(Object.keys(reset).sort()).toEqual(
42
- ['id_users', 'password', 'token'],
43
- );
44
- });
45
- });
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 { API_VERSION_PREFIX, ENDPOINTS } from '../endpoints';
8
+ import type {
9
+ IForgotPasswordRequest,
10
+ IResetPasswordRequest,
11
+ } from '../../types/api/auth';
12
+
13
+ /**
14
+ * Contract coverage for the public auth endpoint catalog. The frontend and the
15
+ * mobile app both build their password-recovery requests from these constants,
16
+ * so a wrong/missing path silently breaks the cross-repo flow (issue #31). The
17
+ * backend serves these under `/cms-api/v1/auth/...`.
18
+ */
19
+ describe('ENDPOINTS.AUTH password recovery', () => {
20
+ it('exposes the forgot-password endpoint at the backend path', () => {
21
+ expect(ENDPOINTS.AUTH.FORGOT_PASSWORD).toBe(
22
+ `${API_VERSION_PREFIX}/auth/forgot-password`,
23
+ );
24
+ });
25
+
26
+ it('exposes the reset-password endpoint at the backend path', () => {
27
+ expect(ENDPOINTS.AUTH.RESET_PASSWORD).toBe(
28
+ `${API_VERSION_PREFIX}/auth/reset-password`,
29
+ );
30
+ });
31
+
32
+ it('models the request DTOs with the backend-required fields', () => {
33
+ const forgot: IForgotPasswordRequest = { email: 'qa.user@selfhelp.test' };
34
+ const reset: IResetPasswordRequest = {
35
+ id_users: 1,
36
+ token: 'qa-token',
37
+ password: 'qa-password-123',
38
+ };
39
+
40
+ expect(Object.keys(forgot)).toEqual(['email']);
41
+ expect(Object.keys(reset).sort()).toEqual(
42
+ ['id_users', 'password', 'token'],
43
+ );
44
+ });
45
+ });
package/src/api/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- export * from './endpoints';
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ export * from './endpoints';
@@ -1,61 +1,61 @@
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 { resolveAssetUrl, resolveAssetSources } from '../resolveAssetUrl';
7
-
8
- describe('resolveAssetUrl', () => {
9
- const base = 'https://api.example.com';
10
-
11
- it('prefixes a root-relative path with the base url', () => {
12
- expect(resolveAssetUrl('/assets/img/foo.png', base)).toBe('https://api.example.com/assets/img/foo.png');
13
- });
14
-
15
- it('adds a leading slash for paths that lack one', () => {
16
- expect(resolveAssetUrl('assets/foo.png', base)).toBe('https://api.example.com/assets/foo.png');
17
- });
18
-
19
- it('strips trailing slashes from the base url', () => {
20
- expect(resolveAssetUrl('/foo.png', 'https://api.example.com/')).toBe('https://api.example.com/foo.png');
21
- });
22
-
23
- it('passes absolute http(s) urls through unchanged', () => {
24
- expect(resolveAssetUrl('https://cdn.example.com/foo.png', base)).toBe('https://cdn.example.com/foo.png');
25
- });
26
-
27
- it('passes protocol-relative urls through unchanged', () => {
28
- expect(resolveAssetUrl('//cdn.example.com/foo.png', base)).toBe('//cdn.example.com/foo.png');
29
- });
30
-
31
- it('passes data/blob/file uris through unchanged', () => {
32
- expect(resolveAssetUrl('data:image/png;base64,AAAA', base)).toBe('data:image/png;base64,AAAA');
33
- expect(resolveAssetUrl('blob:abc', base)).toBe('blob:abc');
34
- });
35
-
36
- it('returns an empty string for null/undefined/empty input', () => {
37
- expect(resolveAssetUrl(null, base)).toBe('');
38
- expect(resolveAssetUrl(undefined, base)).toBe('');
39
- expect(resolveAssetUrl('', base)).toBe('');
40
- });
41
- });
42
-
43
- describe('resolveAssetSources', () => {
44
- it('resolves the src of each source and keeps other fields', () => {
45
- const resolved = resolveAssetSources(
46
- [
47
- { src: '/a.mp4', type: 'video/mp4' },
48
- { src: 'https://cdn/b.webm', type: 'video/webm' },
49
- ],
50
- 'https://api.example.com',
51
- );
52
- expect(resolved).toEqual([
53
- { src: 'https://api.example.com/a.mp4', type: 'video/mp4' },
54
- { src: 'https://cdn/b.webm', type: 'video/webm' },
55
- ]);
56
- });
57
-
58
- it('returns an empty array for undefined sources', () => {
59
- expect(resolveAssetSources(undefined, 'https://api.example.com')).toEqual([]);
60
- });
61
- });
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 { resolveAssetUrl, resolveAssetSources } from '../resolveAssetUrl';
7
+
8
+ describe('resolveAssetUrl', () => {
9
+ const base = 'https://api.example.com';
10
+
11
+ it('prefixes a root-relative path with the base url', () => {
12
+ expect(resolveAssetUrl('/assets/img/foo.png', base)).toBe('https://api.example.com/assets/img/foo.png');
13
+ });
14
+
15
+ it('adds a leading slash for paths that lack one', () => {
16
+ expect(resolveAssetUrl('assets/foo.png', base)).toBe('https://api.example.com/assets/foo.png');
17
+ });
18
+
19
+ it('strips trailing slashes from the base url', () => {
20
+ expect(resolveAssetUrl('/foo.png', 'https://api.example.com/')).toBe('https://api.example.com/foo.png');
21
+ });
22
+
23
+ it('passes absolute http(s) urls through unchanged', () => {
24
+ expect(resolveAssetUrl('https://cdn.example.com/foo.png', base)).toBe('https://cdn.example.com/foo.png');
25
+ });
26
+
27
+ it('passes protocol-relative urls through unchanged', () => {
28
+ expect(resolveAssetUrl('//cdn.example.com/foo.png', base)).toBe('//cdn.example.com/foo.png');
29
+ });
30
+
31
+ it('passes data/blob/file uris through unchanged', () => {
32
+ expect(resolveAssetUrl('data:image/png;base64,AAAA', base)).toBe('data:image/png;base64,AAAA');
33
+ expect(resolveAssetUrl('blob:abc', base)).toBe('blob:abc');
34
+ });
35
+
36
+ it('returns an empty string for null/undefined/empty input', () => {
37
+ expect(resolveAssetUrl(null, base)).toBe('');
38
+ expect(resolveAssetUrl(undefined, base)).toBe('');
39
+ expect(resolveAssetUrl('', base)).toBe('');
40
+ });
41
+ });
42
+
43
+ describe('resolveAssetSources', () => {
44
+ it('resolves the src of each source and keeps other fields', () => {
45
+ const resolved = resolveAssetSources(
46
+ [
47
+ { src: '/a.mp4', type: 'video/mp4' },
48
+ { src: 'https://cdn/b.webm', type: 'video/webm' },
49
+ ],
50
+ 'https://api.example.com',
51
+ );
52
+ expect(resolved).toEqual([
53
+ { src: 'https://api.example.com/a.mp4', type: 'video/mp4' },
54
+ { src: 'https://cdn/b.webm', type: 'video/webm' },
55
+ ]);
56
+ });
57
+
58
+ it('returns an empty array for undefined sources', () => {
59
+ expect(resolveAssetSources(undefined, 'https://api.example.com')).toEqual([]);
60
+ });
61
+ });
@@ -1,5 +1,5 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- export * from './resolveAssetUrl';
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ export * from './resolveAssetUrl';
@@ -1,44 +1,44 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- /**
6
- * Resolve a CMS-supplied asset URL.
7
- *
8
- * resolveAssetUrl('/assets/img/foo.png', 'https://api.example.com')
9
- * -> 'https://api.example.com/assets/img/foo.png'
10
- *
11
- * resolveAssetUrl('https://cdn.example.com/foo.png', 'https://api.example.com')
12
- * -> 'https://cdn.example.com/foo.png' (already absolute, passes through)
13
- *
14
- * resolveAssetUrl('data:image/png;base64,...', 'https://api.example.com')
15
- * -> 'data:image/png;base64,...' (data URIs pass through)
16
- *
17
- * resolveAssetUrl(null, ...) / resolveAssetUrl(undefined, ...) -> ''
18
- */
19
-
20
- const ABSOLUTE_RE = /^(?:[a-z][a-z0-9+\-.]*:)?\/\//i;
21
- const DATA_OR_BLOB_RE = /^(?:data|blob|file):/i;
22
-
23
- export function resolveAssetUrl(url: string | null | undefined, baseUrl: string): string {
24
- if (!url) return '';
25
- if (ABSOLUTE_RE.test(url) || DATA_OR_BLOB_RE.test(url)) return url;
26
-
27
- const base = baseUrl.replace(/\/+$/, '');
28
- const path = url.startsWith('/') ? url : `/${url}`;
29
- return `${base}${path}`;
30
- }
31
-
32
- /**
33
- * Resolve potentially nested `sources` arrays (used by video/audio/carousel).
34
- */
35
- export interface IAssetSource {
36
- src: string;
37
- type?: string;
38
- [extra: string]: unknown;
39
- }
40
-
41
- export function resolveAssetSources<T extends IAssetSource>(sources: T[] | undefined, baseUrl: string): T[] {
42
- if (!sources) return [];
43
- return sources.map((s) => ({ ...s, src: resolveAssetUrl(s.src, baseUrl) }));
44
- }
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Resolve a CMS-supplied asset URL.
7
+ *
8
+ * resolveAssetUrl('/assets/img/foo.png', 'https://api.example.com')
9
+ * -> 'https://api.example.com/assets/img/foo.png'
10
+ *
11
+ * resolveAssetUrl('https://cdn.example.com/foo.png', 'https://api.example.com')
12
+ * -> 'https://cdn.example.com/foo.png' (already absolute, passes through)
13
+ *
14
+ * resolveAssetUrl('data:image/png;base64,...', 'https://api.example.com')
15
+ * -> 'data:image/png;base64,...' (data URIs pass through)
16
+ *
17
+ * resolveAssetUrl(null, ...) / resolveAssetUrl(undefined, ...) -> ''
18
+ */
19
+
20
+ const ABSOLUTE_RE = /^(?:[a-z][a-z0-9+\-.]*:)?\/\//i;
21
+ const DATA_OR_BLOB_RE = /^(?:data|blob|file):/i;
22
+
23
+ export function resolveAssetUrl(url: string | null | undefined, baseUrl: string): string {
24
+ if (!url) return '';
25
+ if (ABSOLUTE_RE.test(url) || DATA_OR_BLOB_RE.test(url)) return url;
26
+
27
+ const base = baseUrl.replace(/\/+$/, '');
28
+ const path = url.startsWith('/') ? url : `/${url}`;
29
+ return `${base}${path}`;
30
+ }
31
+
32
+ /**
33
+ * Resolve potentially nested `sources` arrays (used by video/audio/carousel).
34
+ */
35
+ export interface IAssetSource {
36
+ src: string;
37
+ type?: string;
38
+ [extra: string]: unknown;
39
+ }
40
+
41
+ export function resolveAssetSources<T extends IAssetSource>(sources: T[] | undefined, baseUrl: string): T[] {
42
+ if (!sources) return [];
43
+ return sources.map((s) => ({ ...s, src: resolveAssetUrl(s.src, baseUrl) }));
44
+ }