@metamask-previews/notification-services-controller 21.0.0-preview-565dfca2 → 21.0.0-preview-3fa1672

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 (123) hide show
  1. package/dist/NotificationServicesController/NotificationServicesController.cjs +41 -37
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  3. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  4. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  5. package/dist/NotificationServicesController/NotificationServicesController.mjs +41 -37
  6. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  7. package/dist/NotificationServicesController/constants/notification-schema.cjs +3 -0
  8. package/dist/NotificationServicesController/constants/notification-schema.cjs.map +1 -1
  9. package/dist/NotificationServicesController/constants/notification-schema.d.cts.map +1 -1
  10. package/dist/NotificationServicesController/constants/notification-schema.d.mts.map +1 -1
  11. package/dist/NotificationServicesController/constants/notification-schema.mjs +3 -0
  12. package/dist/NotificationServicesController/constants/notification-schema.mjs.map +1 -1
  13. package/dist/NotificationServicesController/mocks/mockResponses.cjs +6 -0
  14. package/dist/NotificationServicesController/mocks/mockResponses.cjs.map +1 -1
  15. package/dist/NotificationServicesController/mocks/mockResponses.d.cts.map +1 -1
  16. package/dist/NotificationServicesController/mocks/mockResponses.d.mts.map +1 -1
  17. package/dist/NotificationServicesController/mocks/mockResponses.mjs +6 -0
  18. package/dist/NotificationServicesController/mocks/mockResponses.mjs.map +1 -1
  19. package/dist/NotificationServicesController/processors/process-notifications.cjs +11 -9
  20. package/dist/NotificationServicesController/processors/process-notifications.cjs.map +1 -1
  21. package/dist/NotificationServicesController/processors/process-notifications.d.cts +1 -563
  22. package/dist/NotificationServicesController/processors/process-notifications.d.cts.map +1 -1
  23. package/dist/NotificationServicesController/processors/process-notifications.d.mts +1 -563
  24. package/dist/NotificationServicesController/processors/process-notifications.d.mts.map +1 -1
  25. package/dist/NotificationServicesController/processors/process-notifications.mjs +11 -9
  26. package/dist/NotificationServicesController/processors/process-notifications.mjs.map +1 -1
  27. package/dist/NotificationServicesController/services/api-notifications.cjs +21 -21
  28. package/dist/NotificationServicesController/services/api-notifications.cjs.map +1 -1
  29. package/dist/NotificationServicesController/services/api-notifications.d.cts.map +1 -1
  30. package/dist/NotificationServicesController/services/api-notifications.d.mts.map +1 -1
  31. package/dist/NotificationServicesController/services/api-notifications.mjs +21 -21
  32. package/dist/NotificationServicesController/services/api-notifications.mjs.map +1 -1
  33. package/dist/NotificationServicesController/services/feature-announcements.cjs +10 -10
  34. package/dist/NotificationServicesController/services/feature-announcements.cjs.map +1 -1
  35. package/dist/NotificationServicesController/services/feature-announcements.d.cts.map +1 -1
  36. package/dist/NotificationServicesController/services/feature-announcements.d.mts.map +1 -1
  37. package/dist/NotificationServicesController/services/feature-announcements.mjs +10 -10
  38. package/dist/NotificationServicesController/services/feature-announcements.mjs.map +1 -1
  39. package/dist/NotificationServicesController/services/notification-config-cache.cjs +4 -4
  40. package/dist/NotificationServicesController/services/notification-config-cache.cjs.map +1 -1
  41. package/dist/NotificationServicesController/services/notification-config-cache.mjs +4 -4
  42. package/dist/NotificationServicesController/services/notification-config-cache.mjs.map +1 -1
  43. package/dist/NotificationServicesController/services/perp-notifications.cjs +2 -2
  44. package/dist/NotificationServicesController/services/perp-notifications.cjs.map +1 -1
  45. package/dist/NotificationServicesController/services/perp-notifications.d.cts.map +1 -1
  46. package/dist/NotificationServicesController/services/perp-notifications.d.mts.map +1 -1
  47. package/dist/NotificationServicesController/services/perp-notifications.mjs +2 -2
  48. package/dist/NotificationServicesController/services/perp-notifications.mjs.map +1 -1
  49. package/dist/NotificationServicesController/types/notification-api/notification-api.cjs.map +1 -1
  50. package/dist/NotificationServicesController/types/notification-api/notification-api.d.cts.map +1 -1
  51. package/dist/NotificationServicesController/types/notification-api/notification-api.d.mts.map +1 -1
  52. package/dist/NotificationServicesController/types/notification-api/notification-api.mjs.map +1 -1
  53. package/dist/NotificationServicesController/types/notification-api/schema.cjs +2 -0
  54. package/dist/NotificationServicesController/types/notification-api/schema.cjs.map +1 -1
  55. package/dist/NotificationServicesController/types/notification-api/schema.d.cts.map +1 -1
  56. package/dist/NotificationServicesController/types/notification-api/schema.d.mts.map +1 -1
  57. package/dist/NotificationServicesController/types/notification-api/schema.mjs +2 -0
  58. package/dist/NotificationServicesController/types/notification-api/schema.mjs.map +1 -1
  59. package/dist/NotificationServicesController/types/perps/schema.cjs +2 -0
  60. package/dist/NotificationServicesController/types/perps/schema.cjs.map +1 -1
  61. package/dist/NotificationServicesController/types/perps/schema.d.cts.map +1 -1
  62. package/dist/NotificationServicesController/types/perps/schema.d.mts.map +1 -1
  63. package/dist/NotificationServicesController/types/perps/schema.mjs +2 -0
  64. package/dist/NotificationServicesController/types/perps/schema.mjs.map +1 -1
  65. package/dist/NotificationServicesController/utils/isVersionInBounds.cjs.map +1 -1
  66. package/dist/NotificationServicesController/utils/isVersionInBounds.d.cts.map +1 -1
  67. package/dist/NotificationServicesController/utils/isVersionInBounds.d.mts.map +1 -1
  68. package/dist/NotificationServicesController/utils/isVersionInBounds.mjs.map +1 -1
  69. package/dist/NotificationServicesController/utils/should-auto-expire.cjs.map +1 -1
  70. package/dist/NotificationServicesController/utils/should-auto-expire.d.cts.map +1 -1
  71. package/dist/NotificationServicesController/utils/should-auto-expire.d.mts.map +1 -1
  72. package/dist/NotificationServicesController/utils/should-auto-expire.mjs.map +1 -1
  73. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  74. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  75. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  76. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  77. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs.map +1 -1
  78. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts +9 -24
  79. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts.map +1 -1
  80. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts +9 -24
  81. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts.map +1 -1
  82. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs.map +1 -1
  83. package/dist/NotificationServicesPushController/services/endpoints.cjs.map +1 -1
  84. package/dist/NotificationServicesPushController/services/endpoints.d.cts.map +1 -1
  85. package/dist/NotificationServicesPushController/services/endpoints.d.mts.map +1 -1
  86. package/dist/NotificationServicesPushController/services/endpoints.mjs.map +1 -1
  87. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  88. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  89. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  90. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  91. package/dist/NotificationServicesPushController/utils/get-notification-data.cjs +4 -4
  92. package/dist/NotificationServicesPushController/utils/get-notification-data.cjs.map +1 -1
  93. package/dist/NotificationServicesPushController/utils/get-notification-data.d.cts +2 -2
  94. package/dist/NotificationServicesPushController/utils/get-notification-data.d.cts.map +1 -1
  95. package/dist/NotificationServicesPushController/utils/get-notification-data.d.mts +2 -2
  96. package/dist/NotificationServicesPushController/utils/get-notification-data.d.mts.map +1 -1
  97. package/dist/NotificationServicesPushController/utils/get-notification-data.mjs +4 -4
  98. package/dist/NotificationServicesPushController/utils/get-notification-data.mjs.map +1 -1
  99. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs +62 -61
  100. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs.map +1 -1
  101. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts +9 -9
  102. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts.map +1 -1
  103. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts +9 -9
  104. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts.map +1 -1
  105. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +62 -61
  106. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs.map +1 -1
  107. package/dist/NotificationServicesPushController/web/push-utils.cjs +3 -0
  108. package/dist/NotificationServicesPushController/web/push-utils.cjs.map +1 -1
  109. package/dist/NotificationServicesPushController/web/push-utils.d.cts.map +1 -1
  110. package/dist/NotificationServicesPushController/web/push-utils.d.mts.map +1 -1
  111. package/dist/NotificationServicesPushController/web/push-utils.mjs +3 -0
  112. package/dist/NotificationServicesPushController/web/push-utils.mjs.map +1 -1
  113. package/dist/shared/is-onchain-notification.cjs +3 -3
  114. package/dist/shared/is-onchain-notification.cjs.map +1 -1
  115. package/dist/shared/is-onchain-notification.d.cts +2 -2
  116. package/dist/shared/is-onchain-notification.d.cts.map +1 -1
  117. package/dist/shared/is-onchain-notification.d.mts +2 -2
  118. package/dist/shared/is-onchain-notification.d.mts.map +1 -1
  119. package/dist/shared/is-onchain-notification.mjs +3 -3
  120. package/dist/shared/is-onchain-notification.mjs.map +1 -1
  121. package/dist/shared/to-raw-notification.cjs.map +1 -1
  122. package/dist/shared/to-raw-notification.mjs.map +1 -1
  123. package/package.json +1 -1
