@metamask-previews/notification-services-controller 0.17.0-preview-111ffee8 → 0.18.0-preview-c96fc296

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 (88) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs +54 -41
  3. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  4. package/dist/NotificationServicesController/NotificationServicesController.d.cts +4 -4
  5. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  6. package/dist/NotificationServicesController/NotificationServicesController.d.mts +4 -4
  7. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  8. package/dist/NotificationServicesController/NotificationServicesController.mjs +54 -41
  9. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  10. package/dist/NotificationServicesController/index.cjs +1 -0
  11. package/dist/NotificationServicesController/index.cjs.map +1 -1
  12. package/dist/NotificationServicesController/index.d.cts +1 -0
  13. package/dist/NotificationServicesController/index.d.cts.map +1 -1
  14. package/dist/NotificationServicesController/index.d.mts +1 -0
  15. package/dist/NotificationServicesController/index.d.mts.map +1 -1
  16. package/dist/NotificationServicesController/index.mjs +1 -0
  17. package/dist/NotificationServicesController/index.mjs.map +1 -1
  18. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +37 -40
  19. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  20. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +19 -21
  21. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  22. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +19 -21
  23. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  24. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +38 -41
  25. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  26. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs +14 -0
  27. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs.map +1 -0
  28. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts +3 -0
  29. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts.map +1 -0
  30. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts +3 -0
  31. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts.map +1 -0
  32. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs +10 -0
  33. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs.map +1 -0
  34. package/dist/NotificationServicesPushController/services/services.cjs +3 -28
  35. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  36. package/dist/NotificationServicesPushController/services/services.d.cts +1 -15
  37. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  38. package/dist/NotificationServicesPushController/services/services.d.mts +1 -15
  39. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  40. package/dist/NotificationServicesPushController/services/services.mjs +2 -26
  41. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  42. package/dist/NotificationServicesPushController/types/index.cjs +0 -15
  43. package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
  44. package/dist/NotificationServicesPushController/types/index.d.cts +2 -1
  45. package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
  46. package/dist/NotificationServicesPushController/types/index.d.mts +2 -1
  47. package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
  48. package/dist/NotificationServicesPushController/types/index.mjs +1 -1
  49. package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
  50. package/dist/NotificationServicesPushController/{services/push/index.cjs → types/push-service-interface.cjs} +1 -1
  51. package/dist/NotificationServicesPushController/types/push-service-interface.cjs.map +1 -0
  52. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts +30 -0
  53. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts.map +1 -0
  54. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts +30 -0
  55. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts.map +1 -0
  56. package/dist/NotificationServicesPushController/types/push-service-interface.mjs +2 -0
  57. package/dist/NotificationServicesPushController/types/push-service-interface.mjs.map +1 -0
  58. package/dist/NotificationServicesPushController/web/index.cjs +8 -0
  59. package/dist/NotificationServicesPushController/web/index.cjs.map +1 -0
  60. package/dist/NotificationServicesPushController/web/index.d.cts +2 -0
  61. package/dist/NotificationServicesPushController/web/index.d.cts.map +1 -0
  62. package/dist/NotificationServicesPushController/web/index.d.mts +2 -0
  63. package/dist/NotificationServicesPushController/web/index.d.mts.map +1 -0
  64. package/dist/NotificationServicesPushController/web/index.mjs +2 -0
  65. package/dist/NotificationServicesPushController/web/index.mjs.map +1 -0
  66. package/dist/NotificationServicesPushController/{services/push/push-web.cjs → web/push-helpers.cjs} +37 -8
  67. package/dist/NotificationServicesPushController/web/push-helpers.cjs.map +1 -0
  68. package/dist/NotificationServicesPushController/web/push-helpers.d.cts +37 -0
  69. package/dist/NotificationServicesPushController/web/push-helpers.d.cts.map +1 -0
  70. package/dist/NotificationServicesPushController/web/push-helpers.d.mts +37 -0
  71. package/dist/NotificationServicesPushController/web/push-helpers.d.mts.map +1 -0
  72. package/dist/NotificationServicesPushController/{services/push/push-web.mjs → web/push-helpers.mjs} +37 -7
  73. package/dist/NotificationServicesPushController/web/push-helpers.mjs.map +1 -0
  74. package/package.json +15 -5
  75. package/push-services/web/package.json +9 -0
  76. package/dist/NotificationServicesPushController/services/push/index.cjs.map +0 -1
  77. package/dist/NotificationServicesPushController/services/push/index.d.cts +0 -6
  78. package/dist/NotificationServicesPushController/services/push/index.d.cts.map +0 -1
  79. package/dist/NotificationServicesPushController/services/push/index.d.mts +0 -6
  80. package/dist/NotificationServicesPushController/services/push/index.d.mts.map +0 -1
  81. package/dist/NotificationServicesPushController/services/push/index.mjs +0 -2
  82. package/dist/NotificationServicesPushController/services/push/index.mjs.map +0 -1
  83. package/dist/NotificationServicesPushController/services/push/push-web.cjs.map +0 -1
  84. package/dist/NotificationServicesPushController/services/push/push-web.d.cts +0 -33
  85. package/dist/NotificationServicesPushController/services/push/push-web.d.cts.map +0 -1
  86. package/dist/NotificationServicesPushController/services/push/push-web.d.mts +0 -33
  87. package/dist/NotificationServicesPushController/services/push/push-web.d.mts.map +0 -1
  88. package/dist/NotificationServicesPushController/services/push/push-web.mjs.map +0 -1
@@ -26,10 +26,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.listenToPushNotifications = exports.updateTriggerPushNotifications = exports.deactivatePushNotifications = exports.activatePushNotifications = exports.updateLinksAPI = exports.getPushNotificationLinks = void 0;
29
+ exports.updateTriggerPushNotifications = exports.deactivatePushNotifications = exports.activatePushNotifications = exports.updateLinksAPI = exports.getPushNotificationLinks = void 0;
30
30
  const loglevel_1 = __importDefault(require("loglevel"));
