@aws-amplify/notifications 2.0.1-console-preview.5f040a2.0 → 2.0.1-console-preview.23dc225.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/lib/common/AWSPinpointProviderCommon/index.d.ts +0 -1
  2. package/lib/common/AWSPinpointProviderCommon/index.js +14 -19
  3. package/lib/common/AWSPinpointProviderCommon/index.js.map +1 -1
  4. package/lib/common/eventListeners/index.d.ts +2 -2
  5. package/lib/common/eventListeners/index.js +3 -1
  6. package/lib/common/eventListeners/index.js.map +1 -1
  7. package/lib/common/eventListeners/types.d.ts +3 -0
  8. package/lib/common/index.d.ts +1 -1
  9. package/lib/inAppMessaging/errors/validation.d.ts +2 -1
  10. package/lib/inAppMessaging/errors/validation.js +5 -0
  11. package/lib/inAppMessaging/errors/validation.js.map +1 -1
  12. package/lib/inAppMessaging/index.d.ts +1 -1
  13. package/lib/inAppMessaging/index.js +8 -0
  14. package/lib/inAppMessaging/index.js.map +1 -1
  15. package/lib/inAppMessaging/providers/index.d.ts +1 -1
  16. package/lib/inAppMessaging/providers/index.js +8 -0
  17. package/lib/inAppMessaging/providers/index.js.map +1 -1
  18. package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.d.ts +26 -0
  19. package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js +69 -0
  20. package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js.map +1 -0
  21. package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.d.ts +1 -4
  22. package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.js +5 -5
  23. package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.js.map +1 -1
  24. package/lib/inAppMessaging/providers/pinpoint/apis/index.d.ts +8 -0
  25. package/lib/inAppMessaging/providers/pinpoint/apis/index.js +16 -0
  26. package/lib/inAppMessaging/providers/pinpoint/apis/index.js.map +1 -1
  27. package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.d.ts +12 -0
  28. package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js +61 -0
  29. package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js.map +1 -0
  30. package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.d.ts +16 -0
  31. package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js +27 -0
  32. package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js.map +1 -0
  33. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.d.ts +18 -0
  34. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js +27 -0
  35. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js.map +1 -0
  36. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.d.ts +18 -0
  37. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js +27 -0
  38. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js.map +1 -0
  39. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.d.ts +18 -0
  40. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js +27 -0
  41. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js.map +1 -0
  42. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.d.ts +18 -0
  43. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js +27 -0
  44. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js.map +1 -0
  45. package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.d.ts +32 -0
  46. package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js +66 -0
  47. package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js.map +1 -0
  48. package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.d.ts +1 -3
  49. package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.js +20 -18
  50. package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.js.map +1 -1
  51. package/lib/inAppMessaging/providers/pinpoint/index.d.ts +1 -1
  52. package/lib/inAppMessaging/providers/pinpoint/index.js +8 -0
  53. package/lib/inAppMessaging/providers/pinpoint/index.js.map +1 -1
  54. package/lib/inAppMessaging/providers/pinpoint/types/index.d.ts +2 -1
  55. package/lib/inAppMessaging/providers/pinpoint/types/index.js +2 -0
  56. package/lib/inAppMessaging/providers/pinpoint/types/index.js.map +1 -1
  57. package/lib/inAppMessaging/providers/pinpoint/types/inputs.d.ts +33 -2
  58. package/lib/inAppMessaging/providers/pinpoint/types/outputs.d.ts +17 -0
  59. package/lib/inAppMessaging/providers/pinpoint/types/outputs.js +5 -0
  60. package/lib/inAppMessaging/providers/pinpoint/types/outputs.js.map +1 -0
  61. package/lib/inAppMessaging/providers/pinpoint/types/types.d.ts +4 -1
  62. package/lib/inAppMessaging/providers/pinpoint/types/types.js +6 -6
  63. package/lib/inAppMessaging/providers/pinpoint/types/types.js.map +1 -1
  64. package/lib/inAppMessaging/providers/pinpoint/utils/helpers.d.ts +17 -0
  65. package/lib/inAppMessaging/providers/pinpoint/utils/helpers.js +260 -0
  66. package/lib/inAppMessaging/providers/pinpoint/utils/helpers.js.map +1 -0
  67. package/lib/inAppMessaging/providers/pinpoint/utils/index.d.ts +1 -0
  68. package/lib/inAppMessaging/providers/pinpoint/utils/index.js +4 -0
  69. package/lib/inAppMessaging/providers/pinpoint/utils/index.js.map +1 -1
  70. package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.d.ts +6 -0
  71. package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js +247 -0
  72. package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js.map +1 -0
  73. package/lib/inAppMessaging/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
  74. package/lib/inAppMessaging/types/index.d.ts +1 -1
  75. package/lib/inAppMessaging/utils/index.d.ts +1 -0
  76. package/lib/inAppMessaging/utils/index.js +9 -0
  77. package/lib/inAppMessaging/utils/index.js.map +1 -0
  78. package/lib/inAppMessaging/utils/statusHelpers.d.ts +13 -0
  79. package/lib/inAppMessaging/utils/statusHelpers.js +25 -0
  80. package/lib/inAppMessaging/utils/statusHelpers.js.map +1 -0
  81. package/lib-esm/common/AWSPinpointProviderCommon/index.d.ts +0 -1
  82. package/lib-esm/common/AWSPinpointProviderCommon/index.js +15 -20
  83. package/lib-esm/common/AWSPinpointProviderCommon/index.js.map +1 -1
  84. package/lib-esm/common/eventListeners/index.d.ts +2 -2
  85. package/lib-esm/common/eventListeners/index.js +3 -1
  86. package/lib-esm/common/eventListeners/index.js.map +1 -1
  87. package/lib-esm/common/eventListeners/types.d.ts +3 -0
  88. package/lib-esm/common/index.d.ts +1 -1
  89. package/lib-esm/inAppMessaging/errors/validation.d.ts +2 -1
  90. package/lib-esm/inAppMessaging/errors/validation.js +5 -0
  91. package/lib-esm/inAppMessaging/errors/validation.js.map +1 -1
  92. package/lib-esm/inAppMessaging/index.d.ts +1 -1
  93. package/lib-esm/inAppMessaging/index.js +1 -1
  94. package/lib-esm/inAppMessaging/index.js.map +1 -1
  95. package/lib-esm/inAppMessaging/providers/index.d.ts +1 -1
  96. package/lib-esm/inAppMessaging/providers/index.js +1 -1
  97. package/lib-esm/inAppMessaging/providers/index.js.map +1 -1
  98. package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.d.ts +26 -0
  99. package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js +66 -0
  100. package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js.map +1 -0
  101. package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.d.ts +1 -4
  102. package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.js +5 -5
  103. package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.js.map +1 -1
  104. package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.d.ts +8 -0
  105. package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.js +8 -0
  106. package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.js.map +1 -1
  107. package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.d.ts +12 -0
  108. package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js +57 -0
  109. package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js.map +1 -0
  110. package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.d.ts +16 -0
  111. package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js +24 -0
  112. package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js.map +1 -0
  113. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.d.ts +18 -0
  114. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js +24 -0
  115. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js.map +1 -0
  116. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.d.ts +18 -0
  117. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js +24 -0
  118. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js.map +1 -0
  119. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.d.ts +18 -0
  120. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js +24 -0
  121. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js.map +1 -0
  122. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.d.ts +18 -0
  123. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js +24 -0
  124. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js.map +1 -0
  125. package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.d.ts +32 -0
  126. package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js +63 -0
  127. package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js.map +1 -0
  128. package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.d.ts +1 -3
  129. package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.js +20 -18
  130. package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.js.map +1 -1
  131. package/lib-esm/inAppMessaging/providers/pinpoint/index.d.ts +1 -1
  132. package/lib-esm/inAppMessaging/providers/pinpoint/index.js +1 -1
  133. package/lib-esm/inAppMessaging/providers/pinpoint/index.js.map +1 -1
  134. package/lib-esm/inAppMessaging/providers/pinpoint/types/index.d.ts +2 -1
  135. package/lib-esm/inAppMessaging/providers/pinpoint/types/index.js +1 -0
  136. package/lib-esm/inAppMessaging/providers/pinpoint/types/index.js.map +1 -1
  137. package/lib-esm/inAppMessaging/providers/pinpoint/types/inputs.d.ts +33 -2
  138. package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.d.ts +17 -0
  139. package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.js +3 -0
  140. package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.js.map +1 -0
  141. package/lib-esm/inAppMessaging/providers/pinpoint/types/types.d.ts +4 -1
  142. package/lib-esm/inAppMessaging/providers/pinpoint/types/types.js +6 -6
  143. package/lib-esm/inAppMessaging/providers/pinpoint/types/types.js.map +1 -1
  144. package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.d.ts +17 -0
  145. package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.js +258 -0
  146. package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.js.map +1 -0
  147. package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.d.ts +1 -0
  148. package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.js +1 -0
  149. package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.js.map +1 -1
  150. package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.d.ts +6 -0
  151. package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js +242 -0
  152. package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js.map +1 -0
  153. package/lib-esm/inAppMessaging/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
  154. package/lib-esm/inAppMessaging/types/index.d.ts +1 -1
  155. package/lib-esm/inAppMessaging/utils/index.d.ts +1 -0
  156. package/lib-esm/inAppMessaging/utils/index.js +4 -0
  157. package/lib-esm/inAppMessaging/utils/index.js.map +1 -0
  158. package/lib-esm/inAppMessaging/utils/statusHelpers.d.ts +13 -0
  159. package/lib-esm/inAppMessaging/utils/statusHelpers.js +22 -0
  160. package/lib-esm/inAppMessaging/utils/statusHelpers.js.map +1 -0
  161. package/package.json +5 -6
  162. package/src/common/AWSPinpointProviderCommon/index.ts +9 -14
  163. package/src/common/eventListeners/index.ts +5 -3
  164. package/src/common/eventListeners/types.ts +4 -0
  165. package/src/common/index.ts +5 -1
  166. package/src/inAppMessaging/errors/validation.ts +6 -0
  167. package/src/inAppMessaging/index.ts +12 -1
  168. package/src/inAppMessaging/providers/index.ts +12 -1
  169. package/src/inAppMessaging/providers/pinpoint/apis/dispatchEvent.ts +63 -0
  170. package/src/inAppMessaging/providers/pinpoint/apis/identifyUser.ts +3 -4
  171. package/src/inAppMessaging/providers/pinpoint/apis/index.ts +8 -0
  172. package/src/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.ts +64 -0
  173. package/src/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.ts +28 -0
  174. package/src/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.ts +29 -0
  175. package/src/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.ts +29 -0
  176. package/src/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.ts +29 -0
  177. package/src/inAppMessaging/providers/pinpoint/apis/onMessageReceived.ts +29 -0
  178. package/src/inAppMessaging/providers/pinpoint/apis/setConflictHandler.ts +68 -0
  179. package/src/inAppMessaging/providers/pinpoint/apis/syncMessages.ts +6 -5
  180. package/src/inAppMessaging/providers/pinpoint/index.ts +12 -1
  181. package/src/inAppMessaging/providers/pinpoint/types/index.ts +14 -1
  182. package/src/inAppMessaging/providers/pinpoint/types/inputs.ts +49 -2
  183. package/src/inAppMessaging/providers/pinpoint/types/outputs.ts +24 -0
  184. package/src/inAppMessaging/providers/pinpoint/types/types.ts +9 -1
  185. package/src/inAppMessaging/providers/pinpoint/utils/helpers.ts +325 -0
  186. package/src/inAppMessaging/providers/pinpoint/utils/index.ts +6 -0
  187. package/src/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.ts +196 -0
  188. package/src/inAppMessaging/types/index.ts +7 -1
  189. package/src/inAppMessaging/utils/index.ts +8 -0
  190. package/src/inAppMessaging/utils/statusHelpers.ts +32 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/notifications",
