@metamask-previews/notification-services-controller 25.0.0-preview-1590150f2 → 25.0.0-preview-e196c24cd

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 (100) hide show
  1. package/CHANGELOG.md +0 -22
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs +2 -24
  3. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  4. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  5. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  6. package/dist/NotificationServicesController/NotificationServicesController.mjs +2 -24
  7. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  8. package/dist/NotificationServicesController/index.cjs +1 -3
  9. package/dist/NotificationServicesController/index.cjs.map +1 -1
  10. package/dist/NotificationServicesController/index.d.cts +0 -1
  11. package/dist/NotificationServicesController/index.d.cts.map +1 -1
  12. package/dist/NotificationServicesController/index.d.mts +0 -1
  13. package/dist/NotificationServicesController/index.d.mts.map +1 -1
  14. package/dist/NotificationServicesController/index.mjs +0 -1
  15. package/dist/NotificationServicesController/index.mjs.map +1 -1
  16. package/dist/NotificationServicesController/processors/process-api-notifications.cjs +0 -2
  17. package/dist/NotificationServicesController/processors/process-api-notifications.cjs.map +1 -1
  18. package/dist/NotificationServicesController/processors/process-api-notifications.d.cts.map +1 -1
  19. package/dist/NotificationServicesController/processors/process-api-notifications.d.mts.map +1 -1
  20. package/dist/NotificationServicesController/processors/process-api-notifications.mjs +0 -2
  21. package/dist/NotificationServicesController/processors/process-api-notifications.mjs.map +1 -1
  22. package/dist/NotificationServicesController/processors/process-feature-announcement.cjs +0 -2
  23. package/dist/NotificationServicesController/processors/process-feature-announcement.cjs.map +1 -1
  24. package/dist/NotificationServicesController/processors/process-feature-announcement.d.cts.map +1 -1
  25. package/dist/NotificationServicesController/processors/process-feature-announcement.d.mts.map +1 -1
  26. package/dist/NotificationServicesController/processors/process-feature-announcement.mjs +0 -2
  27. package/dist/NotificationServicesController/processors/process-feature-announcement.mjs.map +1 -1
  28. package/dist/NotificationServicesController/processors/process-snap-notifications.cjs +0 -2
  29. package/dist/NotificationServicesController/processors/process-snap-notifications.cjs.map +1 -1
  30. package/dist/NotificationServicesController/processors/process-snap-notifications.d.cts.map +1 -1
  31. package/dist/NotificationServicesController/processors/process-snap-notifications.d.mts.map +1 -1
  32. package/dist/NotificationServicesController/processors/process-snap-notifications.mjs +0 -2
  33. package/dist/NotificationServicesController/processors/process-snap-notifications.mjs.map +1 -1
  34. package/dist/NotificationServicesController/types/notification/notification.cjs.map +1 -1
  35. package/dist/NotificationServicesController/types/notification/notification.d.cts +0 -1
  36. package/dist/NotificationServicesController/types/notification/notification.d.cts.map +1 -1
  37. package/dist/NotificationServicesController/types/notification/notification.d.mts +0 -1
  38. package/dist/NotificationServicesController/types/notification/notification.d.mts.map +1 -1
  39. package/dist/NotificationServicesController/types/notification/notification.mjs.map +1 -1
  40. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  41. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +4 -3
  42. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  43. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +4 -3
  44. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  45. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  46. package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
  47. package/dist/NotificationServicesPushController/types/index.d.cts +0 -1
  48. package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
  49. package/dist/NotificationServicesPushController/types/index.d.mts +0 -1
  50. package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
  51. package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
  52. package/dist/NotificationServicesPushController/utils/index.cjs +0 -1
  53. package/dist/NotificationServicesPushController/utils/index.cjs.map +1 -1
  54. package/dist/NotificationServicesPushController/utils/index.d.cts +0 -1
  55. package/dist/NotificationServicesPushController/utils/index.d.cts.map +1 -1
  56. package/dist/NotificationServicesPushController/utils/index.d.mts +0 -1
  57. package/dist/NotificationServicesPushController/utils/index.d.mts.map +1 -1
  58. package/dist/NotificationServicesPushController/utils/index.mjs +0 -1
  59. package/dist/NotificationServicesPushController/utils/index.mjs.map +1 -1
  60. package/dist/NotificationServicesPushController/web/push-utils.cjs +25 -13
  61. package/dist/NotificationServicesPushController/web/push-utils.cjs.map +1 -1
  62. package/dist/NotificationServicesPushController/web/push-utils.d.cts +3 -3
  63. package/dist/NotificationServicesPushController/web/push-utils.d.cts.map +1 -1
  64. package/dist/NotificationServicesPushController/web/push-utils.d.mts +3 -3
  65. package/dist/NotificationServicesPushController/web/push-utils.d.mts.map +1 -1
  66. package/dist/NotificationServicesPushController/web/push-utils.mjs +25 -13
  67. package/dist/NotificationServicesPushController/web/push-utils.mjs.map +1 -1
  68. package/dist/shared/is-onchain-notification.cjs +10 -4
  69. package/dist/shared/is-onchain-notification.cjs.map +1 -1
  70. package/dist/shared/is-onchain-notification.d.cts +4 -4
  71. package/dist/shared/is-onchain-notification.d.cts.map +1 -1
  72. package/dist/shared/is-onchain-notification.d.mts +4 -4
  73. package/dist/shared/is-onchain-notification.d.mts.map +1 -1
  74. package/dist/shared/is-onchain-notification.mjs +10 -4
  75. package/dist/shared/is-onchain-notification.mjs.map +1 -1
  76. package/package.json +1 -1
  77. package/dist/NotificationServicesController/utils/get-notification-subtype.cjs +0 -33
  78. package/dist/NotificationServicesController/utils/get-notification-subtype.cjs.map +0 -1
  79. package/dist/NotificationServicesController/utils/get-notification-subtype.d.cts +0 -17
  80. package/dist/NotificationServicesController/utils/get-notification-subtype.d.cts.map +0 -1
  81. package/dist/NotificationServicesController/utils/get-notification-subtype.d.mts +0 -17
  82. package/dist/NotificationServicesController/utils/get-notification-subtype.d.mts.map +0 -1
  83. package/dist/NotificationServicesController/utils/get-notification-subtype.mjs +0 -29
  84. package/dist/NotificationServicesController/utils/get-notification-subtype.mjs.map +0 -1
  85. package/dist/NotificationServicesPushController/types/push-analytics.cjs +0 -5
  86. package/dist/NotificationServicesPushController/types/push-analytics.cjs.map +0 -1
  87. package/dist/NotificationServicesPushController/types/push-analytics.d.cts +0 -18
  88. package/dist/NotificationServicesPushController/types/push-analytics.d.cts.map +0 -1
  89. package/dist/NotificationServicesPushController/types/push-analytics.d.mts +0 -18
  90. package/dist/NotificationServicesPushController/types/push-analytics.d.mts.map +0 -1
  91. package/dist/NotificationServicesPushController/types/push-analytics.mjs +0 -4
  92. package/dist/NotificationServicesPushController/types/push-analytics.mjs.map +0 -1
  93. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.cjs +0 -26
  94. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.cjs.map +0 -1
  95. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.d.cts +0 -12
  96. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.d.cts.map +0 -1
  97. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.d.mts +0 -12
  98. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.d.mts.map +0 -1
  99. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.mjs +0 -22
  100. package/dist/NotificationServicesPushController/utils/to-push-analytics-payload.mjs.map +0 -1
