@aws-amplify/notifications 1.3.3-api-v6.25 → 1.3.3-api-v6.29

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 (57) hide show
  1. package/internals/package.json +8 -0
  2. package/lib/InAppMessaging/InAppMessaging.d.ts +3 -43
  3. package/lib/InAppMessaging/InAppMessaging.js +9 -317
  4. package/lib/InAppMessaging/InAppMessaging.js.map +1 -1
  5. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.d.ts +1 -1
  6. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.js +2 -2
  7. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.js.map +1 -1
  8. package/lib/InAppMessaging/types.d.ts +5 -4
  9. package/lib/InAppMessaging/types.js.map +1 -1
  10. package/lib/Notifications.js +1 -1
  11. package/lib/Notifications.js.map +1 -1
  12. package/lib/common/AWSPinpointProviderCommon/index.d.ts +2 -2
  13. package/lib/common/AWSPinpointProviderCommon/index.js +8 -4
  14. package/lib/common/AWSPinpointProviderCommon/index.js.map +1 -1
  15. package/lib/internals/InternalInAppMessaging.d.ts +59 -0
  16. package/lib/internals/InternalInAppMessaging.js +349 -0
  17. package/lib/internals/InternalInAppMessaging.js.map +1 -0
  18. package/lib/internals/index.d.ts +1 -0
  19. package/lib/internals/index.js +7 -0
  20. package/lib/internals/index.js.map +1 -0
  21. package/lib/internals/utils.d.ts +2 -0
  22. package/lib/internals/utils.js +10 -0
  23. package/lib/internals/utils.js.map +1 -0
  24. package/lib/types.d.ts +1 -1
  25. package/lib-esm/InAppMessaging/InAppMessaging.d.ts +3 -43
  26. package/lib-esm/InAppMessaging/InAppMessaging.js +10 -318
  27. package/lib-esm/InAppMessaging/InAppMessaging.js.map +1 -1
  28. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.d.ts +1 -1
  29. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.js +2 -2
  30. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.js.map +1 -1
  31. package/lib-esm/InAppMessaging/types.d.ts +5 -4
  32. package/lib-esm/InAppMessaging/types.js.map +1 -1
  33. package/lib-esm/Notifications.js +1 -1
  34. package/lib-esm/Notifications.js.map +1 -1
  35. package/lib-esm/common/AWSPinpointProviderCommon/index.d.ts +2 -2
  36. package/lib-esm/common/AWSPinpointProviderCommon/index.js +8 -4
  37. package/lib-esm/common/AWSPinpointProviderCommon/index.js.map +1 -1
  38. package/lib-esm/internals/InternalInAppMessaging.d.ts +59 -0
  39. package/lib-esm/internals/InternalInAppMessaging.js +347 -0
  40. package/lib-esm/internals/InternalInAppMessaging.js.map +1 -0
  41. package/lib-esm/internals/index.d.ts +1 -0
  42. package/lib-esm/internals/index.js +4 -0
  43. package/lib-esm/internals/index.js.map +1 -0
  44. package/lib-esm/internals/utils.d.ts +2 -0
  45. package/lib-esm/internals/utils.js +8 -0
  46. package/lib-esm/internals/utils.js.map +1 -0
  47. package/lib-esm/types.d.ts +1 -1
  48. package/package.json +9 -8
  49. package/src/InAppMessaging/InAppMessaging.ts +8 -300
  50. package/src/InAppMessaging/Providers/AWSPinpointProvider/index.ts +2 -2
  51. package/src/InAppMessaging/types.ts +7 -3
  52. package/src/Notifications.ts +1 -1
  53. package/src/common/AWSPinpointProviderCommon/index.ts +13 -4
  54. package/src/internals/InternalInAppMessaging.ts +352 -0
  55. package/src/internals/index.ts +4 -0
  56. package/src/internals/utils.ts +20 -0
  57. package/src/types.ts +5 -1