3
- "version": "2.0.1-console-preview.5f040a2.0+5f040a2",
3
+ "version": "2.0.1-console-preview.23dc225.0+23dc225",
4
4
  "description": "Notifications category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -92,19 +92,18 @@
92
92
  "src"
93
93
  ],
94
94
  "dependencies": {
95
- "lodash": "^4.17.21",
96
- "uuid": "^9.0.0"
95
+ "lodash": "^4.17.21"
97
96
  },
98
97
  "peerDependencies": {
99
- "@aws-amplify/core": "6.0.1-console-preview.5f040a2.0+5f040a2"
98
+ "@aws-amplify/core": "6.0.1-console-preview.23dc225.0+23dc225"
100
99
  },
101
100
  "devDependencies": {
102
- "@aws-amplify/core": "6.0.1-console-preview.5f040a2.0+5f040a2",
101
+ "@aws-amplify/core": "6.0.1-console-preview.23dc225.0+23dc225",
103
102
  "@babel/core": "7.15.5",
104
103
  "@babel/plugin-proposal-class-properties": "^7.0.0",
105
104
  "@babel/plugin-proposal-private-methods": "^7.0.0",
106
105
  "@babel/plugin-proposal-private-property-in-object": "^7.0.0",
107
106
  "metro-react-native-babel-preset": "^0.66.2"
108
107
  },