31
31
  const endpoints = __importStar(require("./endpoints.cjs"));
32
- const push_web_1 = require("./push/push-web.cjs");
33
32
  /**
34
33
  * Fetches push notification links from a remote endpoint using a BearerToken for authorization.
35
34
  *
@@ -64,9 +63,7 @@ exports.getPushNotificationLinks = getPushNotificationLinks;
64
63
  async function updateLinksAPI(bearerToken, triggers, regTokens) {
65
64
  try {
66
65
  const body = {
67
- // eslint-disable-next-line @typescript-eslint/naming-convention
68
66
  trigger_ids: triggers,
69
- // eslint-disable-next-line @typescript-eslint/naming-convention
70
67
  registration_tokens: regTokens,
71
68
  };
72
69
  const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {
@@ -91,12 +88,12 @@ exports.updateLinksAPI = updateLinksAPI;
91
88
  * @returns A promise that resolves with an object containing the success status and the BearerToken token.
92
89
  */
93
90
  async function activatePushNotifications(params) {
94
- const { bearerToken, triggers, env, createRegToken, platform, fcmToken } = params;
91
+ const { bearerToken, triggers, env, createRegToken, platform } = params;
95
92
  const notificationLinks = await getPushNotificationLinks(bearerToken);
96
93
  if (!notificationLinks) {
97
94
  return null;
98
95
  }
99
- const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));
96
+ const regToken = await createRegToken(env).catch(() => null);
100
97
  if (!regToken) {
101
98
  return null;
102
99
  }
@@ -172,26 +169,4 @@ async function updateTriggerPushNotifications(params) {
172
169
  };
173
170
  }
174
171
  exports.updateTriggerPushNotifications = updateTriggerPushNotifications;
175
- /**
176
- * Listens to push notifications and invokes the provided callback function with the received notification data.
177
- *
178
- * @param params - listen params
179
- * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
180
- */
181
- async function listenToPushNotifications(params) {
182
- const { env, listenToPushReceived, listenToPushClicked } = params;
183
- /*
184
- Push notifications require 2 listeners that need tracking (when creating and for tearing down):
185
- 1. handling receiving a push notification (and the content we want to display)
186
- 2. handling when a user clicks on a push notification
187
- */
188
- const unsubscribePushNotifications = await (0, push_web_1.listenToPushNotificationsReceived)(env, listenToPushReceived);
189
- const unsubscribeNotificationClicks = (0, push_web_1.listenToPushNotificationsClicked)(listenToPushClicked);
190
- const unsubscribe = () => {
191
- unsubscribePushNotifications?.();
192
- unsubscribeNotificationClicks();
193
- };
194
- return unsubscribe;
195
- }
196
- exports.listenToPushNotifications = listenToPushNotifications;
197
172
  //# sourceMappingURL=services.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wDAA2B;AAI3B,2DAAyC;AAEzC,kDAGyB;AAiBzB;;;;;GAKG;AACI,KAAK,UAAU,wBAAwB,CAC5C,WAAmB;IAEnB,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,WAAW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B,CAC7C,MAAuC;IAEvC,MAAM,EAAE,GAAG,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,GAAG,MAAM,CAAC;IAElE;;;;MAIE;IACF,MAAM,4BAA4B,GAAG,MAAM,IAAA,4CAAiC,EAC1E,GAAG,EACH,oBAAoB,CACrB,CAAC;IACF,MAAM,6BAA6B,GACjC,IAAA,2CAAgC,EAAC,mBAAmB,CAAC,CAAC;IAExD,MAAM,WAAW,GAAG,GAAG,EAAE;QACvB,4BAA4B,EAAE,EAAE,CAAC;QACjC,6BAA6B,EAAE,CAAC;IAClC,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC;AAvBD,8DAuBC","sourcesContent":["import log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport type { PushNotificationEnv } from '../types';\nimport * as endpoints from './endpoints';\nimport type { CreateRegToken, DeleteRegToken } from './push';\nimport {\n listenToPushNotificationsClicked,\n listenToPushNotificationsReceived,\n} from './push/push-web';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n trigger_ids: string[];\n // eslint-disable-next-line @typescript-eslint/naming-convention\n registration_tokens: RegToken[];\n};\n\n/**\n * Fetches push notification links from a remote endpoint using a BearerToken for authorization.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @returns A promise that resolves with the links result or null if an error occurs.\n */\nexport async function getPushNotificationLinks(\n bearerToken: string,\n): Promise<LinksResult | null> {\n try {\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n headers: { Authorization: `Bearer ${bearerToken}` },\n });\n if (!response.ok) {\n log.error('Failed to fetch the push notification links');\n throw new Error('Failed to fetch the push notification links');\n }\n return response.json() as Promise<LinksResult>;\n } catch (error) {\n log.error('Failed to fetch the push notification links', error);\n return null;\n }\n}\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n trigger_ids: triggers,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n fcmToken?: string;\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform, fcmToken } =\n params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n\n if (!notificationLinks) {\n return null;\n }\n\n const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));\n if (!regToken) {\n return null;\n }\n\n const newRegTokens = new Set(notificationLinks.registration_tokens);\n newRegTokens.add({ token: regToken, platform });\n\n await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token and unlinking triggers.\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, bearerToken, triggers, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return false;\n }\n\n const filteredRegTokens = notificationLinks.registration_tokens.filter(\n (r) => r.token !== regToken,\n );\n\n const isTokenRemovedFromAPI = await updateLinksAPI(\n bearerToken,\n triggers,\n filteredRegTokens,\n );\n if (!isTokenRemovedFromAPI) {\n return false;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n regToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return { isTriggersLinkedToPushNotifications: false };\n }\n // Create new registration token if doesn't exist\n const hasRegToken = Boolean(\n regToken &&\n notificationLinks.registration_tokens.some((r) => r.token === regToken),\n );\n\n let newRegToken: string | null = null;\n if (!hasRegToken) {\n await deleteRegToken(env);\n newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n notificationLinks.registration_tokens.push({\n token: newRegToken,\n platform,\n });\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n notificationLinks.registration_tokens,\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken ?? null,\n };\n}\n\ntype ListenToPushNotificationsParams = {\n env: PushNotificationEnv;\n listenToPushReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n listenToPushClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n};\n\n/**\n * Listens to push notifications and invokes the provided callback function with the received notification data.\n *\n * @param params - listen params\n * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.\n */\nexport async function listenToPushNotifications(\n params: ListenToPushNotificationsParams,\n): Promise<() => void> {\n const { env, listenToPushReceived, listenToPushClicked } = params;\n\n /*\n Push notifications require 2 listeners that need tracking (when creating and for tearing down):\n 1. handling receiving a push notification (and the content we want to display)\n 2. handling when a user clicks on a push notification\n */\n const unsubscribePushNotifications = await listenToPushNotificationsReceived(\n env,\n listenToPushReceived,\n );\n const unsubscribeNotificationClicks =\n listenToPushNotificationsClicked(listenToPushClicked);\n\n const unsubscribe = () => {\n unsubscribePushNotifications?.();\n unsubscribeNotificationClicks();\n };\n\n return unsubscribe;\n}\n"]}
