UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

79 lines (78 loc) 2.65 kB
import { logger as N } from "../../shared-lib/index.js"; import { getErrorMessage as si } from "./error-utils.js"; import { decodeBrazeActions as uo } from "./string-utils.js"; import { BRAZE_ACTION_URI_REGEX as to, isValidBrazeActionType as co, isValidBrazeActionJson as io, } from "./validation-utils.js"; export const BRAZE_ACTIONS = { types: { io: "container", logCustomEvent: "logCustomEvent", setEmailNotificationSubscriptionType: "setEmailNotificationSubscriptionType", setPushNotificationSubscriptionType: "setPushNotificationSubscriptionType", setCustomUserAttribute: "setCustomUserAttribute", requestPushPermission: "requestPushPermission", addToSubscriptionGroup: "addToSubscriptionGroup", removeFromSubscriptionGroup: "removeFromSubscriptionGroup", addToCustomAttributeArray: "addToCustomAttributeArray", removeFromCustomAttributeArray: "removeFromCustomAttributeArray", mo: "openLink", uo: "openLinkInWebView", }, properties: { type: "type", eo: "steps", so: "args" }, }; export const INELIGIBLE_BRAZE_ACTION_URL_ERROR_TYPES = { Qi: "unknownBrazeAction", Ac: "noPushPrompt", }; export const ineligibleBrazeActionURLErrorMessage = (t, o) => t === INELIGIBLE_BRAZE_ACTION_URL_ERROR_TYPES.Qi ? `${o} contains an unknown braze action type and will not be displayed.` : ""; export function getDecodedBrazeAction(t) { try { const o = t.match(to), r = o ? o[0].length : null, n = r ? t.substring(r) : null; if (null == r || r > t.length - 1 || !n) return void N.error( `Did not find base64 encoded brazeAction in url to process : ${t}`, ); const e = uo(n); return e ? JSON.parse(e) : void N.error(`Failed to decode base64 encoded brazeAction: ${n}`); } catch (o) { return void N.error(`Failed to process brazeAction URL ${t} : ${si(o)}`); } } function po(t, o) { let r = !1; if (o) for (const n of o) if (((r = r || t(n)), r)) return !0; return !1; } export function containsUnknownBrazeAction(t) { const o = BRAZE_ACTIONS.properties.type, r = BRAZE_ACTIONS.properties.eo; try { if (null == t) return !0; const n = t[o]; return n === BRAZE_ACTIONS.types.io ? po(containsUnknownBrazeAction, t[r]) : !co(n); } catch (t) { return !0; } } export function containsPushPrimerBrazeAction(t) { if (!t || !io(t)) return !1; const o = BRAZE_ACTIONS.properties.type, r = BRAZE_ACTIONS.properties.eo, n = t[o]; return n === BRAZE_ACTIONS.types.io ? po(containsPushPrimerBrazeAction, t[r]) : n === BRAZE_ACTIONS.types.requestPushPermission; }