109
- "gitHead": "5f040a29ecebf29374ae9b58abc661f45143ac4f"
108
+ "gitHead": "23dc22531b963319f2b421f6f7c6cf867635b20e"
110
109
  }
@@ -3,12 +3,12 @@
3
3
 
4
4
  import {
5
5
  Category,
6
- ClientDevice,
7
6
  ConsoleLogger,
8
7
  CustomUserAgentDetails,
9
8
  getAmplifyUserAgent,
10
9
  InAppMessagingAction,
11
10
  PushNotificationAction,
11
+ amplifyUuid,
12
12
  } from '@aws-amplify/core/internals/utils';
13
13
  import { Cache, fetchAuthSession } from '@aws-amplify/core';
14
14
 
@@ -19,7 +19,6 @@ import {
19
19
  updateEndpoint,
20
20
  UpdateEndpointInput,
21
21
  } from '@aws-amplify/core/internals/aws-clients/pinpoint';
22
- import { v4 as uuid } from 'uuid';
23
22
 
24
23
  import {
25
24
  NotificationsCategory,
@@ -35,15 +34,12 @@ export default abstract class AWSPinpointProviderCommon
35
34
  static category: NotificationsCategory = 'Notifications';
36
35
  static providerName = 'AWSPinpoint';
37
36
 
38
- protected clientInfo;
39
37
  protected config: Record<string, any> = {};
40
38
  protected endpointInitialized = false;
41
39
  protected initialized = false;
42
40
  protected logger: ConsoleLogger;
43
41
 
44
42
  constructor(logger) {
45
- // this.config = { storage: new StorageHelper().getStorage() };
46
- this.clientInfo = ClientDevice.clientInfo() ?? {};
47
43
  this.logger = logger;
48
44
  }
49
45
 
@@ -140,7 +136,7 @@ export default abstract class AWSPinpointProviderCommon
140
136
  [endpointId]: {
141
137
  Endpoint: {},
142
138
  Events: {
143
- [uuid()]: event,
139
+ [amplifyUuid()]: event,
144
140
  },
145
141
  },
146
142
  },
