@selfhelp/shared 1.23.0 → 1.25.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.
- package/CHANGELOG.md +127 -0
- package/dist/index.d.mts +300 -100
- package/dist/index.d.ts +300 -100
- package/dist/index.js +531 -3
- package/dist/index.mjs +506 -3
- package/dist/plugin-sdk/index.d.mts +1 -1
- package/dist/plugin-sdk/index.d.ts +1 -1
- package/dist/plugin-sdk/index.js +7 -1
- package/dist/plugin-sdk/index.mjs +7 -1
- package/dist/registry/index.d.mts +2 -2
- package/dist/registry/index.d.ts +2 -2
- package/dist/registry/index.js +15 -1
- package/dist/registry/index.mjs +14 -1
- package/dist/{styles.registry-BBd9T2Ox.d.ts → styles.registry-C2ACy7H0.d.ts} +734 -2
- package/dist/{styles.registry-IjDTff8P.d.mts → styles.registry-DnIJqEzM.d.mts} +734 -2
- package/package.json +1 -1
- package/src/api/__tests__/endpoints.test.ts +45 -0
- package/src/api/__tests__/formListErrors.test.ts +143 -0
- package/src/api/endpoints.ts +41 -0
- package/src/api/formListErrors.ts +134 -0
- package/src/api/index.ts +1 -0
- package/src/content/__tests__/formListState.test.ts +448 -0
- package/src/content/__tests__/formRecordPrefill.test.ts +120 -0
- package/src/content/formListState.ts +559 -0
- package/src/content/formRecordPrefill.ts +108 -5
- package/src/content/index.ts +1 -0
- package/src/registry/__tests__/platform.test.ts +15 -6
- package/src/registry/__tests__/registry.test.ts +14 -0
- package/src/registry/styles.registry.ts +23 -0
- package/src/types/api/__tests__/formWrites.test.ts +84 -0
- package/src/types/api/form-lists.ts +475 -0
- package/src/types/api/forms.ts +149 -4
- package/src/types/api/index.ts +1 -0
- package/src/types/styles/__tests__/entryFormListFormFields.test.ts +44 -0
- package/src/types/styles/composite.ts +13 -0
- package/src/types/styles/forms.ts +124 -0
- package/src/types/styles/unknown.ts +5 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@selfhelp/shared",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.25.0",
|
|
4
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
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -50,6 +50,51 @@ describe('ENDPOINTS.AUTH password recovery', () => {
|
|
|
50
50
|
});
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* The list routes are served by `FormListController`; the paths are the ones
|
|
55
|
+
* `api_routes` seeds (`docs/reference/api/22-form-lists.md`).
|
|
56
|
+
*/
|
|
57
|
+
describe('ENDPOINTS.LISTS', () => {
|
|
58
|
+
it('builds the state, history, comparison and restore routes of a record', () => {
|
|
59
|
+
expect(ENDPOINTS.LISTS.STATE(311)).toBe(`${API_VERSION_PREFIX}/lists/311/state`);
|
|
60
|
+
expect(ENDPOINTS.LISTS.HISTORY(311, 41)).toBe(`${API_VERSION_PREFIX}/lists/311/records/41/history`);
|
|
61
|
+
expect(ENDPOINTS.LISTS.VERSION(311, 41, 3)).toBe(`${API_VERSION_PREFIX}/lists/311/records/41/versions/3`);
|
|
62
|
+
expect(ENDPOINTS.LISTS.COMPARE(311, 41)).toBe(`${API_VERSION_PREFIX}/lists/311/records/41/compare`);
|
|
63
|
+
expect(ENDPOINTS.LISTS.RESTORE(311, 41)).toBe(`${API_VERSION_PREFIX}/lists/311/records/41/restore`);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('encodes the action id and the field key, the only free-text segments', () => {
|
|
67
|
+
expect(ENDPOINTS.LISTS.QUICK_ACTION(311, 41, 'mark_done')).toBe(`${API_VERSION_PREFIX}/lists/311/records/41/actions/mark_done`);
|
|
68
|
+
expect(ENDPOINTS.LISTS.QUICK_ACTION(311, 41, 'a/b?c')).toBe(`${API_VERSION_PREFIX}/lists/311/records/41/actions/a%2Fb%3Fc`);
|
|
69
|
+
expect(ENDPOINTS.LISTS.VERSION_FILE(311, 41, 2, 'section_9/..')).toBe(
|
|
70
|
+
`${API_VERSION_PREFIX}/lists/311/records/41/versions/2/files/section_9%2F..`,
|
|
71
|
+
);
|
|
72
|
+
expect(ENDPOINTS.LISTS.CURRENT_FILE(311, 41, 'portrait')).toBe(
|
|
73
|
+
`${API_VERSION_PREFIX}/lists/311/records/41/files/portrait`,
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('ENDPOINTS.ADMIN_DATA_HISTORY', () => {
|
|
79
|
+
it('builds the data-browser history routes under admin.data', () => {
|
|
80
|
+
expect(ENDPOINTS.ADMIN_DATA_HISTORY.HISTORY('news', 9)).toBe(
|
|
81
|
+
`${API_VERSION_PREFIX}/admin/data/tables/news/records/9/history`,
|
|
82
|
+
);
|
|
83
|
+
expect(ENDPOINTS.ADMIN_DATA_HISTORY.VERSION('news', 9, 2)).toBe(
|
|
84
|
+
`${API_VERSION_PREFIX}/admin/data/tables/news/records/9/versions/2`,
|
|
85
|
+
);
|
|
86
|
+
expect(ENDPOINTS.ADMIN_DATA_HISTORY.COMPARE('a/b', 1)).toBe(
|
|
87
|
+
`${API_VERSION_PREFIX}/admin/data/tables/a%2Fb/records/1/compare`,
|
|
88
|
+
);
|
|
89
|
+
expect(ENDPOINTS.ADMIN_DATA_HISTORY.RESTORE('news', 9)).toBe(
|
|
90
|
+
`${API_VERSION_PREFIX}/admin/data/tables/news/records/9/restore`,
|
|
91
|
+
);
|
|
92
|
+
expect(ENDPOINTS.ADMIN_DATA_HISTORY.VERSION_FILE('news', 9, 2, 'doc')).toBe(
|
|
93
|
+
`${API_VERSION_PREFIX}/admin/data/tables/news/records/9/versions/2/files/doc`,
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
53
98
|
/**
|
|
54
99
|
* Canonical resolve contract: SSR, browser, and mobile must produce the same
|
|
55
100
|
* query encoding for path / language_id / preview.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
getFormInvalidData,
|
|
8
|
+
getFormListFailure,
|
|
9
|
+
getFormListRestoreBlocked,
|
|
10
|
+
getFormStaleVersion,
|
|
11
|
+
} from '../formListErrors';
|
|
12
|
+
|
|
13
|
+
/** An axios-style failure with the backend's error envelope. */
|
|
14
|
+
const refused = (status: number, data: unknown, message = 'refused') => ({
|
|
15
|
+
response: { status, data: { status, message, error: message, data } },
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('getFormStaleVersion', () => {
|
|
19
|
+
const stale = { reason: 'stale_version', record_id: 41, expected_version: 2, current_version: 3 };
|
|
20
|
+
|
|
21
|
+
it('reads the 409 of a stale write', () => {
|
|
22
|
+
expect(getFormStaleVersion(refused(409, stale))).toEqual(stale);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('ignores other statuses, other reasons and incomplete payloads', () => {
|
|
26
|
+
expect(getFormStaleVersion(refused(422, stale))).toBeNull();
|
|
27
|
+
expect(getFormStaleVersion(refused(409, { ...stale, reason: 'something_else' }))).toBeNull();
|
|
28
|
+
expect(getFormStaleVersion(refused(409, { reason: 'stale_version', record_id: 41 }))).toBeNull();
|
|
29
|
+
expect(getFormStaleVersion(refused(409, { ...stale, current_version: '3' }))).toBeNull();
|
|
30
|
+
expect(getFormStaleVersion(refused(409, null))).toBeNull();
|
|
31
|
+
expect(getFormStaleVersion(refused(409, [stale]))).toBeNull();
|
|
32
|
+
expect(getFormStaleVersion(new Error('Network Error'))).toBeNull();
|
|
33
|
+
expect(getFormStaleVersion(undefined)).toBeNull();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('getFormListRestoreBlocked', () => {
|
|
38
|
+
const blocker = { field_key: 'section_9', field_label: 'Title', language_id: 1, reason: 'field_missing', detail: null };
|
|
39
|
+
const blocked = { reason: 'restore_blocked', record_id: 41, from_version: 2, blockers: [blocker] };
|
|
40
|
+
|
|
41
|
+
it('reads the 422 of a restore that cannot be applied whole', () => {
|
|
42
|
+
expect(getFormListRestoreBlocked(refused(422, blocked))).toEqual(blocked);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('ignores other statuses, other reasons and a payload without blockers', () => {
|
|
46
|
+
expect(getFormListRestoreBlocked(refused(409, blocked))).toBeNull();
|
|
47
|
+
expect(getFormListRestoreBlocked(refused(422, { ...blocked, reason: 'invalid' }))).toBeNull();
|
|
48
|
+
expect(getFormListRestoreBlocked(refused(422, { ...blocked, blockers: 'none' }))).toBeNull();
|
|
49
|
+
expect(getFormListRestoreBlocked(refused(422, { reason: 'restore_blocked', record_id: 41 }))).toBeNull();
|
|
50
|
+
expect(getFormListRestoreBlocked(null)).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('getFormInvalidData', () => {
|
|
55
|
+
const error = { field: 'Title', key: 'section_9', rule: 'required', message: 'This field is required.' };
|
|
56
|
+
const invalid = { reason: 'invalid_form_data', errors: [error, { ...error, field: 'Age', key: 'section_10', rule: 'min' }] };
|
|
57
|
+
|
|
58
|
+
it('reads the 422 of a write that breaks the rules of the current form', () => {
|
|
59
|
+
expect(getFormInvalidData(refused(422, invalid))).toEqual(invalid);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('keeps the order the server sent and every documented rule as it came', () => {
|
|
63
|
+
const rules = ['not_available', 'read_only', 'required', 'invalid', 'too_many', 'type', 'min', 'max', 'max_length', 'format', 'option', 'min_date', 'max_date', 'file_type'];
|
|
64
|
+
const data = { reason: 'invalid_form_data', errors: rules.map((rule, index) => ({ ...error, key: `section_${index}`, rule })) };
|
|
65
|
+
expect(getFormInvalidData(refused(422, data))?.errors.map((entry) => entry.rule)).toEqual(rules);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('drops malformed entries and gives up when none is left', () => {
|
|
69
|
+
expect(getFormInvalidData(refused(422, { ...invalid, errors: [error, null, 'text', { field: 'x' }, { ...error, message: 3 }] }))).toEqual({
|
|
70
|
+
reason: 'invalid_form_data',
|
|
71
|
+
errors: [error],
|
|
72
|
+
});
|
|
73
|
+
expect(getFormInvalidData(refused(422, { ...invalid, errors: [null, { field: 'x' }] }))).toBeNull();
|
|
74
|
+
expect(getFormInvalidData(refused(422, { ...invalid, errors: [] }))).toBeNull();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('ignores other statuses, other reasons and a payload without a list of errors', () => {
|
|
78
|
+
expect(getFormInvalidData(refused(400, invalid))).toBeNull();
|
|
79
|
+
expect(getFormInvalidData(refused(422, { ...invalid, reason: 'restore_blocked' }))).toBeNull();
|
|
80
|
+
expect(getFormInvalidData(refused(422, { reason: 'invalid_form_data' }))).toBeNull();
|
|
81
|
+
expect(getFormInvalidData(refused(422, { reason: 'invalid_form_data', errors: 'none' }))).toBeNull();
|
|
82
|
+
expect(getFormInvalidData(refused(422, null))).toBeNull();
|
|
83
|
+
expect(getFormInvalidData(new Error('Network Error'))).toBeNull();
|
|
84
|
+
expect(getFormInvalidData(undefined)).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('is also reported by reason, without its entries, by getFormListFailure', () => {
|
|
88
|
+
expect(getFormListFailure(refused(422, invalid))).toMatchObject({ status: 422, reason: 'invalid_form_data' });
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('getFormListFailure', () => {
|
|
93
|
+
it.each([
|
|
94
|
+
[400, 'expected_version_required'],
|
|
95
|
+
[400, 'invalid_expected_version'],
|
|
96
|
+
[400, 'unsupported_target_selection'],
|
|
97
|
+
[400, 'protected_field'],
|
|
98
|
+
[400, 'unknown_field'],
|
|
99
|
+
[400, 'invalid_file_value'],
|
|
100
|
+
[404, 'unknown_action'],
|
|
101
|
+
[409, 'action_not_available'],
|
|
102
|
+
])('names the %i write refusal %s so a client branches on it, not on the message', (status, reason) => {
|
|
103
|
+
expect(getFormListFailure(refused(status, { reason }, 'a message that may change'))).toEqual({
|
|
104
|
+
status,
|
|
105
|
+
reason,
|
|
106
|
+
parameter: null,
|
|
107
|
+
message: 'a message that may change',
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('names the reason and the parameter a client may drop', () => {
|
|
112
|
+
expect(getFormListFailure(refused(400, { reason: 'invalid_list_parameter', parameter: 'tasks.page' }, 'bad page'))).toEqual({
|
|
113
|
+
status: 400,
|
|
114
|
+
reason: 'invalid_list_parameter',
|
|
115
|
+
parameter: 'tasks.page',
|
|
116
|
+
message: 'bad page',
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('reports a refusal without a parameter', () => {
|
|
121
|
+
expect(getFormListFailure(refused(403, { reason: 'operation_disabled' }))).toMatchObject({
|
|
122
|
+
status: 403,
|
|
123
|
+
reason: 'operation_disabled',
|
|
124
|
+
parameter: null,
|
|
125
|
+
});
|
|
126
|
+
expect(getFormListFailure(refused(401, { reason: 'login_required', parameter: '' }))).toMatchObject({ parameter: null });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('also reads the stale-version and restore-blocked refusals by reason', () => {
|
|
130
|
+
expect(getFormListFailure(refused(409, { reason: 'stale_version', record_id: 1, expected_version: 1, current_version: 2 }))).toMatchObject({
|
|
131
|
+
status: 409,
|
|
132
|
+
reason: 'stale_version',
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('has nothing to say about errors without a reason', () => {
|
|
137
|
+
expect(getFormListFailure(refused(500, null))).toBeNull();
|
|
138
|
+
expect(getFormListFailure(refused(500, { reason: '' }))).toBeNull();
|
|
139
|
+
expect(getFormListFailure(refused(500, { reason: 12 }))).toBeNull();
|
|
140
|
+
expect(getFormListFailure(new Error('Network Error'))).toBeNull();
|
|
141
|
+
expect(getFormListFailure({ response: { data: { data: { reason: 'x' } } } })).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
});
|
package/src/api/endpoints.ts
CHANGED
|
@@ -62,6 +62,47 @@ export const ENDPOINTS = {
|
|
|
62
62
|
UPDATE: `${API_VERSION_PREFIX}/forms/update`,
|
|
63
63
|
DELETE: `${API_VERSION_PREFIX}/forms/delete`,
|
|
64
64
|
},
|
|
65
|
+
/**
|
|
66
|
+
* `entry-form-list` / `data-view` lists. A list never has its own create,
|
|
67
|
+
* update or delete route: writes go through {@link ENDPOINTS.FORMS}. The
|
|
68
|
+
* first paint reads `list_state` from the page payload; these routes serve
|
|
69
|
+
* the interactions after it. Path segments are numeric ids, except the
|
|
70
|
+
* action id and the field key, which are encoded.
|
|
71
|
+
*/
|
|
72
|
+
LISTS: {
|
|
73
|
+
STATE: (sectionId: number): string => `${API_VERSION_PREFIX}/lists/${sectionId}/state`,
|
|
74
|
+
QUICK_ACTION: (sectionId: number, recordId: number, actionId: string): string =>
|
|
75
|
+
`${API_VERSION_PREFIX}/lists/${sectionId}/records/${recordId}/actions/${encodeURIComponent(actionId)}`,
|
|
76
|
+
HISTORY: (sectionId: number, recordId: number): string =>
|
|
77
|
+
`${API_VERSION_PREFIX}/lists/${sectionId}/records/${recordId}/history`,
|
|
78
|
+
VERSION: (sectionId: number, recordId: number, version: number): string =>
|
|
79
|
+
`${API_VERSION_PREFIX}/lists/${sectionId}/records/${recordId}/versions/${version}`,
|
|
80
|
+
COMPARE: (sectionId: number, recordId: number): string =>
|
|
81
|
+
`${API_VERSION_PREFIX}/lists/${sectionId}/records/${recordId}/compare`,
|
|
82
|
+
RESTORE: (sectionId: number, recordId: number): string =>
|
|
83
|
+
`${API_VERSION_PREFIX}/lists/${sectionId}/records/${recordId}/restore`,
|
|
84
|
+
VERSION_FILE: (sectionId: number, recordId: number, version: number, fieldKey: string): string =>
|
|
85
|
+
`${API_VERSION_PREFIX}/lists/${sectionId}/records/${recordId}/versions/${version}/files/${encodeURIComponent(fieldKey)}`,
|
|
86
|
+
/** Current file of a record shown by a list / data-view / entry-record (F165). */
|
|
87
|
+
CURRENT_FILE: (sectionId: number, recordId: number, fieldKey: string): string =>
|
|
88
|
+
`${API_VERSION_PREFIX}/lists/${sectionId}/records/${recordId}/files/${encodeURIComponent(fieldKey)}`,
|
|
89
|
+
},
|
|
90
|
+
/**
|
|
91
|
+
* Admin data-browser record history (contract 6). Route permission
|
|
92
|
+
* `admin.data.read`; the controller also requires table UPDATE.
|
|
93
|
+
*/
|
|
94
|
+
ADMIN_DATA_HISTORY: {
|
|
95
|
+
HISTORY: (tableName: string, recordId: number): string =>
|
|
96
|
+
`${API_VERSION_PREFIX}/admin/data/tables/${encodeURIComponent(tableName)}/records/${recordId}/history`,
|
|
97
|
+
VERSION: (tableName: string, recordId: number, version: number): string =>
|
|
98
|
+
`${API_VERSION_PREFIX}/admin/data/tables/${encodeURIComponent(tableName)}/records/${recordId}/versions/${version}`,
|
|
99
|
+
COMPARE: (tableName: string, recordId: number): string =>
|
|
100
|
+
`${API_VERSION_PREFIX}/admin/data/tables/${encodeURIComponent(tableName)}/records/${recordId}/compare`,
|
|
101
|
+
RESTORE: (tableName: string, recordId: number): string =>
|
|
102
|
+
`${API_VERSION_PREFIX}/admin/data/tables/${encodeURIComponent(tableName)}/records/${recordId}/restore`,
|
|
103
|
+
VERSION_FILE: (tableName: string, recordId: number, version: number, fieldKey: string): string =>
|
|
104
|
+
`${API_VERSION_PREFIX}/admin/data/tables/${encodeURIComponent(tableName)}/records/${recordId}/versions/${version}/files/${encodeURIComponent(fieldKey)}`,
|
|
105
|
+
},
|
|
65
106
|
} as const;
|
|
66
107
|
|
|
67
108
|
/** Header set the backend reads to switch page-access mode + condition platform var. */
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Structured refusals of the entry-list workflow, read the same way by the web
|
|
8
|
+
* and mobile clients. Every refusal travels in the normal error envelope
|
|
9
|
+
* (`error.response.status`, `error.response.data.data`) with a stable
|
|
10
|
+
* `data.reason`; clients branch on the reason, never on the message text.
|
|
11
|
+
*
|
|
12
|
+
* - `409 stale_version`: the record changed since the client read it. Nothing was
|
|
13
|
+
* written. Keep the draft, offer reload, comparison and re-apply.
|
|
14
|
+
* - `422 restore_blocked`: an earlier version cannot be written back whole today.
|
|
15
|
+
* Nothing was applied; the blockers say which fields and why.
|
|
16
|
+
* - `422 invalid_form_data`: a value breaks the rules of the CURRENT form (a save,
|
|
17
|
+
* the candidate state of a quick action, or a restore). The whole write was
|
|
18
|
+
* refused: keep the draft and the chosen files and show the errors
|
|
19
|
+
* ({@link getFormInvalidData}).
|
|
20
|
+
* - any other list refusal (`invalid_list_parameter`, `unknown_list_parameter`,
|
|
21
|
+
* `duplicate_list_parameter`, `operation_disabled`, `history_disabled`,
|
|
22
|
+
* `login_required`, `expected_version_required`, ...): {@link getFormListFailure};
|
|
23
|
+
* `parameter` names the URL parameter a client may drop before it retries. The
|
|
24
|
+
* reasons a write can answer with are listed in `TFormWriteRefusalReason`
|
|
25
|
+
* (`types/api/forms.ts`); a client never repairs them by guessing, it fixes the
|
|
26
|
+
* request (the version it sends, the inputs it names).
|
|
27
|
+
*/
|
|
28
|
+
import type {
|
|
29
|
+
IFormListRestoreBlockedData,
|
|
30
|
+
IFormListRestoreBlocker,
|
|
31
|
+
IFormStaleVersionData,
|
|
32
|
+
} from '../types/api/form-lists';
|
|
33
|
+
import type { IFormInputError, IFormInvalidData, TFormInputRule } from '../types/api/forms';
|
|
34
|
+
|
|
35
|
+
interface IListErrorShape {
|
|
36
|
+
response?: {
|
|
37
|
+
status?: number;
|
|
38
|
+
data?: { message?: unknown; data?: unknown } | null;
|
|
39
|
+
} | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** What a failed list request says, independent of the status it came with. */
|
|
43
|
+
export interface IFormListFailure {
|
|
44
|
+
status: number;
|
|
45
|
+
/** Stable machine-readable cause (`data.reason`). */
|
|
46
|
+
reason: string;
|
|
47
|
+
/** The URL parameter that caused it, when the server names one. */
|
|
48
|
+
parameter: string | null;
|
|
49
|
+
/** The server's message, for logs; never branch on it. */
|
|
50
|
+
message: string | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function envelope(error: unknown): { status: number; message: unknown; data: Record<string, unknown> } | null {
|
|
54
|
+
const response = (error as IListErrorShape | null | undefined)?.response;
|
|
55
|
+
const status = response?.status;
|
|
56
|
+
const data = response?.data?.data;
|
|
57
|
+
if (typeof status !== 'number' || typeof data !== 'object' || data === null || Array.isArray(data)) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { status, message: response?.data?.message, data: data as Record<string, unknown> };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The 409 of a stale save, delete, quick action or restore, else `null`. */
|
|
65
|
+
export function getFormStaleVersion(error: unknown): IFormStaleVersionData | null {
|
|
66
|
+
const found = envelope(error);
|
|
67
|
+
if (found === null || found.status !== 409 || found.data.reason !== 'stale_version') {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
const { record_id: recordId, expected_version: expected, current_version: current } = found.data;
|
|
71
|
+
if (typeof recordId !== 'number' || typeof expected !== 'number' || typeof current !== 'number') {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return { reason: 'stale_version', record_id: recordId, expected_version: expected, current_version: current };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The 422 of a restore that cannot be applied whole, else `null`. */
|
|
79
|
+
export function getFormListRestoreBlocked(error: unknown): IFormListRestoreBlockedData | null {
|
|
80
|
+
const found = envelope(error);
|
|
81
|
+
if (found === null || found.status !== 422 || found.data.reason !== 'restore_blocked') {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const { record_id: recordId, from_version: fromVersion, blockers } = found.data;
|
|
85
|
+
if (typeof recordId !== 'number' || typeof fromVersion !== 'number' || !Array.isArray(blockers)) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
reason: 'restore_blocked',
|
|
91
|
+
record_id: recordId,
|
|
92
|
+
from_version: fromVersion,
|
|
93
|
+
blockers: blockers as IFormListRestoreBlocker[],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The 422 of a write whose values break the rules of the CURRENT form, else
|
|
99
|
+
* `null`. Entries without the four documented text keys are dropped; when none is
|
|
100
|
+
* left the helper answers `null` and the caller falls back to {@link getFormListFailure}.
|
|
101
|
+
*/
|
|
102
|
+
export function getFormInvalidData(error: unknown): IFormInvalidData | null {
|
|
103
|
+
const found = envelope(error);
|
|
104
|
+
if (found === null || found.status !== 422 || found.data.reason !== 'invalid_form_data' || !Array.isArray(found.data.errors)) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const errors: IFormInputError[] = [];
|
|
108
|
+
for (const entry of found.data.errors as unknown[]) {
|
|
109
|
+
if (typeof entry !== 'object' || entry === null) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const { field, key, rule, message } = entry as Record<string, unknown>;
|
|
113
|
+
if (typeof field === 'string' && typeof key === 'string' && typeof rule === 'string' && typeof message === 'string') {
|
|
114
|
+
errors.push({ field, key, rule: rule as TFormInputRule, message });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return errors.length > 0 ? { reason: 'invalid_form_data', errors } : null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Any refusal that carries a `data.reason`, else `null` (a network error, a bare 500). */
|
|
122
|
+
export function getFormListFailure(error: unknown): IFormListFailure | null {
|
|
123
|
+
const found = envelope(error);
|
|
124
|
+
if (found === null || typeof found.data.reason !== 'string' || found.data.reason === '') {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
status: found.status,
|
|
130
|
+
reason: found.data.reason,
|
|
131
|
+
parameter: typeof found.data.parameter === 'string' && found.data.parameter !== '' ? found.data.parameter : null,
|
|
132
|
+
message: typeof found.message === 'string' ? found.message : null,
|
|
133
|
+
};
|
|
134
|
+
}
|