@@ -22,13 +22,62 @@ export interface InternalErrorResponse {
2222
2323export type InitFlow = ( ) => Promise < FlowNode | InternalErrorResponse > ;
2424
25- export type Updater = (
26- value :
27- | string
28- | string [ ]
29- | PhoneNumberInputValue
30- | FidoRegistrationInputValue
31- | FidoAuthenticationInputValue ,
25+ /**
26+ * Maps collector types to the specific value type they accept.
27+ * This enables type narrowing when using the update method with specific collector types.
28+ *
29+ * @example
30+ * ```typescript
31+ * if (collector.type === "PasswordCollector") {
32+ * const updater = davinciClient.update(collector);
33+ * // updater now only accepts: (value: string, index?: number) => ...
34+ * }
35+ * ```
36+ */
37+ export type CollectorValueType < T > = T extends { type : 'PasswordCollector' }
38+ ? string
39+ : T extends { type : 'TextCollector' ; category : 'SingleValueCollector' }
40+ ? string
41+ : T extends { type : 'TextCollector' ; category : 'ValidatedSingleValueCollector' }
42+ ? string
43+ : T extends { type : 'SingleSelectCollector' }
44+ ? string
45+ : T extends { type : 'MultiSelectCollector' }
46+ ? string [ ]
47+ : T extends { type : 'DeviceRegistrationCollector' }
48+ ? string
49+ : T extends { type : 'DeviceAuthenticationCollector' }
50+ ? string
51+ : T extends { type : 'PhoneNumberCollector' }
52+ ? PhoneNumberInputValue
53+ : T extends { type : 'FidoRegistrationCollector' }
54+ ? FidoRegistrationInputValue
55+ : T extends { type : 'FidoAuthenticationCollector' }
56+ ? FidoAuthenticationInputValue
57+ : T extends { category : 'SingleValueCollector' }
58+ ? string
59+ : T extends { category : 'ValidatedSingleValueCollector' }
60+ ? string
61+ : T extends { category : 'MultiValueCollector' }
62+ ? string [ ]
63+ :
64+ | string
65+ | string [ ]
66+ | PhoneNumberInputValue
67+ | FidoRegistrationInputValue
68+ | FidoAuthenticationInputValue ;
69+
70+ /**
71+ * Generic updater function that accepts values appropriate for the collector type.
72+ * When used with type narrowing, the value parameter will be constrained to the correct type.
73+ *
74+ * @template T The collector type (inferred from the collector passed to update())
75+ * @param value The value to update the collector with (type depends on T)
76+ * @param index Optional index for multi-value collectors
77+ * @returns null on success, or an InternalErrorResponse on failure
78+ */
79+ export type Updater < T = unknown > = (
80+ value : CollectorValueType < T > ,
3281 index ?: number ,
3382) => InternalErrorResponse | null ;
3483export type Validator = ( value : string ) =>
0 commit comments