@@ -1,78 +1,14 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import {
5
- ConsoleLogger as Logger,
6
- HubCallback,
7
- HubCapsule,
8
- Hub,
9
- StorageHelper,
10
- } from '@aws-amplify/core';
11
- import flatten from 'lodash/flatten';
12
-
13
- import {
14
- addEventListener,
15
- EventListener,
16
- notifyEventListeners,
17
- } from '../common';
18
4
  import { UserInfo } from '../types';
19
- import { AWSPinpointProvider } from './Providers';
20
- import {
21
- InAppMessage,
22
- InAppMessageInteractionEvent,
23
- InAppMessagingInterface,
24
- InAppMessagingConfig,
25
- InAppMessageConflictHandler,
26
- InAppMessagingEvent,
27
- InAppMessagingProvider,
28
- NotificationsSubCategory,
29
- OnMessageInteractionEventHandler,
30
- } from './types';
31
-
32
- const STORAGE_KEY_SUFFIX = '_inAppMessages';
33
-
34
- const logger = new Logger('Notifications.InAppMessaging');
35
-
36
- export default class InAppMessaging implements InAppMessagingInterface {
37
- private config: Record<string, any> = {};
38
- private conflictHandler: InAppMessageConflictHandler;
39
- private listeningForAnalyticEvents = false;
40
- private pluggables: InAppMessagingProvider[] = [];
41
- private storageSynced = false;
42
-
43
- constructor() {
44
- this.config = { storage: new StorageHelper().getStorage() };
45
- this.setConflictHandler(this.defaultConflictHandler);
46
- }
47
-
48
- /**
49
- * Configure InAppMessaging
50
- * @param {Object} config - InAppMessaging configuration object
51
- */
52
- configure = ({
53
- listenForAnalyticsEvents = true,
54
- ...config
55
- }: InAppMessagingConfig = {}): InAppMessagingConfig => {
56
- this.config = { ...this.config, ...config };
57
-
58
- logger.debug('configure InAppMessaging', this.config);
59
-
60
- this.pluggables.forEach(pluggable => {
61
- pluggable.configure(this.config[pluggable.getProviderName()]);
62
- });
63
-
64
- if (this.pluggables.length === 0) {
65
- this.addPluggable(new AWSPinpointProvider());
66
- }
67
-
68
- if (listenForAnalyticsEvents && !this.listeningForAnalyticEvents) {
69
- Hub.listen('analytics', this.analyticsListener);
70
- this.listeningForAnalyticEvents = true;
71
- }
72
-
73
- return this.config;
74
- };
5
+ import { InAppMessagingInterface, NotificationsSubCategory } from './types';
6
+ import { InternalInAppMessagingClass } from '../internals/InternalInAppMessaging';
75
7
 
8
+ export default class InAppMessaging
9
+ extends InternalInAppMessagingClass
10
+ implements InAppMessagingInterface
11
+ {
76
12
  /**
77
13
  * Get the name of this module
78
14
  * @returns {string} name of this module
@@ -81,241 +17,13 @@ export default class InAppMessaging implements InAppMessagingInterface {
81
17
  return 'InAppMessaging';
82
18
  }
83
19
 
84
- /**
85
- * Get a plugin from added plugins
86
- * @param {string} providerName - the name of the plugin to get
87
- */
88
- getPluggable = (providerName: string): InAppMessagingProvider => {
89
- const pluggable =
90
- this.pluggables.find(
91
- pluggable => pluggable.getProviderName() === providerName
92
- ) ?? null;
93
-
94
- if (!pluggable) {
95
- logger.debug(`No plugin found with name ${providerName}`);
96
- }
97
-
98
- return pluggable;
99
- };
100
-
101
- /**
102
- * Add plugin into InAppMessaging
103
- * @param {InAppMessagingProvider} pluggable - an instance of the plugin
104
- */
105
- addPluggable = (pluggable: InAppMessagingProvider): void => {
106
- if (
107
- pluggable &&
108
- pluggable.getCategory() === 'Notifications' &&
109
- pluggable.getSubCategory() === 'InAppMessaging'
110
- ) {
111
- if (this.getPluggable(pluggable.getProviderName())) {
112
- throw new Error(
113
- `Pluggable ${pluggable.getProviderName()} has already been added.`
114
- );
115
- }
116
- this.pluggables.push(pluggable);
117
- pluggable.configure(this.config[pluggable.getProviderName()]);
118
- }
119
- };
120
-
121
- /**
122
- * Remove a plugin from added plugins
123
- * @param {string} providerName - the name of the plugin to remove
124
- */
125
- removePluggable = (providerName: string): void => {
126
- const index = this.pluggables.findIndex(
127
- pluggable => pluggable.getProviderName() === providerName
128
- );
129
- if (index === -1) {
130
- logger.debug(`No plugin found with name ${providerName}`);
131
- } else {
132
- this.pluggables.splice(index, 1);
133
- }
134
- };
135
-
136
20
  /**
137
21
  * Get the map resources that are currently available through the provider
138
22
  * @param {string} provider
139
23
  * @returns - Array of available map resources
140
24
  */
141
- syncMessages = (): Promise<void[]> =>
142
- Promise.all<void>(
143
- this.pluggables.map(async pluggable => {
144
- try {
145
- const messages = await pluggable.getInAppMessages();
146
- const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
147
- await this.setMessages(key, messages);
148
- } catch (err) {
149
- logger.error('Failed to sync messages', err);
150
- throw err;
151
- }
152
- })
153
- );
154
-
155
- clearMessages = (): Promise<void[]> =>
156
- Promise.all<void>(
157
- this.pluggables.map(async pluggable => {
158
- const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
159
- await this.removeMessages(key);
160
- })
161
- );
162
-
163
- dispatchEvent = async (event: InAppMessagingEvent): Promise<void> => {
164
- const messages: InAppMessage[][] = await Promise.all<InAppMessage[]>(
165
- this.pluggables.map(async pluggable => {
166
- const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
167
- const messages = await this.getMessages(key);
168
- return pluggable.processInAppMessages(messages, event);
169
- })
170
- );
171
-
172
- const flattenedMessages = flatten(messages);
173
-
174
- if (flattenedMessages.length) {
175
- notifyEventListeners(
176
- InAppMessageInteractionEvent.MESSAGE_RECEIVED,
177
- this.conflictHandler(flattenedMessages)
178
- );
179
- }
180
- };
25
+ syncMessages = (): Promise<void[]> => super.syncMessages();
181
26
 
182
27
  identifyUser = (userId: string, userInfo: UserInfo): Promise<void[]> =>
183
- Promise.all<void>(
184
- this.pluggables.map(async pluggable => {
185
- try {
186
- await pluggable.identifyUser(userId, userInfo);
187
- } catch (err) {
188
- logger.error('Failed to identify user', err);
189
- throw err;
190
- }
191
- })
192
- );
193
-
194
- onMessageReceived = (
195
- handler: OnMessageInteractionEventHandler
196
- ): EventListener<OnMessageInteractionEventHandler> =>
197
- addEventListener(InAppMessageInteractionEvent.MESSAGE_RECEIVED, handler);
198
-
199
- onMessageDisplayed = (
200
- handler: OnMessageInteractionEventHandler
201
- ): EventListener<OnMessageInteractionEventHandler> =>
202
- addEventListener(InAppMessageInteractionEvent.MESSAGE_DISPLAYED, handler);
203
-
204
- onMessageDismissed = (
205
- handler: OnMessageInteractionEventHandler
206
- ): EventListener<OnMessageInteractionEventHandler> =>
207
- addEventListener(InAppMessageInteractionEvent.MESSAGE_DISMISSED, handler);
208
-
209
- onMessageActionTaken = (
210
- handler: OnMessageInteractionEventHandler
211
- ): EventListener<OnMessageInteractionEventHandler> =>
212
- addEventListener(
213
- InAppMessageInteractionEvent.MESSAGE_ACTION_TAKEN,
214
- handler
215
- );
216
-
217
- notifyMessageInteraction = (
218
- message: InAppMessage,
219
- type: InAppMessageInteractionEvent
220
- ): void => {
221
- notifyEventListeners(type, message);
222
- };
223
-
224
- setConflictHandler = (handler: InAppMessageConflictHandler): void => {
225
- this.conflictHandler = handler;
226
- };
227
-
228
- private analyticsListener: HubCallback = ({ payload }: HubCapsule) => {
229
- const { event, data } = payload;
230
- switch (event) {
231
- case 'record': {
232
- this.dispatchEvent(data);
233
- break;
234
- }
235
- default:
236
- break;
237
- }
238
- };
239
-
240
- private syncStorage = async (): Promise<void> => {
241
- const { storage } = this.config;
242
- try {
243
- // Only run sync() if it's available (i.e. React Native)
244
- if (typeof storage.sync === 'function') {
245
- await storage.sync();
246
- }
247
- this.storageSynced = true;
248
- } catch (err) {
249
- logger.error('Failed to sync storage', err);
250
- }
251
- };
252
-
253
- private getMessages = async (key: string): Promise<any> => {
254
- try {
255
- if (!this.storageSynced) {
256
- await this.syncStorage();
257
- }
258
- const { storage } = this.config;
259
- const storedMessages = storage.getItem(key);
260
- return storedMessages ? JSON.parse(storedMessages) : [];
261
- } catch (err) {
262
- logger.error('Failed to retrieve in-app messages from storage', err);
263
- }
264
- };
265
-
266
- private setMessages = async (
267
- key: string,
268
- messages: InAppMessage[]
269
- ): Promise<void> => {
270
- if (!messages) {
271
- return;
272
- }
273
-
274
- try {
275
- if (!this.storageSynced) {
276
- await this.syncStorage();
277
- }
278
- const { storage } = this.config;
279
- storage.setItem(key, JSON.stringify(messages));
280
- } catch (err) {
281
- logger.error('Failed to store in-app messages', err);
282
- }
283
- };
284
-
285
- private removeMessages = async (key: string): Promise<void> => {
286
- try {
287
- if (!this.storageSynced) {
288
- await this.syncStorage();
289
- }
290
- const { storage } = this.config;
291
- storage.removeItem(key);
292
- } catch (err) {
293
- logger.error('Failed to remove in-app messages from storage', err);
294
- }
295
- };
296
-
297
- private defaultConflictHandler = (messages: InAppMessage[]): InAppMessage => {
298
- // default behavior is to return the message closest to expiry
299
- // this function assumes that messages processed by providers already filters out expired messages
300
- const sorted = messages.sort((a, b) => {
301
- const endDateA = a.metadata?.endDate;
302
- const endDateB = b.metadata?.endDate;
303
- // if both message end dates are falsy or have the same date string, treat them as equal
304
- if (endDateA === endDateB) {
305
- return 0;
306
- }
307
- // if only message A has an end date, treat it as closer to expiry
308
- if (endDateA && !endDateB) {
309
- return -1;
310
- }
311
- // if only message B has an end date, treat it as closer to expiry
312
- if (!endDateA && endDateB) {
313
- return 1;
314
- }
315
- // otherwise, compare them
316
- return new Date(endDateA) < new Date(endDateB) ? -1 : 1;
317
- });
318
- // always return the top sorted
319
- return sorted[0];
320
- };
28
+ super.identifyUser(userId, userInfo);
321
29
  }
@@ -111,7 +111,7 @@ export default class AWSPinpointProvider
111
111
  return this.config;
112
112
  };