1
+ {"version":3,"file":"services.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wDAA2B;AAE3B,2DAAyC;AAoBzC;;;;;GAKG;AACI,KAAK,UAAU,wBAAwB,CAC5C,WAAmB;IAEnB,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;Y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v } from '../types';\nimport type {\n CreateRegToken,\n DeleteRegToken,\n} from '../types/push-service-interface';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n trigger_ids: string[];\n registration_tokens: RegToken[];\n};\n\n/**\n * Fetches push notification links from a remote endpoint using a BearerToken for authorization.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @returns A promise that resolves with the links result or null if an error occurs.\n */\nexport async function getPushNotificationLinks(\n bearerToken: string,\n): Promise<LinksResult | null> {\n try {\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n headers: { Authorization: `Bearer ${bearerToken}` },\n });\n if (!response.ok) {\n log.error('Failed to fetch the push notification links');\n throw new Error('Failed to fetch the push notification links');\n }\n return response.json() as Promise<LinksResult>;\n } catch (error) {\n log.error('Failed to fetch the push notification links', error);\n return null;\n }\n}\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n trigger_ids: triggers,\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n\n if (!notificationLinks) {\n return null;\n }\n\n const regToken = await createRegToken(env).catch(() => null);\n if (!regToken) {\n return null;\n }\n\n const newRegTokens = new Set(notificationLinks.registration_tokens);\n newRegTokens.add({ token: regToken, platform });\n\n await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token and unlinking triggers.\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, bearerToken, triggers, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return false;\n }\n\n const filteredRegTokens = notificationLinks.registration_tokens.filter(\n (r) => r.token !== regToken,\n );\n\n const isTokenRemovedFromAPI = await updateLinksAPI(\n bearerToken,\n triggers,\n filteredRegTokens,\n );\n if (!isTokenRemovedFromAPI) {\n return false;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n regToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return { isTriggersLinkedToPushNotifications: false };\n }\n // Create new registration token if doesn't exist\n const hasRegToken = Boolean(\n regToken &&\n notificationLinks.registration_tokens.some((r) => r.token === regToken),\n );\n\n let newRegToken: string | null = null;\n if (!hasRegToken) {\n await deleteRegToken(env);\n newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n notificationLinks.registration_tokens.push({\n token: newRegToken,\n platform,\n });\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n notificationLinks.registration_tokens,\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken ?? null,\n };\n}\n"]}
@@ -1,6 +1,5 @@
1
- import type { Types } from "../../NotificationServicesController/index.cjs";
2
1
  import type { PushNotificationEnv } from "../types/index.cjs";
