UNPKG

klaviyo-react-native-sdk

Version:
124 lines (115 loc) 3.88 kB
"use strict"; /** * Interface for the Klaviyo Forms API */ /** * Configuration for in-app forms */ /** * String constants for form lifecycle event types. * * These values match the `type` discriminants emitted by the native bridge * and are safe to use when comparing {@link FormLifecycleEvent#type} or * building dispatch tables keyed by event type. */ export const FormLifecycleEventType = { /** Emitted when a form is shown to the user. */ Shown: 'formShown', /** Emitted when the user dismisses a visible form. */ Dismissed: 'formDismissed', /** Emitted when the user taps a CTA button with a configured deep link. */ CtaClicked: 'formCtaClicked' }; /** Union of valid {@link FormLifecycleEvent} type discriminants. */ /** * Discriminated union representing a form lifecycle event. * * Each variant carries contextual data about the form, and the `type` field * serves as the discriminant. Use a `switch` on `event.type` to narrow the type * and access variant-specific fields like `buttonLabel` and `deepLinkUrl`. * * Example usage: * ```typescript * Klaviyo.registerFormLifecycleHandler((event) => { * switch (event.type) { * case FormLifecycleEventType.Shown: * console.log(`Form shown: ${event.formId}`); * break; * case FormLifecycleEventType.Dismissed: * console.log(`Form dismissed: ${event.formId}`); * break; * case FormLifecycleEventType.CtaClicked: * console.log(`CTA clicked: ${event.buttonLabel}, deep link: ${event.deepLinkUrl}`); * break; * } * }); * ``` */ /** * Handler function type for form lifecycle events */ const FORM_LIFECYCLE_EVENT_TYPES = Object.values(FormLifecycleEventType); /** * Validates that a value is a non-empty string. */ function isNonEmptyString(value) { return typeof value === 'string' && value.length > 0; } /** * Parses a raw native event payload into a validated {@link FormLifecycleEvent}. * * Returns `null` and logs a warning if required fields are missing or empty. * Required fields vary by event type: * - All events: `type`, `formId`, `formName` * - `formCtaClicked`: additionally requires `deepLinkUrl`; `buttonLabel` defaults to empty string if absent * * @param data Raw event data from the native bridge * @returns A validated FormLifecycleEvent, or null if the payload is invalid */ export function parseFormLifecycleEvent(data) { const { type, formId, formName } = data; if (!isNonEmptyString(type) || !FORM_LIFECYCLE_EVENT_TYPES.includes(type)) { console.warn(`[Klaviyo] Ignoring form lifecycle event with invalid type: ${JSON.stringify(type)}`); return null; } const missingFields = []; if (!isNonEmptyString(formId)) missingFields.push('formId'); if (!isNonEmptyString(formName)) missingFields.push('formName'); if (type === FormLifecycleEventType.CtaClicked) { if (!isNonEmptyString(data.deepLinkUrl)) missingFields.push('deepLinkUrl'); } if (missingFields.length > 0) { console.warn(`[Klaviyo] Ignoring ${type} event: missing required field(s): ${missingFields.join(', ')}`); return null; } const validatedType = type; const validFormId = formId; const validFormName = formName; switch (validatedType) { case FormLifecycleEventType.Shown: return { type: validatedType, formId: validFormId, formName: validFormName }; case FormLifecycleEventType.Dismissed: return { type: validatedType, formId: validFormId, formName: validFormName }; case FormLifecycleEventType.CtaClicked: return { type: validatedType, formId: validFormId, formName: validFormName, buttonLabel: typeof data.buttonLabel === 'string' ? data.buttonLabel : '', deepLinkUrl: data.deepLinkUrl }; } } //# sourceMappingURL=Forms.js.map