@@ -185,14 +181,13 @@ export default abstract class AWSPinpointProviderCommon
185
181
  try {
186
182
  const { address, attributes, demographic, location, metrics, optOut } =
187
183
  userInfo ?? {};
188
- const { appVersion, make, model, platform, version } = this.clientInfo;
189
184
  // Create the UpdateEndpoint input, prioritizing passed in user info and falling back to
190
185
  // defaults (if any) obtained from the config
191
186
  const input: UpdateEndpointInput = {
192
187
  ApplicationId: appId,
193
188
  EndpointId: endpointId,
194
189
  EndpointRequest: {
195
- RequestId: uuid(),
190
+ RequestId: amplifyUuid(),
196
191
  EffectiveDate: new Date().toISOString(),
197
192
  ChannelType: endpointInfo.channelType,
198
193
  Address: address ?? endpointInfo.address,
@@ -201,11 +196,11 @@ export default abstract class AWSPinpointProviderCommon
201
196
  ...attributes,
202
197
  },
203
198
  Demographic: {
204
- AppVersion: appVersion,
205
- Make: make,
206
- Model: model,
207
- ModelVersion: version,
208
- Platform: platform,
199
+ AppVersion: null,
200
+ Make: null,
201
+ Model: null,
202
+ ModelVersion: null,
203
+ Platform: null,
209
204
  ...this.transferKeyToUpperCase({
210
205
  ...endpointInfo.demographic,
211
206
  ...demographic,
@@ -252,7 +247,7 @@ export default abstract class AWSPinpointProviderCommon
252
247
  return cachedEndpointId;
253
248
  }
254
249
  // Otherwise, generate a new ID and store it in long-lived cache before returning it
255
- const endpointId = uuid();
250
+ const endpointId = amplifyUuid();
256
251
  // Set a longer TTL to avoid endpoint id being deleted after the default TTL (3 days)
257
252
  // Also set its priority to the highest to reduce its chance of being deleted when cache is full
258
253
  const ttl = 1000 * 60 * 60 * 24 * 365 * 100; // 100 years
@@ -1,7 +1,7 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { EventListener, EventType } from './types';
4
+ import { EventListener, EventListenerRemover, EventType } from './types';
5
5
 
6
6
  const eventListeners: Record<string, Set<EventListener<Function>>> = {};
7
7
 
@@ -28,7 +28,7 @@ export const notifyEventListenersAndAwaitHandlers = (
28
28
  export const addEventListener = <EventHandler extends Function>(
29
29
  type: EventType,
30
30
  handler: EventHandler
31
- ): EventListener<EventHandler> => {
31
+ ): EventListenerRemover => {
32
32
  // If there is no listener set for the event type, just create it
33
33
  if (!eventListeners[type]) {
34
34
  eventListeners[type] = new Set<EventListener<EventHandler>>();
@@ -40,5 +40,7 @@ export const addEventListener = <EventHandler extends Function>(
40
40
  },
41
41
  };
42
42
  eventListeners[type].add(listener);
43
- return listener;
43
+ return {
44
+ remove: () => listener.remove(),
45
+ };
44
46
  };
@@ -10,3 +10,7 @@ export interface EventListener<EventHandler extends Function> {
10
10
  }
11
11
 
12
12
  export type EventType = InAppMessageInteractionEvent | PushNotificationEvent;
13
+
14
+ export type EventListenerRemover = {
15
+ remove: () => void;
16
+ };
@@ -7,4 +7,8 @@ export {
7
7
  notifyEventListeners,
8
8
  notifyEventListenersAndAwaitHandlers,
9
9
  } from './eventListeners';
10
- export { EventListener, EventType } from './eventListeners/types';
10
+ export {
11
+ EventListener,
12
+ EventType,
13
+ EventListenerRemover,
14
+ } from './eventListeners/types';
@@ -8,6 +8,7 @@ export enum InAppMessagingValidationErrorCode {
8
8
  NoCredentials = 'NoCredentials',
9
9
  NoRegion = 'NoRegion',
10
10
  NoEndpointId = 'NoEndpointId',
11
+ NotInitialized = 'NotInitialized',
11
12
  }
12
13
 
13
14
  export const validationErrorMap: AmplifyErrorMap<InAppMessagingValidationErrorCode> =
@@ -24,4 +25,9 @@ export const validationErrorMap: AmplifyErrorMap<InAppMessagingValidationErrorCo
24
25
  [InAppMessagingValidationErrorCode.NoEndpointId]: {
25
26
  message: 'Could not find or create EndpointId.',
26
27
  },
28
+ [InAppMessagingValidationErrorCode.NotInitialized]: {
29
+ message: 'In-app messaging has not been initialized.',
30
+ recoverySuggestion:
31
+ 'Please make sure to first call `initializePushNotifications`.',
32
+ },
27
33
  };
@@ -1,4 +1,15 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- export { identifyUser, syncMessages } from './providers/pinpoint';
4
+ export {
5
+ identifyUser,
6
+ syncMessages,
7
+ dispatchEvent,
8
+ setConflictHandler,
9
+ initializeInAppMessaging,
10
+ onMessageReceived,
11
+ onMessageDisplayed,
12
+ onMessageDismissed,
13
+ onMessageActionTaken,
14
+ notifyMessageInteraction,
15
+ } from './providers/pinpoint';
@@ -1,4 +1,15 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- export { identifyUser, syncMessages } from './pinpoint/apis';
4
+ export {
5
+ identifyUser,
6
+ syncMessages,
7
+ dispatchEvent,
8
+ setConflictHandler,
9
+ initializeInAppMessaging,
10
+ onMessageReceived,
11
+ onMessageDisplayed,
12
+ onMessageDismissed,
13
+ onMessageActionTaken,
14
+ notifyMessageInteraction,
15
+ } from './pinpoint/apis';
@@ -0,0 +1,63 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ PINPOINT_KEY_PREFIX,
6
+ STORAGE_KEY_SUFFIX,
7
+ processInAppMessages,
8
+ } from '../utils';
9
+ import { InAppMessage } from '../../../types';
10
+ import flatten from 'lodash/flatten';
11
+ import { defaultStorage } from '@aws-amplify/core';
12
+ import { notifyEventListeners } from '../../../../common';
13
+ import { assertServiceError } from '../../../errors';
14
+ import { DispatchEventInput } from '../types';
15
+ import { syncMessages } from './syncMessages';
16
+ import { conflictHandler, setConflictHandler } from './setConflictHandler';
17
+ import { assertIsInitialized } from '../../../utils';
18
+
19
+ /**
20
+ * Triggers an In-App message to be displayed. Use this after your campaigns have been synced to the device using
21
+ * {@link syncMessages}. Based on the messages synced and the event passed to this API, it triggers the display
22
+ * of the In-App message that meets the criteria.
23
+ *
24
+ * @remark
25
+ * If an event would trigger multiple messages, the message closest to expiry will be chosen by default.
26
+ * To change this behavior, you can use the {@link setConflictHandler} API to provide
27
+ * your own logic for resolving message conflicts.
28
+ *
29
+ * @param DispatchEventInput The input object that holds the event to be dispatched.
30
+ * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
31
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
32
+ * @throws service exceptions - Thrown when the underlying Pinpoint service returns an error.
33
+ * @returns A promise that will resolve when the operation is complete.
34
+ * @example
35
+ * ```ts
36
+ * // Sync message before disptaching an event
37
+ * await syncMessages();
38
+ *
39
+ * // Dispatch an event
40
+ * await dispatchEvent({ name: 'test_event' });
41
+ * ```
42
+ */
43
+ export async function dispatchEvent(input: DispatchEventInput): Promise<void> {
44
+ assertIsInitialized();
45
+ try {
46
+ const key = `${PINPOINT_KEY_PREFIX}${STORAGE_KEY_SUFFIX}`;
47
+ const cachedMessages = await defaultStorage.getItem(key);
48
+ const messages: InAppMessage[] = await processInAppMessages(
49
+ cachedMessages ? JSON.parse(cachedMessages) : [],
50
+ input
51
+ );
52
+ const flattenedMessages = flatten(messages);
53
+ if (flattenedMessages.length > 0) {
54
+ notifyEventListeners(
55
+ 'messageReceived',
56
+ conflictHandler(flattenedMessages)
57
+ );
58
+ }
59
+ } catch (error) {
60
+ assertServiceError(error);
61
+ throw error;
62
+ }
63
+ }
@@ -12,6 +12,7 @@ import {
12
12
  resolveCredentials,
13
13
  } from '../utils';
14
14
  import { IdentifyUserInput } from '../types';
15
+ import { assertIsInitialized } from '../../../utils';
15
16
 
16
17
  /**
17
18
  * Sends information about a user to Pinpoint. Sending user information allows you to associate a user to their user
@@ -20,13 +21,10 @@ import { IdentifyUserInput } from '../types';
20
21
  *
21
22
  * @param {IdentifyUserParameters} params The input object used to construct requests sent to Pinpoint's UpdateEndpoint
22
23
  * API.
23
- *
24
24
  * @throws service: {@link UpdateEndpointException} - Thrown when the underlying Pinpoint service returns an error.
25
25
  * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
26
- * configuration is incorrect.
27
- *
26
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
28
27
  * @returns A promise that will resolve when the operation is complete.
29
- *
30
28
  * @example
31
29
  * ```ts
32
30
  * // Identify a user with Pinpoint
@@ -72,6 +70,7 @@ export const identifyUser = async ({
72
70
  userProfile,
73
71
  options,
74
72
  }: IdentifyUserInput): Promise<void> => {
73
+ assertIsInitialized();
75
74
  const { credentials, identityId } = await resolveCredentials();
76
75
  const { appId, region } = resolveConfig();
77
76
  const { address, optOut, userAttributes } = options?.serviceOptions ?? {};
@@ -3,3 +3,11 @@
3
3
 
4
4
  export { identifyUser } from './identifyUser';
5
5
  export { syncMessages } from './syncMessages';
6
+ export { dispatchEvent } from './dispatchEvent';
7
+ export { setConflictHandler } from './setConflictHandler';
8
+ export { initializeInAppMessaging } from './initializeInAppMessaging';
9
+ export { onMessageReceived } from './onMessageReceived';
10
+ export { onMessageDismissed } from './onMessageDismissed';
11
+ export { onMessageDisplayed } from './onMessageDisplayed';
12
+ export { onMessageActionTaken } from './onMessageActionTaken';
13
+ export { notifyMessageInteraction } from './notifyMessageInteraction';
@@ -0,0 +1,64 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import SessionTracker from '../../../sessionTracker';
5
+ import { InAppMessage, InAppMessagingEvent } from '../../../types';
6
+ import { addEventListener } from '../../../../common';
7
+ import { recordAnalyticsEvent } from '../utils/helpers';
8
+ import { PinpointMessageEvent } from '../types';
9
+ import { Hub, HubCapsule } from '@aws-amplify/core';
10
+ import { dispatchEvent } from './dispatchEvent';
11
+ import { incrementMessageCounts, sessionStateChangeHandler } from '../utils';
12
+ import { isInitialized, initialize } from '../../../utils';
13
+
14
+ /**
15
+ * Initialize and set up in-app messaging category. This API needs to be called to enable other InAppMessaging APIs.
16
+ *
17
+ * @remarks
18
+ * Make sure to call this early in your app at the root entry point after configuring Amplify.
19
+ * @example
20
+ * ```ts
21
+ * Amplify.configure(config);
22
+ * initializeInAppMessaging();
23
+ * ```
24
+ */
25
+ export function initializeInAppMessaging(): void {
26
+ if (isInitialized()) {
27
+ return;
28
+ }
29
+ // set up the session tracker and start it
30
+ const sessionTracker = new SessionTracker(sessionStateChangeHandler);
31
+ sessionTracker.start();
32
+
33
+ // wire up default Pinpoint message event handling
34
+ addEventListener('messageDisplayed', (message: InAppMessage) => {
35
+ console.log('Recording message displayed event');
36
+ recordAnalyticsEvent(PinpointMessageEvent.MESSAGE_DISPLAYED, message);
37
+ incrementMessageCounts(message.id);
38
+ });
39
+ addEventListener('messageDismissed', (message: InAppMessage) => {
40
+ recordAnalyticsEvent(PinpointMessageEvent.MESSAGE_DISMISSED, message);
41
+ });
42
+ addEventListener('messageActionTaken', (message: InAppMessage) => {
43
+ recordAnalyticsEvent(PinpointMessageEvent.MESSAGE_ACTION_TAKEN, message);
44
+ });
45
+
46
+ // listen to analytics hub events
47
+ Hub.listen('analytics', analyticsListener);
48
+
49
+ initialize();
50
+ }
51
+
52
+ function analyticsListener({
53
+ payload,
54
+ }: HubCapsule<string, { event: string; data: InAppMessagingEvent }>) {
55
+ const { event, data } = payload;
56
+ switch (event) {
57
+ case 'record': {
58
+ dispatchEvent(data);
59
+ break;
60
+ }
61
+ default:
62
+ break;
63
+ }
64
+ }
@@ -0,0 +1,28 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { notifyEventListeners } from '../../../../common';
5
+ import { assertIsInitialized } from '../../../utils';
6
+ import { NotifyMessageInteractionInput } from '../types/inputs';
7
+
8
+ /**
9
+ * Notifies the respective listener of the specified type with the message given.
10
+ *
11
+ * @param {NotifyMessageInteractionInput} input - The input object that holds the type and message.
12
+ * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
13
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
14
+ * @example
15
+ * ```ts
16
+ * onMessageRecieved((message) => {
17
+ * // Show end users the In-App message and notify event listeners
18
+ * notifyMessageInteraction({ type: 'messageDisplayed', message });
19
+ * });
20
+ * ```
21
+ */
22
+ export function notifyMessageInteraction({
23
+ type,
24
+ message,
25
+ }: NotifyMessageInteractionInput): void {
26
+ assertIsInitialized();
27
+ notifyEventListeners(type, message);
28
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { addEventListener } from '../../../../common';
5
+ import { assertIsInitialized } from '../../../utils';
6
+ import { OnMessageActionTakenInput } from '../types/inputs';
7
+ import { OnMessageActionTakenOutput } from '../types/outputs';
8
+
9
+ /**
10
+ * Registers a callback that will be invoked on `messageActionTaken` events.
11
+ *
12
+ * @param {OnMessageActionTakenInput} input - The input object that holds the callback handler.
13
+ * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
14
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
15
+ * @returns {OnMessageActionTakenOutput} - An object that holds a remove method to stop listening to events.
16
+ * @example
17
+ * ```ts
18
+ * onMessageActionTaken((message) => {
19
+ * // use the message
20
+ * console.log(message.id);
21
+ * });
22
+ * ```
23
+ */
24
+ export function onMessageActionTaken(
25
+ input: OnMessageActionTakenInput
26
+ ): OnMessageActionTakenOutput {
27
+ assertIsInitialized();
28
+ return addEventListener('messageActionTaken', input);
29
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { addEventListener } from '../../../../common';
5
+ import { OnMessageDismissedOutput } from '../types/outputs';
6
+ import { OnMessageDismissedInput } from '../types/inputs';
7
+ import { assertIsInitialized } from '../../../utils';
8
+
9
+ /**
10
+ * Registers a callback that will be invoked on `messageDismissed` events.
11
+ *
12
+ * @param {OnMessageDismissedInput} input - The input object that holds the callback handler.
13
+ * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
14
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
15
+ * @returns {OnMessageDismissedOutput} - An object that holds a remove method to stop listening to events.
16
+ * @example
17
+ * ```ts
18
+ * onMessageDismissed((message) => {
19
+ * // use the message
20
+ * console.log(message.id);
21
+ * });
22
+ * ```
23
+ */
24
+ export function onMessageDismissed(
25
+ input: OnMessageDismissedInput
26
+ ): OnMessageDismissedOutput {
27
+ assertIsInitialized();
28
+ return addEventListener('messageDismissed', input);
29
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { addEventListener } from '../../../../common';
5
+ import { OnMessageDisplayedOutput } from '../types/outputs';
6
+ import { OnMessageDisplayedInput } from '../types/inputs';
7
+ import { assertIsInitialized } from '../../../utils';
8
+
9
+ /**
10
+ * Registers a callback that will be invoked on `messageDisplayed` events.
11
+ *
12
+ * @param {OnMessageDisplayedInput} input - The input object that holds the callback handler.
13
+ * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
14
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
15
+ * @returns {OnMessageDismissedOutput} - An object that holds a remove method to stop listening to events.
16
+ * @example
17
+ * ```ts
18
+ * onMessageDisplayed((message) => {
19
+ * // use the message
20
+ * console.log(message.id);
21
+ * });
22
+ * ```
23
+ */
24
+ export function onMessageDisplayed(
25
+ input: OnMessageDisplayedInput
26
+ ): OnMessageDisplayedOutput {
27
+ assertIsInitialized();
28
+ return addEventListener('messageDisplayed', input);
29
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { addEventListener } from '../../../../common';
5
+ import { assertIsInitialized } from '../../../utils';
6
+ import { OnMessageReceivedInput } from '../types/inputs';
7
+ import { OnMessageReceivedOutput } from '../types/outputs';
8
+
9
+ /**
10
+ * Registers a callback that will be invoked on `messageReceived` events.
11
+ *
12
+ * @param {OnMessageReceivedInput} input - The input object that holds the callback handler.
13
+ * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
14
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
15
+ * @returns {OnMessageReceivedOutput} - An object that holds a remove method to stop listening to events.
16
+ * @example
17
+ * ```ts
18
+ * onMessageReceived((message) => {
19
+ * // use the message
20
+ * console.log(message.id);
21
+ * });
22
+ * ```
23
+ */
24
+ export function onMessageReceived(
25
+ input: OnMessageReceivedInput
26
+ ): OnMessageReceivedOutput {
27
+ assertIsInitialized();
28
+ return addEventListener('messageReceived', input);
29
+ }
@@ -0,0 +1,68 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { InAppMessage } from '../../../types';
5
+ import { assertIsInitialized } from '../../../utils';
6
+ import { InAppMessageConflictHandler, SetConflictHandlerInput } from '../types';
7
+
8
+ export let conflictHandler: InAppMessageConflictHandler =
9
+ defaultConflictHandler;
10
+
11
+ /**
12
+ * Set a conflict handler that will be used to resolve conflicts that may emerge
13
+ * when matching events with synced messages.
14
+ *
15
+ * @remark
16
+ * The conflict handler is not persisted across app restarts and so must be set again before dispatching an event for
17
+ * any custom handling to take effect.
18
+ * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
19
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
20
+ * @param SetConflictHandlerInput: The input object that holds the conflict handler to be used.
21
+ * @example
22
+ * ```ts
23
+ * // Sync messages before dispatching an event
24
+ * await syncMessages();
25
+ *
26
+ * // Example custom conflict handler
27
+ * const myConflictHandler = (messages) => {
28
+ * // Return a random message
29
+ * const randomIndex = Math.floor(Math.random() * messages.length);
30
+ * return messages[randomIndex];
31
+ * };
32
+ *
33
+ * // Set the conflict handler
34
+ * setConflictHandler(myConflictHandler);
35
+ *
36
+ * // Dispatch an event
37
+ * await dispatchEvent({ name: 'test_event' });
38
+ * ```
39
+ */
40
+ export function setConflictHandler(input: SetConflictHandlerInput): void {
41
+ assertIsInitialized();
42
+ conflictHandler = input;
43
+ }
44
+
45
+ function defaultConflictHandler(messages: InAppMessage[]): InAppMessage {
46
+ // default behavior is to return the message closest to expiry
47
+ // this function assumes that messages processed by providers already filters out expired messages
48
+ const sorted = messages.sort((a, b) => {
49
+ const endDateA = a.metadata?.endDate;
50
+ const endDateB = b.metadata?.endDate;
51
+ // if both message end dates are falsy or have the same date string, treat them as equal
52
+ if (endDateA === endDateB) {
53
+ return 0;
54
+ }
55
+ // if only message A has an end date, treat it as closer to expiry
56
+ if (endDateA && !endDateB) {
57
+ return -1;
58
+ }
59
+ // if only message B has an end date, treat it as closer to expiry
60
+ if (!endDateA && endDateB) {
61
+ return 1;
62
+ }
63
+ // otherwise, compare them
64
+ return new Date(endDateA) < new Date(endDateB) ? -1 : 1;
65
+ });
66
+ // always return the top sorted
67
+ return sorted[0];
68
+ }
@@ -26,6 +26,7 @@ import {
26
26
  assertServiceError,
27
27
  assertValidationError,
28
28
  } from '../../../errors';
29
+ import { assertIsInitialized } from '../../../utils';
29
30
 
30
31
  /**
31
32
  * Fetch and persist messages from Pinpoint campaigns.
@@ -33,10 +34,8 @@ import {
33
34
  *
34
35
  * @throws service exceptions - Thrown when the underlying Pinpoint service returns an error.
35
36
  * @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
36
- * configuration is incorrect.
37
- *
37
+ * configuration is incorrect, or if In App messaging hasn't been initialized.
38
38
  * @returns A promise that will resolve when the operation is complete.
39
- *
40
39
  * @example
41
40
  * ```ts
42
41
  * // Sync InApp messages with Pinpoint and device.
@@ -45,8 +44,9 @@ import {
45
44
  * ```
46
45
  */
47
46
  export async function syncMessages(): Promise<void> {
47
+ assertIsInitialized();
48
48
  const messages = await fetchInAppMessages();
49
- if (messages.length === 0) {
49
+ if (!messages || messages.length === 0) {
50
50
  return;
51
51
  }
52
52
  try {
@@ -95,7 +95,8 @@ async function fetchInAppMessages() {
95
95
  { credentials, region },
96
96
  input
97
97
  );
98
- const { InAppMessageCampaigns: messages } = response.InAppMessagesResponse;
98
+ const { InAppMessageCampaigns: messages } =
99
+ response.InAppMessagesResponse ?? {};
99
100
  return messages;
100
101
  } catch (error) {
101
102
  assertServiceError(error);
@@ -1,4 +1,15 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- export { identifyUser, syncMessages } from './apis';
4
+ export {
5
+ identifyUser,
6
+ syncMessages,
7
+ dispatchEvent,
8
+ setConflictHandler,
9
+ initializeInAppMessaging,
10
+ onMessageReceived,
11
+ onMessageDisplayed,
12
+ onMessageDismissed,
13
+ onMessageActionTaken,
14
+ notifyMessageInteraction,
15
+ } from './apis';