3
- import type { CreateRegToken, DeleteRegToken } from "./push/index.cjs";
2
+ import type { CreateRegToken, DeleteRegToken } from "../types/push-service-interface.cjs";
4
3
  export type RegToken = {
5
4
  token: string;
6
5
  platform: 'extension' | 'mobile' | 'portfolio';
@@ -34,7 +33,6 @@ type ActivatePushNotificationsParams = {
34
33
  env: PushNotificationEnv;
35
34
  createRegToken: CreateRegToken;
36
35
  platform: 'extension' | 'mobile' | 'portfolio';
37
- fcmToken?: string;
38
36
  };
39
37
  /**
40
38
  * Enables push notifications by registering the device and linking triggers.
@@ -80,17 +78,5 @@ export declare function updateTriggerPushNotifications(params: UpdateTriggerPush
80
78
  isTriggersLinkedToPushNotifications: boolean;
81
79
  fcmToken?: string | null;
82
80
  }>;
83
- type ListenToPushNotificationsParams = {
84
- env: PushNotificationEnv;
85
- listenToPushReceived: (notification: Types.INotification) => void | Promise<void>;
86
- listenToPushClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
87
- };
88
- /**
89
- * Listens to push notifications and invokes the provided callback function with the received notification data.
90
- *
91
- * @param params - listen params
92
- * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
93
- */
94
- export declare function listenToPushNotifications(params: ListenToPushNotificationsParams): Promise<() => void>;
95
81
  export {};
96
82
  //# sourceMappingURL=services.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AAEpD,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,yBAAe;AAM7D,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IAExB,WAAW,EAAE,MAAM,EAAE,CAAC;IAEtB,mBAAmB,EAAE,QAAQ,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAc7B;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,EAAE,EAClB,SAAS,EAAE,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A+B,GAAG;IACrC,GAAG,EAAE,mBAAmB,CAAC;IACzB,oBAAoB,EAAE,CACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,mBAAmB,EAAE,CACnB,KAAK,EAAE,iBAAiB,EACxB,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,KAC/B,IAAI,CAAC;CACX,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,+BAA+B,GACtC,OAAO,CAAC,MAAM,IAAI,CAAC,CAqBrB"}
1
+ {"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AACpD,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACf,4CAAwC;AAEzC,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,mBAAmB,EAAE,QAAQ,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAc7B;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,EAAE,EAClB,SAAS,EAAE,QAAQ,EAAE,GACpB,OAAO,CAAC,OAAO,CAAC,CAkBlB;AAED,KAAK,+BAA+B,GAAG;IAErC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;IAC/B,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,+BAA+B,GACtC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAmBxB;AAED,KAAK,iCAAiC,GAAG;IAEvC,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,GACxC,OAAO,CAAC,OAAO,CAAC,CAgClB;AAED,KAAK,oCAAoC,GAAG;IAE1C,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;IAC/B,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;IAG/C,cAAc,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,8BAA8B,CAClD,MAAM,EAAE,oCAAoC,GAC3C,OAAO,CAAC;IACT,mCAAmC,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC,CA4CD"}
@@ -1,6 +1,5 @@
1
- import type { Types } from "../../NotificationServicesController/index.mjs";
2
1
  import type { PushNotificationEnv } from "../types/index.mjs";
3
- import type { CreateRegToken, DeleteRegToken } from "./push/index.mjs";
2
+ import type { CreateRegToken, DeleteRegToken } from "../types/push-service-interface.mjs";
4
3
  export type RegToken = {
5
4
  token: string;
6
5
  platform: 'extension' | 'mobile' | 'portfolio';
@@ -34,7 +33,6 @@ type ActivatePushNotificationsParams = {
34
33
  env: PushNotificationEnv;
35
34
  createRegToken: CreateRegToken;
36
35
  platform: 'extension' | 'mobile' | 'portfolio';
37
- fcmToken?: string;
38
36
  };
39
37
  /**
40
38
  * Enables push notifications by registering the device and linking triggers.
@@ -80,17 +78,5 @@ export declare function updateTriggerPushNotifications(params: UpdateTriggerPush
80
78
  isTriggersLinkedToPushNotifications: boolean;
81
79
  fcmToken?: string | null;
82
80
  }>;
83
- type ListenToPushNotificationsParams = {
84
- env: PushNotificationEnv;
85
- listenToPushReceived: (notification: Types.INotification) => void | Promise<void>;
86
- listenToPushClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
87
- };
88
- /**
89
- * Listens to push notifications and invokes the provided callback function with the received notification data.
90
- *
91
- * @param params - listen params
92
- * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
93
- */
94
- export declare function listenToPushNotifications(params: ListenToPushNotificationsParams): Promise<() => void>;
95
81
  export {};
96
82
  //# sourceMappingURL=services.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AAEpD,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,yBAAe;AAM7D,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IAExB,WAAW,EAAE,MAAM,EAAE,CAAC;IAEtB,mBAAmB,EAAE,QAAQ,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,GAClB,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AM,EAAE,oCAAoC,GAC3C,OAAO,CAAC;IACT,mCAAmC,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC,CA4CD;AAED,KAAK,+BAA+B,GAAG;IACrC,GAAG,EAAE,mBAAmB,CAAC;IACzB,oBAAoB,EAAE,CACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,mBAAmB,EAAE,CACnB,KAAK,EAAE,iBAAiB,EACxB,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,KAC/B,IAAI,CAAC;CACX,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,+BAA+B,GACtC,OAAO,CAAC,MAAM,IAAI,CAAC,CAqBrB"}
1
+ {"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AACpD,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACf,4CAAwC;AAEzC,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,mBAAmB,EAAE,QAAQ,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAc7B;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,EAAE,EAClB,SAAS,EAAE,QAAQ,EAAE,GACpB,OAAO,CAA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* as endpoints from "./endpoints.mjs";
10
- import { listenToPushNotificationsClicked, listenToPushNotificationsReceived } from "./push/push-web.mjs";
11
10
  /**
12
11
  * Fetches push notification links from a remote endpoint using a BearerToken for authorization.
13
12
  *
@@ -41,9 +40,7 @@ export async function getPushNotificationLinks(bearerToken) {
41
40
  export async function updateLinksAPI(bearerToken, triggers, regTokens) {
42
41
  try {
43
42
  const body = {
44
- // eslint-disable-next-line @typescript-eslint/naming-convention
45
43
  trigger_ids: triggers,
46
- // eslint-disable-next-line @typescript-eslint/naming-convention
47
44
  registration_tokens: regTokens,
48
45
  };
49
46
  const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {
@@ -67,12 +64,12 @@ export async function updateLinksAPI(bearerToken, triggers, regTokens) {
67
64
  * @returns A promise that resolves with an object containing the success status and the BearerToken token.
68
65
  */
69
66
  export async function activatePushNotifications(params) {
70
- const { bearerToken, triggers, env, createRegToken, platform, fcmToken } = params;
67
+ const { bearerToken, triggers, env, createRegToken, platform } = params;
71
68
  const notificationLinks = await getPushNotificationLinks(bearerToken);
72
69
  if (!notificationLinks) {
73
70
  return null;
74
71
  }
75
- const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));
72
+ const regToken = await createRegToken(env).catch(() => null);
76
73
  if (!regToken) {
77
74
  return null;
78
75
  }
@@ -145,25 +142,4 @@ export async function updateTriggerPushNotifications(params) {
145
142
  fcmToken: newRegToken ?? null,
146
143
  };
147
144
  }
148
- /**
149
- * Listens to push notifications and invokes the provided callback function with the received notification data.
150
- *
151
- * @param params - listen params
152
- * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
153
- */
154
- export async function listenToPushNotifications(params) {
155
- const { env, listenToPushReceived, listenToPushClicked } = params;
156
- /*
157
- Push notifications require 2 listeners that need tracking (when creating and for tearing down):
158
- 1. handling receiving a push notification (and the content we want to display)
159
- 2. handling when a user clicks on a push notification
160
- */
161
- const unsubscribePushNotifications = await listenToPushNotificationsReceived(env, listenToPushReceived);
162
- const unsubscribeNotificationClicks = listenToPushNotificationsClicked(listenToPushClicked);
163
- const unsubscribe = () => {
164
- unsubscribePushNotifications?.();
165
- unsubscribeNotificationClicks();
166
- };
167
- return unsubscribe;
168
- }
169
145
  //# sourceMappingURL=services.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAI3B,OAAO,KAAK,SAAS,wBAAoB;AAEzC,OAAO,EACL,gCAAgC,EAChC,iCAAiC,EAClC,4BAAwB;AAiBzB;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,WAAmB;IAEnB,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE;SACpD,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,GAAG,CAAC,KAAK,CAAC,6CAA6C,CAAC,CAAC;YACzD,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;SAChE;QACD,OAAO,QAAQ,CAAC,IAAI,EAA0B,CAAC;KAChD;IAAC,OAAO,KAAK,EAAE;QACd,GAAG,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;QAChE,OAAO,IAAI,CAAC;KACb;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,WAAmB,EACnB,QAAkB,EAClB,SAAqB;IAErB,IAAI;QACF,MAAM,IAAI,GAAgB;YACxB,gEAAgE;YAChE,WAAW,EAAE,QAAQ;YACrB,gEAAgE;YAChE,mBAAmB,EAAE,SAAS;SAC/B,CAAC;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACn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ACtC,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC;QAC1B,WAAW,GAAG,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,CAAC,WAAW,EAAE;YAChB,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;SAC9D;QACD,iBAAiB,CAAC,mBAAmB,CAAC,IAAI,CAAC;YACzC,KAAK,EAAE,WAAW;YAClB,QAAQ;SACT,CAAC,CAAC;KACJ;IAED,MAAM,mCAAmC,GAAG,MAAM,cAAc,CAC9D,WAAW,EACX,QAAQ,EACR,iBAAiB,CAAC,mBAAmB,CACtC,CAAC;IAEF,OAAO;QACL,mCAAmC;QACnC,QAAQ,EAAE,WAAW,IAAI,IAAI;KAC9B,CAAC;AACJ,CAAC;AAaD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAC7C,MAAuC;IAEvC,MAAM,EAAE,GAAG,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,GAAG,MAAM,CAAC;IAElE;;;;MAIE;IACF,MAAM,4BAA4B,GAAG,MAAM,iCAAiC,CAC1E,GAAG,EACH,oBAAoB,CACrB,CAAC;IACF,MAAM,6BAA6B,GACjC,gCAAgC,CAAC,mBAAmB,CAAC,CAAC;IAExD,MAAM,WAAW,GAAG,GAAG,EAAE;QACvB,4BAA4B,EAAE,EAAE,CAAC;QACjC,6BAA6B,EAAE,CAAC;IAClC,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport type { PushNotificationEnv } from '../types';\nimport * as endpoints from './endpoints';\nimport type { CreateRegToken, DeleteRegToken } from './push';\nimport {\n listenToPushNotificationsClicked,\n listenToPushNotificationsReceived,\n} from './push/push-web';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n trigger_ids: string[];\n // eslint-disable-next-line @typescript-eslint/naming-convention\n registration_tokens: RegToken[];\n};\n\n/**\n * Fetches push notification links from a remote endpoint using a BearerToken for authorization.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @returns A promise that resolves with the links result or null if an error occurs.\n */\nexport async function getPushNotificationLinks(\n bearerToken: string,\n): Promise<LinksResult | null> {\n try {\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n headers: { Authorization: `Bearer ${bearerToken}` },\n });\n if (!response.ok) {\n log.error('Failed to fetch the push notification links');\n throw new Error('Failed to fetch the push notification links');\n }\n return response.json() as Promise<LinksResult>;\n } catch (error) {\n log.error('Failed to fetch the push notification links', error);\n return null;\n }\n}\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n trigger_ids: triggers,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n fcmToken?: string;\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform, fcmToken } =\n params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n\n if (!notificationLinks) {\n return null;\n }\n\n const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));\n if (!regToken) {\n return null;\n }\n\n const newRegTokens = new Set(notificationLinks.registration_tokens);\n newRegTokens.add({ token: regToken, platform });\n\n await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token and unlinking triggers.\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, bearerToken, triggers, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return false;\n }\n\n const filteredRegTokens = notificationLinks.registration_tokens.filter(\n (r) => r.token !== regToken,\n );\n\n const isTokenRemovedFromAPI = await updateLinksAPI(\n bearerToken,\n triggers,\n filteredRegTokens,\n );\n if (!isTokenRemovedFromAPI) {\n return false;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n regToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return { isTriggersLinkedToPushNotifications: false };\n }\n // Create new registration token if doesn't exist\n const hasRegToken = Boolean(\n regToken &&\n notificationLinks.registration_tokens.some((r) => r.token === regToken),\n );\n\n let newRegToken: string | null = null;\n if (!hasRegToken) {\n await deleteRegToken(env);\n newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n notificationLinks.registration_tokens.push({\n token: newRegToken,\n platform,\n });\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n notificationLinks.registration_tokens,\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken ?? null,\n };\n}\n\ntype ListenToPushNotificationsParams = {\n env: PushNotificationEnv;\n listenToPushReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n listenToPushClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n};\n\n/**\n * Listens to push notifications and invokes the provided callback function with the received notification data.\n *\n * @param params - listen params\n * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.\n */\nexport async function listenToPushNotifications(\n params: ListenToPushNotificationsParams,\n): Promise<() => void> {\n const { env, listenToPushReceived, listenToPushClicked } = params;\n\n /*\n Push notifications require 2 listeners that need tracking (when creating and for tearing down):\n 1. handling receiving a push notification (and the content we want to display)\n 2. handling when a user clicks on a push notification\n */\n const unsubscribePushNotifications = await listenToPushNotificationsReceived(\n env,\n listenToPushReceived,\n );\n const unsubscribeNotificationClicks =\n listenToPushNotificationsClicked(listenToPushClicked);\n\n const unsubscribe = () => {\n unsubscribePushNotifications?.();\n unsubscribeNotificationClicks();\n };\n\n return unsubscribe;\n}\n"]}
1
+ {"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,KAAK,SAAS,wBAAoB;AAoBzC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,WAAmB;IAEnB,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE;SACpD,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,GAAG,CAAC,KAAK,CAAC,6CAA6C,CAAC,CAAC;YACzD,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;SAChE;QACD,OAAO,QAAQ,CAAC,IAAI,EAA0B,CAAC;KAChD;IAAC,OAAO,KAAK,EAAE;QACd,GAAG,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;QAChE,OAAO,IAAI,CAAC;KACb;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,WAAmB,EACnB,QAAkB,EAClB,SAAqB;IAErB,IAAI;QACF,MAAM,IAAI,GAAgB;YACxB,WAAW,EAAE,QAAQ;YACrB,mBAAmB,EAAE,SAAS;SAC/B,CAAC;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,MAAM,EAAE,M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GAAG,CAAC,CAAC;QAC1B,WAAW,GAAG,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,CAAC,WAAW,EAAE;YAChB,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;SAC9D;QACD,iBAAiB,CAAC,mBAAmB,CAAC,IAAI,CAAC;YACzC,KAAK,EAAE,WAAW;YAClB,QAAQ;SACT,CAAC,CAAC;KACJ;IAED,MAAM,mCAAmC,GAAG,MAAM,cAAc,CAC9D,WAAW,EACX,QAAQ,EACR,iBAAiB,CAAC,mBAAmB,CACtC,CAAC;IAEF,OAAO;QACL,mCAAmC;QACnC,QAAQ,EAAE,WAAW,IAAI,IAAI;KAC9B,CAAC;AACJ,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport * as endpoints from './endpoints';\nimport type { PushNotificationEnv } from '../types';\nimport type {\n CreateRegToken,\n DeleteRegToken,\n} from '../types/push-service-interface';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n trigger_ids: string[];\n registration_tokens: RegToken[];\n};\n\n/**\n * Fetches push notification links from a remote endpoint using a BearerToken for authorization.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @returns A promise that resolves with the links result or null if an error occurs.\n */\nexport async function getPushNotificationLinks(\n bearerToken: string,\n): Promise<LinksResult | null> {\n try {\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n headers: { Authorization: `Bearer ${bearerToken}` },\n });\n if (!response.ok) {\n log.error('Failed to fetch the push notification links');\n throw new Error('Failed to fetch the push notification links');\n }\n return response.json() as Promise<LinksResult>;\n } catch (error) {\n log.error('Failed to fetch the push notification links', error);\n return null;\n }\n}\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n trigger_ids: triggers,\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n\n if (!notificationLinks) {\n return null;\n }\n\n const regToken = await createRegToken(env).catch(() => null);\n if (!regToken) {\n return null;\n }\n\n const newRegTokens = new Set(notificationLinks.registration_tokens);\n newRegTokens.add({ token: regToken, platform });\n\n await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token and unlinking triggers.\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, bearerToken, triggers, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return false;\n }\n\n const filteredRegTokens = notificationLinks.registration_tokens.filter(\n (r) => r.token !== regToken,\n );\n\n const isTokenRemovedFromAPI = await updateLinksAPI(\n bearerToken,\n triggers,\n filteredRegTokens,\n );\n if (!isTokenRemovedFromAPI) {\n return false;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n regToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return { isTriggersLinkedToPushNotifications: false };\n }\n // Create new registration token if doesn't exist\n const hasRegToken = Boolean(\n regToken &&\n notificationLinks.registration_tokens.some((r) => r.token === regToken),\n );\n\n let newRegToken: string | null = null;\n if (!hasRegToken) {\n await deleteRegToken(env);\n newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n notificationLinks.registration_tokens.push({\n token: newRegToken,\n platform,\n });\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n notificationLinks.registration_tokens,\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken ?? null,\n };\n}\n"]}
@@ -1,18 +1,3 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./firebase.cjs"), exports);
18
3
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA2B","sourcesContent":["export * from './firebase';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"","sourcesContent":["export type * from './firebase';\nexport type * from './push-service-interface';\n"]}
@@ -1,2 +1,3 @@
1
- export * from "./firebase.cjs";
1
+ export type * from "./firebase.cjs";
2
+ export type * from "./push-service-interface.cjs";
2
3
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,+BAA2B"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,oCAAgC;AAChC,kDAA8C"}
@@ -1,2 +1,3 @@
1
- export * from "./firebase.mjs";
1
+ export type * from "./firebase.mjs";
2
+ export type * from "./push-service-interface.mjs";
2
3
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,+BAA2B"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,oCAAgC;AAChC,kDAA8C"}
@@ -1,2 +1,2 @@
1
- export * from "./firebase.mjs";
1
+ export {};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,+BAA2B","sourcesContent":["export * from './firebase';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"","sourcesContent":["export type * from './firebase';\nexport type * from './push-service-interface';\n"]}
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=index.cjs.map
3
+ //# sourceMappingURL=push-service-interface.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-service-interface.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/push-service-interface.ts"],"names":[],"mappings":"","sourcesContent":["import type { PushNotificationEnv } from '.';\n\ntype Unsubscribe = () => void;\n\n/**\n * Firebase - allows creating of a registration token for push notifications\n */\nexport type CreateRegToken = (\n env: PushNotificationEnv,\n) => Promise<string | null>;\n\n/**\n * Firebase - allows deleting a reg token. Returns true if successful, otherwise false if failed\n */\nexport type DeleteRegToken = (env: PushNotificationEnv) => Promise<boolean>;\n\n/**\n * Firebase + Platform Specific Logic.\n * Will be used to subscribe to the `onMessage` and `onBackgroundMessage` handlers\n * But will also need client specific logic for showing a notification and clicking a notification\n * (browser APIs for web, and Notifee on mobile)\n *\n * We can either create \"creator\"/\"builder\" function in platform specific files (see push-web.ts),\n * Or the platform needs to correctly handle:\n * - subscriptions\n * - click events\n * - publishing PushController events using it's messenger\n */\nexport type SubscribeToPushNotifications = (\n env: PushNotificationEnv,\n) => Promise<Unsubscribe | null>;\n\nexport type PushService = {\n createRegToken: CreateRegToken;\n\n deleteRegToken: DeleteRegToken;\n\n subscribeToPushNotifications: SubscribeToPushNotifications;\n};\n"]}
@@ -0,0 +1,30 @@
1
+ import type { PushNotificationEnv } from "./index.cjs";
2
+ type Unsubscribe = () => void;
3
+ /**
4
+ * Firebase - allows creating of a registration token for push notifications
5
+ */
6
+ export type CreateRegToken = (env: PushNotificationEnv) => Promise<string | null>;
7
+ /**
8
+ * Firebase - allows deleting a reg token. Returns true if successful, otherwise false if failed
9
+ */
10
+ export type DeleteRegToken = (env: PushNotificationEnv) => Promise<boolean>;
11
+ /**
12
+ * Firebase + Platform Specific Logic.
13
+ * Will be used to subscribe to the `onMessage` and `onBackgroundMessage` handlers
14
+ * But will also need client specific logic for showing a notification and clicking a notification
15
+ * (browser APIs for web, and Notifee on mobile)
16
+ *
17
+ * We can either create "creator"/"builder" function in platform specific files (see push-web.ts),
18
+ * Or the platform needs to correctly handle:
19
+ * - subscriptions
20
+ * - click events
21
+ * - publishing PushController events using it's messenger
22
+ */
23
+ export type SubscribeToPushNotifications = (env: PushNotificationEnv) => Promise<Unsubscribe | null>;
24
+ export type PushService = {
25
+ createRegToken: CreateRegToken;
26
+ deleteRegToken: DeleteRegToken;
27
+ subscribeToPushNotifications: SubscribeToPushNotifications;
28
+ };
29
+ export {};
30
+ //# sourceMappingURL=push-service-interface.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-service-interface.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/push-service-interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAU;AAE7C,KAAK,WAAW,GAAG,MAAM,IAAI,CAAC;AAE9B;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAC3B,GAAG,EAAE,mBAAmB,KACrB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAE5E;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,4BAA4B,GAAG,CACzC,GAAG,EAAE,mBAAmB,KACrB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;AAEjC,MAAM,MAAM,WAAW,GAAG;IACxB,cAAc,EAAE,cAAc,CAAC;IAE/B,cAAc,EAAE,cAAc,CAAC;IAE/B,4BAA4B,EAAE,4BAA4B,CAAC;CAC5D,CAAC"}
@@ -0,0 +1,30 @@
1
+ import type { PushNotificationEnv } from "./index.mjs";
2
+ type Unsubscribe = () => void;
3
+ /**
4
+ * Firebase - allows creating of a registration token for push notifications
5
+ */
6
+ export type CreateRegToken = (env: PushNotificationEnv) => Promise<string | null>;
7
+ /**
8
+ * Firebase - allows deleting a reg token. Returns true if successful, otherwise false if failed
9
+ */
10
+ export type DeleteRegToken = (env: PushNotificationEnv) => Promise<boolean>;
11
+ /**
12
+ * Firebase + Platform Specific Logic.
13
+ * Will be used to subscribe to the `onMessage` and `onBackgroundMessage` handlers
14
+ * But will also need client specific logic for showing a notification and clicking a notification
15
+ * (browser APIs for web, and Notifee on mobile)
16
+ *
17
+ * We can either create "creator"/"builder" function in platform specific files (see push-web.ts),
18
+ * Or the platform needs to correctly handle:
19
+ * - subscriptions
20
+ * - click events
21
+ * - publishing PushController events using it's messenger
22
+ */
23
+ export type SubscribeToPushNotifications = (env: PushNotificationEnv) => Promise<Unsubscribe | null>;
24
+ export type PushService = {
25
+ createRegToken: CreateRegToken;
26
+ deleteRegToken: DeleteRegToken;
27
+ subscribeToPushNotifications: SubscribeToPushNotifications;
28
+ };
29
+ export {};
30
+ //# sourceMappingURL=push-service-interface.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-service-interface.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/push-service-interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAU;AAE7C,KAAK,WAAW,GAAG,MAAM,IAAI,CAAC;AAE9B;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAC3B,GAAG,EAAE,mBAAmB,KACrB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAE5E;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,4BAA4B,GAAG,CACzC,GAAG,EAAE,mBAAmB,KACrB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;AAEjC,MAAM,MAAM,WAAW,GAAG;IACxB,cAAc,EAAE,cAAc,CAAC;IAE/B,cAAc,EAAE,cAAc,CAAC;IAE/B,4BAA4B,EAAE,4BAA4B,CAAC;CAC5D,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=push-service-interface.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-service-interface.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/push-service-interface.ts"],"names":[],"mappings":"","sourcesContent":["import type { PushNotificationEnv } from '.';\n\ntype Unsubscribe = () => void;\n\n/**\n * Firebase - allows creating of a registration token for push notifications\n */\nexport type CreateRegToken = (\n env: PushNotificationEnv,\n) => Promise<string | null>;\n\n/**\n * Firebase - allows deleting a reg token. Returns true if successful, otherwise false if failed\n */\nexport type DeleteRegToken = (env: PushNotificationEnv) => Promise<boolean>;\n\n/**\n * Firebase + Platform Specific Logic.\n * Will be used to subscribe to the `onMessage` and `onBackgroundMessage` handlers\n * But will also need client specific logic for showing a notification and clicking a notification\n * (browser APIs for web, and Notifee on mobile)\n *\n * We can either create \"creator\"/\"builder\" function in platform specific files (see push-web.ts),\n * Or the platform needs to correctly handle:\n * - subscriptions\n * - click events\n * - publishing PushController events using it's messenger\n */\nexport type SubscribeToPushNotifications = (\n env: PushNotificationEnv,\n) => Promise<Unsubscribe | null>;\n\nexport type PushService = {\n createRegToken: CreateRegToken;\n\n deleteRegToken: DeleteRegToken;\n\n subscribeToPushNotifications: SubscribeToPushNotifications;\n};\n"]}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSubscribeToPushNotifications = exports.deleteRegToken = exports.createRegToken = void 0;
4
+ var push_helpers_1 = require("./push-helpers.cjs");
5
+ Object.defineProperty(exports, "createRegToken", { enumerable: true, get: function () { return push_helpers_1.createRegToken; } });
6
+ Object.defineProperty(exports, "deleteRegToken", { enumerable: true, get: function () { return push_helpers_1.deleteRegToken; } });
7
+ Object.defineProperty(exports, "createSubscribeToPushNotifications", { enumerable: true, get: function () { return push_helpers_1.createSubscribeToPushNotifications; } });
8
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/index.ts"],"names":[],"mappings":";;;AAAA,mDAIwB;AAHtB,8GAAA,cAAc,OAAA;AACd,8GAAA,cAAc,OAAA;AACd,kIAAA,kCAAkC,OAAA","sourcesContent":["export {\n createRegToken,\n deleteRegToken,\n createSubscribeToPushNotifications,\n} from './push-helpers';\n"]}
@@ -0,0 +1,2 @@
1
+ export { createRegToken, deleteRegToken, createSubscribeToPushNotifications, } from "./push-helpers.cjs";
2
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,kCAAkC,GACnC,2BAAuB"}
@@ -0,0 +1,2 @@
1
+ export { createRegToken, deleteRegToken, createSubscribeToPushNotifications, } from "./push-helpers.mjs";
2
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,kCAAkC,GACnC,2BAAuB"}
@@ -0,0 +1,2 @@
1
+ export { createRegToken, deleteRegToken, createSubscribeToPushNotifications } from "./push-helpers.mjs";
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,kCAAkC,EACnC,2BAAuB","sourcesContent":["export {\n createRegToken,\n deleteRegToken,\n createSubscribeToPushNotifications,\n} from './push-helpers';\n"]}
@@ -3,13 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.listenToPushNotificationsClicked = exports.listenToPushNotificationsReceived = exports.deleteRegToken = exports.createRegToken = exports.supportedCache = void 0;
6
+ exports.createSubscribeToPushNotifications = exports.deleteRegToken = exports.createRegToken = exports.supportedCache = void 0;
7
7
  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 NotificationServicesController_1 = require("../../../NotificationServicesController/index.cjs");