113
113
 
114
- getInAppMessages = async () => {
114
+ getInAppMessages = async (userAgentValue?: string) => {
115
115
  if (!this.initialized) {
116
116
  await this.init();
117
117
  }
@@ -129,7 +129,7 @@ export default class AWSPinpointProvider
129
129
  };
130
130
  this.logger.debug('getting in-app messages');
131
131
  const response: GetInAppMessagesOutput = await getInAppMessages(
132
- { credentials, region },
132
+ { credentials, region, userAgentValue },
133
133
  input
134
134
  );
135
135
  const { InAppMessageCampaigns: messages } =
@@ -4,6 +4,7 @@
4
4
  import { EventListener } from '../common';
5
5
  import { AWSPinpointProviderConfig } from '../common/AWSPinpointProviderCommon/types';
6
6
  import {
7
+ NotificationsConfig,
7
8
  NotificationsProvider,
8
9
  NotificationsSubCategory as NotificationsSubCategories,
9
10
  UserInfo,
@@ -13,10 +14,13 @@ export type NotificationsSubCategory = Extract<
13
14
  NotificationsSubCategories,
14
15
  'InAppMessaging'
15
16
  >;
17
+ export type InternalNotificationsSubCategory = 'InternalInAppMessaging';
16
18
 
17
19
  export interface InAppMessagingInterface {
18
- configure: (config: InAppMessagingConfig) => InAppMessagingConfig;
19
- getModuleName: () => NotificationsSubCategory;
20
+ configure: (config: NotificationsConfig) => InAppMessagingConfig;
21
+ getModuleName: () =>
22
+ | NotificationsSubCategory
23
+ | InternalNotificationsSubCategory;
20
24
  getPluggable: (providerName: string) => InAppMessagingProvider;
21
25
  addPluggable: (pluggable: InAppMessagingProvider) => void;
22
26
  removePluggable: (providerName: string) => void;
@@ -48,7 +52,7 @@ export interface InAppMessagingProvider extends NotificationsProvider {
48
52
  getSubCategory(): NotificationsSubCategory;
49
53
 
50
54
  // get in-app messages from provider
51
- getInAppMessages(): Promise<any>;
55
+ getInAppMessages(userAgentValue?: string): Promise<any>;
52
56
 
53
57
  // filters in-app messages based on event input and provider logic
54
58
  processInAppMessages(
@@ -38,7 +38,7 @@ class NotificationsClass {
38
38
  logger.debug('configure Notifications', config);
39
39
 
40
40
  // Configure sub-categories
41
- this.inAppMessaging.configure(this.config.InAppMessaging);
41
+ this.inAppMessaging.configure({ Notifications: this.config });
42
42
 
43
43
  if (this.config.Push) {
44
44
  try {
@@ -77,12 +77,16 @@ export default abstract class AWSPinpointProviderCommon
77
77
  return this.config;
78
78
  }
79
79
 
80
- identifyUser = async (userId: string, userInfo: UserInfo): Promise<void> => {
80
+ identifyUser = async (
81
+ userId: string,
82
+ userInfo: UserInfo,
83
+ userAgentValue?: string
84
+ ): Promise<void> => {
81
85
  if (!this.initialized) {
82
86
  await this.init();
83
87
  }
84
88
  try {
85
- await this.updateEndpoint(userId, userInfo);
89
+ await this.updateEndpoint(userId, userInfo, userAgentValue);
86
90
  } catch (err) {
87
91
  this.logger.error('Error identifying user', err);
88
92
  throw err;
@@ -161,7 +165,8 @@ export default abstract class AWSPinpointProviderCommon
161
165
 
162
166
  protected updateEndpoint = async (
163
167
  userId: string = null,
164
- userInfo: AWSPinpointUserInfo = null
168
+ userInfo: AWSPinpointUserInfo = null,
169
+ userAgentValue?: string
165
170
  ): Promise<void> => {
166
171
  const credentials = await this.getCredentials();
167
172
  // Shallow compare to determine if credentials stored here are outdated
@@ -230,7 +235,11 @@ export default abstract class AWSPinpointProviderCommon
230
235
  };
231
236
  this.logger.debug('updating endpoint');
232
237
  await updateEndpoint(
233
- { credentials, region, userAgentValue: this.getUserAgentValue() },
238
+ {
239
+ credentials,
240
+ region,
241
+ userAgentValue: userAgentValue || this.getUserAgentValue(),
242
+ },
234
243
  input
235
244
  );
236
245
  this.endpointInitialized = true;