@braze/web-sdk 4.3.0 → 4.5.0

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.
Files changed (178) hide show
  1. package/index.d.ts +68 -6
  2. package/package.json +1 -1
  3. package/shared-lib/braze-shared-lib.js +8 -1
  4. package/shared-lib/encoding-utils.js +12 -1
  5. package/shared-lib/event-types.js +32 -1
  6. package/shared-lib/guid.js +10 -1
  7. package/shared-lib/indexed-db-adapter.js +315 -1
  8. package/shared-lib/logger.js +38 -1
  9. package/shared-lib/supported-options.js +26 -1
  10. package/src/Card/card-manager-factory.js +14 -1
  11. package/src/Card/card-manager.js +72 -1
  12. package/src/Card/display/card-display.js +134 -1
  13. package/src/Card/index.js +5 -1
  14. package/src/Card/log-card-click.js +11 -1
  15. package/src/Card/log-card-dismissal.js +11 -1
  16. package/src/Card/log-card-impressions.js +13 -1
  17. package/src/Card/log-content-card-impressions.js +4 -1
  18. package/src/Card/models/banner.js +30 -1
  19. package/src/Card/models/captioned-image.js +32 -1
  20. package/src/Card/models/card.js +175 -1
  21. package/src/Card/models/classic-card.js +32 -1
  22. package/src/Card/models/control-card.js +39 -1
  23. package/src/Card/models/index.js +5 -1
  24. package/src/Card/util/card-factory.js +104 -1
  25. package/src/ContentCards/content-cards-provider-factory.js +20 -1
  26. package/src/ContentCards/content-cards-provider.js +283 -1
  27. package/src/ContentCards/content-cards.js +24 -1
  28. package/src/ContentCards/get-cached-content-cards.js +5 -1
  29. package/src/ContentCards/index.js +8 -1
  30. package/src/ContentCards/log-content-cards-displayed.js +5 -1
  31. package/src/ContentCards/request-content-cards-refresh.js +5 -1
  32. package/src/ContentCards/subscribe-to-content-cards-updates.js +14 -1
  33. package/src/ContentCards/ui/hide-content-cards.js +8 -1
  34. package/src/ContentCards/ui/show-content-cards.js +72 -1
  35. package/src/ContentCards/ui/toggle-content-cards.js +8 -1
  36. package/src/Core/add-sdk-metadata.js +27 -1
  37. package/src/Core/braze-sdk-metadata.js +10 -1
  38. package/src/Core/change-user.js +19 -1
  39. package/src/Core/destroy.js +5 -1
  40. package/src/Core/device-properties.js +9 -1
  41. package/src/Core/disable-sdk.js +10 -1
  42. package/src/Core/enable-sdk.js +8 -1
  43. package/src/Core/get-device-id.js +10 -1
  44. package/src/Core/get-user.js +4 -1
  45. package/src/Core/handle-braze-action.js +64 -1
  46. package/src/Core/index.js +23 -1
  47. package/src/Core/initialize.js +4 -1
  48. package/src/Core/is-disabled.js +4 -1
  49. package/src/Core/log-custom-event.js +36 -1
  50. package/src/Core/log-purchase.js +245 -1
  51. package/src/Core/open-session.js +23 -1
  52. package/src/Core/remove-all-subscriptions.js +4 -1
  53. package/src/Core/remove-subscription.js +4 -1
  54. package/src/Core/request-immediate-data-flush.js +4 -1
  55. package/src/Core/set-logger.js +4 -1
  56. package/src/Core/set-sdk-authentication-signature.js +9 -1
  57. package/src/Core/subscribe-to-sdk-authentication-failures.js +4 -1
  58. package/src/Core/toggle-logging.js +4 -1
  59. package/src/Core/wipe-data.js +15 -1
  60. package/src/FeatureFlags/feature-flag-factory.js +14 -0
  61. package/src/FeatureFlags/feature-flag.js +16 -0
  62. package/src/FeatureFlags/feature-flags-provider-factory.js +19 -0
  63. package/src/FeatureFlags/feature-flags-provider.js +94 -0
  64. package/src/FeatureFlags/get-all-feature-flags.js +12 -0
  65. package/src/FeatureFlags/get-feature-flag.js +12 -0
  66. package/src/FeatureFlags/index.js +4 -0
  67. package/src/FeatureFlags/refresh-feature-flags.js +9 -0
  68. package/src/FeatureFlags/subscribe-to-feature-flags-updates.js +6 -0
  69. package/src/Feed/feed-provider-factory.js +18 -1
  70. package/src/Feed/feed-provider.js +79 -1
  71. package/src/Feed/feed.js +20 -1
  72. package/src/Feed/get-cached-feed.js +5 -1
  73. package/src/Feed/index.js +8 -1
  74. package/src/Feed/log-feed-displayed.js +5 -1
  75. package/src/Feed/request-feed-refresh.js +5 -1
  76. package/src/Feed/subscribe-to-feed-updates.js +5 -1
  77. package/src/Feed/ui/hide-feed.js +7 -1
  78. package/src/Feed/ui/show-feed.js +89 -1
  79. package/src/Feed/ui/toggle-feed.js +8 -1
  80. package/src/InAppMessage/display/get-animation-effect.js +19 -1
  81. package/src/InAppMessage/display/html-message-to-html.js +223 -1
  82. package/src/InAppMessage/display/in-app-message-to-html.js +152 -1
  83. package/src/InAppMessage/display/modal-utils.js +68 -1
  84. package/src/InAppMessage/in-app-message-factory.js +145 -1
  85. package/src/InAppMessage/in-app-message-manager-factory.js +16 -1
  86. package/src/InAppMessage/in-app-message-manager.js +189 -1
  87. package/src/InAppMessage/index.js +14 -1
  88. package/src/InAppMessage/log-in-app-message-button-click.js +19 -1
  89. package/src/InAppMessage/log-in-app-message-click.js +19 -1
  90. package/src/InAppMessage/log-in-app-message-html-click.js +23 -1
  91. package/src/InAppMessage/log-in-app-message-impression.js +13 -1
  92. package/src/InAppMessage/models/control-message.js +8 -1
  93. package/src/InAppMessage/models/full-screen-message.js +71 -1
  94. package/src/InAppMessage/models/html-message.js +54 -1
  95. package/src/InAppMessage/models/in-app-message-button.js +40 -1
  96. package/src/InAppMessage/models/in-app-message.js +271 -1
  97. package/src/InAppMessage/models/modal-message.js +68 -1
  98. package/src/InAppMessage/models/slide-up-message.js +58 -1
  99. package/src/InAppMessage/models/templated-in-app-message.js +14 -1
  100. package/src/InAppMessage/subscribe-to-in-app-message.js +10 -1
  101. package/src/InAppMessage/ui/automatically-show-in-app-messages.js +15 -1
  102. package/src/InAppMessage/ui/show-in-app-message.js +170 -1
  103. package/src/Push/index.js +5 -1
  104. package/src/Push/is-push-blocked.js +5 -1
  105. package/src/Push/is-push-permission-granted.js +5 -1
  106. package/src/Push/is-push-supported.js +5 -1
  107. package/src/Push/push-manager-factory.js +30 -1
  108. package/src/Push/push-manager.js +360 -1
  109. package/src/Push/request-push-permission.js +8 -1
  110. package/src/Push/unregister-push.js +5 -1
  111. package/src/Push/utils/push-utils.js +34 -1
  112. package/src/User/index.js +1 -1
  113. package/src/User/user-manager.js +70 -1
  114. package/src/User/user.js +278 -1
  115. package/src/common/base-feed.js +25 -1
  116. package/src/common/base-provider.js +6 -1
  117. package/src/common/constants.js +5 -1
  118. package/src/common/event-logger.js +24 -1
  119. package/src/common/feed-display.js +200 -1
  120. package/src/common/translations.js +148 -1
  121. package/src/index.js +9 -1
  122. package/src/l10n/l10n-manager-factory.js +22 -1
  123. package/src/l10n/l10n-manager.js +21 -1
  124. package/src/managers/auth-manager.js +48 -1
  125. package/src/managers/braze-instance.js +307 -1
  126. package/src/managers/device-manager.js +80 -1
  127. package/src/managers/network-manager.js +219 -1
  128. package/src/managers/server-config-manager.js +88 -1
  129. package/src/managers/session-manager.js +68 -1
  130. package/src/managers/storage-manager-factory.js +35 -1
  131. package/src/managers/storage-manager.js +394 -1
  132. package/src/managers/subscription-manager.js +25 -1
  133. package/src/models/backend-errors.js +8 -1
  134. package/src/models/braze-event.js +41 -1
  135. package/src/models/braze-sdk-metadata.js +10 -1
  136. package/src/models/device.js +26 -1
  137. package/src/models/identifier.js +20 -1
  138. package/src/models/push-token.js +22 -1
  139. package/src/models/request-result.js +8 -1
  140. package/src/models/server-config.js +32 -1
  141. package/src/request-controller.js +288 -1
  142. package/src/triggers/models/custom-event-data.js +14 -1
  143. package/src/triggers/models/custom-event-property-data.js +21 -1
  144. package/src/triggers/models/filter-set.js +55 -1
  145. package/src/triggers/models/filter.js +98 -1
  146. package/src/triggers/models/in-app-message-click-data.js +25 -1
  147. package/src/triggers/models/purchase-data.js +14 -1
  148. package/src/triggers/models/purchase-property-data.js +21 -1
  149. package/src/triggers/models/push-click-data.js +15 -1
  150. package/src/triggers/models/trigger-condition.js +109 -1
  151. package/src/triggers/models/trigger-events.js +8 -1
  152. package/src/triggers/models/trigger.js +110 -1
  153. package/src/triggers/triggers-provider-factory.js +30 -1
  154. package/src/triggers/triggers-provider.js +307 -1
  155. package/src/ui/js/attach-css.js +12 -1
  156. package/src/ui/js/feed-css.js +12 -1
  157. package/src/ui/js/iam-css.js +12 -1
  158. package/src/ui/js/index.js +3 -1
  159. package/src/ui/js/load-font-awesome.js +13 -1
  160. package/src/util/base-device-parser.js +14 -1
  161. package/src/util/braze-actions.js +79 -1
  162. package/src/util/browser-detector.js +85 -1
  163. package/src/util/client-hints-parser.js +60 -1
  164. package/src/util/code-utils.js +92 -1
  165. package/src/util/color-utils.js +23 -1
  166. package/src/util/component-utils.js +26 -1
  167. package/src/util/date-utils.js +27 -1
  168. package/src/util/deprecation-utils.js +5 -1
  169. package/src/util/device-constants.js +14 -1
  170. package/src/util/dom-utils.js +87 -1
  171. package/src/util/key-codes.js +1 -1
  172. package/src/util/math.js +5 -1
  173. package/src/util/net.js +75 -1
  174. package/src/util/string-utils.js +22 -1
  175. package/src/util/url-utils.js +23 -1
  176. package/src/util/user-agent-parser.js +85 -1
  177. package/src/util/validation-utils.js +136 -1
  178. package/src/util/window-utils.js +35 -1