12
- const to_raw_notification_1 = require("../../../shared/to-raw-notification.cjs");
11
+ const NotificationServicesController_1 = require("../../NotificationServicesController/index.cjs");
12
+ const to_raw_notification_1 = require("../../shared/to-raw-notification.cjs");
13
13
  // Exported to help testing
14
14
  // eslint-disable-next-line import-x/no-mutable-exports
15
15
  exports.supportedCache = null;
@@ -80,15 +80,16 @@ async function deleteRegToken(env) {
80
80
  await (0, messaging_1.deleteToken)(messaging);
81
81
  return true;
82
82
  }
83
- catch (error) {
83
+ catch {
84
84
  return false;
85
85
  }
86
86
  }
87
87
  exports.deleteRegToken = deleteRegToken;
88
88
  /**
89
89
  * Service Worker Listener for when push notifications are received.
90
+ *
90
91
  * @param env - push notification environment
91
- * @param handler - handler to actually showing notification, MUST BE PROVEDED
92
+ * @param handler - handler to actually showing notification, MUST BE PROVIDED
92
93
  * @returns unsubscribe handler
93
94
  */
94
95
  async function listenToPushNotificationsReceived(env, handler) {
@@ -120,7 +121,6 @@ async function listenToPushNotificationsReceived(env, handler) {
120
121
  const unsubscribe = () => unsubscribePushNotifications();
121
122
  return unsubscribe;
122
123
  }
123
- exports.listenToPushNotificationsReceived = listenToPushNotificationsReceived;
124
124
  /**
125
125
  * Service Worker Listener for when a notification is clicked
126
126
  *
@@ -137,5 +137,34 @@ function listenToPushNotificationsClicked(handler) {
137
137
  const unsubscribe = () => self.removeEventListener('notificationclick', clickHandler);
138
138
  return unsubscribe;
139
139
  }
140
- exports.listenToPushNotificationsClicked = listenToPushNotificationsClicked;
141
- //# sourceMappingURL=push-web.cjs.map
140
+ /**
141
+ * A creator function that assists creating web-specific push notification subscription:
142
+ * 1. Creates subscriptions for receiving and clicking notifications
143
+ * 2. Creates click events when a notification is clicked
144
+ * 3. Publishes controller messenger events
145
+ *
146
+ * @param props - props for this creator function.
147
+ * @param props.onReceivedHandler - allows the developer to handle showing a notification
148
+ * @param props.onClickHandler - allows the developer to handle clicking the notification
149
+ * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events
150
+ * @returns a function that can be used by the controller
151
+ */
152
+ function createSubscribeToPushNotifications(props) {
153
+ return async function (env) {
154
+ const onBackgroundMessageSub = await listenToPushNotificationsReceived(env, async (notification) => {
155
+ props.messenger.publish('NotificationServicesPushController:onNewNotifications', notification);
156
+ await props.onReceivedHandler(notification);
157
+ });
158
+ const onClickSub = listenToPushNotificationsClicked((event, notification) => {
159
+ props.messenger.publish('NotificationServicesPushController:pushNotificationClicked', notification);
160
+ props.onClickHandler(event, notification);
161
+ });
162
+ const unsubscribe = () => {
163
+ onBackgroundMessageSub?.();
164
+ onClickSub();
165
+ };
166
+ return unsubscribe;
167
+ };
168
+ }
169
+ exports.createSubscribeToPushNotifications = createSubscribeToPushNotifications;
170
+ //# sourceMappingURL=push-helpers.cjs.map