@velion-la/onboarding-sdk-react-native 1.4.0-beta.7 → 1.4.0-beta.9
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 +56 -0
- package/README.md +82 -1
- package/lib/commonjs/core/VelionOnboarding.js +156 -46
- package/lib/commonjs/core/VelionOnboarding.js.map +1 -1
- package/lib/commonjs/core/routingCommands.js +33 -0
- package/lib/commonjs/core/routingCommands.js.map +1 -1
- package/lib/commonjs/core/sessionStateMapper.js +48 -36
- package/lib/commonjs/core/sessionStateMapper.js.map +1 -1
- package/lib/commonjs/i18n/strings/en.js +27 -0
- package/lib/commonjs/i18n/strings/en.js.map +1 -1
- package/lib/commonjs/i18n/strings/es.js +27 -0
- package/lib/commonjs/i18n/strings/es.js.map +1 -1
- package/lib/commonjs/index.js +6 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/steps/CustomFormStep.js +348 -0
- package/lib/commonjs/steps/CustomFormStep.js.map +1 -0
- package/lib/commonjs/steps/EntityTypeSelectionStep.js +194 -0
- package/lib/commonjs/steps/EntityTypeSelectionStep.js.map +1 -0
- package/lib/commonjs/steps/customForm.js +251 -0
- package/lib/commonjs/steps/customForm.js.map +1 -0
- package/lib/commonjs/steps/entityType.js +30 -0
- package/lib/commonjs/steps/entityType.js.map +1 -0
- package/lib/commonjs/steps/submissionOutcome.js +101 -0
- package/lib/commonjs/steps/submissionOutcome.js.map +1 -0
- package/lib/commonjs/steps/unsupportedStep.js +33 -0
- package/lib/commonjs/steps/unsupportedStep.js.map +1 -0
- package/lib/commonjs/transport/api.js +9 -0
- package/lib/commonjs/transport/api.js.map +1 -1
- package/lib/commonjs/types/index.js +60 -4
- package/lib/commonjs/types/index.js.map +1 -1
- package/lib/commonjs/ui/VelionOnboardingScreen.js +20 -3
- package/lib/commonjs/ui/VelionOnboardingScreen.js.map +1 -1
- package/lib/commonjs/ui/primitives/RadioCard.js +120 -0
- package/lib/commonjs/ui/primitives/RadioCard.js.map +1 -0
- package/lib/commonjs/ui/primitives/Select.js +60 -7
- package/lib/commonjs/ui/primitives/Select.js.map +1 -1
- package/lib/commonjs/ui/primitives/index.js +7 -0
- package/lib/commonjs/ui/primitives/index.js.map +1 -1
- package/lib/commonjs/utils/sdkVersion.js +1 -1
- package/lib/module/core/VelionOnboarding.js +158 -48
- package/lib/module/core/VelionOnboarding.js.map +1 -1
- package/lib/module/core/routingCommands.js +33 -0
- package/lib/module/core/routingCommands.js.map +1 -1
- package/lib/module/core/sessionStateMapper.js +47 -36
- package/lib/module/core/sessionStateMapper.js.map +1 -1
- package/lib/module/i18n/strings/en.js +27 -0
- package/lib/module/i18n/strings/en.js.map +1 -1
- package/lib/module/i18n/strings/es.js +27 -0
- package/lib/module/i18n/strings/es.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/steps/CustomFormStep.js +345 -0
- package/lib/module/steps/CustomFormStep.js.map +1 -0
- package/lib/module/steps/EntityTypeSelectionStep.js +189 -0
- package/lib/module/steps/EntityTypeSelectionStep.js.map +1 -0
- package/lib/module/steps/customForm.js +237 -0
- package/lib/module/steps/customForm.js.map +1 -0
- package/lib/module/steps/entityType.js +24 -0
- package/lib/module/steps/entityType.js.map +1 -0
- package/lib/module/steps/submissionOutcome.js +94 -0
- package/lib/module/steps/submissionOutcome.js.map +1 -0
- package/lib/module/steps/unsupportedStep.js +30 -0
- package/lib/module/steps/unsupportedStep.js.map +1 -0
- package/lib/module/transport/api.js +9 -0
- package/lib/module/transport/api.js.map +1 -1
- package/lib/module/types/index.js +68 -3
- package/lib/module/types/index.js.map +1 -1
- package/lib/module/ui/VelionOnboardingScreen.js +21 -4
- package/lib/module/ui/VelionOnboardingScreen.js.map +1 -1
- package/lib/module/ui/primitives/RadioCard.js +116 -0
- package/lib/module/ui/primitives/RadioCard.js.map +1 -0
- package/lib/module/ui/primitives/Select.js +62 -9
- package/lib/module/ui/primitives/Select.js.map +1 -1
- package/lib/module/ui/primitives/index.js +1 -0
- package/lib/module/ui/primitives/index.js.map +1 -1
- package/lib/module/utils/sdkVersion.js +1 -1
- package/lib/typescript/core/VelionOnboarding.d.ts +42 -5
- package/lib/typescript/core/VelionOnboarding.d.ts.map +1 -1
- package/lib/typescript/core/routingCommands.d.ts +23 -0
- package/lib/typescript/core/routingCommands.d.ts.map +1 -1
- package/lib/typescript/core/sessionStateMapper.d.ts +13 -5
- package/lib/typescript/core/sessionStateMapper.d.ts.map +1 -1
- package/lib/typescript/i18n/strings/en.d.ts.map +1 -1
- package/lib/typescript/i18n/strings/es.d.ts +27 -0
- package/lib/typescript/i18n/strings/es.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +3 -3
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/steps/CustomFormStep.d.ts +28 -0
- package/lib/typescript/steps/CustomFormStep.d.ts.map +1 -0
- package/lib/typescript/steps/EntityTypeSelectionStep.d.ts +24 -0
- package/lib/typescript/steps/EntityTypeSelectionStep.d.ts.map +1 -0
- package/lib/typescript/steps/customForm.d.ts +99 -0
- package/lib/typescript/steps/customForm.d.ts.map +1 -0
- package/lib/typescript/steps/entityType.d.ts +15 -0
- package/lib/typescript/steps/entityType.d.ts.map +1 -0
- package/lib/typescript/steps/submissionOutcome.d.ts +43 -0
- package/lib/typescript/steps/submissionOutcome.d.ts.map +1 -0
- package/lib/typescript/steps/types.d.ts +5 -0
- package/lib/typescript/steps/types.d.ts.map +1 -1
- package/lib/typescript/steps/unsupportedStep.d.ts +15 -0
- package/lib/typescript/steps/unsupportedStep.d.ts.map +1 -0
- package/lib/typescript/transport/api.d.ts +1 -0
- package/lib/typescript/transport/api.d.ts.map +1 -1
- package/lib/typescript/types/index.d.ts +139 -14
- package/lib/typescript/types/index.d.ts.map +1 -1
- package/lib/typescript/ui/VelionOnboardingScreen.d.ts +21 -2
- package/lib/typescript/ui/VelionOnboardingScreen.d.ts.map +1 -1
- package/lib/typescript/ui/primitives/RadioCard.d.ts +22 -0
- package/lib/typescript/ui/primitives/RadioCard.d.ts.map +1 -0
- package/lib/typescript/ui/primitives/Select.d.ts +9 -3
- package/lib/typescript/ui/primitives/Select.d.ts.map +1 -1
- package/lib/typescript/ui/primitives/index.d.ts +1 -0
- package/lib/typescript/ui/primitives/index.d.ts.map +1 -1
- package/lib/typescript/utils/sdkVersion.d.ts +1 -1
- package/package.json +1 -1
- package/src/core/VelionOnboarding.ts +168 -55
- package/src/core/routingCommands.ts +42 -0
- package/src/core/sessionStateMapper.ts +63 -52
- package/src/i18n/strings/en.ts +28 -0
- package/src/i18n/strings/es.ts +28 -0
- package/src/index.ts +10 -0
- package/src/steps/CustomFormStep.tsx +400 -0
- package/src/steps/EntityTypeSelectionStep.tsx +207 -0
- package/src/steps/customForm.ts +302 -0
- package/src/steps/entityType.ts +25 -0
- package/src/steps/submissionOutcome.ts +93 -0
- package/src/steps/types.ts +5 -0
- package/src/steps/unsupportedStep.ts +32 -0
- package/src/transport/api.ts +16 -1
- package/src/types/index.ts +151 -17
- package/src/ui/VelionOnboardingScreen.tsx +38 -1
- package/src/ui/primitives/RadioCard.tsx +127 -0
- package/src/ui/primitives/Select.tsx +73 -7
- package/src/ui/primitives/index.ts +1 -0
- package/src/utils/sdkVersion.ts +1 -1
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Pure logic of the `CUSTOM_FORM` step: reading `form_fields`,
|
|
3
|
+
* the per-type validation and the `responses` payload (spike VON-131 §3.2).
|
|
4
|
+
*
|
|
5
|
+
* JSX-free so the rules run under Jest in the `node` environment. The server
|
|
6
|
+
* validates the same rules and is the authority; the SDK checks them only to
|
|
7
|
+
* answer before the round trip.
|
|
8
|
+
*
|
|
9
|
+
* | type | value sent | required means |
|
|
10
|
+
* |-----------|-----------------------------|---------------------------|
|
|
11
|
+
* | `TEXT` | string, trimmed, ≤ 255 | not empty after the trim |
|
|
12
|
+
* | `NUMBER` | JSON number (`,` or `.`) | present and finite |
|
|
13
|
+
* | `BOOLEAN` | `true` / `false` (Yes / No) | answered |
|
|
14
|
+
* | `DATE` | `"yyyy-MM-dd"`, a real date | a valid date |
|
|
15
|
+
* | `LIST` | one string of `options` | one of `options` |
|
|
16
|
+
*
|
|
17
|
+
* An optional field left unanswered is omitted from `responses`, never `null`.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
CustomFormFieldType,
|
|
22
|
+
CustomFormResponseValue,
|
|
23
|
+
} from '../types';
|
|
24
|
+
import type { StringsTable } from '../i18n';
|
|
25
|
+
|
|
26
|
+
/** Longest `TEXT` answer the backend accepts, after trimming. */
|
|
27
|
+
export const MAX_TEXT_LENGTH = 255;
|
|
28
|
+
|
|
29
|
+
/** `LIST` fields with more options than this render a searchable picker. */
|
|
30
|
+
export const SEARCHABLE_LIST_THRESHOLD = 8;
|
|
31
|
+
|
|
32
|
+
const KNOWN_TYPES: ReadonlySet<string> = new Set<CustomFormFieldType>([
|
|
33
|
+
'TEXT',
|
|
34
|
+
'NUMBER',
|
|
35
|
+
'BOOLEAN',
|
|
36
|
+
'DATE',
|
|
37
|
+
'LIST',
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
/** Prefix of every per-field key in the backend's 400 `errors`. */
|
|
41
|
+
const RESPONSES_ERROR_PREFIX = 'responses.';
|
|
42
|
+
|
|
43
|
+
/** A `form_fields` entry, normalized. */
|
|
44
|
+
export interface CustomFormFieldDef {
|
|
45
|
+
fieldKey: string;
|
|
46
|
+
name: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
/** Rendered type; an unknown optional type is already downgraded to `TEXT`. */
|
|
49
|
+
type: CustomFormFieldType;
|
|
50
|
+
required: boolean;
|
|
51
|
+
/** Choices of a `LIST` field; empty for the other types. */
|
|
52
|
+
options: string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The step config, normalized. */
|
|
56
|
+
export interface ParsedCustomForm {
|
|
57
|
+
title?: string;
|
|
58
|
+
subtitle?: string;
|
|
59
|
+
fields: CustomFormFieldDef[];
|
|
60
|
+
/** Required fields whose type this SDK does not know: the step cannot be answered. */
|
|
61
|
+
unsupportedFields: Array<{ fieldKey: string; type: string }>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* What the form holds for one field while the person edits it: the text typed
|
|
66
|
+
* for `TEXT` / `NUMBER`, the choice for `BOOLEAN`, the ISO date for `DATE` and
|
|
67
|
+
* the option for `LIST` (`null` while unanswered).
|
|
68
|
+
*/
|
|
69
|
+
export type FieldDraft = string | boolean | null;
|
|
70
|
+
|
|
71
|
+
/** Validation copy, from `StringsTable.custom_form`. */
|
|
72
|
+
export type CustomFormMessages = Pick<
|
|
73
|
+
StringsTable['custom_form'],
|
|
74
|
+
'required' | 'textTooLong' | 'invalidNumber' | 'invalidBoolean' | 'invalidDate' | 'invalidOption'
|
|
75
|
+
>;
|
|
76
|
+
|
|
77
|
+
function nonEmptyString(value: unknown): string | undefined {
|
|
78
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Reads `title`, `subtitle` and `form_fields` from the step config. Entries
|
|
83
|
+
* without a `field_key` are dropped. A field whose `type` is unknown is shown
|
|
84
|
+
* as `TEXT` when optional and reported in `unsupportedFields` when required.
|
|
85
|
+
*/
|
|
86
|
+
export function parseCustomFormConfig(
|
|
87
|
+
config: Record<string, unknown> | undefined,
|
|
88
|
+
): ParsedCustomForm {
|
|
89
|
+
const title = nonEmptyString(config?.title);
|
|
90
|
+
const subtitle = nonEmptyString(config?.subtitle);
|
|
91
|
+
const rawFields = Array.isArray(config?.form_fields) ? config.form_fields : [];
|
|
92
|
+
const fields: CustomFormFieldDef[] = [];
|
|
93
|
+
const unsupportedFields: ParsedCustomForm['unsupportedFields'] = [];
|
|
94
|
+
const seen = new Set<string>();
|
|
95
|
+
|
|
96
|
+
for (const raw of rawFields) {
|
|
97
|
+
if (raw === null || typeof raw !== 'object') continue;
|
|
98
|
+
const entry = raw as Record<string, unknown>;
|
|
99
|
+
const fieldKey = nonEmptyString(entry.field_key);
|
|
100
|
+
if (!fieldKey || seen.has(fieldKey)) continue;
|
|
101
|
+
seen.add(fieldKey);
|
|
102
|
+
const required = entry.required === true;
|
|
103
|
+
const rawType = typeof entry.type === 'string' ? entry.type.trim().toUpperCase() : '';
|
|
104
|
+
if (!KNOWN_TYPES.has(rawType)) {
|
|
105
|
+
if (required) {
|
|
106
|
+
unsupportedFields.push({ fieldKey, type: String(entry.type) });
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const type = (KNOWN_TYPES.has(rawType) ? rawType : 'TEXT') as CustomFormFieldType;
|
|
111
|
+
const description = nonEmptyString(entry.description);
|
|
112
|
+
const options =
|
|
113
|
+
type === 'LIST' && Array.isArray(entry.options)
|
|
114
|
+
? entry.options.filter((o): o is string => typeof o === 'string' && o !== '')
|
|
115
|
+
: [];
|
|
116
|
+
fields.push({
|
|
117
|
+
fieldKey,
|
|
118
|
+
name: nonEmptyString(entry.name) ?? fieldKey,
|
|
119
|
+
...(description !== undefined && { description }),
|
|
120
|
+
type,
|
|
121
|
+
required,
|
|
122
|
+
options,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
...(title !== undefined && { title }),
|
|
128
|
+
...(subtitle !== undefined && { subtitle }),
|
|
129
|
+
fields,
|
|
130
|
+
unsupportedFields,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** True for a `yyyy-MM-dd` string naming a real calendar date. */
|
|
135
|
+
export function isRealIsoDate(value: string): boolean {
|
|
136
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
|
|
137
|
+
if (!m) return false;
|
|
138
|
+
const year = Number(m[1]);
|
|
139
|
+
const month = Number(m[2]);
|
|
140
|
+
const day = Number(m[3]);
|
|
141
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
142
|
+
return (
|
|
143
|
+
date.getUTCFullYear() === year &&
|
|
144
|
+
date.getUTCMonth() === month - 1 &&
|
|
145
|
+
date.getUTCDate() === day
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Parses what the person typed in a `NUMBER` field. Either a comma or a dot is
|
|
151
|
+
* the decimal separator; thousands separators are not accepted. Returns `null`
|
|
152
|
+
* for blank or invalid text.
|
|
153
|
+
*/
|
|
154
|
+
export function parseNumberInput(text: string): number | null {
|
|
155
|
+
const normalized = text.trim().replace(',', '.');
|
|
156
|
+
if (!/^[+-]?(\d+(\.\d+)?|\.\d+)$/.test(normalized)) return null;
|
|
157
|
+
const value = Number(normalized);
|
|
158
|
+
return Number.isFinite(value) ? value : null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The empty draft of a field. */
|
|
162
|
+
export function emptyDraft(field: CustomFormFieldDef): FieldDraft {
|
|
163
|
+
return field.type === 'TEXT' || field.type === 'NUMBER' ? '' : null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Draft seeded from a previous answer (`result.responses` on reverse
|
|
168
|
+
* navigation). An answer that no longer fits the field is ignored.
|
|
169
|
+
*/
|
|
170
|
+
export function draftFromAnswer(field: CustomFormFieldDef, answer: unknown): FieldDraft {
|
|
171
|
+
switch (field.type) {
|
|
172
|
+
case 'TEXT':
|
|
173
|
+
return typeof answer === 'string' ? answer : emptyDraft(field);
|
|
174
|
+
case 'NUMBER':
|
|
175
|
+
if (typeof answer === 'number' && Number.isFinite(answer)) return String(answer);
|
|
176
|
+
return typeof answer === 'string' && parseNumberInput(answer) !== null
|
|
177
|
+
? answer
|
|
178
|
+
: emptyDraft(field);
|
|
179
|
+
case 'BOOLEAN':
|
|
180
|
+
return typeof answer === 'boolean' ? answer : null;
|
|
181
|
+
case 'DATE':
|
|
182
|
+
return typeof answer === 'string' && isRealIsoDate(answer) ? answer : null;
|
|
183
|
+
case 'LIST':
|
|
184
|
+
return typeof answer === 'string' && field.options.includes(answer) ? answer : null;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Initial drafts of every field. `stepResult` is the step's previous `result`
|
|
190
|
+
* (`{ responses: { <field_key>: value } }`), present after reverse navigation.
|
|
191
|
+
*/
|
|
192
|
+
export function initialDrafts(
|
|
193
|
+
fields: ReadonlyArray<CustomFormFieldDef>,
|
|
194
|
+
stepResult: Record<string, unknown> | undefined,
|
|
195
|
+
): Record<string, FieldDraft> {
|
|
196
|
+
const rawResponses = stepResult?.responses;
|
|
197
|
+
const responses =
|
|
198
|
+
rawResponses !== null && typeof rawResponses === 'object' && !Array.isArray(rawResponses)
|
|
199
|
+
? (rawResponses as Record<string, unknown>)
|
|
200
|
+
: {};
|
|
201
|
+
const drafts: Record<string, FieldDraft> = {};
|
|
202
|
+
for (const field of fields) {
|
|
203
|
+
drafts[field.fieldKey] = draftFromAnswer(field, responses[field.fieldKey]);
|
|
204
|
+
}
|
|
205
|
+
return drafts;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function isUnanswered(field: CustomFormFieldDef, draft: FieldDraft): boolean {
|
|
209
|
+
if (draft === null) return true;
|
|
210
|
+
return typeof draft === 'string' && (field.type === 'TEXT' || field.type === 'NUMBER')
|
|
211
|
+
? draft.trim() === ''
|
|
212
|
+
: false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Validates one field. Returns the message to show, or `null` when valid. */
|
|
216
|
+
export function validateField(
|
|
217
|
+
field: CustomFormFieldDef,
|
|
218
|
+
draft: FieldDraft,
|
|
219
|
+
messages: CustomFormMessages,
|
|
220
|
+
): string | null {
|
|
221
|
+
if (isUnanswered(field, draft)) {
|
|
222
|
+
return field.required ? messages.required : null;
|
|
223
|
+
}
|
|
224
|
+
switch (field.type) {
|
|
225
|
+
case 'TEXT':
|
|
226
|
+
return typeof draft === 'string' && draft.trim().length <= MAX_TEXT_LENGTH
|
|
227
|
+
? null
|
|
228
|
+
: messages.textTooLong;
|
|
229
|
+
case 'NUMBER':
|
|
230
|
+
return typeof draft === 'string' && parseNumberInput(draft) !== null
|
|
231
|
+
? null
|
|
232
|
+
: messages.invalidNumber;
|
|
233
|
+
case 'BOOLEAN':
|
|
234
|
+
return typeof draft === 'boolean' ? null : messages.invalidBoolean;
|
|
235
|
+
case 'DATE':
|
|
236
|
+
return typeof draft === 'string' && isRealIsoDate(draft) ? null : messages.invalidDate;
|
|
237
|
+
case 'LIST':
|
|
238
|
+
return typeof draft === 'string' && field.options.includes(draft)
|
|
239
|
+
? null
|
|
240
|
+
: messages.invalidOption;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Validates every field; the result maps `field_key` to its message (empty when valid). */
|
|
245
|
+
export function validateForm(
|
|
246
|
+
fields: ReadonlyArray<CustomFormFieldDef>,
|
|
247
|
+
drafts: Readonly<Record<string, FieldDraft>>,
|
|
248
|
+
messages: CustomFormMessages,
|
|
249
|
+
): Record<string, string> {
|
|
250
|
+
const errors: Record<string, string> = {};
|
|
251
|
+
for (const field of fields) {
|
|
252
|
+
const message = validateField(field, drafts[field.fieldKey] ?? emptyDraft(field), messages);
|
|
253
|
+
if (message !== null) errors[field.fieldKey] = message;
|
|
254
|
+
}
|
|
255
|
+
return errors;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Builds the `responses` body from valid drafts: trimmed text, numbers as JSON
|
|
260
|
+
* numbers, and unanswered optional fields left out.
|
|
261
|
+
*/
|
|
262
|
+
export function buildResponses(
|
|
263
|
+
fields: ReadonlyArray<CustomFormFieldDef>,
|
|
264
|
+
drafts: Readonly<Record<string, FieldDraft>>,
|
|
265
|
+
): Record<string, CustomFormResponseValue> {
|
|
266
|
+
const responses: Record<string, CustomFormResponseValue> = {};
|
|
267
|
+
for (const field of fields) {
|
|
268
|
+
const draft = drafts[field.fieldKey] ?? emptyDraft(field);
|
|
269
|
+
if (isUnanswered(field, draft)) continue;
|
|
270
|
+
if (field.type === 'TEXT' && typeof draft === 'string') {
|
|
271
|
+
responses[field.fieldKey] = draft.trim();
|
|
272
|
+
} else if (field.type === 'NUMBER' && typeof draft === 'string') {
|
|
273
|
+
const value = parseNumberInput(draft);
|
|
274
|
+
if (value !== null) responses[field.fieldKey] = value;
|
|
275
|
+
} else if (draft !== null) {
|
|
276
|
+
responses[field.fieldKey] = draft;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return responses;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Splits the backend's per-field messages of a `CUSTOM_FORM` 400 into those of
|
|
284
|
+
* fields on screen (by `field_key`) and the rest (e.g. an unknown key), which
|
|
285
|
+
* the form shows in its banner.
|
|
286
|
+
*/
|
|
287
|
+
export function splitCustomFormErrors(
|
|
288
|
+
fields: ReadonlyArray<CustomFormFieldDef>,
|
|
289
|
+
errors: Readonly<Record<string, string>>,
|
|
290
|
+
): { byField: Record<string, string>; other: string[] } {
|
|
291
|
+
const keys = new Set(fields.map((f) => f.fieldKey));
|
|
292
|
+
const byField: Record<string, string> = {};
|
|
293
|
+
const other: string[] = [];
|
|
294
|
+
for (const [path, message] of Object.entries(errors)) {
|
|
295
|
+
const fieldKey = path.startsWith(RESPONSES_ERROR_PREFIX)
|
|
296
|
+
? path.slice(RESPONSES_ERROR_PREFIX.length)
|
|
297
|
+
: path;
|
|
298
|
+
if (keys.has(fieldKey)) byField[fieldKey] = message;
|
|
299
|
+
else other.push(message);
|
|
300
|
+
}
|
|
301
|
+
return { byField, other };
|
|
302
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Pure logic of the `ENTITY_TYPE_SELECTION` step. JSX-free so it
|
|
3
|
+
* runs under Jest in the `node` environment.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { EntityType } from '../types';
|
|
7
|
+
|
|
8
|
+
/** The two choices, in the order the step shows them. */
|
|
9
|
+
export const ENTITY_TYPES: ReadonlyArray<EntityType> = ['HUMAN', 'LEGAL'];
|
|
10
|
+
|
|
11
|
+
/** Narrows an unknown value to an {@link EntityType}. */
|
|
12
|
+
export function isEntityType(value: unknown): value is EntityType {
|
|
13
|
+
return value === 'HUMAN' || value === 'LEGAL';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The previous choice from the step's `result` (`{ selected_type }`, present on
|
|
18
|
+
* reverse navigation), or `null` when there is none.
|
|
19
|
+
*/
|
|
20
|
+
export function prefillEntityType(
|
|
21
|
+
stepResult: Record<string, unknown> | undefined,
|
|
22
|
+
): EntityType | null {
|
|
23
|
+
const value = stepResult?.selected_type;
|
|
24
|
+
return isEntityType(value) ? value : null;
|
|
25
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview How a step reads a failed submit of `CUSTOM_FORM` or
|
|
3
|
+
* `ENTITY_TYPE_SELECTION` (VON-148 contract).
|
|
4
|
+
*
|
|
5
|
+
* | Response | Outcome | What the step does |
|
|
6
|
+
* |-----------------------------------------|------------|--------------------------------------------|
|
|
7
|
+
* | 400 `VAL-4000` with `errors` | `invalid` | shows each message next to its field |
|
|
8
|
+
* | 409 `VOB-40908` (not the current step) | `stale` | ends the step; the orchestrator re-routes |
|
|
9
|
+
* | request aborted by the flow | `aborted` | nothing: the flow already ended |
|
|
10
|
+
* | anything else | `failed` | banner + retry dialog, like the other steps|
|
|
11
|
+
*
|
|
12
|
+
* A `stale` submit cannot succeed by retrying: the session moved on (e.g. the
|
|
13
|
+
* same answer was already accepted). Ending the step hands control to the
|
|
14
|
+
* orchestrator's routing wait, whose fallback GET reads where the session
|
|
15
|
+
* really is, instead of a retry dialog that would loop on the same error. Same
|
|
16
|
+
* rule as the web SDK (OSDKW-41).
|
|
17
|
+
*
|
|
18
|
+
* JSX-free so it runs under Jest in the `node` environment.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Backend code: the step submitted is not the session's current step (409). */
|
|
22
|
+
export const STEP_NOT_CURRENT_CODE = 'VOB-40908';
|
|
23
|
+
|
|
24
|
+
export type SubmissionOutcome =
|
|
25
|
+
| { kind: 'invalid'; errors: Record<string, string> }
|
|
26
|
+
| { kind: 'stale'; code: string }
|
|
27
|
+
| { kind: 'aborted' }
|
|
28
|
+
| { kind: 'failed' };
|
|
29
|
+
|
|
30
|
+
/** Backend error body (`application/problem+json`), carried in `SdkError.cause`. */
|
|
31
|
+
interface ProblemBody {
|
|
32
|
+
code?: unknown;
|
|
33
|
+
errors?: unknown;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function problemBodyOf(err: unknown): ProblemBody | undefined {
|
|
37
|
+
if (err === null || typeof err !== 'object') return undefined;
|
|
38
|
+
const cause = (err as { cause?: unknown }).cause;
|
|
39
|
+
return cause !== null && typeof cause === 'object' ? (cause as ProblemBody) : undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The backend error code (`VAL-4000`, `VOB-40908`, …) of a failed request, when it sent one. */
|
|
43
|
+
export function backendErrorCode(err: unknown): string | undefined {
|
|
44
|
+
const code = problemBodyOf(err)?.code;
|
|
45
|
+
return typeof code === 'string' ? code : undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Per-field messages of a 400: the `errors` object of the problem body, keyed
|
|
50
|
+
* by request path (`responses.<field_key>`, `selected_type`). Also reads the
|
|
51
|
+
* list form `[{ field, message }]`. Empty when the error carries none.
|
|
52
|
+
*/
|
|
53
|
+
export function backendFieldErrors(err: unknown): Record<string, string> {
|
|
54
|
+
const errors = problemBodyOf(err)?.errors;
|
|
55
|
+
const out: Record<string, string> = {};
|
|
56
|
+
if (Array.isArray(errors)) {
|
|
57
|
+
for (const item of errors) {
|
|
58
|
+
if (item === null || typeof item !== 'object') continue;
|
|
59
|
+
const { field, message, code } = item as Record<string, unknown>;
|
|
60
|
+
const text = typeof message === 'string' ? message : typeof code === 'string' ? code : '';
|
|
61
|
+
if (typeof field === 'string' && text !== '') out[field] = text;
|
|
62
|
+
}
|
|
63
|
+
} else if (errors !== null && typeof errors === 'object') {
|
|
64
|
+
for (const [key, value] of Object.entries(errors as Record<string, unknown>)) {
|
|
65
|
+
if (typeof value === 'string' && value !== '') out[key] = value;
|
|
66
|
+
else if (Array.isArray(value) && typeof value[0] === 'string') out[key] = value[0];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isAbort(err: unknown): boolean {
|
|
73
|
+
return (
|
|
74
|
+
err !== null &&
|
|
75
|
+
typeof err === 'object' &&
|
|
76
|
+
(err as { name?: unknown }).name === 'AbortError'
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Classifies a failed submit; see the table at the top of the file. */
|
|
81
|
+
export function classifySubmissionError(err: unknown): SubmissionOutcome {
|
|
82
|
+
if (isAbort(err)) return { kind: 'aborted' };
|
|
83
|
+
const code = backendErrorCode(err);
|
|
84
|
+
if (code === STEP_NOT_CURRENT_CODE) {
|
|
85
|
+
return { kind: 'stale', code };
|
|
86
|
+
}
|
|
87
|
+
const httpStatus = (err as { httpStatus?: unknown } | null)?.httpStatus;
|
|
88
|
+
if (httpStatus === 400) {
|
|
89
|
+
const errors = backendFieldErrors(err);
|
|
90
|
+
if (Object.keys(errors).length > 0) return { kind: 'invalid', errors };
|
|
91
|
+
}
|
|
92
|
+
return { kind: 'failed' };
|
|
93
|
+
}
|
package/src/steps/types.ts
CHANGED
|
@@ -41,6 +41,11 @@ export interface StepDeps {
|
|
|
41
41
|
colorMode: 'light' | 'dark';
|
|
42
42
|
ui: StepUiHandle;
|
|
43
43
|
step: OnboardingStep;
|
|
44
|
+
/**
|
|
45
|
+
* Instance key of the step (`step_key`, or the code when the backend sends
|
|
46
|
+
* none). `CUSTOM_FORM` submits to it; every other step can ignore it.
|
|
47
|
+
*/
|
|
48
|
+
stepKey: string;
|
|
44
49
|
stepConfig?: Record<string, unknown>;
|
|
45
50
|
/**
|
|
46
51
|
* Previously-captured data for this step, used as form prefill when the step
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The `UNSUPPORTED_STEP` error, shared by the orchestrator (a
|
|
3
|
+
* step code this SDK does not know) and `CUSTOM_FORM` (a required field of a
|
|
4
|
+
* type it does not know).
|
|
5
|
+
*
|
|
6
|
+
* JSX-free so the orchestrator tests can import it in the `node` environment.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { SdkErrorCode, type SdkError } from '../types';
|
|
10
|
+
import { getStrings } from '../i18n';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Builds the error that ends the flow on a step this SDK cannot render. The
|
|
14
|
+
* localized `declineTitle` / `declineMessage` feed the built-in error screen;
|
|
15
|
+
* `message` is the English text for the host's logs.
|
|
16
|
+
*/
|
|
17
|
+
export function unsupportedStepError(
|
|
18
|
+
locale: string | undefined,
|
|
19
|
+
stepCode: string,
|
|
20
|
+
stepKey: string,
|
|
21
|
+
message: string,
|
|
22
|
+
): SdkError {
|
|
23
|
+
const copy = getStrings(locale).unsupported_step;
|
|
24
|
+
return {
|
|
25
|
+
code: SdkErrorCode.UNSUPPORTED_STEP,
|
|
26
|
+
message,
|
|
27
|
+
stepCode,
|
|
28
|
+
stepKey,
|
|
29
|
+
declineTitle: copy.title,
|
|
30
|
+
declineMessage: copy.message,
|
|
31
|
+
};
|
|
32
|
+
}
|
package/src/transport/api.ts
CHANGED
|
@@ -303,6 +303,21 @@ export function createApiCaller(
|
|
|
303
303
|
);
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
+
/** Authenticated POST without a body (e.g. `POST /sessions/:id/cancel`). */
|
|
307
|
+
async function postEmpty<TResponse>(
|
|
308
|
+
endpoint: string,
|
|
309
|
+
options?: ApiCallOptions,
|
|
310
|
+
): Promise<TResponse> {
|
|
311
|
+
return call<TResponse>(
|
|
312
|
+
endpoint,
|
|
313
|
+
{
|
|
314
|
+
method: 'POST',
|
|
315
|
+
headers: buildAuthHeaders(options?.extraHeaders),
|
|
316
|
+
},
|
|
317
|
+
options,
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
306
321
|
/**
|
|
307
322
|
* Authenticated POST with a `FormData` body. RN's `fetch` mirrors the
|
|
308
323
|
* standard contract: don't set `Content-Type` so the runtime injects the
|
|
@@ -324,7 +339,7 @@ export function createApiCaller(
|
|
|
324
339
|
);
|
|
325
340
|
}
|
|
326
341
|
|
|
327
|
-
return { get, post, postFormData, setSessionToken, close } as const;
|
|
342
|
+
return { get, post, postEmpty, postFormData, setSessionToken, close } as const;
|
|
328
343
|
}
|
|
329
344
|
|
|
330
345
|
/** The type of the object returned by `createApiCaller`. */
|