@selfhelp/shared 1.21.5 → 1.21.7
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 +46 -0
- package/dist/index.d.mts +102 -5
- package/dist/index.d.ts +102 -5
- package/dist/index.js +163 -5
- package/dist/index.mjs +156 -5
- package/package.json +1 -1
- package/src/api/__tests__/endpoints.test.ts +37 -12
- package/src/api/endpoints.ts +5 -5
- package/src/api/index.ts +1 -0
- package/src/api/pagesResolve.ts +78 -0
- package/src/content/__tests__/formRecordPrefill.test.ts +104 -0
- package/src/content/formRecordPrefill.ts +191 -0
- package/src/content/index.ts +1 -0
- package/src/types/__tests__/should-fallback.test.ts +19 -0
- package/src/types/pages.ts +9 -0
- package/src/types/preview-bridge.ts +10 -1
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
flattenFormRecordPrefillValues,
|
|
9
|
+
parseFormRecordPrefill,
|
|
10
|
+
} from '../formRecordPrefill';
|
|
11
|
+
|
|
12
|
+
describe('parseFormRecordPrefill', () => {
|
|
13
|
+
it('returns empty create state when section_data is missing', () => {
|
|
14
|
+
expect(parseFormRecordPrefill({})).toEqual({ recordId: null, values: {} });
|
|
15
|
+
expect(parseFormRecordPrefill({ section_data: null })).toEqual({
|
|
16
|
+
recordId: null,
|
|
17
|
+
values: {},
|
|
18
|
+
});
|
|
19
|
+
expect(parseFormRecordPrefill({ section_data: [] })).toEqual({
|
|
20
|
+
recordId: null,
|
|
21
|
+
values: {},
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('flattens the first hydrated record and skips metadata fields', () => {
|
|
26
|
+
const result = parseFormRecordPrefill({
|
|
27
|
+
section_data: [
|
|
28
|
+
{
|
|
29
|
+
record_id: 12,
|
|
30
|
+
id_users: 3,
|
|
31
|
+
user_name: 'qa.user',
|
|
32
|
+
id_languages: 1,
|
|
33
|
+
title: 'Hello',
|
|
34
|
+
body: 'World',
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
children: [
|
|
38
|
+
{ name: { content: 'title' }, translatable: { content: '0' } },
|
|
39
|
+
{ name: { content: 'body' }, translatable: { content: '0' } },
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
expect(result).toEqual({
|
|
43
|
+
recordId: 12,
|
|
44
|
+
values: { title: 'Hello', body: 'World' },
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('accumulates per-language values for translatable fields', () => {
|
|
49
|
+
const result = parseFormRecordPrefill({
|
|
50
|
+
section_data: [
|
|
51
|
+
{ record_id: 5, id_languages: 1, bio: 'Independent seed' },
|
|
52
|
+
{ record_id: 5, id_languages: 2, bio: 'DE bio' },
|
|
53
|
+
{ record_id: 5, id_languages: 3, bio: 'EN bio' },
|
|
54
|
+
],
|
|
55
|
+
children: [{ name: { content: 'bio' }, translatable: { content: '1' } }],
|
|
56
|
+
});
|
|
57
|
+
expect(result.recordId).toBe(5);
|
|
58
|
+
expect(result.values.bio).toEqual([
|
|
59
|
+
{ language_id: 2, value: 'DE bio' },
|
|
60
|
+
{ language_id: 3, value: 'EN bio' },
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('ignores malformed rows and nested non-scalar field values', () => {
|
|
65
|
+
const result = parseFormRecordPrefill({
|
|
66
|
+
section_data: [
|
|
67
|
+
null,
|
|
68
|
+
'skip',
|
|
69
|
+
{ record_id: 'nope', title: 'x' },
|
|
70
|
+
{ record_id: 9, id_languages: 1, title: 'ok', nested: { a: 1 }, list: [1, 2] },
|
|
71
|
+
],
|
|
72
|
+
children: [{ name: { content: 'title' }, translatable: { content: '0' } }],
|
|
73
|
+
});
|
|
74
|
+
expect(result).toEqual({ recordId: 9, values: { title: 'ok' } });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('accepts numeric string record_id from JSON payloads', () => {
|
|
78
|
+
const result = parseFormRecordPrefill({
|
|
79
|
+
section_data: [{ record_id: '42', id_languages: 1, name: 'Ada' }],
|
|
80
|
+
children: [{ name: { content: 'name' }, translatable: { content: '0' } }],
|
|
81
|
+
});
|
|
82
|
+
expect(result).toEqual({ recordId: 42, values: { name: 'Ada' } });
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('flattenFormRecordPrefillValues', () => {
|
|
87
|
+
it('prefers the requested language, else first public language', () => {
|
|
88
|
+
const values = {
|
|
89
|
+
bio: [
|
|
90
|
+
{ language_id: 2, value: 'DE' },
|
|
91
|
+
{ language_id: 3, value: 'EN' },
|
|
92
|
+
],
|
|
93
|
+
title: 'plain',
|
|
94
|
+
};
|
|
95
|
+
expect(flattenFormRecordPrefillValues(values, 3)).toEqual({
|
|
96
|
+
bio: 'EN',
|
|
97
|
+
title: 'plain',
|
|
98
|
+
});
|
|
99
|
+
expect(flattenFormRecordPrefillValues(values)).toEqual({
|
|
100
|
+
bio: 'DE',
|
|
101
|
+
title: 'plain',
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Flatten server-hydrated form `section_data` into a form-record prefill map.
|
|
8
|
+
*
|
|
9
|
+
* Used by web `FormStyle` and mobile `FormUserInput` so create/edit hydration
|
|
10
|
+
* cannot drift between clients. The backend owns which row is present
|
|
11
|
+
* (`load_record_from`); this helper only reshapes that payload.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Per-language value for a translatable form field. */
|
|
15
|
+
export interface IFormRecordPrefillTranslatedValue {
|
|
16
|
+
language_id: number;
|
|
17
|
+
value: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type TFormRecordPrefillFieldValue = string | IFormRecordPrefillTranslatedValue[];
|
|
21
|
+
|
|
22
|
+
export interface IFormRecordPrefill {
|
|
23
|
+
recordId: number | null;
|
|
24
|
+
values: Record<string, TFormRecordPrefillFieldValue>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Metadata fields that are never form inputs. */
|
|
28
|
+
export const FORM_RECORD_PREFILL_SKIP_FIELDS: ReadonlySet<string> = new Set([
|
|
29
|
+
'record_id',
|
|
30
|
+
'entry_date',
|
|
31
|
+
'id_users',
|
|
32
|
+
'user_name',
|
|
33
|
+
'user_code',
|
|
34
|
+
'id_actionTriggerTypes',
|
|
35
|
+
'triggerType',
|
|
36
|
+
'id_languages',
|
|
37
|
+
'language_locale',
|
|
38
|
+
'language_name',
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
export interface IFormRecordPrefillChild {
|
|
42
|
+
name?: { content?: string } | null;
|
|
43
|
+
translatable?: { content?: string } | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface IFormRecordPrefillSection {
|
|
47
|
+
section_data?: unknown;
|
|
48
|
+
children?: IFormRecordPrefillChild[] | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isTranslatableChild(
|
|
52
|
+
children: IFormRecordPrefillChild[] | null | undefined,
|
|
53
|
+
fieldName: string,
|
|
54
|
+
): boolean {
|
|
55
|
+
if (!Array.isArray(children)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const child = children.find((item) => item?.name?.content === fieldName);
|
|
59
|
+
return child?.translatable?.content === '1';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function asScalarString(value: unknown): string | null {
|
|
63
|
+
if (value === null || value === undefined) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
67
|
+
return String(value);
|
|
68
|
+
}
|
|
69
|
+
// Nested objects/arrays are not form field values — skip.
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parse hydrated `section_data` rows into `{ recordId, values }`.
|
|
75
|
+
*
|
|
76
|
+
* - Missing / empty / non-array `section_data` → empty create state.
|
|
77
|
+
* - Rows without a numeric `record_id` are ignored.
|
|
78
|
+
* - Translatable fields accumulate `{ language_id, value }[]` (language id `1`
|
|
79
|
+
* is an Independent seed kept only until a public-language row arrives).
|
|
80
|
+
* - Non-translatable fields prefer language id `1`, else the first seen value.
|
|
81
|
+
*/
|
|
82
|
+
export function parseFormRecordPrefill(section: IFormRecordPrefillSection): IFormRecordPrefill {
|
|
83
|
+
const sectionDataArray = section?.section_data;
|
|
84
|
+
if (!Array.isArray(sectionDataArray) || sectionDataArray.length === 0) {
|
|
85
|
+
return { recordId: null, values: {} };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const recordGroups: Record<number, Record<string, TFormRecordPrefillFieldValue>> = {};
|
|
89
|
+
|
|
90
|
+
for (const row of sectionDataArray) {
|
|
91
|
+
if (!row || typeof row !== 'object' || Array.isArray(row)) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const record = row as Record<string, unknown>;
|
|
95
|
+
const rawRecordId = record.record_id;
|
|
96
|
+
const recordId =
|
|
97
|
+
typeof rawRecordId === 'number'
|
|
98
|
+
? rawRecordId
|
|
99
|
+
: typeof rawRecordId === 'string' && rawRecordId.trim() !== '' && Number.isFinite(Number(rawRecordId))
|
|
100
|
+
? Number(rawRecordId)
|
|
101
|
+
: null;
|
|
102
|
+
if (recordId === null) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!recordGroups[recordId]) {
|
|
107
|
+
recordGroups[recordId] = {};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const languageId =
|
|
111
|
+
typeof record.id_languages === 'number'
|
|
112
|
+
? record.id_languages
|
|
113
|
+
: typeof record.id_languages === 'string' && Number.isFinite(Number(record.id_languages))
|
|
114
|
+
? Number(record.id_languages)
|
|
115
|
+
: undefined;
|
|
116
|
+
|
|
117
|
+
for (const [fieldName, fieldValue] of Object.entries(record)) {
|
|
118
|
+
if (FORM_RECORD_PREFILL_SKIP_FIELDS.has(fieldName)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const value = asScalarString(fieldValue);
|
|
122
|
+
if (value === null) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const translatable = isTranslatableChild(section.children, fieldName);
|
|
127
|
+
|
|
128
|
+
if (translatable) {
|
|
129
|
+
if (languageId === 1) {
|
|
130
|
+
if (!recordGroups[recordId][fieldName]) {
|
|
131
|
+
recordGroups[recordId][fieldName] = value;
|
|
132
|
+
}
|
|
133
|
+
} else if (typeof languageId === 'number') {
|
|
134
|
+
const current = recordGroups[recordId][fieldName];
|
|
135
|
+
if (typeof current === 'string' || !current) {
|
|
136
|
+
recordGroups[recordId][fieldName] = [{ language_id: languageId, value }];
|
|
137
|
+
} else {
|
|
138
|
+
const langValues = current;
|
|
139
|
+
const existingIndex = langValues.findIndex((v) => v.language_id === languageId);
|
|
140
|
+
if (existingIndex >= 0) {
|
|
141
|
+
langValues[existingIndex] = { language_id: languageId, value };
|
|
142
|
+
} else {
|
|
143
|
+
langValues.push({ language_id: languageId, value });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
} else if (languageId === 1 || !recordGroups[recordId][fieldName]) {
|
|
148
|
+
recordGroups[recordId][fieldName] = value;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const firstRecordId = Object.keys(recordGroups)[0];
|
|
154
|
+
if (!firstRecordId) {
|
|
155
|
+
return { recordId: null, values: {} };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const id = Number(firstRecordId);
|
|
159
|
+
return { recordId: id, values: recordGroups[id] ?? {} };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Flatten rich prefill values to plain strings for clients that do not keep
|
|
164
|
+
* per-language arrays (mobile). Prefers `languageId` when present, else the
|
|
165
|
+
* first translated entry, else the Independent seed string.
|
|
166
|
+
*/
|
|
167
|
+
export function flattenFormRecordPrefillValues(
|
|
168
|
+
values: Record<string, TFormRecordPrefillFieldValue>,
|
|
169
|
+
languageId?: number | null,
|
|
170
|
+
): Record<string, string> {
|
|
171
|
+
const out: Record<string, string> = {};
|
|
172
|
+
for (const [key, value] of Object.entries(values)) {
|
|
173
|
+
if (typeof value === 'string') {
|
|
174
|
+
out[key] = value;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (languageId != null) {
|
|
181
|
+
const match = value.find((entry) => entry.language_id === languageId);
|
|
182
|
+
if (match) {
|
|
183
|
+
out[key] = match.value;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const nonIndependent = value.find((entry) => entry.language_id !== 1);
|
|
188
|
+
out[key] = (nonIndependent ?? value[0]).value;
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
package/src/content/index.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
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 type { IPageContent } from '../pages';
|
|
8
|
+
|
|
9
|
+
describe('IPageContent.should_fallback contract', () => {
|
|
10
|
+
it('accepts explicit true/false and absence without requiring sections inference', () => {
|
|
11
|
+
const withTrue = { should_fallback: true } as Pick<IPageContent, 'should_fallback'>;
|
|
12
|
+
const withFalse = { should_fallback: false } as Pick<IPageContent, 'should_fallback'>;
|
|
13
|
+
const absent = {} as Pick<IPageContent, 'should_fallback'>;
|
|
14
|
+
|
|
15
|
+
expect(withTrue.should_fallback === true).toBe(true);
|
|
16
|
+
expect(withFalse.should_fallback === true).toBe(false);
|
|
17
|
+
expect(absent.should_fallback === true).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
});
|
package/src/types/pages.ts
CHANGED
|
@@ -114,6 +114,15 @@ export interface IPageContent {
|
|
|
114
114
|
* exposes these to interpolation as `{{route.<name>}}`.
|
|
115
115
|
*/
|
|
116
116
|
route_params?: Record<string, string>;
|
|
117
|
+
/**
|
|
118
|
+
* When true, the web frontend should redirect to the hardcoded static
|
|
119
|
+
* `/auth/*` fallback because this system page is missing its required
|
|
120
|
+
* functional section. Emitted by core >=0.1.36 only for the fallback-check
|
|
121
|
+
* keyword set (`login`, `profile`, `reset-password`, …). Absence means the
|
|
122
|
+
* page does not participate in static fallback — consumers must not infer
|
|
123
|
+
* fallback from empty `sections` (that was an older-backend shim).
|
|
124
|
+
*/
|
|
125
|
+
should_fallback?: boolean;
|
|
117
126
|
/** The `page_routes` pattern that matched (e.g. `/reset/{user_id}/{token}`). Resolve responses only. */
|
|
118
127
|
matched_url_pattern?: string | null;
|
|
119
128
|
/** The canonical active route pattern for the page, for canonical-link generation. Resolve responses only. */
|
|
@@ -105,6 +105,12 @@ export interface IPreviewNavigatedMessage {
|
|
|
105
105
|
keyword: string | null;
|
|
106
106
|
/** Full in-frame href, for diagnostics / the toolbar URL display. */
|
|
107
107
|
href: string;
|
|
108
|
+
/**
|
|
109
|
+
* Public CMS path when the frame resolved a parameterized route
|
|
110
|
+
* (`/team-members/5`). The shell re-resolves via `/pages/resolve` so
|
|
111
|
+
* `route_params` stay in sync. Optional for static keyword pages.
|
|
112
|
+
*/
|
|
113
|
+
path?: string | null;
|
|
108
114
|
locale?: string | null;
|
|
109
115
|
}
|
|
110
116
|
|
|
@@ -180,7 +186,10 @@ export function isPreviewBridgeMessage(value: unknown): value is TPreviewBridgeM
|
|
|
180
186
|
return (
|
|
181
187
|
(record.source === 'web' || record.source === 'mobile') &&
|
|
182
188
|
(typeof record.keyword === 'string' || record.keyword === null) &&
|
|
183
|
-
typeof record.href === 'string'
|
|
189
|
+
typeof record.href === 'string' &&
|
|
190
|
+
(record.path === undefined ||
|
|
191
|
+
record.path === null ||
|
|
192
|
+
typeof record.path === 'string')
|
|
184
193
|
);
|
|
185
194
|
case PREVIEW_BRIDGE_MESSAGE.NAVIGATE:
|
|
186
195
|
return (
|