@metamask-previews/notification-services-controller 0.0.0-preview-bc4ac8fa → 0.1.0-preview-509f7952
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.
- package/CHANGELOG.md +4 -1
- package/dist/NotificationServicesController/NotificationServicesController.js +2 -2
- package/dist/NotificationServicesController/NotificationServicesController.mjs +2 -2
- package/dist/NotificationServicesController/index.js +3 -3
- package/dist/NotificationServicesController/index.mjs +3 -3
- package/dist/NotificationServicesPushController/NotificationServicesPushController.js +6 -6
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +6 -6
- package/dist/NotificationServicesPushController/index.js +7 -7
- package/dist/NotificationServicesPushController/index.mjs +7 -7
- package/dist/NotificationServicesPushController/services/push/push-web.js +4 -4
- package/dist/NotificationServicesPushController/services/push/push-web.mjs +3 -3
- package/dist/NotificationServicesPushController/services/services.js +5 -5
- package/dist/NotificationServicesPushController/services/services.mjs +4 -4
- package/dist/NotificationServicesPushController/utils/get-notification-message.js +2 -2
- package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +2 -2
- package/dist/NotificationServicesPushController/utils/index.js +2 -2
- package/dist/NotificationServicesPushController/utils/index.mjs +2 -2
- package/dist/{chunk-TT56I53C.mjs → chunk-5DA74RWP.mjs} +2 -2
- package/dist/{chunk-KTQP5UDT.mjs → chunk-5I4DIH6O.mjs} +3 -3
- package/dist/{chunk-KTQP5UDT.mjs.map → chunk-5I4DIH6O.mjs.map} +1 -1
- package/dist/{chunk-OUPWMPLU.mjs → chunk-7ET5GUSY.mjs} +3 -3
- package/dist/chunk-7ET5GUSY.mjs.map +1 -0
- package/dist/{chunk-NJBJIZLR.js → chunk-ABJDEX2S.js} +4 -5
- package/dist/chunk-ABJDEX2S.js.map +1 -0
- package/dist/{chunk-3F4M3OUO.mjs → chunk-ATR6FRAX.mjs} +4 -4
- package/dist/chunk-ATR6FRAX.mjs.map +1 -0
- package/dist/{chunk-DNTK4U5M.js → chunk-K75FPFTD.js} +4 -4
- package/dist/{chunk-DNTK4U5M.js.map → chunk-K75FPFTD.js.map} +1 -1
- package/dist/{chunk-5WWCWQCE.js → chunk-O22GVG7K.js} +11 -11
- package/dist/chunk-O22GVG7K.js.map +1 -0
- package/dist/{chunk-52NKJDI2.mjs → chunk-QUNLD7F2.mjs} +4 -5
- package/dist/chunk-QUNLD7F2.mjs.map +1 -0
- package/dist/{chunk-5OYPOYXZ.mjs → chunk-R6AIAXQV.mjs} +25 -5
- package/dist/chunk-R6AIAXQV.mjs.map +1 -0
- package/dist/{chunk-6B5FOWRH.js → chunk-WTE64QEK.js} +5 -5
- package/dist/chunk-WTE64QEK.js.map +1 -0
- package/dist/{chunk-XNBTOK6G.js → chunk-YD3VVORT.js} +25 -5
- package/dist/chunk-YD3VVORT.js.map +1 -0
- package/dist/{chunk-NMLYB3XT.js → chunk-YETZSCGK.js} +4 -4
- package/dist/index.js +7 -7
- package/dist/index.mjs +6 -6
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/NotificationServicesController/NotificationServicesController.d.ts +3 -1
- package/dist/types/NotificationServicesController/NotificationServicesController.d.ts.map +1 -1
- package/dist/types/NotificationServicesController/index.d.ts +2 -0
- package/dist/types/NotificationServicesController/index.d.ts.map +1 -1
- package/dist/types/NotificationServicesPushController/NotificationServicesPushController.d.ts +1 -1
- package/dist/types/NotificationServicesPushController/NotificationServicesPushController.d.ts.map +1 -1
- package/dist/types/NotificationServicesPushController/index.d.ts +2 -0
- package/dist/types/NotificationServicesPushController/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-3F4M3OUO.mjs.map +0 -1
- package/dist/chunk-52NKJDI2.mjs.map +0 -1
- package/dist/chunk-5OYPOYXZ.mjs.map +0 -1
- package/dist/chunk-5WWCWQCE.js.map +0 -1
- package/dist/chunk-6B5FOWRH.js.map +0 -1
- package/dist/chunk-NJBJIZLR.js.map +0 -1
- package/dist/chunk-OUPWMPLU.mjs.map +0 -1
- package/dist/chunk-XNBTOK6G.js.map +0 -1
- /package/dist/{chunk-TT56I53C.mjs.map → chunk-5DA74RWP.mjs.map} +0 -0
- /package/dist/{chunk-NMLYB3XT.js.map → chunk-YETZSCGK.js.map} +0 -0
|
@@ -3,11 +3,11 @@ import {
|
|
|
3
3
|
deactivatePushNotifications,
|
|
4
4
|
listenToPushNotifications,
|
|
5
5
|
updateTriggerPushNotifications
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-5DA74RWP.mjs";
|
|
7
7
|
import {
|
|
8
8
|
createRegToken,
|
|
9
9
|
deleteRegToken
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-QUNLD7F2.mjs";
|
|
11
11
|
import {
|
|
12
12
|
__privateAdd,
|
|
13
13
|
__privateGet,
|
|
@@ -211,4 +211,4 @@ getAndAssertBearerToken_fn = async function() {
|
|
|
211
211
|
export {
|
|
212
212
|
NotificationServicesPushController
|
|
213
213
|
};
|
|
214
|
-
//# sourceMappingURL=chunk-
|
|
214
|
+
//# sourceMappingURL=chunk-7ET5GUSY.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"sourcesContent":["import type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport type { Types } from '../NotificationServicesController';\nimport { createRegToken, deleteRegToken } from './services/push/push-web';\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n listenToPushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n fcmToken: string;\n};\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\n\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\n\nexport type Actions =\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | ControllerGetStateAction<'state', NotificationServicesPushControllerState>;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClicked = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type AllowedEvents =\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClicked;\n\nexport type NotificationServicesPushControllerMessenger =\n RestrictedControllerMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n >;\n\nconst metadata = {\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n /**\n * Config to turn on/off push notifications.\n * This is currently linked to MV3 builds on extension.\n */\n isPushEnabled: boolean;\n\n /**\n * Must handle when a push notification is received.\n * You must call `registration.showNotification` or equivalent to show the notification on web/mobile\n */\n onPushNotificationReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n\n /**\n * Must handle when a push notification is clicked.\n * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.\n */\n onPushNotificationClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n};\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n *\n * @augments {BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger>}\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n #env: PushNotificationEnv;\n\n #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n env: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: {\n fcmToken: state?.fcmToken || '',\n },\n });\n\n this.#env = env;\n this.#config = config;\n\n this.#registerMessageHandlers();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n */\n async enablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n // Activate Push Notifications\n const regToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n platform: this.#config.platform,\n });\n\n if (!regToken) {\n return;\n }\n\n this.#pushListenerUnsubscribe = await listenToPushNotifications({\n env: this.#env,\n listenToPushReceived: async (n) => {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:onNewNotifications',\n n,\n );\n await this.#config.onPushNotificationReceived(n);\n },\n listenToPushClicked: (e, n) => {\n if (n) {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n n,\n );\n }\n\n this.#config.onPushNotificationClicked(e);\n },\n });\n\n // Update state\n this.update((state) => {\n state.fcmToken = regToken;\n });\n } catch (error) {\n log.error('Failed to enable push notifications:', error);\n throw new Error('Failed to enable push notifications');\n }\n }\n\n /**\n * Disables push notifications for the application.\n * This method handles the process of disabling push notifications by:\n * 1. Unregistering the service worker to stop listening for messages.\n * 2. Sending a request to the server to unregister the device using the FCM token.\n * 3. Removing the FCM token from the state to complete the process.\n *\n * @param UUIDs - An array of UUIDs for which push notifications should be disabled.\n */\n async disablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n let isPushNotificationsDisabled: boolean;\n\n try {\n // Send a request to the server to unregister the token/device\n isPushNotificationsDisabled = await deactivatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n\n // Remove the FCM token from the state\n if (!isPushNotificationsDisabled) {\n return;\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n if (isPushNotificationsDisabled) {\n this.update((state) => {\n state.fcmToken = '';\n });\n }\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n deleteRegToken,\n platform: this.#config.platform,\n regToken: this.state.fcmToken,\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.update((state) => {\n state.fcmToken = fcmToken;\n });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAIA,SAAS,sBAAsB;AAE/B,OAAO,SAAS;AAYhB,IAAM,iBAAiB;AAqDvB,IAAM,WAAW;AAAA,EACf,UAAU;AAAA,IACR,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AACF;AA5EA;AAqHA,IAAqB,qCAArB,cAAgE,eAI9D;AAAA,EAOA,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAKG;AACD,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA,MAAM;AAAA,MACN,OAAO;AAAA,QACL,UAAU,OAAO,YAAY;AAAA,MAC/B;AAAA,IACF,CAAC;AAQH;AAeA,uBAAM;AA/CN,iDAAqD;AAErD;AAEA;AAsBE,uBAAK,MAAO;AACZ,uBAAK,SAAU;AAEf,0BAAK,sDAAL;AAAA,EACF;AAAA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K,+BAA+B,KAAK,IAAI;AAAA,EAC/C;AACF;AAEM;AAAA,6BAAwB,iBAAG;AAC/B,QAAM,cAAc,MAAM,KAAK,gBAAgB;AAAA,IAC7C;AAAA,EACF;AACA,MAAI,CAAC,aAAa;AAChB,QAAI;AAAA,MACF;AAAA,IACF;AACA,UAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -7,7 +7,6 @@ var _app = require('firebase/app');
|
|
|
7
7
|
var _messaging = require('firebase/messaging');
|
|
8
8
|
var _sw = require('firebase/messaging/sw');
|
|
9
9
|
var _loglevel = require('loglevel'); var _loglevel2 = _interopRequireDefault(_loglevel);
|
|
10
|
-
var sw = self;
|
|
11
10
|
var createFirebaseApp = async (env) => {
|
|
12
11
|
try {
|
|
13
12
|
return _app.getApp.call(void 0, );
|
|
@@ -32,7 +31,7 @@ async function createRegToken(env) {
|
|
|
32
31
|
try {
|
|
33
32
|
const messaging = await getFirebaseMessaging(env);
|
|
34
33
|
const token = await _messaging.getToken.call(void 0, messaging, {
|
|
35
|
-
serviceWorkerRegistration:
|
|
34
|
+
serviceWorkerRegistration: self.registration,
|
|
36
35
|
vapidKey: env.vapidKey
|
|
37
36
|
});
|
|
38
37
|
return token;
|
|
@@ -79,8 +78,8 @@ function listenToPushNotificationsClicked(handler) {
|
|
|
79
78
|
const data = event?.notification?.data;
|
|
80
79
|
handler(event, data);
|
|
81
80
|
};
|
|
82
|
-
|
|
83
|
-
const unsubscribe = () =>
|
|
81
|
+
self.addEventListener("notificationclick", clickHandler);
|
|
82
|
+
const unsubscribe = () => self.removeEventListener("notificationclick", clickHandler);
|
|
84
83
|
return unsubscribe;
|
|
85
84
|
}
|
|
86
85
|
|
|
@@ -90,4 +89,4 @@ function listenToPushNotificationsClicked(handler) {
|
|
|
90
89
|
|
|
91
90
|
|
|
92
91
|
exports.createRegToken = createRegToken; exports.deleteRegToken = deleteRegToken; exports.listenToPushNotificationsReceived = listenToPushNotificationsReceived; exports.listenToPushNotificationsClicked = listenToPushNotificationsClicked;
|
|
93
|
-
//# sourceMappingURL=chunk-
|
|
92
|
+
//# sourceMappingURL=chunk-ABJDEX2S.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/NotificationServicesPushController/services/push/push-web.ts"],"names":[],"mappings":";;;;;AAIA,SAAS,QAAQ,qBAAqB;AACtC,SAAS,UAAU,mBAAmB;AACtC,SAAS,cAAc,2BAA2B;AAElD,OAAO,SAAS;AAQhB,IAAM,oBAAoB,OACxB,QACyB;AACzB,MAAI;AACF,WAAO,OAAO;AAAA,EAChB,QAAQ;AACN,UAAM,iBAAiB;AAAA,MACrB,QAAQ,IAAI;AAAA,MACZ,YAAY,IAAI;AAAA,MAChB,eAAe,IAAI;AAAA,MACnB,WAAW,IAAI;AAAA,MACf,mBAAmB,IAAI;AAAA,MACvB,OAAO,IAAI;AAAA,MACX,eAAe,IAAI;AAAA,IACrB;AACA,WAAO,cAAc,cAAc;AAAA,EACrC;AACF;AAEA,IAAM,uBAAuB,OAC3B,QACuB;AACvB,QAAM,MAAM,MAAM,kBAAkB,GAAG;AACvC,SAAO,aAAa,GAAG;AACzB;AAQA,eAAsB,eACpB,KACwB;AACxB,MAAI;AACF,UAAM,YAAY,MAAM,qBAAqB,GAAG;AAChD,UAAM,QAAQ,MAAM,SAAS,WAAW;AAAA,MACtC,2BAA2B,KAAK;AAAA,MAChC,UAAU,IAAI;AAAA,IAChB,CAAC;AACD,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAQA,eAAsB,eACpB,KACkB;AAClB,MAAI;AACF,UAAM,YAAY,MAAM,qBAAqB,GAAG;AAChD,UAAM,YAAY,SAAS;AAC3B,WAAO;AAAA,EACT,SAAS,OAAO;AACd,WAAO;AAAA,EACT;AACF;AAQA,eAAsB,kCACpB,KACA,SACA;AACA,QAAM,YAAY,MAAM,qBAAqB,GAAG;AAChD,QAAM,+BAA+B;AAAA,IACnC;AAAA;AAAA,IAEA,OAAO,YAA4B;AACjC,UAAI;AACF,cAAM,mBAA4C,SAAS,MAAM,OAC7D,KAAK,MAAM,SAAS,MAAM,IAAI,IAC9B;AAEJ,YAAI,CAAC,kBAAkB;AACrB;AAAA,QACF;AAEA,cAAM,eAAe,mBAAW,oBAAoB,gBAAgB;AACpE,cAAM,QAAQ,YAAY;AAAA,MAC5B,SAAS,OAAO;AAEd,YAAI,MAAM,qCAAqC;AAAA,UAC7C,cAAc,SAAS,MAAM;AAAA,UAC7B;AAAA,QACF,CAAC;AACD,cAAM,IAAI,MAAM,kCAAkC;AAAA,MACpD;AAAA,IACF;AAAA,EACF;AAEA,QAAM,cAAc,MAAM,6BAA6B;AACvD,SAAO;AACT;AAQO,SAAS,iCACd,SACA;AACA,QAAM,eAAe,CAAC,UAA6B;AAEjD,UAAM,OAA4B,OAAO,cAAc;AACvD,YAAQ,OAAO,IAAI;AAAA,EACrB;AAEA,OAAK,iBAAiB,qBAAqB,YAAY;AACvD,QAAM,cAAc,MAClB,KAAK,oBAAoB,qBAAqB,YAAY;AAC5D,SAAO;AACT","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 { getMessaging, onBackgroundMessage } from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../../NotificationServicesController';\nimport { Processors } from '../../../NotificationServicesController';\nimport type { PushNotificationEnv } from '../../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\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> => {\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 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 await deleteToken(messaging);\n return true;\n } catch (error) {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVEDED\n * @returns unsubscribe handler\n */\nexport async function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler: (notification: Types.INotification) => void | Promise<void>,\n) {\n const messaging = await getFirebaseMessaging(env);\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload) => {\n try {\n const notificationData: Types.NotificationUnion = payload?.data?.data\n ? JSON.parse(payload?.data?.data)\n : undefined;\n\n if (!notificationData) {\n return;\n }\n\n const notification = Processors.processNotification(notificationData);\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 throw new Error('Unable to send push notification');\n }\n },\n );\n\n const unsubscribe = () => 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 */\nexport function listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification?: Types.INotification) => void,\n) {\n const clickHandler = (event: NotificationEvent) => {\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 = () =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n"]}
|
|
@@ -6,13 +6,13 @@ import {
|
|
|
6
6
|
} from "./chunk-IOMDG67D.mjs";
|
|
7
7
|
import {
|
|
8
8
|
NotificationServicesPushController
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-7ET5GUSY.mjs";
|
|
10
10
|
import {
|
|
11
11
|
REGISTRATION_TOKENS_ENDPOINT
|
|
12
12
|
} from "./chunk-IKWNHNJQ.mjs";
|
|
13
13
|
import {
|
|
14
14
|
require_nock
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-5I4DIH6O.mjs";
|
|
16
16
|
import {
|
|
17
17
|
__export,
|
|
18
18
|
__toESM
|
|
@@ -21,8 +21,8 @@ import {
|
|
|
21
21
|
// src/NotificationServicesPushController/index.ts
|
|
22
22
|
var NotificationServicesPushController_exports = {};
|
|
23
23
|
__export(NotificationServicesPushController_exports, {
|
|
24
|
+
Controller: () => NotificationServicesPushController,
|
|
24
25
|
Mocks: () => fixtures_exports,
|
|
25
|
-
NotificationServicesPushController: () => NotificationServicesPushController,
|
|
26
26
|
Types: () => types_exports,
|
|
27
27
|
Utils: () => utils_exports
|
|
28
28
|
});
|
|
@@ -116,4 +116,4 @@ export {
|
|
|
116
116
|
fixtures_exports,
|
|
117
117
|
NotificationServicesPushController_exports
|
|
118
118
|
};
|
|
119
|
-
//# sourceMappingURL=chunk-
|
|
119
|
+
//# sourceMappingURL=chunk-ATR6FRAX.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/NotificationServicesPushController/index.ts","../src/NotificationServicesPushController/__fixtures__/index.ts","../src/NotificationServicesPushController/__fixtures__/mockResponse.ts","../src/NotificationServicesPushController/__fixtures__/mockServices.ts"],"sourcesContent":["import Controller from './NotificationServicesPushController';\n\nexport { Controller };\nexport * from './NotificationServicesPushController';\nexport * as Types from './types';\nexport * as Utils from './utils';\nexport * as Mocks from './__fixtures__';\n","export * from './mockResponse';\nexport * from './mockServices';\n","import { REGISTRATION_TOKENS_ENDPOINT } from '../services/endpoints';\nimport type { LinksResult } from '../services/services';\n\ntype MockResponse = {\n url: string | RegExp;\n requestMethod: 'GET' | 'POST' | 'PUT';\n response: unknown;\n};\n\nexport const MOCK_REG_TOKEN = 'REG_TOKEN';\nexport const MOCK_LINKS_RESPONSE: LinksResult = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n trigger_ids: ['1', '2', '3'],\n // eslint-disable-next-line @typescript-eslint/naming-convention\n registration_tokens: [\n { token: 'reg_token_1', platform: 'portfolio' },\n { token: 'reg_token_2', platform: 'extension' },\n ],\n};\n\nexport const getMockRetrievePushNotificationLinksResponse = () => {\n return {\n url: REGISTRATION_TOKENS_ENDPOINT,\n requestMethod: 'GET',\n response: MOCK_LINKS_RESPONSE,\n } satisfies MockResponse;\n};\n\nexport const getMockUpdatePushNotificationLinksResponse = () => {\n return {\n url: REGISTRATION_TOKENS_ENDPOINT,\n requestMethod: 'POST',\n response: null,\n } satisfies MockResponse;\n};\n\nexport const MOCK_FCM_RESPONSE = {\n name: '',\n token: 'fcm-token',\n web: {\n endpoint: '',\n p256dh: '',\n auth: '',\n applicationPubKey: '',\n },\n};\n\nexport const getMockCreateFCMRegistrationTokenResponse = () => {\n return {\n url: /^https:\\/\\/fcmregistrations\\.googleapis\\.com\\/v1\\/projects\\/.*$/u,\n requestMethod: 'POST',\n response: MOCK_FCM_RESPONSE,\n } satisfies MockResponse;\n};\n\nexport const getMockDeleteFCMRegistrationTokenResponse = () => {\n return {\n url: /^https:\\/\\/fcmregistrations\\.googleapis\\.com\\/v1\\/projects\\/.*$/u,\n requestMethod: 'POST',\n response: {},\n } satisfies MockResponse;\n};\n","import nock from 'nock';\n\nimport {\n getMockRetrievePushNotificationLinksResponse,\n getMockUpdatePushNotificationLinksResponse,\n} from './mockResponse';\n\ntype MockReply = {\n status: nock.StatusCode;\n body?: nock.Body;\n};\n\nexport const mockEndpointGetPushNotificationLinks = (mockReply?: MockReply) => {\n const mockResponse = getMockRetrievePushNotificationLinksResponse();\n const reply = mockReply ?? {\n status: 200,\n body: mockResponse.response,\n };\n\n const mockEndpoint = nock(mockResponse.url)\n .get('')\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const mockEndpointUpdatePushNotificationLinks = (\n mockReply?: MockReply,\n) => {\n const mockResponse = getMockUpdatePushNotificationLinksResponse();\n const reply = mockReply ?? {\n status: 200,\n body: mockResponse.response,\n };\n\n const mockEndpoint = nock(mockResponse.url).post('').reply(reply.status);\n\n return mockEndpoint;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACSO,IAAM,iBAAiB;AACvB,IAAM,sBAAmC;AAAA;AAAA,EAE9C,aAAa,CAAC,KAAK,KAAK,GAAG;AAAA;AAAA,EAE3B,qBAAqB;AAAA,IACnB,EAAE,OAAO,eAAe,UAAU,YAAY;AAAA,IAC9C,EAAE,OAAO,eAAe,UAAU,YAAY;AAAA,EAChD;AACF;AAEO,IAAM,+CAA+C,MAAM;AAChE,SAAO;AAAA,IACL,KAAK;AAAA,IACL,eAAe;AAAA,IACf,UAAU;AAAA,EACZ;AACF;AAEO,IAAM,6CAA6C,MAAM;AAC9D,SAAO;AAAA,IACL,KAAK;AAAA,IACL,eAAe;AAAA,IACf,UAAU;AAAA,EACZ;AACF;AAEO,IAAM,oBAAoB;AAAA,EAC/B,MAAM;AAAA,EACN,OAAO;AAAA,EACP,KAAK;AAAA,IACH,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,mBAAmB;AAAA,EACrB;AACF;AAEO,IAAM,4CAA4C,MAAM;AAC7D,SAAO;AAAA,IACL,KAAK;AAAA,IACL,eAAe;AAAA,IACf,UAAU;AAAA,EACZ;AACF;AAEO,IAAM,4CAA4C,MAAM;AAC7D,SAAO;AAAA,IACL,KAAK;AAAA,IACL,eAAe;AAAA,IACf,UAAU,CAAC;AAAA,EACb;AACF;;;AC7DA,kBAAiB;AAYV,IAAM,uCAAuC,CAAC,cAA0B;AAC7E,QAAM,eAAe,6CAA6C;AAClE,QAAM,QAAQ,aAAa;AAAA,IACzB,QAAQ;AAAA,IACR,MAAM,aAAa;AAAA,EACrB;AAEA,QAAM,mBAAe,YAAAA,SAAK,aAAa,GAAG,EACvC,IAAI,EAAE,EACN,MAAM,MAAM,QAAQ,MAAM,IAAI;AAEjC,SAAO;AACT;AAEO,IAAM,0CAA0C,CACrD,cACG;AACH,QAAM,eAAe,2CAA2C;AAChE,QAAM,QAAQ,aAAa;AAAA,IACzB,QAAQ;AAAA,IACR,MAAM,aAAa;AAAA,EACrB;AAEA,QAAM,mBAAe,YAAAA,SAAK,aAAa,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,MAAM,MAAM;AAEvE,SAAO;AACT;","names":["nock"]}
|
|
@@ -7,7 +7,7 @@ var _chunk232HZSEVjs = require('./chunk-232HZSEV.js');
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _chunkYD3VVORTjs = require('./chunk-YD3VVORT.js');
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
var _chunkZMNXLHACjs = require('./chunk-ZMNXLHAC.js');
|
|
@@ -3369,11 +3369,11 @@ var require_nock = _chunkV46WVGWNjs.__commonJS.call(void 0, {
|
|
|
3369
3369
|
var NotificationServicesController_exports = {};
|
|
3370
3370
|
_chunkV46WVGWNjs.__export.call(void 0, NotificationServicesController_exports, {
|
|
3371
3371
|
Constants: () => _chunkCQ6CGSKZjs.constants_exports,
|
|
3372
|
+
Controller: () => _chunkYD3VVORTjs.NotificationServicesController,
|
|
3372
3373
|
Mocks: () => fixtures_exports,
|
|
3373
|
-
NotificationServicesController: () => _chunkXNBTOK6Gjs.NotificationServicesController,
|
|
3374
3374
|
Processors: () => _chunkUPVIT75Fjs.processors_exports,
|
|
3375
3375
|
Types: () => _chunk232HZSEVjs.types_exports,
|
|
3376
|
-
defaultState: () =>
|
|
3376
|
+
defaultState: () => _chunkYD3VVORTjs.defaultState
|
|
3377
3377
|
});
|
|
3378
3378
|
|
|
3379
3379
|
// src/NotificationServicesController/__fixtures__/index.ts
|
|
@@ -4297,4 +4297,4 @@ var mockMarkNotificationsAsRead = (mockReply) => {
|
|
|
4297
4297
|
|
|
4298
4298
|
|
|
4299
4299
|
exports.require_nock = require_nock; exports.fixtures_exports = fixtures_exports; exports.NotificationServicesController_exports = NotificationServicesController_exports;
|
|
4300
|
-
//# sourceMappingURL=chunk-
|
|
4300
|
+
//# sourceMappingURL=chunk-K75FPFTD.js.map
|