@@ -1 +1,30 @@
1
- import e,{OPTIONS as T}from"../managers/braze-instance.js";import aa from"./push-manager.js";const ea={t:!1,pushManager:null,aa:()=>(ea.o(),ea.pushManager||(ea.pushManager=new aa(e.gr(),e.ea(),e.ce(),e.Ks(),e.nn(T.na),e.nn(T.ra),e.ir(),e.nn(T.ta),e.nn(T.ia),e.l())),ea.pushManager),o:()=>{ea.t||(e.u(ea),ea.t=!0)},destroy:()=>{ea.pushManager=null,ea.t=!1}};export default ea;
1
+ import e, { OPTIONS as T } from "../managers/braze-instance.js";
2
+ import aa from "./push-manager.js";
3
+ const ea = {
4
+ t: !1,
5
+ pushManager: null,
6
+ aa: () => (
7
+ ea.o(),
8
+ ea.pushManager ||
9
+ (ea.pushManager = new aa(
10
+ e.gr(),
11
+ e.ea(),
12
+ e.ce(),
13
+ e.Ks(),
14
+ e.nn(T.na),
15
+ e.nn(T.ra),
16
+ e.ir(),
17
+ e.nn(T.ta),
18
+ e.nn(T.ia),
19
+ e.l()
20
+ )),
21
+ ea.pushManager
22
+ ),
23
+ o: () => {
24
+ ea.t || (e.u(ea), (ea.t = !0));
25
+ },
26
+ destroy: () => {
27
+ (ea.pushManager = null), (ea.t = !1);
28
+ }
29
+ };
30
+ export default ea;
@@ -1 +1,360 @@
1
- import{isArray as p,isEqual as ii}from"../util/code-utils.js";import ei from"../models/push-token.js";import r from"../../shared-lib/braze-shared-lib.js";import{STORAGE_KEYS as o}from"../managers/storage-manager.js";import{User}from"../User/index.js";import{WindowUtils as H}from"../util/window-utils.js";import ht from"./utils/push-utils.js";export default class aa{constructor(i,e,t,s,r,n,o,u,a,h){this.qi=i,this.Ji=e,this.Qi=t,this.Zi=s+"/safari/"+e,this.Sr=r||"/service-worker.js",this._r=n,this.gt=o,this.Ur=u||!1,this.Wr=a||!1,this.h=h,this.Nr=ht.Ir(),this.Vr=ht.Cr()}Er(){return this.Wr}Or(i,e,t,s,n){i.unsubscribe().then((i=>{i?this.Br(e,t,s,n):(r.D.error("Failed to unsubscribe device from push."),"function"==typeof n&&n(!1))})).catch((i=>{r.D.error("Push unsubscription error: "+i),"function"==typeof n&&n(!1)}))}Mr(i,e,t){const s=(i=>{if("string"==typeof i)return i;if(0!==i.endpoint.indexOf("https://android.googleapis.com/gcm/send"))return i.endpoint;let e=i.endpoint;return i.Yr&&-1===i.endpoint.indexOf(i.Yr)&&(e=i.endpoint+"/"+i.Yr),e})(i);let r=null,n=null;if(null!=i.getKey)try{r=btoa(String.fromCharCode.apply(null,new Uint8Array(i.getKey("p256dh")))),n=btoa(String.fromCharCode.apply(null,new Uint8Array(i.getKey("auth"))))}catch(i){if("invalid arguments"!==i.message)throw i}const o=(i=>{let e;return i.options&&(e=i.options.applicationServerKey)&&e.byteLength&&e.byteLength>0?btoa(String.fromCharCode.apply(null,new Uint8Array(e))).replace(/\+/g,"-").replace(/\//g,"_"):null})(i);this.qi.Gr(s,t,r,n,o),s&&"function"==typeof e&&e(s,r,n)}Hr(){this.qi.Jr(!0)}Kr(i,e){this.qi.Jr(!1),r.D.info(i),"function"==typeof e&&e(!1)}Lr(i,e,t,s){if("default"===e.permission)try{window.safari.pushNotification.requestPermission(this.Zi,i,{api_key:this.Ji,device_id:this.Qi.te().id},(e=>{"granted"===e.permission&&this.qi.setPushNotificationSubscriptionType(User.NotificationSubscriptionTypes.OPTED_IN),this.Lr(i,e,t,s)}))}catch(i){this.Kr("Could not request permission for push: "+i,s)}else"denied"===e.permission?this.Kr("The user has blocked notifications from this site, or Safari push is not configured in the Braze dashboard.",s):"granted"===e.permission&&(r.D.info("Device successfully subscribed to push."),this.Mr(e.deviceToken,t,new Date))}Qr(i,e,t){const s=s=>{switch(s){case"granted":return void("function"==typeof i&&i());case"default":return void("function"==typeof e&&e());case"denied":return void("function"==typeof t&&t());default:r.D.error("Received unexpected permission result "+s)}};let n=!1;const o=window.Notification.requestPermission((i=>{n&&s(i)}));o?o.then((i=>{s(i)})):n=!0}Br(i,e,t,s){const n={userVisibleOnly:!0};null!=e&&(n.applicationServerKey=e),i.pushManager.subscribe(n).then((i=>{r.D.info("Device successfully subscribed to push."),this.Mr(i,t,new Date)})).catch((i=>{ht.isPushBlocked()?(r.D.info("Permission for push notifications was denied."),"function"==typeof s&&s(!1)):r.D.error("Push subscription failed: "+i)}))}Xr(){return this.Ur?navigator.serviceWorker.getRegistration():navigator.serviceWorker.register(this.Sr).then((()=>navigator.serviceWorker.ready.then((i=>(i&&"function"==typeof i.update&&i.update().catch((i=>{r.D.info("ServiceWorker update failed: "+i)})),i)))))}Zr(i){this.Ur||(i.unregister(),r.D.info("Service worker successfully unregistered."))}subscribe(i,e){if(ht.isPushSupported()){if(this.Nr){if(!this.Ur&&null!=window.location){let i=this.Sr;-1===i.indexOf(window.location.host)&&(i=window.location.host+i),-1===i.indexOf(window.location.protocol)&&(i=window.location.protocol+"//"+i);const e=i.substr(0,i.lastIndexOf("/")+1);if(0!==H.hn().indexOf(e))return void r.D.error("Cannot subscribe to push from a path higher than the service worker location (tried to subscribe from "+window.location.pathname+" but service worker is at "+i+")")}if(ht.isPushBlocked())return void this.Kr("Notifications from this site are blocked. This may be a temporary embargo or a permanent denial.",e);if(this.gt&&!this.gt.fn()&&0===this.gt.Zs())return r.D.info("Waiting for VAPID key from server config before subscribing to push."),void this.gt.dn((()=>{this.subscribe(i,e)}));const t=()=>{r.D.info("Permission for push notifications was denied."),"function"==typeof e&&e(!1)},s=()=>{let i="Permission for push notifications was ignored.";ht.isPushBlocked()&&(i+=" The browser has automatically blocked further permission requests for a period (probably 1 week)."),r.D.info(i),"function"==typeof e&&e(!0)},n=ht.isPushPermissionGranted(),u=()=>{n||this.qi.setPushNotificationSubscriptionType(User.NotificationSubscriptionTypes.OPTED_IN),this.Xr().then((t=>{if(null==t)return r.D.error("No service worker registration. Set the `manageServiceWorkerExternally` initialization option to false or ensure that your service worker is registered before calling registerPush."),void("function"==typeof e&&e());t.pushManager.getSubscription().then((s=>{let n=null;if(this.gt&&null!=this.gt.fn()&&(n=r.bn.pn(this.gt.fn())),s){let u=null,a=null;const h=this.h.I(o.q.gn);if(h&&!p(h)){let i;try{i=ei.yn(h).wn}catch(e){i=null}null==i||isNaN(i.getTime())||0===i.getTime()||(u=i,a=new Date(u),a.setMonth(u.getMonth()+6))}null!=n&&s.options&&s.options.applicationServerKey&&s.options.applicationServerKey.byteLength&&s.options.applicationServerKey.byteLength>0&&!ii(n,new Uint8Array(s.options.applicationServerKey))?(s.options.applicationServerKey.byteLength>12?r.D.info("Device was already subscribed to push using a different VAPID provider, creating new subscription."):r.D.info("Attempting to upgrade a gcm_sender_id-based push registration to VAPID - depending on the browser this may or may not result in the same gcm_sender_id-based subscription."),this.Or(s,t,n,i,e)):s.expirationTime&&new Date(s.expirationTime)<=(new Date).valueOf()?(r.D.info("Push subscription is expired, creating new subscription."),this.Or(s,t,n,i,e)):h&&p(h)?this.Or(s,t,n,i,e):null==a?(r.D.info("No push subscription creation date found, creating new subscription."),this.Or(s,t,n,i,e)):a<=(new Date).valueOf()?(r.D.info("Push subscription older than 6 months, creating new subscription."),this.Or(s,t,n,i,e)):(r.D.info("Device already subscribed to push, sending existing subscription to backend."),this.Mr(s,i,u))}else this.Br(t,n,i,e)})).catch((i=>{r.D.error("Error checking current push subscriptions: "+i)}))})).catch((i=>{r.D.error("ServiceWorker registration failed: "+i)}))};this.Qr(u,s,t)}else if(this.Vr){if(null==this._r||""===this._r)return void r.D.error("You must supply the safariWebsitePushId initialization option in order to use registerPush on Safari");const t=window.safari.pushNotification.permission(this._r);this.Lr(this._r,t,i,e)}}else r.D.info(this.vn)}unsubscribe(i,e){ht.isPushSupported()?this.Nr?navigator.serviceWorker.getRegistration().then((t=>{t?t.pushManager.getSubscription().then((s=>{s&&(this.Hr(),s.unsubscribe().then((s=>{s?(r.D.info("Device successfully unsubscribed from push."),"function"==typeof i&&i()):(r.D.error("Failed to unsubscribe device from push."),"function"==typeof e&&e()),this.Zr(t)})).catch((i=>{r.D.error("Push unsubscription error: "+i),"function"==typeof e&&e()})))})).catch((i=>{r.D.error("Error unsubscribing from push: "+i),"function"==typeof e&&e()})):(r.D.info("Device already unsubscribed from push."),"function"==typeof i&&i())})):this.Vr&&(this.Hr(),r.D.info("Device unsubscribed from push."),"function"==typeof i&&i()):r.D.info(this.vn)}}aa.vn="Push notifications are not supported in this browser.";
1
+ import { isArray as p, isEqual as ii } from "../util/code-utils.js";
2
+ import ei from "../models/push-token.js";
3
+ import r from "../../shared-lib/braze-shared-lib.js";
4
+ import { STORAGE_KEYS as o } from "../managers/storage-manager.js";
5
+ import { User } from "../User/index.js";
6
+ import { WindowUtils as H } from "../util/window-utils.js";
7
+ import dt from "./utils/push-utils.js";
8
+ export default class aa {
9
+ constructor(i, e, t, s, r, n, o, u, a, h) {
10
+ (this.Sr = i),
11
+ (this._r = e),
12
+ (this.Ur = t),
13
+ (this.Wr = s + "/safari/" + e),
14
+ (this.Nr = r || "/service-worker.js"),
15
+ (this.Ir = n),
16
+ (this.gt = o),
17
+ (this.Vr = u || !1),
18
+ (this.Er = a || !1),
19
+ (this.h = h),
20
+ (this.Or = dt.Br()),
21
+ (this.Mr = dt.Yr());
22
+ }
23
+ Gr() {
24
+ return this.Er;
25
+ }
26
+ Hr(i, e, t, s, n) {
27
+ i.unsubscribe()
28
+ .then(i => {
29
+ i
30
+ ? this.Jr(e, t, s, n)
31
+ : (r.D.error("Failed to unsubscribe device from push."),
32
+ "function" == typeof n && n(!1));
33
+ })
34
+ .catch(i => {
35
+ r.D.error("Push unsubscription error: " + i),
36
+ "function" == typeof n && n(!1);
37
+ });
38
+ }
39
+ Kr(i, e, t) {
40
+ const s = (i => {
41
+ if ("string" == typeof i) return i;
42
+ if (0 !== i.endpoint.indexOf("https://android.googleapis.com/gcm/send"))
43
+ return i.endpoint;
44
+ let e = i.endpoint;
45
+ return (
46
+ i.Lr &&
47
+ -1 === i.endpoint.indexOf(i.Lr) &&
48
+ (e = i.endpoint + "/" + i.Lr),
49
+ e
50
+ );
51
+ })(i);
52
+ let r = null,
53
+ n = null;
54
+ if (null != i.getKey)
55
+ try {
56
+ (r = btoa(
57
+ String.fromCharCode.apply(null, new Uint8Array(i.getKey("p256dh")))
58
+ )),
59
+ (n = btoa(
60
+ String.fromCharCode.apply(null, new Uint8Array(i.getKey("auth")))
61
+ ));
62
+ } catch (i) {
63
+ if ("invalid arguments" !== i.message) throw i;
64
+ }
65
+ const o = (i => {
66
+ let e;
67
+ return i.options &&
68
+ (e = i.options.applicationServerKey) &&
69
+ e.byteLength &&
70
+ e.byteLength > 0
71
+ ? btoa(String.fromCharCode.apply(null, new Uint8Array(e)))
72
+ .replace(/\+/g, "-")
73
+ .replace(/\//g, "_")
74
+ : null;
75
+ })(i);
76
+ this.Sr.Qr(s, t, r, n, o), s && "function" == typeof e && e(s, r, n);
77
+ }
78
+ Xr() {
79
+ this.Sr.Zr(!0);
80
+ }
81
+ hn(i, e) {
82
+ this.Sr.Zr(!1), r.D.info(i), "function" == typeof e && e(!1);
83
+ }
84
+ fn(i, e, t, s) {
85
+ if ("default" === e.permission)
86
+ try {
87
+ window.safari.pushNotification.requestPermission(
88
+ this.Wr,
89
+ i,
90
+ { api_key: this._r, device_id: this.Ur.te().id },
91
+ e => {
92
+ "granted" === e.permission &&
93
+ this.Sr.setPushNotificationSubscriptionType(
94
+ User.NotificationSubscriptionTypes.OPTED_IN
95
+ ),
96
+ this.fn(i, e, t, s);
97
+ }
98
+ );
99
+ } catch (i) {
100
+ this.hn("Could not request permission for push: " + i, s);
101
+ }
102
+ else
103
+ "denied" === e.permission
104
+ ? this.hn(
105
+ "The user has blocked notifications from this site, or Safari push is not configured in the Braze dashboard.",
106
+ s
107
+ )
108
+ : "granted" === e.permission &&
109
+ (r.D.info("Device successfully subscribed to push."),
110
+ this.Kr(e.deviceToken, t, new Date()));
111
+ }
112
+ dn(i, e, t) {
113
+ const s = s => {
114
+ switch (s) {
115
+ case "granted":
116
+ return void ("function" == typeof i && i());
117
+ case "default":
118
+ return void ("function" == typeof e && e());
119
+ case "denied":
120
+ return void ("function" == typeof t && t());
121
+ default:
122
+ r.D.error("Received unexpected permission result " + s);
123
+ }
124
+ };
125
+ let n = !1;
126
+ const o = window.Notification.requestPermission(i => {
127
+ n && s(i);
128
+ });
129
+ o
130
+ ? o.then(i => {
131
+ s(i);
132
+ })
133
+ : (n = !0);
134
+ }
135
+ Jr(i, e, t, s) {
136
+ const n = { userVisibleOnly: !0 };
137
+ null != e && (n.applicationServerKey = e),
138
+ i.pushManager
139
+ .subscribe(n)
140
+ .then(i => {
141
+ r.D.info("Device successfully subscribed to push."),
142
+ this.Kr(i, t, new Date());
143
+ })
144
+ .catch(i => {
145
+ dt.isPushBlocked()
146
+ ? (r.D.info("Permission for push notifications was denied."),
147
+ "function" == typeof s && s(!1))
148
+ : r.D.error("Push subscription failed: " + i);
149
+ });
150
+ }
151
+ pn() {
152
+ return this.Vr
153
+ ? navigator.serviceWorker.getRegistration()
154
+ : navigator.serviceWorker.register(this.Nr).then(() =>
155
+ navigator.serviceWorker.ready.then(
156
+ i => (
157
+ i &&
158
+ "function" == typeof i.update &&
159
+ i.update().catch(i => {
160
+ r.D.info("ServiceWorker update failed: " + i);
161
+ }),
162
+ i
163
+ )
164
+ )
165
+ );
166
+ }
167
+ bn(i) {
168
+ this.Vr ||
169
+ (i.unregister(), r.D.info("Service worker successfully unregistered."));
170
+ }
171
+ subscribe(i, e) {
172
+ if (dt.isPushSupported()) {
173
+ if (this.Or) {
174
+ if (!this.Vr && null != window.location) {
175
+ let i = this.Nr;
176
+ -1 === i.indexOf(window.location.host) &&
177
+ (i = window.location.host + i),
178
+ -1 === i.indexOf(window.location.protocol) &&
179
+ (i = window.location.protocol + "//" + i);
180
+ const e = i.substr(0, i.lastIndexOf("/") + 1);
181
+ if (0 !== H.gn().indexOf(e))
182
+ return void r.D.error(
183
+ "Cannot subscribe to push from a path higher than the service worker location (tried to subscribe from " +
184
+ window.location.pathname +
185
+ " but service worker is at " +
186
+ i +
187
+ ")"
188
+ );
189
+ }
190
+ if (dt.isPushBlocked())
191
+ return void this.hn(
192
+ "Notifications from this site are blocked. This may be a temporary embargo or a permanent denial.",
193
+ e
194
+ );
195
+ if (this.gt && !this.gt.wn() && 0 === this.gt.Zs())
196
+ return (
197
+ r.D.info(
198
+ "Waiting for VAPID key from server config before subscribing to push."
199
+ ),
200
+ void this.gt.yn(() => {
201
+ this.subscribe(i, e);
202
+ })
203
+ );
204
+ const t = () => {
205
+ r.D.info("Permission for push notifications was denied."),
206
+ "function" == typeof e && e(!1);
207
+ },
208
+ s = () => {
209
+ let i = "Permission for push notifications was ignored.";
210
+ dt.isPushBlocked() &&
211
+ (i +=
212
+ " The browser has automatically blocked further permission requests for a period (probably 1 week)."),
213
+ r.D.info(i),
214
+ "function" == typeof e && e(!0);
215
+ },
216
+ n = dt.isPushPermissionGranted(),
217
+ u = () => {
218
+ n ||
219
+ this.Sr.setPushNotificationSubscriptionType(
220
+ User.NotificationSubscriptionTypes.OPTED_IN
221
+ ),
222
+ this.pn()
223
+ .then(t => {
224
+ if (null == t)
225
+ return (
226
+ r.D.error(
227
+ "No service worker registration. Set the `manageServiceWorkerExternally` initialization option to false or ensure that your service worker is registered before calling registerPush."
228
+ ),
229
+ void ("function" == typeof e && e())
230
+ );
231
+ t.pushManager
232
+ .getSubscription()
233
+ .then(s => {
234
+ let n = null;
235
+ if (
236
+ (this.gt &&
237
+ null != this.gt.wn() &&
238
+ (n = r.kn.vn(this.gt.wn())),
239
+ s)
240
+ ) {
241
+ let u = null,
242
+ a = null;
243
+ const h = this.h.I(o.q.Pn);
244
+ if (h && !p(h)) {
245
+ let i;
246
+ try {
247
+ i = ei.Sn(h).Dn;
248
+ } catch (e) {
249
+ i = null;
250
+ }
251
+ null == i ||
252
+ isNaN(i.getTime()) ||
253
+ 0 === i.getTime() ||
254
+ ((u = i),
255
+ (a = new Date(u)),
256
+ a.setMonth(u.getMonth() + 6));
257
+ }
258
+ null != n &&
259
+ s.options &&
260
+ s.options.applicationServerKey &&
261
+ s.options.applicationServerKey.byteLength &&
262
+ s.options.applicationServerKey.byteLength > 0 &&
263
+ !ii(n, new Uint8Array(s.options.applicationServerKey))
264
+ ? (s.options.applicationServerKey.byteLength > 12
265
+ ? r.D.info(
266
+ "Device was already subscribed to push using a different VAPID provider, creating new subscription."
267
+ )
268
+ : r.D.info(
269
+ "Attempting to upgrade a gcm_sender_id-based push registration to VAPID - depending on the browser this may or may not result in the same gcm_sender_id-based subscription."
270
+ ),
271
+ this.Hr(s, t, n, i, e))
272
+ : s.expirationTime &&
273
+ new Date(s.expirationTime) <= new Date().valueOf()
274
+ ? (r.D.info(
275
+ "Push subscription is expired, creating new subscription."
276
+ ),
277
+ this.Hr(s, t, n, i, e))
278
+ : h && p(h)
279
+ ? this.Hr(s, t, n, i, e)
280
+ : null == a
281
+ ? (r.D.info(
282
+ "No push subscription creation date found, creating new subscription."
283
+ ),
284
+ this.Hr(s, t, n, i, e))
285
+ : a <= new Date().valueOf()
286
+ ? (r.D.info(
287
+ "Push subscription older than 6 months, creating new subscription."
288
+ ),
289
+ this.Hr(s, t, n, i, e))
290
+ : (r.D.info(
291
+ "Device already subscribed to push, sending existing subscription to backend."
292
+ ),
293
+ this.Kr(s, i, u));
294
+ } else this.Jr(t, n, i, e);
295
+ })
296
+ .catch(i => {
297
+ r.D.error(
298
+ "Error checking current push subscriptions: " + i
299
+ );
300
+ });
301
+ })
302
+ .catch(i => {
303
+ r.D.error("ServiceWorker registration failed: " + i);
304
+ });
305
+ };
306
+ this.dn(u, s, t);
307
+ } else if (this.Mr) {
308
+ if (null == this.Ir || "" === this.Ir)
309
+ return void r.D.error(
310
+ "You must supply the safariWebsitePushId initialization option in order to use registerPush on Safari"
311
+ );
312
+ const t = window.safari.pushNotification.permission(this.Ir);
313
+ this.fn(this.Ir, t, i, e);
314
+ }
315
+ } else r.D.info(this._n);
316
+ }
317
+ unsubscribe(i, e) {
318
+ dt.isPushSupported()
319
+ ? this.Or
320
+ ? navigator.serviceWorker.getRegistration().then(t => {
321
+ t
322
+ ? t.pushManager
323
+ .getSubscription()
324
+ .then(s => {
325
+ s &&
326
+ (this.Xr(),
327
+ s
328
+ .unsubscribe()
329
+ .then(s => {
330
+ s
331
+ ? (r.D.info(
332
+ "Device successfully unsubscribed from push."
333
+ ),
334
+ "function" == typeof i && i())
335
+ : (r.D.error(
336
+ "Failed to unsubscribe device from push."
337
+ ),
338
+ "function" == typeof e && e()),
339
+ this.bn(t);
340
+ })
341
+ .catch(i => {
342
+ r.D.error("Push unsubscription error: " + i),
343
+ "function" == typeof e && e();
344
+ }));
345
+ })
346
+ .catch(i => {
347
+ r.D.error("Error unsubscribing from push: " + i),
348
+ "function" == typeof e && e();
349
+ })
350
+ : (r.D.info("Device already unsubscribed from push."),
351
+ "function" == typeof i && i());
352
+ })
353
+ : this.Mr &&
354
+ (this.Xr(),
355
+ r.D.info("Device unsubscribed from push."),
356
+ "function" == typeof i && i())
357
+ : r.D.info(this._n);
358
+ }
359
+ }
360
+ aa._n = "Push notifications are not supported in this browser.";
@@ -1 +1,8 @@
1
- import e from"../managers/braze-instance.js";import ea from"./push-manager-factory.js";export function requestPushPermission(r,n){if(e.rr())return ea.aa().subscribe(((n,o,t)=>{e.mr().requestImmediateDataFlush(),"function"==typeof r&&r(n,o,t)}),n)}
1
+ import e from "../managers/braze-instance.js";
2
+ import ea from "./push-manager-factory.js";
3
+ export function requestPushPermission(r, n) {
4
+ if (e.rr())
5
+ return ea.aa().subscribe((n, o, t) => {
6
+ e.mr().requestImmediateDataFlush(), "function" == typeof r && r(n, o, t);
7
+ }, n);
8
+ }
@@ -1 +1,5 @@
1
- import e from"../managers/braze-instance.js";import ea from"./push-manager-factory.js";export function unregisterPush(r,n){if(e.rr())return ea.aa().unsubscribe(r,n)}
1
+ import e from "../managers/braze-instance.js";
2
+ import ea from "./push-manager-factory.js";
3
+ export function unregisterPush(r, n) {
4
+ if (e.rr()) return ea.aa().unsubscribe(r, n);
5
+ }
@@ -1 +1,34 @@
1
- const ht={Ir:()=>"serviceWorker"in navigator&&"undefined"!=typeof ServiceWorkerRegistration&&"showNotification"in ServiceWorkerRegistration.prototype&&"PushManager"in window,Cr:()=>"safari"in window&&"pushNotification"in window.safari&&"function"==typeof window.safari.pushNotification.permission&&"function"==typeof window.safari.pushNotification.requestPermission,isPushSupported:()=>ht.Ir()||ht.Cr(),isPushBlocked:()=>{const i=ht.isPushSupported()&&"Notification"in window&&null!=window.Notification&&null!=window.Notification.permission&&"denied"===window.Notification.permission,n=ht.isPushSupported()&&(!("Notification"in window)||null==window.Notification);return i||n},isPushPermissionGranted:()=>ht.isPushSupported()&&"Notification"in window&&null!=window.Notification&&null!=window.Notification.permission&&"granted"===window.Notification.permission,Xe:()=>!ht.isPushBlocked()&&ht.isPushSupported()&&!ht.isPushPermissionGranted()};export default ht;
1
+ const dt = {
2
+ Br: () =>
3
+ "serviceWorker" in navigator &&
4
+ "undefined" != typeof ServiceWorkerRegistration &&
5
+ "showNotification" in ServiceWorkerRegistration.prototype &&
6
+ "PushManager" in window,
7
+ Yr: () =>
8
+ "safari" in window &&
9
+ "pushNotification" in window.safari &&
10
+ "function" == typeof window.safari.pushNotification.permission &&
11
+ "function" == typeof window.safari.pushNotification.requestPermission,
12
+ isPushSupported: () => dt.Br() || dt.Yr(),
13
+ isPushBlocked: () => {
14
+ const i =
15
+ dt.isPushSupported() &&
16
+ "Notification" in window &&
17
+ null != window.Notification &&
18
+ null != window.Notification.permission &&
19
+ "denied" === window.Notification.permission,
20
+ n =
21
+ dt.isPushSupported() &&
22
+ (!("Notification" in window) || null == window.Notification);
23
+ return i || n;
24
+ },
25
+ isPushPermissionGranted: () =>
26
+ dt.isPushSupported() &&
27
+ "Notification" in window &&
28
+ null != window.Notification &&
29
+ null != window.Notification.permission &&
30
+ "granted" === window.Notification.permission,
31
+ ji: () =>
32
+ !dt.isPushBlocked() && dt.isPushSupported() && !dt.isPushPermissionGranted()
33
+ };
34
+ export default dt;
package/src/User/index.js CHANGED
@@ -1 +1 @@
1
- export{default as User}from"./user.js";
1
+ export { default as User } from "./user.js";
@@ -1 +1,70 @@
1
- import _t from"../models/identifier.js";import{getByteLength as U}from"../util/string-utils.js";import ei from"../models/push-token.js";import r from"../../shared-lib/braze-shared-lib.js";import{STORAGE_KEYS as o}from"../managers/storage-manager.js";import{User}from"../User/index.js";export default class bt{constructor(t,s){this.gt=t,this.h=s}getUserId(){const t=this.h.tu(o.iu.su);if(null==t)return null;let s=t.eu,i=U(s);if(i>User.ee){for(;i>User.ee;)s=s.slice(0,s.length-1),i=U(s);t.eu=s,this.h.ru(o.iu.su,t)}return s}uu(t){const s=null==this.getUserId();this.h.ru(o.iu.su,new _t(t)),s&&this.h.ou(t)}setCustomUserAttribute(t,s){if(this.gt.hu(t))return r.D.info('Custom Attribute "'+t+'" is blocklisted, ignoring.'),!1;const i={};return i[t]=s,this.nu(User.lu,i,!0)}nu(t,s,i,e){const u=this.h.mu(this.getUserId(),t,s);return!e&&u&&r.D.info(`Logged${i?" custom":""} attribute ${t} with value ${s}`),u}Gr(t,s,i,e,u){this.nu("push_token",t,!1,!0),this.nu("custom_push_public_key",i,!1,!0),this.nu("custom_push_user_auth",e,!1,!0),this.nu("custom_push_vapid_public_key",u,!1,!0);const h=r.xt.Ft,n=new r.qt(h,r.D),l=new ei(t,s,i,e,u);this.h.B(o.q.gn,l.ss()),n.setItem(h.$t.pu,h.se,!0)}Jr(t){if(this.nu("push_token",null,!1,!0),this.nu("custom_push_public_key",null,!1,!0),this.nu("custom_push_user_auth",null,!1,!0),this.nu("custom_push_vapid_public_key",null,!1,!0),t){const t=r.xt.Ft,s=new r.qt(t,r.D);this.h.B(o.q.gn,!1),s.setItem(t.$t.pu,t.se,!1)}}}
1
+ import _t from "../models/identifier.js";
2
+ import { getByteLength as U } from "../util/string-utils.js";
3
+ import ei from "../models/push-token.js";
4
+ import r from "../../shared-lib/braze-shared-lib.js";
5
+ import { STORAGE_KEYS as o } from "../managers/storage-manager.js";
6
+ import { User } from "../User/index.js";
7
+ export default class bt {
8
+ constructor(t, s) {
9
+ (this.gt = t), (this.h = s);
10
+ }
11
+ getUserId() {
12
+ const t = this.h.tu(o.eu.su);
13
+ if (null == t) return null;
14
+ let s = t.iu,
15
+ e = U(s);
16
+ if (e > User.ee) {
17
+ for (; e > User.ee; ) (s = s.slice(0, s.length - 1)), (e = U(s));
18
+ (t.iu = s), this.h.ru(o.eu.su, t);
19
+ }
20
+ return s;
21
+ }
22
+ uu(t) {
23
+ const s = null == this.getUserId();
24
+ this.h.ru(o.eu.su, new _t(t)), s && this.h.ou(t);
25
+ }
26
+ setCustomUserAttribute(t, s) {
27
+ if (this.gt.hu(t))
28
+ return (
29
+ r.D.info('Custom Attribute "' + t + '" is blocklisted, ignoring.'), !1
30
+ );
31
+ const e = {};
32
+ return (e[t] = s), this.nu(User.lu, e, !0);
33
+ }
34
+ nu(t, s, e, i) {
35
+ const u = this.h.mu(this.getUserId(), t, s);
36
+ let o = "",
37
+ h = t,
38
+ n = s;
39
+ return (
40
+ e &&
41
+ ((o = " custom"),
42
+ "object" == typeof s && ((h = Object.keys(s)[0]), (n = s[h]))),
43
+ !i && u && r.D.info(`Logged${o} attribute ${h} with value ${n}`),
44
+ u
45
+ );
46
+ }
47
+ Qr(t, s, e, i, u) {
48
+ this.nu("push_token", t, !1, !0),
49
+ this.nu("custom_push_public_key", e, !1, !0),
50
+ this.nu("custom_push_user_auth", i, !1, !0),
51
+ this.nu("custom_push_vapid_public_key", u, !1, !0);
52
+ const h = r.xt.Ft,
53
+ n = new r.qt(h, r.D),
54
+ l = new ei(t, s, e, i, u);
55
+ this.h.B(o.q.Pn, l.ss()), n.setItem(h.$t.cu, h.se, !0);
56
+ }
57
+ Zr(t) {
58
+ if (
59
+ (this.nu("push_token", null, !1, !0),
60
+ this.nu("custom_push_public_key", null, !1, !0),
61
+ this.nu("custom_push_user_auth", null, !1, !0),
62
+ this.nu("custom_push_vapid_public_key", null, !1, !0),
63
+ t)
64
+ ) {
65
+ const t = r.xt.Ft,
66
+ s = new r.qt(t, r.D);
67
+ this.h.B(o.q.Pn, !1), s.setItem(t.$t.cu, t.se, !1);
68
+ }
69
+ }
70
+ }