UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

315 lines (314 loc) 9.54 kB
import { isArray as D, isDate as Nt, isObject as It, validateValueIsFromEnum as ta, } from "../util/code-utils.js"; import { logger as E, EventTypes as m } from "../../shared-lib/index.js"; import { isValidEmail as Ct, validateCustomAttributeArrayType as St, validateCustomAttributeKey as Ut, validateCustomProperties as nt, validateCustomString as mt, validatePropertyType as Dt, validateStandardString as ir, } from "../util/validation-utils.js"; import { SET_CUSTOM_USER_ATTRIBUTE_STRING as Ft } from "../common/constants.js"; import { toValidBackendTimeString as Lt } from "../util/date-utils.js"; export default class User { constructor(t, e) { (this.Cs = t), (this.Cn = e), (this.Cs = t), (this.Cn = e); } getUserId(t) { const e = this.Cs.getUserId(); if ("function" != typeof t) return e; E.warn( "The callback for getUserId is deprecated. You can access its return value directly instead (e.g. `const id = braze.getUser().getUserId()`)", ), t(e); } addAlias(t, e) { return !ir(t, "add alias", "the alias", !1) || t.length <= 0 ? (E.error("addAlias requires a non-empty alias"), !1) : !ir(e, "add alias", "the label", !1) || e.length <= 0 ? (E.error("addAlias requires a non-empty label"), !1) : this.Cn.En(t, e).W; } setFirstName(t) { return ( !!ir(t, "set first name", "the firstName", !0) && this.Cs.nu("first_name", t) ); } setLastName(t) { return ( !!ir(t, "set last name", "the lastName", !0) && this.Cs.nu("last_name", t) ); } setEmail(t) { return null === t || Ct(t) ? this.Cs.nu("email", t) : (E.error( `Cannot set email address - "${t}" did not pass RFC-5322 validation.`, ), !1); } setGender(t) { return ( "string" == typeof t && (t = t.toLowerCase()), !( null !== t && !ta( User.Genders, t, `Gender "${t}" is not a valid gender.`, "User.Genders", ) ) && this.Cs.nu("gender", t) ); } setDateOfBirth(t, e, r) { return null === t && null === e && null === r ? this.Cs.nu("dob", null) : ((t = null != t ? parseInt(t.toString()) : null), (e = null != e ? parseInt(e.toString()) : null), (r = null != r ? parseInt(r.toString()) : null), null == t || null == e || null == r || isNaN(t) || isNaN(e) || isNaN(r) || e > 12 || e < 1 || r > 31 || r < 1 ? (E.error( "Cannot set date of birth - parameters should comprise a valid date e.g. setDateOfBirth(1776, 7, 4);", ), !1) : this.Cs.nu("dob", `${t}-${e}-${r}`)); } setCountry(t) { return ( !!ir(t, "set country", "the country", !0) && this.Cs.nu("country", t) ); } setHomeCity(t) { return ( !!ir(t, "set home city", "the homeCity", !0) && this.Cs.nu("home_city", t) ); } setLanguage(t) { return ( !!ir(t, "set language", "the language", !0) && this.Cs.nu("language", t) ); } setEmailNotificationSubscriptionType(t) { return ( !!ta( User.NotificationSubscriptionTypes, t, `Email notification setting "${t}" is not a valid subscription type.`, "User.NotificationSubscriptionTypes", ) && this.Cs.nu("email_subscribe", t) ); } setPushNotificationSubscriptionType(t) { return ( !!ta( User.NotificationSubscriptionTypes, t, `Push notification setting "${t}" is not a valid subscription type.`, "User.NotificationSubscriptionTypes", ) && this.Cs.nu("push_subscribe", t) ); } setPhoneNumber(t) { return ( !!ir(t, "set phone number", "the phoneNumber", !0) && (null === t || t.match(User.Fn) ? this.Cs.nu("phone", t) : (E.error(`Cannot set phone number - "${t}" did not pass validation.`), !1)) ); } setLastKnownLocation(t, e, r, s, n) { return null == t || null == e ? (E.error( "Cannot set last-known location - latitude and longitude are required.", ), !1) : ((t = parseFloat(t.toString())), (e = parseFloat(e.toString())), null != r && (r = parseFloat(r.toString())), null != s && (s = parseFloat(s.toString())), null != n && (n = parseFloat(n.toString())), isNaN(t) || isNaN(e) || (null != r && isNaN(r)) || (null != s && isNaN(s)) || (null != n && isNaN(n)) ? (E.error( "Cannot set last-known location - all supplied parameters must be numeric.", ), !1) : t > 90 || t < -90 || e > 180 || e < -180 ? (E.error( "Cannot set last-known location - latitude and longitude are bounded by ±90 and ±180 respectively.", ), !1) : (null != r && r < 0) || (null != n && n < 0) ? (E.error( "Cannot set last-known location - accuracy and altitudeAccuracy may not be negative.", ), !1) : this.Cn.setLastKnownLocation(this.Cs.getUserId(), t, e, s, r, n).W); } setCustomUserAttribute(t, e, r) { if (!Ut(t)) return !1; const s = (e) => { const [r] = nt( e, Ft, "attribute value", `set custom user attribute "${t}"`, "custom user attribute", ); return r; }; if (D(e)) { const [r, n] = St(t, e); if (!r && !n && 0 !== e.length) return !1; if (r || 0 === e.length) return this.Cn.Ln(m.$n, t, e).W; for (const t of e) if (!s(t)) return !1; } else if (It(e)) { if (!s(e)) return !1; if (r) return this.Cn.Ln(m.Bn, t, e).W; } else { if (!(void 0 !== e && Dt(e))) return !1; if ( (Nt(e) && (e = Lt(e)), "string" == typeof e && !mt( e, `set custom user attribute "${t}"`, "the element in the given array", )) ) return !1; } return this.Cs.setCustomUserAttribute(t, e); } addToCustomAttributeArray(t, e) { return ( !!mt(t, "add to custom user attribute array", "the given key") && !( null != e && !mt(e, "add to custom user attribute array", "the given value") ) && this.Cn.Ln(m.Rn, t, e).W ); } removeFromCustomAttributeArray(t, e) { return ( !!mt(t, "remove from custom user attribute array", "the given key") && !( null != e && !mt(e, "remove from custom user attribute array", "the given value") ) && this.Cn.Ln(m.On, t, e).W ); } incrementCustomUserAttribute(t, e) { if (!mt(t, "increment custom user attribute", "the given key")) return !1; null == e && (e = 1); const r = parseInt(e.toString()); return isNaN(r) || r !== parseFloat(e.toString()) ? (E.error( `Cannot increment custom user attribute because the given incrementValue "${e}" is not an integer.`, ), !1) : this.Cn.Ln(m.Gn, t, r).W; } setCustomLocationAttribute(t, e, r) { return ( !!mt(t, "set custom location attribute", "the given key") && ((null !== e || null !== r) && ((e = null != e ? parseFloat(e.toString()) : null), (r = null != r ? parseFloat(r.toString()) : null), (null == e && null != r) || (null != e && null == r) || (null != e && (isNaN(e) || e > 90 || e < -90)) || (null != r && (isNaN(r) || r > 180 || r < -180))) ? (E.error( "Received invalid values for latitude and/or longitude. Latitude and longitude are bounded by ±90 and ±180 respectively, or must both be null for removal.", ), !1) : this.Cn.Hn(t, e, r).W) ); } addToSubscriptionGroup(t) { return !ir( t, "add user to subscription group", "subscription group ID", !1, ) || t.length <= 0 ? (E.error( "addToSubscriptionGroup requires a non-empty subscription group ID", ), !1) : this.Cn.Kn(t, User.Mn.SUBSCRIBED).W; } removeFromSubscriptionGroup(t) { return !ir( t, "remove user from subscription group", "subscription group ID", !1, ) || t.length <= 0 ? (E.error( "removeFromSubscriptionGroup requires a non-empty subscription group ID", ), !1) : this.Cn.Kn(t, User.Mn.UNSUBSCRIBED).W; } setLineId(t) { return ir(t, "set LINE user ID", "the ID", !0) && 0 !== (null == t ? void 0 : t.length) ? t && t.length > User.Yn ? (E.error( `Rejected LINE user ID ${t} because it is longer than ${User.Yn} characters.`, ), !1) : this.Cs.nu("native_line_id", t) : (E.error("setLineId requires a non-empty ID"), !1); } Pn(t, e, r, s, n) { this.Cs.Pn(t, e, r, s, n), this.Cn.zn(); } Sn(t) { this.Cs.Sn(t); } } (User.Genders = { MALE: "m", FEMALE: "f", OTHER: "o", UNKNOWN: "u", NOT_APPLICABLE: "n", PREFER_NOT_TO_SAY: "p", }), (User.NotificationSubscriptionTypes = { OPTED_IN: "opted_in", SUBSCRIBED: "subscribed", UNSUBSCRIBED: "unsubscribed", }), (User.Fn = /^[0-9 .\\(\\)\\+\\-]+$/), (User.Mn = { SUBSCRIBED: "subscribed", UNSUBSCRIBED: "unsubscribed" }), (User.Jn = "user_id"), (User.lu = "custom"), (User.mr = 997), (User.Yn = 33);