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