@@ -8,7 +8,8 @@ const app_1 = require("firebase/app");
8
8
  const messaging_1 = require("firebase/messaging");
9
9
  const sw_1 = require("firebase/messaging/sw");
10
10
  const loglevel_1 = __importDefault(require("loglevel"));
11
- const to_push_analytics_payload_1 = require("../utils/to-push-analytics-payload.cjs");
11
+ const NotificationServicesController_1 = require("../../NotificationServicesController/index.cjs");
12
+ const to_raw_notification_1 = require("../../shared/to-raw-notification.cjs");
12
13
  // Exported to help testing
13
14
  // eslint-disable-next-line import-x/no-mutable-exports
14
15
  exports.supportedCache = null;
@@ -103,16 +104,27 @@ async function listenToPushNotificationsReceived(env, handler) {
103
104
  // eslint-disable-next-line @typescript-eslint/no-misused-promises
104
105
  async (payload) => {
105
106
  try {
106
- const analyticsPayload = (0, to_push_analytics_payload_1.toPushAnalyticsPayload)(payload?.data);
107
- if (!analyticsPayload) {
107
+ // MessagePayload shapes are not known
108
+ // TODO - provide open-api unfied backend/frontend types
109
+ // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile
110
+ const data = JSON.parse(payload?.data?.data ?? 'null');
111
+ if (!data) {
108
112
  return;
109
113
  }
110
- await handler?.(analyticsPayload);
114
+ if (!(0, NotificationServicesController_1.isOnChainRawNotification)(data)) {
115
+ return;
116
+ }
117
+ const notificationData = (0, to_raw_notification_1.toRawAPINotification)(data);
118
+ const notification = (0, NotificationServicesController_1.safeProcessNotification)(notificationData);
119
+ if (!notification) {
120
+ return;
121
+ }
122
+ await handler?.(notification);
111
123
  }
112
124
  catch (error) {
113
- // Do Nothing, cannot handle a bad notification
114
- loglevel_1.default.error('Unable to handle push notification:', {
115
- notification: payload?.data,
125
+ // Do Nothing, cannot parse a bad notification
126
+ loglevel_1.default.error('Unable to send push notification:', {
127
+ notification: payload?.data?.data,
116
128
  error,
117
129
  });
118
130
  }
@@ -150,13 +162,13 @@ function listenToPushNotificationsClicked(handler) {
150
162
  */
151
163
  function createSubscribeToPushNotifications(props) {
152
164
  return async function (env) {
153
- const onBackgroundMessageSub = await listenToPushNotificationsReceived(env, async (analyticsPayload) => {
154
- props.messenger.publish('NotificationServicesPushController:onNewNotifications', analyticsPayload);
155
- await props.onReceivedHandler(analyticsPayload);
165
+ const onBackgroundMessageSub = await listenToPushNotificationsReceived(env, async (notification) => {
166
+ props.messenger.publish('NotificationServicesPushController:onNewNotifications', notification);
167
+ await props.onReceivedHandler(notification);
156
168
  });
157
- const onClickSub = listenToPushNotificationsClicked((event, analyticsPayload) => {
158
- props.messenger.publish('NotificationServicesPushController:pushNotificationClicked', analyticsPayload);
159
- props.onClickHandler(event, analyticsPayload);
169
+ const onClickSub = listenToPushNotificationsClicked((event, notification) => {
170
+ props.messenger.publish('NotificationServicesPushController:pushNotificationClicked', notification);
171
+ props.onClickHandler(event, notification);
160
172
  });
161
173
  const unsubscribe = () => {
162
174
  onBackgroundMessageSub?.();
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";;;;;;AAIA,sCAAqD;AACrD,kDAA2D;AAC3D,8CAI+B;AAE/B,wDAA2B;AAK3B,sFAA4E;AAI5E,2BAA2B;AAC3B,uDAAuD;AAC5C,QAAA,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAsB,EAAE;IACvD,4EAA4E;IAC5E,gFAAgF;IAChF,kDAAkD;IAClD,sBAAc,KAAd,sBAAc,GAAK,MAAM,IAAA,gBAAW,GAAE,EAAC;IACvC,OAAO,sBAAc,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,KAAK,EAC7B,GAAwB,EACF,EAAE;IACxB,IAAI,CAAC;QACH,OAAO,IAAA,YAAM,GAAE,CAAC;IAClB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,cAAc,GAAG;YACrB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,aAAa,EAAE,GAAG,CAAC,aAAa;YAChC,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;YACxC,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,aAAa,EAAE,GAAG,CAAC,aAAa;SACjC,CAAC;QACF,OAAO,IAAA,mBAAa,EAAC,cAAc,CAAC,CAAC;IACvC,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,oBAAoB,GAAG,KAAK,EAChC,GAAwB,EACG,EAAE;IAC7B,MAAM,SAAS,GAAG,MAAM,mBAAmB,EAAE,CAAC;IAC9C,IAAI,CAAC,SAAS,EAAE,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AAS,CAAC,OAAO,CACrB,uDAAuD,EACvD,gBAAgB,CACjB,CAAC;YACF,MAAM,KAAK,CAAC,iBAAiB,CAAC,gBAAgB,CAAC,CAAC;QAClD,CAAC,CACF,CAAC;QACF,MAAM,UAAU,GAAG,gCAAgC,CACjD,CAAC,KAAK,EAAE,gBAAgB,EAAQ,EAAE;YAChC,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,4DAA4D,EAC5D,gBAAgB,CACjB,CAAC;YACF,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;QAChD,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAS,EAAE;YAC7B,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAjCD,gFAiCC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushAnalyticsPayload } from '../types';\nimport type { PushNotificationEnv } from '../types/firebase';\nimport { toPushAnalyticsPayload } from '../utils/to-push-analytics-payload';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async (): Promise<boolean> => {\n // Race condition is acceptable here - worst case is isSupported() is called\n // multiple times during initialization, which is harmless for caching a boolean\n // eslint-disable-next-line require-atomic-updates\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (payload: PushAnalyticsPayload) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload): Promise<void> => {\n try {\n const analyticsPayload = toPushAnalyticsPayload(payload?.data);\n\n if (!analyticsPayload) {\n return;\n }\n\n await handler?.(analyticsPayload);\n } catch (error) {\n // Do Nothing, cannot handle a bad notification\n log.error('Unable to handle push notification:', {\n notification: payload?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = (): void => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, payload: PushAnalyticsPayload) => void,\n): () => void {\n const clickHandler = (event: NotificationEvent): void => {\n // Get Data\n const data: PushAnalyticsPayload = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = (): void =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (payload: PushAnalyticsPayload) => void | Promise<void>;\n onClickHandler: (e: NotificationEvent, payload: PushAnalyticsPayload) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}): (env: PushNotificationEnv) => Promise<() => void> {\n return async function (env: PushNotificationEnv): Promise<() => void> {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (analyticsPayload): Promise<void> => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n analyticsPayload,\n );\n await props.onReceivedHandler(analyticsPayload);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, analyticsPayload): void => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n analyticsPayload,\n );\n props.onClickHandler(event, analyticsPayload);\n },\n );\n\n const unsubscribe = (): void => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
1
+ {"version":3,"file":"push-utils.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";;;;;;AAIA,sCAAqD;AACrD,kDAA2D;AAC3D,8CAI+B;AAE/B,wDAA2B;AAG3B,mGAG8C;AAC9C,8EAAwE;AAMxE,2BAA2B;AAC3B,uDAAuD;AAC5C,QAAA,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAsB,EAAE;IACvD,4EAA4E;IAC5E,gFAAgF;IAChF,kDAAkD;IAClD,sBAAc,KAAd,sBAAc,GAAK,MAAM,IAAA,gBAAW,GAAE,EAAC;IACvC,OAAO,sBAAc,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,KAAK,EAC7B,GAAwB,EACF,EAAE;IACxB,IAAI,CAAC;QACH,OAAO,IAAA,YAAM,GAAE,CAAC;IAClB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,cAAc,GAAG;YACrB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,aAAa,EAAE,GAAG,CAAC,aAAa;YAChC,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;YACxC,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,aAAa,EAAE,GAAG,CAAC,aAAa;SACjC,CAAC;QACF,OAAO,IAAA,mBAAa,EAAC,cAAc,CAAC,CAAC;IACvC,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,oBAAoB,GAAG,KAAK,EAChC,GAAwB,EACG,EAAE;I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AC,CAAC;IACvB,CAAC,CAAC;IAEF,IAAI,CAAC,gBAAgB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IACzD,MAAM,WAAW,GAAG,GAAS,EAAE,CAC7B,IAAI,CAAC,mBAAmB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IAC9D,OAAO,WAAW,CAAC;AACrB,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,kCAAkC,CAAC,KASlD;IACC,OAAO,KAAK,WAAW,GAAwB;QAC7C,MAAM,sBAAsB,GAAG,MAAM,iCAAiC,CACpE,GAAG,EACH,KAAK,EAAE,YAAY,EAAiB,EAAE;YACpC,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,uDAAuD,EACvD,YAAY,CACb,CAAC;YACF,MAAM,KAAK,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC;QAC9C,CAAC,CACF,CAAC;QACF,MAAM,UAAU,GAAG,gCAAgC,CACjD,CAAC,KAAK,EAAE,YAAY,EAAQ,EAAE;YAC5B,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,4DAA4D,EAC5D,YAAY,CACb,CAAC;YACF,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;QAC5C,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAS,EAAE;YAC7B,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAtCD,gFAsCC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport {\n isOnChainRawNotification,\n safeProcessNotification,\n} from '../../NotificationServicesController';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async (): Promise<boolean> => {\n // Race condition is acceptable here - worst case is isSupported() is called\n // multiple times during initialization, which is harmless for caching a boolean\n // eslint-disable-next-line require-atomic-updates\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload): Promise<void> => {\n try {\n // MessagePayload shapes are not known\n // TODO - provide open-api unfied backend/frontend types\n // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile\n const data: unknown | null = JSON.parse(payload?.data?.data ?? 'null');\n\n if (!data) {\n return;\n }\n\n if (!isOnChainRawNotification(data)) {\n return;\n }\n\n const notificationData = toRawAPINotification(data);\n const notification = safeProcessNotification(notificationData);\n\n if (!notification) {\n return;\n }\n\n await handler?.(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = (): void => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n): () => void {\n const clickHandler = (event: NotificationEvent): void => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = (): void =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}): (env: PushNotificationEnv) => Promise<() => void> {\n return async function (env: PushNotificationEnv): Promise<() => void> {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification): Promise<void> => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification): void => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = (): void => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  /// <reference lib="webworker" />
2
+ import type { Types } from "../../NotificationServicesController/index.cjs";
2
3
  import type { NotificationServicesPushControllerMessenger } from "../NotificationServicesPushController.cjs";
3
- import type { PushAnalyticsPayload } from "../types/index.cjs";
4
4
  import type { PushNotificationEnv } from "../types/firebase.cjs";
5
5
  export declare let supportedCache: boolean | null;
6
6
  /**
@@ -30,8 +30,8 @@ export declare function deleteRegToken(env: PushNotificationEnv): Promise<boolea
30
30
  * @returns a function that can be used by the controller
31
31
  */
32
32
  export declare function createSubscribeToPushNotifications(props: {
33
- onReceivedHandler: (payload: PushAnalyticsPayload) => void | Promise<void>;
34
- onClickHandler: (e: NotificationEvent, payload: PushAnalyticsPayload) => void;
33
+ onReceivedHandler: (notification: Types.INotification) => void | Promise<void>;
34
+ onClickHandler: (e: NotificationEvent, notification: Types.INotification) => void;
35
35
  messenger: NotificationServicesPushControllerMessenger;
36
36
  }): (env: PushNotificationEnv) => Promise<() => void>;
37
37
  //# sourceMappingURL=push-utils.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,oBAAoB,EAAE,2BAAiB;AACrD,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAO7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAyCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AAiED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3E,cAAc,EAAE,CAAC,CAAC,EAAE,iBAAiB,EAAE,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAC;IAC9E,SAAS,EAAE,2CAA2C,CAAC;CACxD,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,MAAM,IAAI,CAAC,CA6BpD"}
1
+ {"version":3,"file":"push-utils.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAMlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAyCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AA+ED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,MAAM,IAAI,CAAC,CA6BpD"}
@@ -1,6 +1,6 @@
1
1
  /// <reference lib="webworker" />
2
+ import type { Types } from "../../NotificationServicesController/index.mjs";
2
3
  import type { NotificationServicesPushControllerMessenger } from "../NotificationServicesPushController.mjs";
3
- import type { PushAnalyticsPayload } from "../types/index.mjs";
4
4
  import type { PushNotificationEnv } from "../types/firebase.mjs";
5
5
  export declare let supportedCache: boolean | null;
6
6
  /**
@@ -30,8 +30,8 @@ export declare function deleteRegToken(env: PushNotificationEnv): Promise<boolea
30
30
  * @returns a function that can be used by the controller
31
31
  */
32
32
  export declare function createSubscribeToPushNotifications(props: {
33
- onReceivedHandler: (payload: PushAnalyticsPayload) => void | Promise<void>;
34
- onClickHandler: (e: NotificationEvent, payload: PushAnalyticsPayload) => void;
33
+ onReceivedHandler: (notification: Types.INotification) => void | Promise<void>;
34
+ onClickHandler: (e: NotificationEvent, notification: Types.INotification) => void;
35
35
  messenger: NotificationServicesPushControllerMessenger;
36
36
  }): (env: PushNotificationEnv) => Promise<() => void>;
37
37
  //# sourceMappingURL=push-utils.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,oBAAoB,EAAE,2BAAiB;AACrD,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAO7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAyCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AAiED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3E,cAAc,EAAE,CAAC,CAAC,EAAE,iBAAiB,EAAE,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAC;IAC9E,SAAS,EAAE,2CAA2C,CAAC;CACxD,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,MAAM,IAAI,CAAC,CA6BpD"}
1
+ {"version":3,"file":"push-utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAMlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAyCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AA+ED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,MAAM,IAAI,CAAC,CA6BpD"}
@@ -9,7 +9,8 @@ import { getToken, deleteToken } from "firebase/messaging";
9
9
  import { getMessaging, onBackgroundMessage, isSupported } from "firebase/messaging/sw";
10
10
  import $log from "loglevel";
11
11
  const log = $importDefault($log);
12
- import { toPushAnalyticsPayload } from "../utils/to-push-analytics-payload.mjs";
12
+ import { isOnChainRawNotification, safeProcessNotification } from "../../NotificationServicesController/index.mjs";
13
+ import { toRawAPINotification } from "../../shared/to-raw-notification.mjs";
13
14
  // Exported to help testing
14
15
  // eslint-disable-next-line import-x/no-mutable-exports
15
16
  export let supportedCache = null;
@@ -102,16 +103,27 @@ async function listenToPushNotificationsReceived(env, handler) {
102
103
  // eslint-disable-next-line @typescript-eslint/no-misused-promises
103
104
  async (payload) => {
104
105
  try {
105
- const analyticsPayload = toPushAnalyticsPayload(payload?.data);
106
- if (!analyticsPayload) {
106
+ // MessagePayload shapes are not known
107
+ // TODO - provide open-api unfied backend/frontend types
108
+ // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile
109
+ const data = JSON.parse(payload?.data?.data ?? 'null');
110
+ if (!data) {
107
111
  return;
108
112
  }
109
- await handler?.(analyticsPayload);
113
+ if (!isOnChainRawNotification(data)) {
114
+ return;
115
+ }
116
+ const notificationData = toRawAPINotification(data);
117
+ const notification = safeProcessNotification(notificationData);
118
+ if (!notification) {
119
+ return;
120
+ }
121
+ await handler?.(notification);
110
122
  }
111
123
  catch (error) {
112
- // Do Nothing, cannot handle a bad notification
113
- log.error('Unable to handle push notification:', {
114
- notification: payload?.data,
124
+ // Do Nothing, cannot parse a bad notification
125
+ log.error('Unable to send push notification:', {
126
+ notification: payload?.data?.data,
115
127
  error,
116
128
  });
117
129
  }
@@ -149,13 +161,13 @@ function listenToPushNotificationsClicked(handler) {
149
161
  */
150
162
  export function createSubscribeToPushNotifications(props) {
151
163
  return async function (env) {
152
- const onBackgroundMessageSub = await listenToPushNotificationsReceived(env, async (analyticsPayload) => {
153
- props.messenger.publish('NotificationServicesPushController:onNewNotifications', analyticsPayload);
154
- await props.onReceivedHandler(analyticsPayload);
164
+ const onBackgroundMessageSub = await listenToPushNotificationsReceived(env, async (notification) => {
165
+ props.messenger.publish('NotificationServicesPushController:onNewNotifications', notification);
166
+ await props.onReceivedHandler(notification);
155
167
  });
156
- const onClickSub = listenToPushNotificationsClicked((event, analyticsPayload) => {
157
- props.messenger.publish('NotificationServicesPushController:pushNotificationClicked', analyticsPayload);
158
- props.onClickHandler(event, analyticsPayload);
168
+ const onClickSub = listenToPushNotificationsClicked((event, notification) => {
169
+ props.messenger.publish('NotificationServicesPushController:pushNotificationClicked', notification);
170
+ props.onClickHandler(event, notification);
159
171
  });
160
172
  const unsubscribe = () => {
161
173
  onBackgroundMessageSub?.();
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts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B,CAAC,CAAC;QAClD,CAAC,CACF,CAAC;QACF,MAAM,UAAU,GAAG,gCAAgC,CACjD,CAAC,KAAK,EAAE,gBAAgB,EAAQ,EAAE;YAChC,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,4DAA4D,EAC5D,gBAAgB,CACjB,CAAC;YACF,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;QAChD,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAS,EAAE;YAC7B,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushAnalyticsPayload } from '../types';\nimport type { PushNotificationEnv } from '../types/firebase';\nimport { toPushAnalyticsPayload } from '../utils/to-push-analytics-payload';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async (): Promise<boolean> => {\n // Race condition is acceptable here - worst case is isSupported() is called\n // multiple times during initialization, which is harmless for caching a boolean\n // eslint-disable-next-line require-atomic-updates\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (payload: PushAnalyticsPayload) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload): Promise<void> => {\n try {\n const analyticsPayload = toPushAnalyticsPayload(payload?.data);\n\n if (!analyticsPayload) {\n return;\n }\n\n await handler?.(analyticsPayload);\n } catch (error) {\n // Do Nothing, cannot handle a bad notification\n log.error('Unable to handle push notification:', {\n notification: payload?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = (): void => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, payload: PushAnalyticsPayload) => void,\n): () => void {\n const clickHandler = (event: NotificationEvent): void => {\n // Get Data\n const data: PushAnalyticsPayload = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = (): void =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (payload: PushAnalyticsPayload) => void | Promise<void>;\n onClickHandler: (e: NotificationEvent, payload: PushAnalyticsPayload) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}): (env: PushNotificationEnv) => Promise<() => void> {\n return async function (env: PushNotificationEnv): Promise<() => void> {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (analyticsPayload): Promise<void> => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n analyticsPayload,\n );\n await props.onReceivedHandler(analyticsPayload);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, analyticsPayload): void => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n analyticsPayload,\n );\n props.onClickHandler(event, analyticsPayload);\n },\n );\n\n const unsubscribe = (): void => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
1
+ {"version":3,"file":"push-utils.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";;;;;;AAIA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,qBAAqB;AACrD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,2BAA2B;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACZ,8BAA8B;AAE/B,OAAO,IAAG,iBAAiB;;AAG3B,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACxB,uDAA6C;AAC9C,OAAO,EAAE,oBAAoB,EAAE,6CAAyC;AAMxE,2BAA2B;AAC3B,uDAAuD;AACvD,MAAM,CAAC,IAAI,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAsB,EAAE;IACvD,4EAA4E;IAC5E,gFAAgF;IAChF,kDAAkD;IAClD,cAAc,KAAd,cAAc,GAAK,MAAM,WAAW,EAAE,EAAC;IACvC,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,KAAK,EAC7B,GAAwB,EACF,EAAE;IACxB,IAAI,CAAC;QACH,OAAO,MAAM,EAAE,CAAC;IAClB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,cAAc,GAAG;YACrB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,aAAa,EAAE,GAAG,CAAC,aAAa;YAChC,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;YACxC,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,aAAa,EAAE,GAAG,CAAC,aAAa;SACjC,CAAC;QACF,OAAO,aAAa,CAAC,cAAc,CAAC,CAAC;IACvC,CAAC;AAC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KAAK,EAAE,IAAI,CAAC,CAAC;IACvB,CAAC,CAAC;IAEF,IAAI,CAAC,gBAAgB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IACzD,MAAM,WAAW,GAAG,GAAS,EAAE,CAC7B,IAAI,CAAC,mBAAmB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IAC9D,OAAO,WAAW,CAAC;AACrB,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kCAAkC,CAAC,KASlD;IACC,OAAO,KAAK,WAAW,GAAwB;QAC7C,MAAM,sBAAsB,GAAG,MAAM,iCAAiC,CACpE,GAAG,EACH,KAAK,EAAE,YAAY,EAAiB,EAAE;YACpC,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,uDAAuD,EACvD,YAAY,CACb,CAAC;YACF,MAAM,KAAK,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC;QAC9C,CAAC,CACF,CAAC;QACF,MAAM,UAAU,GAAG,gCAAgC,CACjD,CAAC,KAAK,EAAE,YAAY,EAAQ,EAAE;YAC5B,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,4DAA4D,EAC5D,YAAY,CACb,CAAC;YACF,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;QAC5C,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAS,EAAE;YAC7B,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport {\n isOnChainRawNotification,\n safeProcessNotification,\n} from '../../NotificationServicesController';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async (): Promise<boolean> => {\n // Race condition is acceptable here - worst case is isSupported() is called\n // multiple times during initialization, which is harmless for caching a boolean\n // eslint-disable-next-line require-atomic-updates\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload): Promise<void> => {\n try {\n // MessagePayload shapes are not known\n // TODO - provide open-api unfied backend/frontend types\n // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile\n const data: unknown | null = JSON.parse(payload?.data?.data ?? 'null');\n\n if (!data) {\n return;\n }\n\n if (!isOnChainRawNotification(data)) {\n return;\n }\n\n const notificationData = toRawAPINotification(data);\n const notification = safeProcessNotification(notificationData);\n\n if (!notification) {\n return;\n }\n\n await handler?.(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = (): void => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n): () => void {\n const clickHandler = (event: NotificationEvent): void => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = (): void =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}): (env: PushNotificationEnv) => Promise<() => void> {\n return async function (env: PushNotificationEnv): Promise<() => void> {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification): Promise<void> => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification): void => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = (): void => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
@@ -1,15 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isOnChainRawNotification = void 0;
4
- const notification_api_type_guards_1 = require("./notification-api-type-guards.cjs");
5
4
  /**
6
- * Checks if the given value is an on-chain notification using the v4 `notification_type` discriminator.
5
+ * Checks if the given value is an OnChainRawNotification object.
7
6
  *
8
7
  * @param notification - The value to check.
9
- * @returns True if the value is an on-chain notification, false otherwise.
8
+ * @returns True if the value is an OnChainRawNotification object, false otherwise.
10
9
  */
11
10
  function isOnChainRawNotification(notification) {
12
- return (0, notification_api_type_guards_1.isOnChainNotification)(notification);
11
+ const assumed = notification;
12
+ // We don't have a validation/parsing library to check all possible types of an on chain notification
13
+ // It is safe enough just to check "some" fields, and catch any errors down the line if the shape is bad.
14
+ const isValidEnoughToBeOnChainNotification = [
15
+ assumed?.id,
16
+ assumed?.payload?.data,
17
+ ].every((field) => field !== undefined);
18
+ return isValidEnoughToBeOnChainNotification;
13
19
  }
14
20
  exports.isOnChainRawNotification = isOnChainRawNotification;
15
21
  //# sourceMappingURL=is-onchain-notification.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.cjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":";;;AAIA,qFAAuE;AAEvE;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,YAAqB;IAErB,OAAO,IAAA,oDAAqB,EAAC,YAA0C,CAAC,CAAC;AAC3E,CAAC;AAJD,4DAIC","sourcesContent":["import type {\n UnprocessedRawNotification,\n OnChainNotification,\n} from '../NotificationServicesController/types/notification-api';\nimport { isOnChainNotification } from './notification-api-type-guards';\n\n/**\n * Checks if the given value is an on-chain notification using the v4 `notification_type` discriminator.\n *\n * @param notification - The value to check.\n * @returns True if the value is an on-chain notification, false otherwise.\n */\nexport function isOnChainRawNotification(\n notification: unknown,\n): notification is OnChainNotification {\n return isOnChainNotification(notification as UnprocessedRawNotification);\n}\n"]}
1
+ {"version":3,"file":"is-onchain-notification.cjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,YAAqB;IAErB,MAAM,OAAO,GAAG,YAAsC,CAAC;IAEvD,qGAAqG;IACrG,yGAAyG;IACzG,MAAM,oCAAoC,GAAG;QAC3C,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,OAAO,EAAE,IAAI;KACvB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IACxC,OAAO,oCAAoC,CAAC;AAC9C,CAAC;AAZD,4DAYC","sourcesContent":["import type { OnChainRawNotification } from '../NotificationServicesController';\n\n/**\n * Checks if the given value is an OnChainRawNotification object.\n *\n * @param notification - The value to check.\n * @returns True if the value is an OnChainRawNotification object, false otherwise.\n */\nexport function isOnChainRawNotification(\n notification: unknown,\n): notification is OnChainRawNotification {\n const assumed = notification as OnChainRawNotification;\n\n // We don't have a validation/parsing library to check all possible types of an on chain notification\n // It is safe enough just to check \"some\" fields, and catch any errors down the line if the shape is bad.\n const isValidEnoughToBeOnChainNotification = [\n assumed?.id,\n assumed?.payload?.data,\n ].every((field) => field !== undefined);\n return isValidEnoughToBeOnChainNotification;\n}\n"]}
@@ -1,9 +1,9 @@
1
- import type { OnChainNotification } from "../NotificationServicesController/types/notification-api/index.cjs";
1
+ import type { OnChainRawNotification } from "../NotificationServicesController/index.cjs";
2
2
  /**
3
- * Checks if the given value is an on-chain notification using the v4 `notification_type` discriminator.
3
+ * Checks if the given value is an OnChainRawNotification object.
4
4
  *
5
5
  * @param notification - The value to check.
6
- * @returns True if the value is an on-chain notification, false otherwise.
6
+ * @returns True if the value is an OnChainRawNotification object, false otherwise.
7
7
  */
8
- export declare function isOnChainRawNotification(notification: unknown): notification is OnChainNotification;
8
+ export declare function isOnChainRawNotification(notification: unknown): notification is OnChainRawNotification;
9
9
  //# sourceMappingURL=is-onchain-notification.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.d.cts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,mBAAmB,EACpB,2EAAiE;AAGlE;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,OAAO,GACpB,YAAY,IAAI,mBAAmB,CAErC"}
1
+ {"version":3,"file":"is-onchain-notification.d.cts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,oDAA0C;AAEhF;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,OAAO,GACpB,YAAY,IAAI,sBAAsB,CAUxC"}
@@ -1,9 +1,9 @@
1
- import type { OnChainNotification } from "../NotificationServicesController/types/notification-api/index.mjs";
1
+ import type { OnChainRawNotification } from "../NotificationServicesController/index.mjs";
2
2
  /**
3
- * Checks if the given value is an on-chain notification using the v4 `notification_type` discriminator.
3
+ * Checks if the given value is an OnChainRawNotification object.
4
4
  *
5
5
  * @param notification - The value to check.
6
- * @returns True if the value is an on-chain notification, false otherwise.
6
+ * @returns True if the value is an OnChainRawNotification object, false otherwise.
7
7
  */
8
- export declare function isOnChainRawNotification(notification: unknown): notification is OnChainNotification;
8
+ export declare function isOnChainRawNotification(notification: unknown): notification is OnChainRawNotification;
9
9
  //# sourceMappingURL=is-onchain-notification.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.d.mts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,mBAAmB,EACpB,2EAAiE;AAGlE;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,OAAO,GACpB,YAAY,IAAI,mBAAmB,CAErC"}
1
+ {"version":3,"file":"is-onchain-notification.d.mts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,oDAA0C;AAEhF;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,OAAO,GACpB,YAAY,IAAI,sBAAsB,CAUxC"}
@@ -1,11 +1,17 @@
1
- import { isOnChainNotification } from "./notification-api-type-guards.mjs";
2
1
  /**
3
- * Checks if the given value is an on-chain notification using the v4 `notification_type` discriminator.
2
+ * Checks if the given value is an OnChainRawNotification object.
4
3
  *
5
4
  * @param notification - The value to check.
6
- * @returns True if the value is an on-chain notification, false otherwise.
5
+ * @returns True if the value is an OnChainRawNotification object, false otherwise.
7
6
  */
8
7
  export function isOnChainRawNotification(notification) {
9
- return isOnChainNotification(notification);
8
+ const assumed = notification;
9
+ // We don't have a validation/parsing library to check all possible types of an on chain notification
10
+ // It is safe enough just to check "some" fields, and catch any errors down the line if the shape is bad.
11
+ const isValidEnoughToBeOnChainNotification = [
12
+ assumed?.id,
13
+ assumed?.payload?.data,
14
+ ].every((field) => field !== undefined);
15
+ return isValidEnoughToBeOnChainNotification;
10
16
  }
11
17
  //# sourceMappingURL=is-onchain-notification.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.mjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,qBAAqB,EAAE,2CAAuC;AAEvE;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,YAAqB;IAErB,OAAO,qBAAqB,CAAC,YAA0C,CAAC,CAAC;AAC3E,CAAC","sourcesContent":["import type {\n UnprocessedRawNotification,\n OnChainNotification,\n} from '../NotificationServicesController/types/notification-api';\nimport { isOnChainNotification } from './notification-api-type-guards';\n\n/**\n * Checks if the given value is an on-chain notification using the v4 `notification_type` discriminator.\n *\n * @param notification - The value to check.\n * @returns True if the value is an on-chain notification, false otherwise.\n */\nexport function isOnChainRawNotification(\n notification: unknown,\n): notification is OnChainNotification {\n return isOnChainNotification(notification as UnprocessedRawNotification);\n}\n"]}
1
+ {"version":3,"file":"is-onchain-notification.mjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,YAAqB;IAErB,MAAM,OAAO,GAAG,YAAsC,CAAC;IAEvD,qGAAqG;IACrG,yGAAyG;IACzG,MAAM,oCAAoC,GAAG;QAC3C,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,OAAO,EAAE,IAAI;KACvB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IACxC,OAAO,oCAAoC,CAAC;AAC9C,CAAC","sourcesContent":["import type { OnChainRawNotification } from '../NotificationServicesController';\n\n/**\n * Checks if the given value is an OnChainRawNotification object.\n *\n * @param notification - The value to check.\n * @returns True if the value is an OnChainRawNotification object, false otherwise.\n */\nexport function isOnChainRawNotification(\n notification: unknown,\n): notification is OnChainRawNotification {\n const assumed = notification as OnChainRawNotification;\n\n // We don't have a validation/parsing library to check all possible types of an on chain notification\n // It is safe enough just to check \"some\" fields, and catch any errors down the line if the shape is bad.\n const isValidEnoughToBeOnChainNotification = [\n assumed?.id,\n assumed?.payload?.data,\n ].every((field) => field !== undefined);\n return isValidEnoughToBeOnChainNotification;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/notification-services-controller",
3
- "version": "25.0.0-preview-1590150f2",
3
+ "version": "25.0.0-preview-e196c24cd",
4
4
  "description": "Manages New MetaMask decentralized Notification system",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNotificationSubtype = void 0;
4
- const is_onchain_notification_1 = require("../../shared/is-onchain-notification.cjs");
5
- const notification_schema_1 = require("../constants/notification-schema.cjs");
6
- /**
7
- * Derives the normalised `notification_subtype` for a processed in-app
8
- * notification. This is the team-owned axis (e.g. `eth_received`) and is
9
- * always derivable from an `INotification`, so every consumer (both clients)
10
- * pulls it from one place rather than recomputing a fallback chain.
11
- *
12
- * - on-chain: the trigger kind (`payload.data.kind`, e.g. `eth_received`).
13
- * - platform: the server-set `notification_subtype` from the inbox API.
14
- * - everything else (snap, feature-announcement): the top-level `type`
15
- * (`snap` / `features_announcement`).
16
- *
17
- * @param notification - a raw or processed notification.
18
- * @returns the normalised subtype string.
19
- */
20
- function getNotificationSubtype(notification) {
21
- // On-chain: the trigger kind (e.g. `eth_received`).
22
- if ((0, is_onchain_notification_1.isOnChainRawNotification)(notification)) {
23
- return notification.payload.data.kind;
24
- }
25
- // Platform: the server-set `notification_subtype` from the inbox API.
26
- if (notification.type === notification_schema_1.TRIGGER_TYPES.PLATFORM) {
27
- return notification.notification_subtype;
28
- }
29
- // Fallback (snap, feature-announcement): the top-level `type`.
30
- return notification.type;
31
- }
32
- exports.getNotificationSubtype = getNotificationSubtype;
33
- //# sourceMappingURL=get-notification-subtype.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-notification-subtype.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/get-notification-subtype.ts"],"names":[],"mappings":";;;AAAA,sFAAgF;AAChF,8EAAiE;AAGjE;;;;;;;;;;;;;GAaG;AACH,SAAgB,sBAAsB,CACpC,YAAkC;IAElC,oDAAoD;IACpD,IAAI,IAAA,kDAAwB,EAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,OAAO,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;IACxC,CAAC;IAED,sEAAsE;IACtE,IAAI,YAAY,CAAC,IAAI,KAAK,mCAAa,CAAC,QAAQ,EAAE,CAAC;QACjD,OAAO,YAAY,CAAC,oBAAoB,CAAC;IAC3C,CAAC;IAED,+DAA+D;IAC/D,OAAO,YAAY,CAAC,IAAI,CAAC;AAC3B,CAAC;AAfD,wDAeC","sourcesContent":["import { isOnChainRawNotification } from '../../shared/is-onchain-notification';\nimport { TRIGGER_TYPES } from '../constants/notification-schema';\nimport type { RawNotificationUnion } from '../types/notification/notification';\n\n/**\n * Derives the normalised `notification_subtype` for a processed in-app\n * notification. This is the team-owned axis (e.g. `eth_received`) and is\n * always derivable from an `INotification`, so every consumer (both clients)\n * pulls it from one place rather than recomputing a fallback chain.\n *\n * - on-chain: the trigger kind (`payload.data.kind`, e.g. `eth_received`).\n * - platform: the server-set `notification_subtype` from the inbox API.\n * - everything else (snap, feature-announcement): the top-level `type`\n * (`snap` / `features_announcement`).\n *\n * @param notification - a raw or processed notification.\n * @returns the normalised subtype string.\n */\nexport function getNotificationSubtype(\n notification: RawNotificationUnion,\n): string {\n // On-chain: the trigger kind (e.g. `eth_received`).\n if (isOnChainRawNotification(notification)) {\n return notification.payload.data.kind;\n }\n\n // Platform: the server-set `notification_subtype` from the inbox API.\n if (notification.type === TRIGGER_TYPES.PLATFORM) {\n return notification.notification_subtype;\n }\n\n // Fallback (snap, feature-announcement): the top-level `type`.\n return notification.type;\n}\n"]}
@@ -1,17 +0,0 @@
1
- import type { RawNotificationUnion } from "../types/notification/notification.cjs";
2
- /**
3
- * Derives the normalised `notification_subtype` for a processed in-app
4
- * notification. This is the team-owned axis (e.g. `eth_received`) and is
5
- * always derivable from an `INotification`, so every consumer (both clients)
6
- * pulls it from one place rather than recomputing a fallback chain.
7
- *
8
- * - on-chain: the trigger kind (`payload.data.kind`, e.g. `eth_received`).
9
- * - platform: the server-set `notification_subtype` from the inbox API.
10
- * - everything else (snap, feature-announcement): the top-level `type`
11
- * (`snap` / `features_announcement`).
12
- *
13
- * @param notification - a raw or processed notification.
14
- * @returns the normalised subtype string.
15
- */
16
- export declare function getNotificationSubtype(notification: RawNotificationUnion): string;
17
- //# sourceMappingURL=get-notification-subtype.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-notification-subtype.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/get-notification-subtype.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,+CAA2C;AAE/E;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,CACpC,YAAY,EAAE,oBAAoB,GACjC,MAAM,CAaR"}
@@ -1,17 +0,0 @@
1
- import type { RawNotificationUnion } from "../types/notification/notification.mjs";
2
- /**
3
- * Derives the normalised `notification_subtype` for a processed in-app
4
- * notification. This is the team-owned axis (e.g. `eth_received`) and is
5
- * always derivable from an `INotification`, so every consumer (both clients)
6
- * pulls it from one place rather than recomputing a fallback chain.
7
- *
8
- * - on-chain: the trigger kind (`payload.data.kind`, e.g. `eth_received`).
9
- * - platform: the server-set `notification_subtype` from the inbox API.
10
- * - everything else (snap, feature-announcement): the top-level `type`
11
- * (`snap` / `features_announcement`).
12
- *
13
- * @param notification - a raw or processed notification.
14
- * @returns the normalised subtype string.
15
- */
16
- export declare function getNotificationSubtype(notification: RawNotificationUnion): string;
17
- //# sourceMappingURL=get-notification-subtype.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-notification-subtype.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/get-notification-subtype.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,+CAA2C;AAE/E;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,CACpC,YAAY,EAAE,oBAAoB,GACjC,MAAM,CAaR"}
@@ -1,29 +0,0 @@
1
- import { isOnChainRawNotification } from "../../shared/is-onchain-notification.mjs";
2
- import { TRIGGER_TYPES } from "../constants/notification-schema.mjs";
3
- /**
4
- * Derives the normalised `notification_subtype` for a processed in-app
5
- * notification. This is the team-owned axis (e.g. `eth_received`) and is
6
- * always derivable from an `INotification`, so every consumer (both clients)
7
- * pulls it from one place rather than recomputing a fallback chain.
8
- *
9
- * - on-chain: the trigger kind (`payload.data.kind`, e.g. `eth_received`).
10
- * - platform: the server-set `notification_subtype` from the inbox API.
11
- * - everything else (snap, feature-announcement): the top-level `type`
12
- * (`snap` / `features_announcement`).
13
- *
14
- * @param notification - a raw or processed notification.
15
- * @returns the normalised subtype string.
16
- */
17
- export function getNotificationSubtype(notification) {
18
- // On-chain: the trigger kind (e.g. `eth_received`).
19
- if (isOnChainRawNotification(notification)) {
20
- return notification.payload.data.kind;
21
- }
22
- // Platform: the server-set `notification_subtype` from the inbox API.
23
- if (notification.type === TRIGGER_TYPES.PLATFORM) {
24
- return notification.notification_subtype;
25
- }
26
- // Fallback (snap, feature-announcement): the top-level `type`.
27
- return notification.type;
28
- }
29
- //# sourceMappingURL=get-notification-subtype.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-notification-subtype.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/get-notification-subtype.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,iDAA6C;AAChF,OAAO,EAAE,aAAa,EAAE,6CAAyC;AAGjE;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,sBAAsB,CACpC,YAAkC;IAElC,oDAAoD;IACpD,IAAI,wBAAwB,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,OAAO,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;IACxC,CAAC;IAED,sEAAsE;IACtE,IAAI,YAAY,CAAC,IAAI,KAAK,aAAa,CAAC,QAAQ,EAAE,CAAC;QACjD,OAAO,YAAY,CAAC,oBAAoB,CAAC;IAC3C,CAAC;IAED,+DAA+D;IAC/D,OAAO,YAAY,CAAC,IAAI,CAAC;AAC3B,CAAC","sourcesContent":["import { isOnChainRawNotification } from '../../shared/is-onchain-notification';\nimport { TRIGGER_TYPES } from '../constants/notification-schema';\nimport type { RawNotificationUnion } from '../types/notification/notification';\n\n/**\n * Derives the normalised `notification_subtype` for a processed in-app\n * notification. This is the team-owned axis (e.g. `eth_received`) and is\n * always derivable from an `INotification`, so every consumer (both clients)\n * pulls it from one place rather than recomputing a fallback chain.\n *\n * - on-chain: the trigger kind (`payload.data.kind`, e.g. `eth_received`).\n * - platform: the server-set `notification_subtype` from the inbox API.\n * - everything else (snap, feature-announcement): the top-level `type`\n * (`snap` / `features_announcement`).\n *\n * @param notification - a raw or processed notification.\n * @returns the normalised subtype string.\n */\nexport function getNotificationSubtype(\n notification: RawNotificationUnion,\n): string {\n // On-chain: the trigger kind (e.g. `eth_received`).\n if (isOnChainRawNotification(notification)) {\n return notification.payload.data.kind;\n }\n\n // Platform: the server-set `notification_subtype` from the inbox API.\n if (notification.type === TRIGGER_TYPES.PLATFORM) {\n return notification.notification_subtype;\n }\n\n // Fallback (snap, feature-announcement): the top-level `type`.\n return notification.type;\n}\n"]}
@@ -1,5 +0,0 @@
1
- "use strict";
2
- // snake_case mirrors the FCM payload and Segment schema keys
3
- /* eslint-disable @typescript-eslint/naming-convention */
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- //# sourceMappingURL=push-analytics.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"push-analytics.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/push-analytics.ts"],"names":[],"mappings":";AAAA,6DAA6D;AAC7D,yDAAyD","sourcesContent":["// snake_case mirrors the FCM payload and Segment schema keys\n/* eslint-disable @typescript-eslint/naming-convention */\n\n/**\n * Analytics fields carried by the `NotificationServicesPushController` messenger\n * events (`onNewNotifications`, `pushNotificationClicked`). Read directly from\n * top-level FCM payload keys, so clients build Segment events without fallback\n * chains or parsing a `metadata` blob.\n */\nexport type PushAnalyticsPayload = {\n notification_id: string;\n /** Free-form snake_case label set by the producer. */\n notification_type: string;\n /** Team-owned, open-ended (e.g. `eth_received`). */\n notification_subtype: string;\n /** Only present when the notification has a chain context. */\n chain_id?: number;\n /** Platform notifications only; the CTA link to route to on tap. */\n deeplink?: string;\n};\n"]}
@@ -1,18 +0,0 @@
1
- /**
2
- * Analytics fields carried by the `NotificationServicesPushController` messenger
3
- * events (`onNewNotifications`, `pushNotificationClicked`). Read directly from
4
- * top-level FCM payload keys, so clients build Segment events without fallback
5
- * chains or parsing a `metadata` blob.
6
- */
7
- export type PushAnalyticsPayload = {
8
- notification_id: string;
9
- /** Free-form snake_case label set by the producer. */
10
- notification_type: string;
11
- /** Team-owned, open-ended (e.g. `eth_received`). */
12
- notification_subtype: string;
13
- /** Only present when the notification has a chain context. */
14
- chain_id?: number;
15
- /** Platform notifications only; the CTA link to route to on tap. */
16
- deeplink?: string;
17
- };
18
- //# sourceMappingURL=push-analytics.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"push-analytics.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/push-analytics.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,eAAe,EAAE,MAAM,CAAC;IACxB,sDAAsD;IACtD,iBAAiB,EAAE,MAAM,CAAC;IAC1B,oDAAoD;IACpD,oBAAoB,EAAE,MAAM,CAAC;IAC7B,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC"}
@@ -1,18 +0,0 @@
1
- /**
2
- * Analytics fields carried by the `NotificationServicesPushController` messenger
3
- * events (`onNewNotifications`, `pushNotificationClicked`). Read directly from
4
- * top-level FCM payload keys, so clients build Segment events without fallback
5
- * chains or parsing a `metadata` blob.
6
- */
7
- export type PushAnalyticsPayload = {
8
- notification_id: string;
9
- /** Free-form snake_case label set by the producer. */
10
- notification_type: string;
11
- /** Team-owned, open-ended (e.g. `eth_received`). */
12
- notification_subtype: string;
13
- /** Only present when the notification has a chain context. */
14
- chain_id?: number;
15
- /** Platform notifications only; the CTA link to route to on tap. */
16
- deeplink?: string;
17
- };
18
- //# sourceMappingURL=push-analytics.d.mts.map