@@ -8,7 +8,7 @@ import { NOTIFICATION_NETWORK_CURRENCY_SYMBOL } from "../../NotificationServices
8
8
  * @returns A translation push message object.
9
9
  */
10
10
  export const createOnChainPushNotificationMessages = (translationKeys) => {
11
- const t = (...args) => {
11
+ const translate = (...args) => {
12
12
  const [key, ...otherArgs] = args;
13
13
  // Coerce types for the translation function
14
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -17,115 +17,115 @@ export const createOnChainPushNotificationMessages = (translationKeys) => {
17
17
  };
18
18
  return {
19
19
  erc20_sent: {
20
- title: () => t('pushPlatformNotificationsFundsSentTitle'),
21
- defaultDescription: () => t('pushPlatformNotificationsFundsSentDescriptionDefault'),
22
- getDescription: (n) => {
23
- const symbol = n?.payload?.data?.token?.symbol;
24
- const tokenAmount = n?.payload?.data?.token?.amount;
25
- const tokenDecimals = n?.payload?.data?.token?.decimals;
20
+ title: () => translate('pushPlatformNotificationsFundsSentTitle'),
21
+ defaultDescription: () => translate('pushPlatformNotificationsFundsSentDescriptionDefault'),
22
+ getDescription: (notification) => {
23
+ const symbol = notification?.payload?.data?.token?.symbol;
24
+ const tokenAmount = notification?.payload?.data?.token?.amount;
25
+ const tokenDecimals = notification?.payload?.data?.token?.decimals;
26
26
  if (!symbol || !tokenAmount || !tokenDecimals) {
27
27
  return null;
28
28
  }
29
29
  const amount = getAmount(tokenAmount, tokenDecimals, {
30
30
  shouldEllipse: true,
31
31
  });
32
- return t('pushPlatformNotificationsFundsSentDescription', amount, symbol);
32
+ return translate('pushPlatformNotificationsFundsSentDescription', amount, symbol);
33
33
  },
34
34
  },
35
35
  eth_sent: {
36
- title: () => t('pushPlatformNotificationsFundsSentTitle'),
37
- defaultDescription: () => t('pushPlatformNotificationsFundsSentDescriptionDefault'),
38
- getDescription: (n) => {
39
- const symbol = getChainSymbol(n?.payload?.chain_id);
40
- const tokenAmount = n?.payload?.data?.amount?.eth;
36
+ title: () => translate('pushPlatformNotificationsFundsSentTitle'),
37
+ defaultDescription: () => translate('pushPlatformNotificationsFundsSentDescriptionDefault'),
38
+ getDescription: (notification) => {
39
+ const symbol = getChainSymbol(notification?.payload?.chain_id);
40
+ const tokenAmount = notification?.payload?.data?.amount?.eth;
41
41
  if (!symbol || !tokenAmount) {
42
42
  return null;
43
43
  }
44
44
  const amount = formatAmount(parseFloat(tokenAmount), {
45
45
  shouldEllipse: true,
46
46
  });
47
- return t('pushPlatformNotificationsFundsSentDescription', amount, symbol);
47
+ return translate('pushPlatformNotificationsFundsSentDescription', amount, symbol);
48
48
  },
49
49
  },
50
50
  erc20_received: {
51
- title: () => t('pushPlatformNotificationsFundsReceivedTitle'),
52
- defaultDescription: () => t('pushPlatformNotificationsFundsReceivedDescriptionDefault'),
53
- getDescription: (n) => {
54
- const symbol = n?.payload?.data?.token?.symbol;
55
- const tokenAmount = n?.payload?.data?.token?.amount;
56
- const tokenDecimals = n?.payload?.data?.token?.decimals;
51
+ title: () => translate('pushPlatformNotificationsFundsReceivedTitle'),
52
+ defaultDescription: () => translate('pushPlatformNotificationsFundsReceivedDescriptionDefault'),
53
+ getDescription: (notification) => {
54
+ const symbol = notification?.payload?.data?.token?.symbol;
55
+ const tokenAmount = notification?.payload?.data?.token?.amount;
56
+ const tokenDecimals = notification?.payload?.data?.token?.decimals;
57
57
  if (!symbol || !tokenAmount || !tokenDecimals) {
58
58
  return null;
59
59
  }
60
60
  const amount = getAmount(tokenAmount, tokenDecimals, {
61
61
  shouldEllipse: true,
62
62
  });
63
- return t('pushPlatformNotificationsFundsReceivedDescription', amount, symbol);
63
+ return translate('pushPlatformNotificationsFundsReceivedDescription', amount, symbol);
64
64
  },
65
65
  },
66
66
  eth_received: {
67
- title: () => t('pushPlatformNotificationsFundsReceivedTitle'),
68
- defaultDescription: () => t('pushPlatformNotificationsFundsReceivedDescriptionDefault'),
69
- getDescription: (n) => {
70
- const symbol = getChainSymbol(n?.payload?.chain_id);
71
- const tokenAmount = n?.payload?.data?.amount?.eth;
67
+ title: () => translate('pushPlatformNotificationsFundsReceivedTitle'),
68
+ defaultDescription: () => translate('pushPlatformNotificationsFundsReceivedDescriptionDefault'),
69
+ getDescription: (notification) => {
70
+ const symbol = getChainSymbol(notification?.payload?.chain_id);
71
+ const tokenAmount = notification?.payload?.data?.amount?.eth;
72
72
  if (!symbol || !tokenAmount) {
73
73
  return null;
74
74
  }
75
75
  const amount = formatAmount(parseFloat(tokenAmount), {
76
76
  shouldEllipse: true,
77
77
  });
78
- return t('pushPlatformNotificationsFundsReceivedDescription', amount, symbol);
78
+ return translate('pushPlatformNotificationsFundsReceivedDescription', amount, symbol);
79
79
  },
80
80
  },
81
81
  metamask_swap_completed: {
82
- title: () => t('pushPlatformNotificationsSwapCompletedTitle'),
83
- defaultDescription: () => t('pushPlatformNotificationsSwapCompletedDescription'),
82
+ title: () => translate('pushPlatformNotificationsSwapCompletedTitle'),
83
+ defaultDescription: () => translate('pushPlatformNotificationsSwapCompletedDescription'),
84
84
  },
85
85
  erc721_sent: {
86
- title: () => t('pushPlatformNotificationsNftSentTitle'),
87
- defaultDescription: () => t('pushPlatformNotificationsNftSentDescription'),
86
+ title: () => translate('pushPlatformNotificationsNftSentTitle'),
87
+ defaultDescription: () => translate('pushPlatformNotificationsNftSentDescription'),
88
88
  },
89
89
  erc1155_sent: {
90
- title: () => t('pushPlatformNotificationsNftSentTitle'),
91
- defaultDescription: () => t('pushPlatformNotificationsNftSentDescription'),
90
+ title: () => translate('pushPlatformNotificationsNftSentTitle'),
91
+ defaultDescription: () => translate('pushPlatformNotificationsNftSentDescription'),
92
92
  },
93
93
  erc721_received: {
94
- title: () => t('pushPlatformNotificationsNftReceivedTitle'),
95
- defaultDescription: () => t('pushPlatformNotificationsNftReceivedDescription'),
94
+ title: () => translate('pushPlatformNotificationsNftReceivedTitle'),
95
+ defaultDescription: () => translate('pushPlatformNotificationsNftReceivedDescription'),
96
96
  },
97
97
  erc1155_received: {
98
- title: () => t('pushPlatformNotificationsNftReceivedTitle'),
99
- defaultDescription: () => t('pushPlatformNotificationsNftReceivedDescription'),
98
+ title: () => translate('pushPlatformNotificationsNftReceivedTitle'),
99
+ defaultDescription: () => translate('pushPlatformNotificationsNftReceivedDescription'),
100
100
  },
101
101
  rocketpool_stake_completed: {
102
- title: () => t('pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle'),
103
- defaultDescription: () => t('pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription'),
102
+ title: () => translate('pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle'),
103
+ defaultDescription: () => translate('pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription'),
104
104
  },
105
105
  rocketpool_unstake_completed: {
106
- title: () => t('pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle'),
107
- defaultDescription: () => t('pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription'),
106
+ title: () => translate('pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle'),
107
+ defaultDescription: () => translate('pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription'),
108
108
  },
109
109
  lido_stake_completed: {
110
- title: () => t('pushPlatformNotificationsStakingLidoStakeCompletedTitle'),
111
- defaultDescription: () => t('pushPlatformNotificationsStakingLidoStakeCompletedDescription'),
110
+ title: () => translate('pushPlatformNotificationsStakingLidoStakeCompletedTitle'),
111
+ defaultDescription: () => translate('pushPlatformNotificationsStakingLidoStakeCompletedDescription'),
112
112
  },
113
113
  lido_stake_ready_to_be_withdrawn: {
114
- title: () => t('pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle'),
115
- defaultDescription: () => t('pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription'),
114
+ title: () => translate('pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle'),
115
+ defaultDescription: () => translate('pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription'),
116
116
  },
117
117
  lido_withdrawal_requested: {
118
- title: () => t('pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle'),
119
- defaultDescription: () => t('pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription'),
118
+ title: () => translate('pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle'),
119
+ defaultDescription: () => translate('pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription'),
120
120
  },
121
121
  lido_withdrawal_completed: {
122
- title: () => t('pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle'),
123
- defaultDescription: () => t('pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription'),
122
+ title: () => translate('pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle'),
123
+ defaultDescription: () => translate('pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription'),
124
124
  },
125
125
  platform: {
126
- title: (n) => n.template.title,
127
- defaultDescription: (n) => n.template.body,
128
- getDescription: (n) => n.template.body,
126
+ title: (notification) => notification.template.title,
127
+ defaultDescription: (notification) => notification.template.body,
128
+ getDescription: (notification) => notification.template.body,
129
129
  },
130
130
  };
131
131
  };
@@ -141,15 +141,15 @@ function getChainSymbol(chainId) {
141
141
  /**
142
142
  * Creates a push notification message based on the given on-chain raw notification.
143
143
  *
144
- * @param n - processed notification.
144
+ * @param notification - processed notification.
145
145
  * @param translations - translates keys into text
146
146
  * @returns The push notification message object, or null if the notification is invalid.
147
147
  */
148
- export function createOnChainPushNotificationMessage(n, translations) {
149
- if (!n?.type) {
148
+ export function createOnChainPushNotificationMessage(notification, translations) {
149
+ if (!notification?.type) {
150
150
  return null;
151
151
  }
152
- const notificationMessage = createOnChainPushNotificationMessages(translations)[n.type];
152
+ const notificationMessage = createOnChainPushNotificationMessages(translations)[notification.type];
153
153
  if (!notificationMessage) {
154
154
  return null;
155
155
  }
@@ -157,18 +157,19 @@ export function createOnChainPushNotificationMessage(n, translations) {
157
157
  try {
158
158
  description =
159
159
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
160
- notificationMessage?.getDescription?.(n) ??
160
+ notificationMessage?.getDescription?.(notification) ??
161
161
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
162
- notificationMessage.defaultDescription?.(n) ??
162
+ notificationMessage.defaultDescription?.(notification) ??
163
163
  null;
164
164
  }
165
165
  catch {
166
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
167
- description = notificationMessage.defaultDescription?.(n) ?? null;
166
+ description =
167
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
168
+ notificationMessage.defaultDescription?.(notification) ?? null;
168
169
  }
169
170
  return {
170
171
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
171
- title: notificationMessage?.title?.(n) ?? '', // Ensure title is always a string
172
+ title: notificationMessage?.title?.(notification) ?? '', // Ensure title is always a string
172
173
  description: description ?? '', // Fallback to empty string if null
173
174
  };
174
175
  }
@@ -1 +1 @@
1
- {"version":3,"file":"get-notification-message.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/utils/get-notification-message.ts"],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WAAW,EAAE,WAAW,IAAI,EAAE,EAAE,mCAAmC;KACpE,CAAC;AACJ,CAAC","sourcesContent":["import { getAmount, formatAmount } from './get-notification-data';\nimport type {\n NOTIFICATION_CHAINS_IDS,\n Types,\n} from '../../NotificationServicesController';\nimport type { Constants } from '../../NotificationServicesController';\nimport { NOTIFICATION_NETWORK_CURRENCY_SYMBOL } from '../../NotificationServicesController/ui';\n\nexport type TranslationKeys = {\n pushPlatformNotificationsFundsSentTitle: () => string;\n pushPlatformNotificationsFundsSentDescriptionDefault: () => string;\n pushPlatformNotificationsFundsSentDescription: (\n ...args: [string, string]\n ) => string;\n pushPlatformNotificationsFundsReceivedTitle: () => string;\n pushPlatformNotificationsFundsReceivedDescriptionDefault: () => string;\n pushPlatformNotificationsFundsReceivedDescription: (\n ...args: [string, string]\n ) => string;\n pushPlatformNotificationsSwapCompletedTitle: () => string;\n pushPlatformNotificationsSwapCompletedDescription: () => string;\n pushPlatformNotificationsNftSentTitle: () => string;\n pushPlatformNotificationsNftSentDescription: () => string;\n pushPlatformNotificationsNftReceivedTitle: () => string;\n pushPlatformNotificationsNftReceivedDescription: () => string;\n pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle: () => string;\n pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription: () => string;\n pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle: () => string;\n pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription: () => string;\n pushPlatformNotificationsStakingLidoStakeCompletedTitle: () => string;\n pushPlatformNotificationsStakingLidoStakeCompletedDescription: () => string;\n pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle: () => string;\n pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription: () => string;\n};\n\ntype PushNotificationMessage = {\n title: string;\n description: string;\n ctaLink?: string;\n};\n\ntype NotificationMessage<N extends Types.INotification> = {\n title: (n: N) => string | null;\n defaultDescription: (n: N) => string | null;\n getDescription?: (n: N) => string | null;\n link?: (n: N) => string | null;\n};\n\ntype NotificationMessageDict = {\n [K in Constants.TRIGGER_TYPES]?: NotificationMessage<\n Extract<Types.INotification, { type: K }>\n >;\n};\n\n/**\n * On Chain Push Notification Messages.\n * This is a list of all the push notifications we support. Update this for synced notifications on mobile and extension\n *\n * @param translationKeys - all translations supported\n * @returns A translation push message object.\n */\nexport const createOnChainPushNotificationMessages = (\n translationKeys: TranslationKeys,\n): NotificationMessageDict => {\n type TranslationFn = <K extends keyof TranslationKeys>(\n ...args: [K, ...Parameters<TranslationKeys[K]>]\n ) => string;\n const t: TranslationFn = (...args) => {\n const [key, ...otherArgs] = args;\n\n // Coerce types for the translation function\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const fn: any = translationKeys[key];\n return fn(...otherArgs);\n };\n\n return {\n erc20_sent: {\n title: () => t('pushPlatformNotificationsFundsSentTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsFundsSentDescriptionDefault'),\n getDescription: (n) => {\n const symbol = n?.payload?.data?.token?.symbol;\n const tokenAmount = n?.payload?.data?.token?.amount;\n const tokenDecimals = n?.payload?.data?.token?.decimals;\n if (!symbol || !tokenAmount || !tokenDecimals) {\n return null;\n }\n\n const amount = getAmount(tokenAmount, tokenDecimals, {\n shouldEllipse: true,\n });\n return t(\n 'pushPlatformNotificationsFundsSentDescription',\n amount,\n symbol,\n );\n },\n },\n eth_sent: {\n title: () => t('pushPlatformNotificationsFundsSentTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsFundsSentDescriptionDefault'),\n getDescription: (n) => {\n const symbol = getChainSymbol(n?.payload?.chain_id);\n const tokenAmount = n?.payload?.data?.amount?.eth;\n if (!symbol || !tokenAmount) {\n return null;\n }\n\n const amount = formatAmount(parseFloat(tokenAmount), {\n shouldEllipse: true,\n });\n return t(\n 'pushPlatformNotificationsFundsSentDescription',\n amount,\n symbol,\n );\n },\n },\n erc20_received: {\n title: () => t('pushPlatformNotificationsFundsReceivedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsFundsReceivedDescriptionDefault'),\n getDescription: (n) => {\n const symbol = n?.payload?.data?.token?.symbol;\n const tokenAmount = n?.payload?.data?.token?.amount;\n const tokenDecimals = n?.payload?.data?.token?.decimals;\n if (!symbol || !tokenAmount || !tokenDecimals) {\n return null;\n }\n\n const amount = getAmount(tokenAmount, tokenDecimals, {\n shouldEllipse: true,\n });\n return t(\n 'pushPlatformNotificationsFundsReceivedDescription',\n amount,\n symbol,\n );\n },\n },\n eth_received: {\n title: () => t('pushPlatformNotificationsFundsReceivedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsFundsReceivedDescriptionDefault'),\n getDescription: (n) => {\n const symbol = getChainSymbol(n?.payload?.chain_id);\n const tokenAmount = n?.payload?.data?.amount?.eth;\n if (!symbol || !tokenAmount) {\n return null;\n }\n\n const amount = formatAmount(parseFloat(tokenAmount), {\n shouldEllipse: true,\n });\n return t(\n 'pushPlatformNotificationsFundsReceivedDescription',\n amount,\n symbol,\n );\n },\n },\n metamask_swap_completed: {\n title: () => t('pushPlatformNotificationsSwapCompletedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsSwapCompletedDescription'),\n },\n erc721_sent: {\n title: () => t('pushPlatformNotificationsNftSentTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsNftSentDescription'),\n },\n erc1155_sent: {\n title: () => t('pushPlatformNotificationsNftSentTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsNftSentDescription'),\n },\n erc721_received: {\n title: () => t('pushPlatformNotificationsNftReceivedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsNftReceivedDescription'),\n },\n erc1155_received: {\n title: () => t('pushPlatformNotificationsNftReceivedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsNftReceivedDescription'),\n },\n rocketpool_stake_completed: {\n title: () =>\n t('pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle'),\n defaultDescription: () =>\n t(\n 'pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription',\n ),\n },\n rocketpool_unstake_completed: {\n title: () =>\n t('pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle'),\n defaultDescription: () =>\n t(\n 'pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription',\n ),\n },\n lido_stake_completed: {\n title: () => t('pushPlatformNotificationsStakingLidoStakeCompletedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsStakingLidoStakeCompletedDescription'),\n },\n lido_stake_ready_to_be_withdrawn: {\n title: () =>\n t('pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle'),\n defaultDescription: () =>\n t(\n 'pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription',\n ),\n },\n lido_withdrawal_requested: {\n title: () =>\n t('pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription'),\n },\n lido_withdrawal_completed: {\n title: () =>\n t('pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle'),\n defaultDescription: () =>\n t('pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription'),\n },\n platform: {\n title: (n) => n.template.title,\n defaultDescription: (n) => n.template.body,\n getDescription: (n) => n.template.body,\n },\n };\n};\n\n/**\n * Retrieves the symbol associated with a given chain ID.\n *\n * @param chainId - The ID of the chain.\n * @returns The symbol associated with the chain ID, or null if not found.\n */\nfunction getChainSymbol(chainId: number) {\n return (\n NOTIFICATION_NETWORK_CURRENCY_SYMBOL[\n chainId.toString() as NOTIFICATION_CHAINS_IDS\n ] ?? null\n );\n}\n\n/**\n * Creates a push notification message based on the given on-chain raw notification.\n *\n * @param n - processed notification.\n * @param translations - translates keys into text\n * @returns The push notification message object, or null if the notification is invalid.\n */\nexport function createOnChainPushNotificationMessage(\n n: Types.INotification,\n translations: TranslationKeys,\n): PushNotificationMessage | null {\n if (!n?.type) {\n return null;\n }\n const notificationMessage =\n createOnChainPushNotificationMessages(translations)[n.type];\n\n if (!notificationMessage) {\n return null;\n }\n\n let description: string | null = null;\n try {\n description =\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n notificationMessage?.getDescription?.(n as any) ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n notificationMessage.defaultDescription?.(n as any) ??\n null;\n } catch {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n description = notificationMessage.defaultDescription?.(n as any) ?? null;\n }\n\n return {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n title: notificationMessage?.title?.(n as any) ?? '', // Ensure title is always a string\n description: description ?? '', // Fallback to empty string if null\n };\n}\n"]}
1
+ {"version":3,"file":"get-notification-message.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/utils/get-notification-message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,oCAAgC;AAMlE,OAAO,EAAE,oCAAoC,EAAE,0DAAgD;AAoD/F;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,qCAAqC,GAAG,CACnD,eAAgC,EACP,EAAE;IAI3B,MAAM,SAAS,GAAkB,CAAC,GAAG,IAAI,EAAE,EAAE;QAC3C,MAAM,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC;QAEjC,4CAA4C;QAC5C,8DAA8D;QAC9D,MAAM,EAAE,GAAQ,eAAe,CAAC,GAAG,CAAC,CAAC;QACrC,OAAO,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC;IAC1B,CAAC,CAAC;IAEF,OAAO;QACL,UAAU,EAAE;YACV,KAAK,EAAE,GAAkB,EAAE,CACzB,SAAS,CAAC,yCAAyC,CAAC;YACtD,kBAAkB,EAAE,GAAkB,EAAE,CACtC,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ACxB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,mBAAmB,GACvB,qCAAqC,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAEzE,IAAI,CAAC,mBAAmB,EAAE,CAAC;QACzB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,WAAW,GAAkB,IAAI,CAAC;IACtC,IAAI,CAAC;QACH,WAAW;YACT,8DAA8D;YAC9D,mBAAmB,EAAE,cAAc,EAAE,CAAC,YAAmB,CAAC;gBAC1D,8DAA8D;gBAC9D,mBAAmB,CAAC,kBAAkB,EAAE,CAAC,YAAmB,CAAC;gBAC7D,IAAI,CAAC;IACT,CAAC;IAAC,MAAM,CAAC;QACP,WAAW;YACT,8DAA8D;YAC9D,mBAAmB,CAAC,kBAAkB,EAAE,CAAC,YAAmB,CAAC,IAAI,IAAI,CAAC;IAC1E,CAAC;IAED,OAAO;QACL,8DAA8D;QAC9D,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,CAAC,YAAmB,CAAC,IAAI,EAAE,EAAE,kCAAkC;QAClG,WAAW,EAAE,WAAW,IAAI,EAAE,EAAE,mCAAmC;KACpE,CAAC;AACJ,CAAC","sourcesContent":["import { getAmount, formatAmount } from './get-notification-data';\nimport type {\n NOTIFICATION_CHAINS_IDS,\n Types,\n} from '../../NotificationServicesController';\nimport type { Constants } from '../../NotificationServicesController';\nimport { NOTIFICATION_NETWORK_CURRENCY_SYMBOL } from '../../NotificationServicesController/ui';\n\nexport type TranslationKeys = {\n pushPlatformNotificationsFundsSentTitle: () => string;\n pushPlatformNotificationsFundsSentDescriptionDefault: () => string;\n pushPlatformNotificationsFundsSentDescription: (\n ...args: [string, string]\n ) => string;\n pushPlatformNotificationsFundsReceivedTitle: () => string;\n pushPlatformNotificationsFundsReceivedDescriptionDefault: () => string;\n pushPlatformNotificationsFundsReceivedDescription: (\n ...args: [string, string]\n ) => string;\n pushPlatformNotificationsSwapCompletedTitle: () => string;\n pushPlatformNotificationsSwapCompletedDescription: () => string;\n pushPlatformNotificationsNftSentTitle: () => string;\n pushPlatformNotificationsNftSentDescription: () => string;\n pushPlatformNotificationsNftReceivedTitle: () => string;\n pushPlatformNotificationsNftReceivedDescription: () => string;\n pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle: () => string;\n pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription: () => string;\n pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle: () => string;\n pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription: () => string;\n pushPlatformNotificationsStakingLidoStakeCompletedTitle: () => string;\n pushPlatformNotificationsStakingLidoStakeCompletedDescription: () => string;\n pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle: () => string;\n pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle: () => string;\n pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription: () => string;\n};\n\ntype PushNotificationMessage = {\n title: string;\n description: string;\n ctaLink?: string;\n};\n\ntype NotificationMessage<TNotification extends Types.INotification> = {\n title: (notification: TNotification) => string | null;\n defaultDescription: (notification: TNotification) => string | null;\n getDescription?: (notification: TNotification) => string | null;\n link?: (notification: TNotification) => string | null;\n};\n\ntype NotificationMessageDict = {\n [TriggerType in Constants.TRIGGER_TYPES]?: NotificationMessage<\n Extract<Types.INotification, { type: TriggerType }>\n >;\n};\n\n/**\n * On Chain Push Notification Messages.\n * This is a list of all the push notifications we support. Update this for synced notifications on mobile and extension\n *\n * @param translationKeys - all translations supported\n * @returns A translation push message object.\n */\nexport const createOnChainPushNotificationMessages = (\n translationKeys: TranslationKeys,\n): NotificationMessageDict => {\n type TranslationFn = <TKey extends keyof TranslationKeys>(\n ...args: [TKey, ...Parameters<TranslationKeys[TKey]>]\n ) => string;\n const translate: TranslationFn = (...args) => {\n const [key, ...otherArgs] = args;\n\n // Coerce types for the translation function\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const fn: any = translationKeys[key];\n return fn(...otherArgs);\n };\n\n return {\n erc20_sent: {\n title: (): string | null =>\n translate('pushPlatformNotificationsFundsSentTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsFundsSentDescriptionDefault'),\n getDescription: (notification): string | null => {\n const symbol = notification?.payload?.data?.token?.symbol;\n const tokenAmount = notification?.payload?.data?.token?.amount;\n const tokenDecimals = notification?.payload?.data?.token?.decimals;\n if (!symbol || !tokenAmount || !tokenDecimals) {\n return null;\n }\n\n const amount = getAmount(tokenAmount, tokenDecimals, {\n shouldEllipse: true,\n });\n return translate(\n 'pushPlatformNotificationsFundsSentDescription',\n amount,\n symbol,\n );\n },\n },\n eth_sent: {\n title: (): string | null =>\n translate('pushPlatformNotificationsFundsSentTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsFundsSentDescriptionDefault'),\n getDescription: (notification): string | null => {\n const symbol = getChainSymbol(notification?.payload?.chain_id);\n const tokenAmount = notification?.payload?.data?.amount?.eth;\n if (!symbol || !tokenAmount) {\n return null;\n }\n\n const amount = formatAmount(parseFloat(tokenAmount), {\n shouldEllipse: true,\n });\n return translate(\n 'pushPlatformNotificationsFundsSentDescription',\n amount,\n symbol,\n );\n },\n },\n erc20_received: {\n title: (): string | null =>\n translate('pushPlatformNotificationsFundsReceivedTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsFundsReceivedDescriptionDefault'),\n getDescription: (notification): string | null => {\n const symbol = notification?.payload?.data?.token?.symbol;\n const tokenAmount = notification?.payload?.data?.token?.amount;\n const tokenDecimals = notification?.payload?.data?.token?.decimals;\n if (!symbol || !tokenAmount || !tokenDecimals) {\n return null;\n }\n\n const amount = getAmount(tokenAmount, tokenDecimals, {\n shouldEllipse: true,\n });\n return translate(\n 'pushPlatformNotificationsFundsReceivedDescription',\n amount,\n symbol,\n );\n },\n },\n eth_received: {\n title: (): string | null =>\n translate('pushPlatformNotificationsFundsReceivedTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsFundsReceivedDescriptionDefault'),\n getDescription: (notification): string | null => {\n const symbol = getChainSymbol(notification?.payload?.chain_id);\n const tokenAmount = notification?.payload?.data?.amount?.eth;\n if (!symbol || !tokenAmount) {\n return null;\n }\n\n const amount = formatAmount(parseFloat(tokenAmount), {\n shouldEllipse: true,\n });\n return translate(\n 'pushPlatformNotificationsFundsReceivedDescription',\n amount,\n symbol,\n );\n },\n },\n metamask_swap_completed: {\n title: (): string | null =>\n translate('pushPlatformNotificationsSwapCompletedTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsSwapCompletedDescription'),\n },\n erc721_sent: {\n title: (): string | null =>\n translate('pushPlatformNotificationsNftSentTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsNftSentDescription'),\n },\n erc1155_sent: {\n title: (): string | null =>\n translate('pushPlatformNotificationsNftSentTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsNftSentDescription'),\n },\n erc721_received: {\n title: (): string | null =>\n translate('pushPlatformNotificationsNftReceivedTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsNftReceivedDescription'),\n },\n erc1155_received: {\n title: (): string | null =>\n translate('pushPlatformNotificationsNftReceivedTitle'),\n defaultDescription: (): string | null =>\n translate('pushPlatformNotificationsNftReceivedDescription'),\n },\n rocketpool_stake_completed: {\n title: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle',\n ),\n defaultDescription: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription',\n ),\n },\n rocketpool_unstake_completed: {\n title: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle',\n ),\n defaultDescription: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription',\n ),\n },\n lido_stake_completed: {\n title: (): string | null =>\n translate('pushPlatformNotificationsStakingLidoStakeCompletedTitle'),\n defaultDescription: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingLidoStakeCompletedDescription',\n ),\n },\n lido_stake_ready_to_be_withdrawn: {\n title: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle',\n ),\n defaultDescription: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription',\n ),\n },\n lido_withdrawal_requested: {\n title: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle',\n ),\n defaultDescription: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription',\n ),\n },\n lido_withdrawal_completed: {\n title: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle',\n ),\n defaultDescription: (): string | null =>\n translate(\n 'pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription',\n ),\n },\n platform: {\n title: (notification): string | null => notification.template.title,\n defaultDescription: (notification): string | null =>\n notification.template.body,\n getDescription: (notification): string | null =>\n notification.template.body,\n },\n };\n};\n\n/**\n * Retrieves the symbol associated with a given chain ID.\n *\n * @param chainId - The ID of the chain.\n * @returns The symbol associated with the chain ID, or null if not found.\n */\nfunction getChainSymbol(chainId: number): string | null {\n return (\n NOTIFICATION_NETWORK_CURRENCY_SYMBOL[\n chainId.toString() as NOTIFICATION_CHAINS_IDS\n ] ?? null\n );\n}\n\n/**\n * Creates a push notification message based on the given on-chain raw notification.\n *\n * @param notification - processed notification.\n * @param translations - translates keys into text\n * @returns The push notification message object, or null if the notification is invalid.\n */\nexport function createOnChainPushNotificationMessage(\n notification: Types.INotification,\n translations: TranslationKeys,\n): PushNotificationMessage | null {\n if (!notification?.type) {\n return null;\n }\n const notificationMessage =\n createOnChainPushNotificationMessages(translations)[notification.type];\n\n if (!notificationMessage) {\n return null;\n }\n\n let description: string | null = null;\n try {\n description =\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n notificationMessage?.getDescription?.(notification as any) ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n notificationMessage.defaultDescription?.(notification as any) ??\n null;\n } catch {\n description =\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n notificationMessage.defaultDescription?.(notification as any) ?? null;\n }\n\n return {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n title: notificationMessage?.title?.(notification as any) ?? '', // Ensure title is always a string\n description: description ?? '', // Fallback to empty string if null\n };\n}\n"]}
@@ -14,6 +14,9 @@ const to_raw_notification_1 = require("../../shared/to-raw-notification.cjs");
14
14
  // eslint-disable-next-line import-x/no-mutable-exports
15
15
  exports.supportedCache = null;
16
16
  const getPushAvailability = async () => {
17
+ // Race condition is acceptable here - worst case is isSupported() is called
18
+ // multiple times during initialization, which is harmless for caching a boolean
19
+ // eslint-disable-next-line require-atomic-updates
17
20
  exports.supportedCache ?? (exports.supportedCache = await (0, sw_1.isSupported)());
18
21
  return exports.supportedCache;
19
22
  };
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";;;;;;AAIA,sCAAqD;AACrD,kDAA2D;AAC3D,8CAI+B;AAE/B,wDAA2B;AAG3B,mGAG8C;AAC9C,8EAAwE;AAMxE,2BAA2B;AAC3B,uDAAuD;AAC5C,QAAA,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAI,EAAE;IACrC,sBAAc,KAAd,sBAAc,GAAK,MAAM,IAAA,gBAAW,GAAE,EAAC;IACvC,OAAO,sBAAc,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,KAAK,EAC7B,GAAwB,EACF,EAAE;IACxB,IAAI,CAAC;QACH,OAAO,IAAA,YAA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;QAC5C,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAG,EAAE;YACvB,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAtCD,gFAsCC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport {\n isOnChainRawNotification,\n safeProcessNotification,\n} from '../../NotificationServicesController';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async () => {\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload) => {\n try {\n // MessagePayload shapes are not known\n // TODO - provide open-api unfied backend/frontend types\n // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile\n const data: unknown | null = JSON.parse(payload?.data?.data ?? 'null');\n\n if (!data) {\n return;\n }\n\n if (!isOnChainRawNotification(data)) {\n return;\n }\n\n const notificationData = toRawAPINotification(data);\n const notification = safeProcessNotification(notificationData);\n\n if (!notification) {\n return;\n }\n\n await handler?.(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = () => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n) {\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\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}) {\n return async function (env: PushNotificationEnv) {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = () => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
1
+ {"version":3,"file":"push-utils.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";;;;;;AAIA,sCAAqD;AACrD,kDAA2D;AA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AsB,GAAG,MAAM,iCAAiC,CACpE,GAAG,EACH,KAAK,EAAE,YAAY,EAAiB,EAAE;YACpC,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,uDAAuD,EACvD,YAAY,CACb,CAAC;YACF,MAAM,KAAK,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC;QAC9C,CAAC,CACF,CAAC;QACF,MAAM,UAAU,GAAG,gCAAgC,CACjD,CAAC,KAAK,EAAE,YAAY,EAAQ,EAAE;YAC5B,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,4DAA4D,EAC5D,YAAY,CACb,CAAC;YACF,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;QAC5C,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAS,EAAE;YAC7B,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAtCD,gFAsCC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport {\n isOnChainRawNotification,\n safeProcessNotification,\n} from '../../NotificationServicesController';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async (): Promise<boolean> => {\n // Race condition is acceptable here - worst case is isSupported() is called\n // multiple times during initialization, which is harmless for caching a boolean\n // eslint-disable-next-line require-atomic-updates\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload): Promise<void> => {\n try {\n // MessagePayload shapes are not known\n // TODO - provide open-api unfied backend/frontend types\n // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile\n const data: unknown | null = JSON.parse(payload?.data?.data ?? 'null');\n\n if (!data) {\n return;\n }\n\n if (!isOnChainRawNotification(data)) {\n return;\n }\n\n const notificationData = toRawAPINotification(data);\n const notification = safeProcessNotification(notificationData);\n\n if (!notification) {\n return;\n }\n\n await handler?.(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = (): void => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n): () => void {\n const clickHandler = (event: NotificationEvent): void => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = (): void =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}): (env: PushNotificationEnv) => Promise<() => void> {\n return async function (env: PushNotificationEnv): Promise<() => void> {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification): Promise<void> => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification): void => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = (): void => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAMlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAsCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AA+ED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,SAC6B,mBAAmB,yBA4BhD"}
1
+ {"version":3,"file":"push-utils.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAMlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAyCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AA+ED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,MAAM,IAAI,CAAC,CA6BpD"}
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAMlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAsCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AA+ED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,SAC6B,mBAAmB,yBA4BhD"}
1
+ {"version":3,"file":"push-utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAMlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAyCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AA+ED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,OAAO,CAAC,MAAM,IAAI,CAAC,CA6BpD"}
@@ -15,6 +15,9 @@ import { toRawAPINotification } from "../../shared/to-raw-notification.mjs";
15
15
  // eslint-disable-next-line import-x/no-mutable-exports
16
16
  export let supportedCache = null;
17
17
  const getPushAvailability = async () => {
18
+ // Race condition is acceptable here - worst case is isSupported() is called
19
+ // multiple times during initialization, which is harmless for caching a boolean
20
+ // eslint-disable-next-line require-atomic-updates
18
21
  supportedCache ?? (supportedCache = await isSupported());
19
22
  return supportedCache;
20
23
  };
@@ -1 +1 @@
1
- {"version":3,"file":"push-utils.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";;;;;;AAIA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,qBAAqB;AACrD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,2BAA2B;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACZ,8BAA8B;AAE/B,OAAO,IAAG,iBAAiB;;AAG3B,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACxB,uDAA6C;AAC9C,OAAO,EAAE,oBAAoB,EAAE,6CAAyC;AAMxE,2BAA2B;AAC3B,uDAAuD;AACvD,MAAM,CAAC,IAAI,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAI,EAAE;IACrC,cAAc,KAAd,cAAc,GAAK,MAAM,WAAW,EAAE,EAAC;IACvC,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,KAAK,EAC7B,GAAwB,EACF,EAAE;IACxB,IAAI,CAAC;QACH,OAAO,MAA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,EAAE;YACvB,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport {\n isOnChainRawNotification,\n safeProcessNotification,\n} from '../../NotificationServicesController';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async () => {\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload) => {\n try {\n // MessagePayload shapes are not known\n // TODO - provide open-api unfied backend/frontend types\n // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile\n const data: unknown | null = JSON.parse(payload?.data?.data ?? 'null');\n\n if (!data) {\n return;\n }\n\n if (!isOnChainRawNotification(data)) {\n return;\n }\n\n const notificationData = toRawAPINotification(data);\n const notification = safeProcessNotification(notificationData);\n\n if (!notification) {\n return;\n }\n\n await handler?.(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = () => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n) {\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\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}) {\n return async function (env: PushNotificationEnv) {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = () => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
1
+ {"version":3,"file":"push-utils.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-utils.ts"],"names":[],"mappings":";;;;;;AAIA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,qBAAqB;AACrD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,2BAA2B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AAU,kCAAkC,CAAC,KASlD;IACC,OAAO,KAAK,WAAW,GAAwB;QAC7C,MAAM,sBAAsB,GAAG,MAAM,iCAAiC,CACpE,GAAG,EACH,KAAK,EAAE,YAAY,EAAiB,EAAE;YACpC,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,uDAAuD,EACvD,YAAY,CACb,CAAC;YACF,MAAM,KAAK,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC;QAC9C,CAAC,CACF,CAAC;QACF,MAAM,UAAU,GAAG,gCAAgC,CACjD,CAAC,KAAK,EAAE,YAAY,EAAQ,EAAE;YAC5B,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,4DAA4D,EAC5D,YAAY,CACb,CAAC;YACF,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;QAC5C,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAS,EAAE;YAC7B,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport {\n isOnChainRawNotification,\n safeProcessNotification,\n} from '../../NotificationServicesController';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async (): Promise<boolean> => {\n // Race condition is acceptable here - worst case is isSupported() is called\n // multiple times during initialization, which is harmless for caching a boolean\n // eslint-disable-next-line require-atomic-updates\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler?: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload): Promise<void> => {\n try {\n // MessagePayload shapes are not known\n // TODO - provide open-api unfied backend/frontend types\n // TODO - we will replace the underlying Data payload with the same Notification payload used by mobile\n const data: unknown | null = JSON.parse(payload?.data?.data ?? 'null');\n\n if (!data) {\n return;\n }\n\n if (!isOnChainRawNotification(data)) {\n return;\n }\n\n const notificationData = toRawAPINotification(data);\n const notification = safeProcessNotification(notificationData);\n\n if (!notification) {\n return;\n }\n\n await handler?.(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n }\n },\n );\n\n const unsubscribe = (): void => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n): () => void {\n const clickHandler = (event: NotificationEvent): void => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = (): void =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}): (env: PushNotificationEnv) => Promise<() => void> {\n return async function (env: PushNotificationEnv): Promise<() => void> {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification): Promise<void> => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification): void => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = (): void => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
@@ -4,11 +4,11 @@ exports.isOnChainRawNotification = void 0;
4
4
  /**
5
5
  * Checks if the given value is an OnChainRawNotification object.
6
6
  *
7
- * @param n - The value to check.
7
+ * @param notification - The value to check.
8
8
  * @returns True if the value is an OnChainRawNotification object, false otherwise.
9
9
  */
10
- function isOnChainRawNotification(n) {
11
- const assumed = n;
10
+ function isOnChainRawNotification(notification) {
11
+ const assumed = notification;
12
12
  // We don't have a validation/parsing library to check all possible types of an on chain notification
13
13
  // It is safe enough just to check "some" fields, and catch any errors down the line if the shape is bad.
14
14
  const isValidEnoughToBeOnChainNotification = [
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.cjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,CAAU;IAEV,MAAM,OAAO,GAAG,CAA2B,CAAC;IAE5C,qGAAqG;IACrG,yGAAyG;IACzG,MAAM,oCAAoC,GAAG;QAC3C,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,OAAO,EAAE,IAAI;KACvB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IACxC,OAAO,oCAAoC,CAAC;AAC9C,CAAC;AAZD,4DAYC","sourcesContent":["import type { OnChainRawNotification } from '../NotificationServicesController';\n\n/**\n * Checks if the given value is an OnChainRawNotification object.\n *\n * @param n - The value to check.\n * @returns True if the value is an OnChainRawNotification object, false otherwise.\n */\nexport function isOnChainRawNotification(\n n: unknown,\n): n is OnChainRawNotification {\n const assumed = n as OnChainRawNotification;\n\n // We don't have a validation/parsing library to check all possible types of an on chain notification\n // It is safe enough just to check \"some\" fields, and catch any errors down the line if the shape is bad.\n const isValidEnoughToBeOnChainNotification = [\n assumed?.id,\n assumed?.payload?.data,\n ].every((field) => field !== undefined);\n return isValidEnoughToBeOnChainNotification;\n}\n"]}
1
+ {"version":3,"file":"is-onchain-notification.cjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,YAAqB;IAErB,MAAM,OAAO,GAAG,YAAsC,CAAC;IAEvD,qGAAqG;IACrG,yGAAyG;IACzG,MAAM,oCAAoC,GAAG;QAC3C,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,OAAO,EAAE,IAAI;KACvB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IACxC,OAAO,oCAAoC,CAAC;AAC9C,CAAC;AAZD,4DAYC","sourcesContent":["import type { OnChainRawNotification } from '../NotificationServicesController';\n\n/**\n * Checks if the given value is an OnChainRawNotification object.\n *\n * @param notification - The value to check.\n * @returns True if the value is an OnChainRawNotification object, false otherwise.\n */\nexport function isOnChainRawNotification(\n notification: unknown,\n): notification is OnChainRawNotification {\n const assumed = notification as OnChainRawNotification;\n\n // We don't have a validation/parsing library to check all possible types of an on chain notification\n // It is safe enough just to check \"some\" fields, and catch any errors down the line if the shape is bad.\n const isValidEnoughToBeOnChainNotification = [\n assumed?.id,\n assumed?.payload?.data,\n ].every((field) => field !== undefined);\n return isValidEnoughToBeOnChainNotification;\n}\n"]}
@@ -2,8 +2,8 @@ import type { OnChainRawNotification } from "../NotificationServicesController/i
2
2
  /**
3
3
  * Checks if the given value is an OnChainRawNotification object.
4
4
  *
5
- * @param n - The value to check.
5
+ * @param notification - The value to check.
6
6
  * @returns True if the value is an OnChainRawNotification object, false otherwise.
7
7
  */
8
- export declare function isOnChainRawNotification(n: unknown): n is OnChainRawNotification;
8
+ export declare function isOnChainRawNotification(notification: unknown): notification is OnChainRawNotification;
9
9
  //# sourceMappingURL=is-onchain-notification.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.d.cts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,oDAA0C;AAEhF;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,CAAC,EAAE,OAAO,GACT,CAAC,IAAI,sBAAsB,CAU7B"}
1
+ {"version":3,"file":"is-onchain-notification.d.cts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,oDAA0C;AAEhF;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,OAAO,GACpB,YAAY,IAAI,sBAAsB,CAUxC"}
@@ -2,8 +2,8 @@ import type { OnChainRawNotification } from "../NotificationServicesController/i
2
2
  /**
3
3
  * Checks if the given value is an OnChainRawNotification object.
4
4
  *
5
- * @param n - The value to check.
5
+ * @param notification - The value to check.
6
6
  * @returns True if the value is an OnChainRawNotification object, false otherwise.
7
7
  */
8
- export declare function isOnChainRawNotification(n: unknown): n is OnChainRawNotification;
8
+ export declare function isOnChainRawNotification(notification: unknown): notification is OnChainRawNotification;
9
9
  //# sourceMappingURL=is-onchain-notification.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.d.mts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,oDAA0C;AAEhF;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,CAAC,EAAE,OAAO,GACT,CAAC,IAAI,sBAAsB,CAU7B"}
1
+ {"version":3,"file":"is-onchain-notification.d.mts","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,oDAA0C;AAEhF;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,OAAO,GACpB,YAAY,IAAI,sBAAsB,CAUxC"}
@@ -1,11 +1,11 @@
1
1
  /**
2
2
  * Checks if the given value is an OnChainRawNotification object.
3
3
  *
4
- * @param n - The value to check.
4
+ * @param notification - The value to check.
5
5
  * @returns True if the value is an OnChainRawNotification object, false otherwise.
6
6
  */
7
- export function isOnChainRawNotification(n) {
8
- const assumed = n;
7
+ export function isOnChainRawNotification(notification) {
8
+ const assumed = notification;
9
9
  // We don't have a validation/parsing library to check all possible types of an on chain notification
10
10
  // It is safe enough just to check "some" fields, and catch any errors down the line if the shape is bad.
11
11
  const isValidEnoughToBeOnChainNotification = [
@@ -1 +1 @@
1
- {"version":3,"file":"is-onchain-notification.mjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,CAAU;IAEV,MAAM,OAAO,GAAG,CAA2B,CAAC;IAE5C,qGAAqG;IACrG,yGAAyG;IACzG,MAAM,oCAAoC,GAAG;QAC3C,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,OAAO,EAAE,IAAI;KACvB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IACxC,OAAO,oCAAoC,CAAC;AAC9C,CAAC","sourcesContent":["import type { OnChainRawNotification } from '../NotificationServicesController';\n\n/**\n * Checks if the given value is an OnChainRawNotification object.\n *\n * @param n - The value to check.\n * @returns True if the value is an OnChainRawNotification object, false otherwise.\n */\nexport function isOnChainRawNotification(\n n: unknown,\n): n is OnChainRawNotification {\n const assumed = n as OnChainRawNotification;\n\n // We don't have a validation/parsing library to check all possible types of an on chain notification\n // It is safe enough just to check \"some\" fields, and catch any errors down the line if the shape is bad.\n const isValidEnoughToBeOnChainNotification = [\n assumed?.id,\n assumed?.payload?.data,\n ].every((field) => field !== undefined);\n return isValidEnoughToBeOnChainNotification;\n}\n"]}
1
+ {"version":3,"file":"is-onchain-notification.mjs","sourceRoot":"","sources":["../../src/shared/is-onchain-notification.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,YAAqB;IAErB,MAAM,OAAO,GAAG,YAAsC,CAAC;IAEvD,qGAAqG;IACrG,yGAAyG;IACzG,MAAM,oCAAoC,GAAG;QAC3C,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,OAAO,EAAE,IAAI;KACvB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IACxC,OAAO,oCAAoC,CAAC;AAC9C,CAAC","sourcesContent":["import type { OnChainRawNotification } from '../NotificationServicesController';\n\n/**\n * Checks if the given value is an OnChainRawNotification object.\n *\n * @param notification - The value to check.\n * @returns True if the value is an OnChainRawNotification object, false otherwise.\n */\nexport function isOnChainRawNotification(\n notification: unknown,\n): notification is OnChainRawNotification {\n const assumed = notification as OnChainRawNotification;\n\n // We don't have a validation/parsing library to check all possible types of an on chain notification\n // It is safe enough just to check \"some\" fields, and catch any errors down the line if the shape is bad.\n const isValidEnoughToBeOnChainNotification = [\n assumed?.id,\n assumed?.payload?.data,\n ].every((field) => field !== undefined);\n return isValidEnoughToBeOnChainNotification;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"to-raw-notification.cjs","sourceRoot":"","sources":["../../src/shared/to-raw-notification.ts"],"names":[],"mappings":";;;AAOA;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAClC,IAAgC;IAEhC,MAAM,iBAAiB,GAAG,CAAC,CAAQ,EAAE,EAAE;QACrC,MAAM,IAAI,GAAW,IAAI,EAAE,iBAAiB,CAAC;QAC7C,MAAM,IAAI,KAAK,CACb,mEAAmE,IAAI,EAAE,CAC1E,CAAC;IACJ,CAAC,CAAC;IAEF,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;QACJ,CAAC;QACD,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;SACH,CAAC;IAC9B,CAAC;IAED,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,iBAAiB;SACF,CAAC;IAC/B,CAAC;IAED,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC;AA9BD,oDA8BC","sourcesContent":["import type {\n UnprocessedRawNotification,\n NormalisedAPINotification,\n OnChainRawNotification,\n PlatformRawNotification,\n} from 'src/NotificationServicesController/types/notification-api';\n\n/**\n * A true \"raw notification\" does not have some fields that exist on this type. E.g. the `type` field.\n * This is retro-actively added when we fetch notifications to be able to easily type-discriminate notifications.\n * We use this to ensure that the correct missing fields are added to the raw shapes\n *\n * @param data - raw onchain notification\n * @returns a complete raw onchain notification\n */\nexport function toRawAPINotification(\n data: UnprocessedRawNotification,\n): NormalisedAPINotification {\n const exhaustedAllCases = (_: never) => {\n const type: string = data?.notification_type;\n throw new Error(\n `toRawAPINotification - No processor found for notification kind ${type}`,\n );\n };\n\n if (data.notification_type === 'on-chain') {\n if (!data?.payload?.data?.kind) {\n throw new Error(\n 'toRawAPINotification - No kind found for on-chain notification',\n );\n }\n return {\n ...data,\n type: data.payload.data.kind,\n } as OnChainRawNotification;\n }\n\n if (data.notification_type === 'platform') {\n return {\n ...data,\n type: data.notification_type,\n } as PlatformRawNotification;\n }\n\n return exhaustedAllCases(data);\n}\n"]}
1
+ {"version":3,"file":"to-raw-notification.cjs","sourceRoot":"","sources":["../../src/shared/to-raw-notification.ts"],"names":[],"mappings":";;;AAOA;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAClC,IAAgC;IAEhC,MAAM,iBAAiB,GAAG,CAAC,CAAQ,EAAS,EAAE;QAC5C,MAAM,IAAI,GAAW,IAAI,EAAE,iBAAiB,CAAC;QAC7C,MAAM,IAAI,KAAK,CACb,mEAAmE,IAAI,EAAE,CAC1E,CAAC;IACJ,CAAC,CAAC;IAEF,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;QACJ,CAAC;QACD,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;SACH,CAAC;IAC9B,CAAC;IAED,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,iBAAiB;SACF,CAAC;IAC/B,CAAC;IAED,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC;AA9BD,oDA8BC","sourcesContent":["import type {\n UnprocessedRawNotification,\n NormalisedAPINotification,\n OnChainRawNotification,\n PlatformRawNotification,\n} from 'src/NotificationServicesController/types/notification-api';\n\n/**\n * A true \"raw notification\" does not have some fields that exist on this type. E.g. the `type` field.\n * This is retro-actively added when we fetch notifications to be able to easily type-discriminate notifications.\n * We use this to ensure that the correct missing fields are added to the raw shapes\n *\n * @param data - raw onchain notification\n * @returns a complete raw onchain notification\n */\nexport function toRawAPINotification(\n data: UnprocessedRawNotification,\n): NormalisedAPINotification {\n const exhaustedAllCases = (_: never): never => {\n const type: string = data?.notification_type;\n throw new Error(\n `toRawAPINotification - No processor found for notification kind ${type}`,\n );\n };\n\n if (data.notification_type === 'on-chain') {\n if (!data?.payload?.data?.kind) {\n throw new Error(\n 'toRawAPINotification - No kind found for on-chain notification',\n );\n }\n return {\n ...data,\n type: data.payload.data.kind,\n } as OnChainRawNotification;\n }\n\n if (data.notification_type === 'platform') {\n return {\n ...data,\n type: data.notification_type,\n } as PlatformRawNotification;\n }\n\n return exhaustedAllCases(data);\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"to-raw-notification.mjs","sourceRoot":"","sources":["../../src/shared/to-raw-notification.ts"],"names":[],"mappings":"AAOA;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAClC,IAAgC;IAEhC,MAAM,iBAAiB,GAAG,CAAC,CAAQ,EAAE,EAAE;QACrC,MAAM,IAAI,GAAW,IAAI,EAAE,iBAAiB,CAAC;QAC7C,MAAM,IAAI,KAAK,CACb,mEAAmE,IAAI,EAAE,CAC1E,CAAC;IACJ,CAAC,CAAC;IAEF,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;QACJ,CAAC;QACD,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;SACH,CAAC;IAC9B,CAAC;IAED,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,iBAAiB;SACF,CAAC;IAC/B,CAAC;IAED,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC","sourcesContent":["import type {\n UnprocessedRawNotification,\n NormalisedAPINotification,\n OnChainRawNotification,\n PlatformRawNotification,\n} from 'src/NotificationServicesController/types/notification-api';\n\n/**\n * A true \"raw notification\" does not have some fields that exist on this type. E.g. the `type` field.\n * This is retro-actively added when we fetch notifications to be able to easily type-discriminate notifications.\n * We use this to ensure that the correct missing fields are added to the raw shapes\n *\n * @param data - raw onchain notification\n * @returns a complete raw onchain notification\n */\nexport function toRawAPINotification(\n data: UnprocessedRawNotification,\n): NormalisedAPINotification {\n const exhaustedAllCases = (_: never) => {\n const type: string = data?.notification_type;\n throw new Error(\n `toRawAPINotification - No processor found for notification kind ${type}`,\n );\n };\n\n if (data.notification_type === 'on-chain') {\n if (!data?.payload?.data?.kind) {\n throw new Error(\n 'toRawAPINotification - No kind found for on-chain notification',\n );\n }\n return {\n ...data,\n type: data.payload.data.kind,\n } as OnChainRawNotification;\n }\n\n if (data.notification_type === 'platform') {\n return {\n ...data,\n type: data.notification_type,\n } as PlatformRawNotification;\n }\n\n return exhaustedAllCases(data);\n}\n"]}
1
+ {"version":3,"file":"to-raw-notification.mjs","sourceRoot":"","sources":["../../src/shared/to-raw-notification.ts"],"names":[],"mappings":"AAOA;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAClC,IAAgC;IAEhC,MAAM,iBAAiB,GAAG,CAAC,CAAQ,EAAS,EAAE;QAC5C,MAAM,IAAI,GAAW,IAAI,EAAE,iBAAiB,CAAC;QAC7C,MAAM,IAAI,KAAK,CACb,mEAAmE,IAAI,EAAE,CAC1E,CAAC;IACJ,CAAC,CAAC;IAEF,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;QACJ,CAAC;QACD,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;SACH,CAAC;IAC9B,CAAC;IAED,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC1C,OAAO;YACL,GAAG,IAAI;YACP,IAAI,EAAE,IAAI,CAAC,iBAAiB;SACF,CAAC;IAC/B,CAAC;IAED,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC","sourcesContent":["import type {\n UnprocessedRawNotification,\n NormalisedAPINotification,\n OnChainRawNotification,\n PlatformRawNotification,\n} from 'src/NotificationServicesController/types/notification-api';\n\n/**\n * A true \"raw notification\" does not have some fields that exist on this type. E.g. the `type` field.\n * This is retro-actively added when we fetch notifications to be able to easily type-discriminate notifications.\n * We use this to ensure that the correct missing fields are added to the raw shapes\n *\n * @param data - raw onchain notification\n * @returns a complete raw onchain notification\n */\nexport function toRawAPINotification(\n data: UnprocessedRawNotification,\n): NormalisedAPINotification {\n const exhaustedAllCases = (_: never): never => {\n const type: string = data?.notification_type;\n throw new Error(\n `toRawAPINotification - No processor found for notification kind ${type}`,\n );\n };\n\n if (data.notification_type === 'on-chain') {\n if (!data?.payload?.data?.kind) {\n throw new Error(\n 'toRawAPINotification - No kind found for on-chain notification',\n );\n }\n return {\n ...data,\n type: data.payload.data.kind,\n } as OnChainRawNotification;\n }\n\n if (data.notification_type === 'platform') {\n return {\n ...data,\n type: data.notification_type,\n } as PlatformRawNotification;\n }\n\n return exhaustedAllCases(data);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/notification-services-controller",
3
- "version": "21.0.0-preview-565dfca2",
3
+ "version": "21.0.0-preview-3fa1672",
4
4
  "description": "Manages New MetaMask decentralized Notification system",
5
5
  "keywords": [
6
6
  "MetaMask",