web-push-notifications 3.44.7 → 3.44.9

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 (224) hide show
  1. package/core/Pushwoosh.d.ts +199 -0
  2. package/core/Pushwoosh.types.d.ts +213 -0
  3. package/core/Safari.types.d.ts +17 -0
  4. package/core/constants.d.ts +41 -0
  5. package/core/events.types.d.ts +55 -0
  6. package/core/functions.d.ts +5 -0
  7. package/core/legacyEventsMap.d.ts +5 -0
  8. package/core/logger.d.ts +14 -0
  9. package/core/modules/EventBus/EventBus.d.ts +10 -0
  10. package/core/storage.d.ts +27 -0
  11. package/helpers/logger.d.ts +11 -0
  12. package/helpers/pwlogger/Logger.constants.d.ts +21 -0
  13. package/helpers/pwlogger/Logger.d.ts +37 -0
  14. package/helpers/pwlogger/Logger.types.d.ts +44 -0
  15. package/helpers/pwlogger/handlers/handler-console/handler-console.d.ts +2 -0
  16. package/{src/helpers/unescape.ts → helpers/unescape.d.ts} +1 -18
  17. package/models/InboxMessages.d.ts +82 -0
  18. package/models/InboxMessages.types.d.ts +83 -0
  19. package/models/NotificationPayload.d.ts +39 -0
  20. package/models/NotificationPayload.types.d.ts +63 -0
  21. package/modules/Api/Api.d.ts +45 -0
  22. package/modules/Api/Api.types.d.ts +7 -0
  23. package/modules/ApiClient/ApiClient.d.ts +29 -0
  24. package/modules/ApiClient/ApiClient.types.d.ts +188 -0
  25. package/modules/Data/Data.d.ts +76 -0
  26. package/modules/DateModule.d.ts +27 -0
  27. package/modules/InboxMessagesPublic.d.ts +64 -0
  28. package/modules/PlatformChecker/PlatformChecker.d.ts +45 -0
  29. package/modules/PlatformChecker/PlatformChecker.types.d.ts +5 -0
  30. package/modules/storage/Storage.d.ts +63 -0
  31. package/{src/modules/storage/Storage.types.ts → modules/storage/Storage.types.d.ts} +7 -25
  32. package/modules/storage/Store.d.ts +22 -0
  33. package/modules/storage/migrations/26-11-2018.d.ts +2 -0
  34. package/modules/storage/migrations/MigrationExecutor.d.ts +9 -0
  35. package/modules/storage/migrations/Migrations.d.ts +15 -0
  36. package/modules/storage/migrations/constants.d.ts +6 -0
  37. package/modules/storage/migrations/helpers.d.ts +7 -0
  38. package/modules/storage/migrations/initial.d.ts +2 -0
  39. package/modules/storage/version.d.ts +2 -0
  40. package/npm.js +2 -0
  41. package/npm.js.map +1 -0
  42. package/package.json +1 -1
  43. package/service-worker.d.ts +1 -0
  44. package/service-worker.js +2 -0
  45. package/service-worker.js.map +1 -0
  46. package/services/PushService/PushService.types.d.ts +64 -0
  47. package/services/PushService/drivers/PushServiceDefault/PushServiceDefault.d.ts +29 -0
  48. package/{src/services/PushService/drivers/PushServiceDefault/PushServiceDefault.types.ts → services/PushService/drivers/PushServiceDefault/PushServiceDefault.types.d.ts} +1 -1
  49. package/services/PushService/drivers/PushServiceSafari/PushServiceSafari.d.ts +21 -0
  50. package/services/PushService/drivers/PushServiceSafari/PushServiceSafari.types.d.ts +4 -0
  51. package/widget-inbox.js +2 -0
  52. package/widget-inbox.js.map +1 -0
  53. package/widget-subscribe-popup.js +2 -0
  54. package/widget-subscribe-popup.js.map +1 -0
  55. package/widget-subscription-button.js +2 -0
  56. package/widget-subscription-button.js.map +1 -0
  57. package/widget-subscription-prompt.d.ts +2 -0
  58. package/widget-subscription-prompt.js +2 -0
  59. package/widget-subscription-prompt.js.map +1 -0
  60. package/widgets/Inbox/InboxWidget.d.ts +49 -0
  61. package/widgets/Inbox/constants.d.ts +7 -0
  62. package/widgets/Inbox/helpers.d.ts +4 -0
  63. package/widgets/Inbox/inbox_widget.types.d.ts +34 -0
  64. package/widgets/Inbox/widgetTemplates.d.ts +4 -0
  65. package/widgets/SubscribePopup/SubscribePopup.d.ts +20 -0
  66. package/widgets/SubscribePopup/constants.d.ts +6 -0
  67. package/widgets/SubscribePopup/helpers.d.ts +1 -0
  68. package/widgets/SubscribePopup/popupTemplates.d.ts +9 -0
  69. package/widgets/SubscribePopup/types/subscribe-popup.d.ts +43 -0
  70. package/widgets/SubscriptionButton/bell.d.ts +2 -0
  71. package/widgets/SubscriptionButton/constants.d.ts +7 -0
  72. package/widgets/SubscriptionButton/index.d.ts +90 -0
  73. package/widgets/SubscriptionButton/positioning.d.ts +11 -0
  74. package/widgets/SubscriptionButton/subscribe_widget.types.d.ts +35 -0
  75. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.constants.d.ts +1 -0
  76. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.d.ts +14 -0
  77. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.helpers.d.ts +3 -0
  78. package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.types.d.ts +19 -0
  79. package/widgets/SubscriptionPrompt/constants.d.ts +21 -0
  80. package/widgets/SubscriptionPrompt/helpers.d.ts +5 -0
  81. package/worker/global.d.ts +16 -0
  82. package/worker/notification.d.ts +9 -0
  83. package/worker/worker.types.d.ts +4 -0
  84. package/.editorconfig +0 -11
  85. package/.gitlab-ci.yml +0 -193
  86. package/babel.config.js +0 -7
  87. package/ci/cdn/Dockerfile +0 -12
  88. package/ci/dev/Dockerfile +0 -30
  89. package/ci/dev/rootfs/entrypoint.sh +0 -18
  90. package/ci/dev/rootfs/entrypoint.sh.d/nginx.sh +0 -6
  91. package/ci/dev/rootfs/entrypoint.sh.d/supervisor.sh +0 -5
  92. package/ci/dev/rootfs/etc/nginx/_real_ip.conf +0 -2
  93. package/ci/dev/rootfs/etc/nginx/conf.d/default.conf +0 -20
  94. package/ci/dev/rootfs/etc/supervisor.d/nginx.ini +0 -11
  95. package/ci/github/Dockerfile +0 -59
  96. package/ci/github/release-zip.js +0 -61
  97. package/ci/npm/Dockerfile +0 -22
  98. package/config/config.js +0 -24
  99. package/config/configBuilder.js +0 -126
  100. package/config/helpers.js +0 -9
  101. package/config/index.js +0 -1
  102. package/develop/README.md +0 -42
  103. package/develop/favicon.png +0 -0
  104. package/develop/index.html +0 -511
  105. package/eslint.config.mjs +0 -114
  106. package/public/index.d.ts +0 -337
  107. package/scripts/zip.js +0 -26
  108. package/src/core/Pushwoosh.ts +0 -768
  109. package/src/core/Pushwoosh.types.ts +0 -254
  110. package/src/core/Safari.types.ts +0 -26
  111. package/src/core/constants.ts +0 -58
  112. package/src/core/events.types.ts +0 -46
  113. package/src/core/functions.ts +0 -33
  114. package/src/core/legacyEventsMap.ts +0 -64
  115. package/src/core/logger.ts +0 -64
  116. package/src/core/modules/EventBus/EventBus.ts +0 -66
  117. package/src/core/storage.ts +0 -254
  118. package/src/helpers/logger.ts +0 -81
  119. package/src/helpers/pwlogger/Logger.constants.ts +0 -31
  120. package/src/helpers/pwlogger/Logger.ts +0 -218
  121. package/src/helpers/pwlogger/Logger.types.ts +0 -66
  122. package/src/helpers/pwlogger/handlers/handler-console/handler-console.ts +0 -40
  123. package/src/models/InboxMessages.ts +0 -202
  124. package/src/models/InboxMessages.types.ts +0 -111
  125. package/src/models/NotificationPayload.ts +0 -216
  126. package/src/models/NotificationPayload.types.ts +0 -65
  127. package/src/modules/Api/Api.ts +0 -386
  128. package/src/modules/Api/Api.types.ts +0 -7
  129. package/src/modules/ApiClient/ApiClient.ts +0 -153
  130. package/src/modules/ApiClient/ApiClient.types.ts +0 -222
  131. package/src/modules/Data/Data.ts +0 -345
  132. package/src/modules/DateModule.ts +0 -53
  133. package/src/modules/InboxMessagesPublic.ts +0 -222
  134. package/src/modules/PlatformChecker/PlatformChecker.ts +0 -170
  135. package/src/modules/PlatformChecker/PlatformChecker.types.ts +0 -5
  136. package/src/modules/storage/Storage.ts +0 -164
  137. package/src/modules/storage/Store.ts +0 -104
  138. package/src/modules/storage/migrations/26-11-2018.ts +0 -25
  139. package/src/modules/storage/migrations/MigrationExecutor.ts +0 -31
  140. package/src/modules/storage/migrations/Migrations.ts +0 -41
  141. package/src/modules/storage/migrations/constants.ts +0 -8
  142. package/src/modules/storage/migrations/helpers.ts +0 -16
  143. package/src/modules/storage/migrations/initial.ts +0 -47
  144. package/src/modules/storage/version.ts +0 -2
  145. package/src/pushwoosh-web-notifications.ts +0 -47
  146. package/src/pushwoosh-widget-inbox.ts +0 -8
  147. package/src/pushwoosh-widget-subscribe-popup.ts +0 -9
  148. package/src/pushwoosh-widget-subscription-button.ts +0 -8
  149. package/src/pushwoosh-widget-subscription-prompt.ts +0 -6
  150. package/src/service-worker.ts +0 -455
  151. package/src/services/PushService/PushService.types.ts +0 -74
  152. package/src/services/PushService/drivers/PushServiceDefault/PushServiceDefault.ts +0 -235
  153. package/src/services/PushService/drivers/PushServiceSafari/PushServiceSafari.ts +0 -125
  154. package/src/services/PushService/drivers/PushServiceSafari/PushServiceSafari.types.ts +0 -4
  155. package/src/widget-subscription-prompt.ts +0 -33
  156. package/src/widgets/Inbox/InboxWidget.ts +0 -564
  157. package/src/widgets/Inbox/constants.ts +0 -49
  158. package/src/widgets/Inbox/css/inboxWidgetStyle.css +0 -274
  159. package/src/widgets/Inbox/helpers.ts +0 -63
  160. package/src/widgets/Inbox/inbox.d.ts +0 -9
  161. package/src/widgets/Inbox/inbox_widget.types.ts +0 -41
  162. package/src/widgets/Inbox/widgetTemplates.ts +0 -55
  163. package/src/widgets/SubscribePopup/SubscribePopup.ts +0 -241
  164. package/src/widgets/SubscribePopup/constants.ts +0 -66
  165. package/src/widgets/SubscribePopup/helpers.ts +0 -11
  166. package/src/widgets/SubscribePopup/popupTemplates.ts +0 -24
  167. package/src/widgets/SubscribePopup/styles/popup.css +0 -226
  168. package/src/widgets/SubscribePopup/types/subscribe-popup.ts +0 -68
  169. package/src/widgets/SubscriptionButton/assets/css/main.css +0 -205
  170. package/src/widgets/SubscriptionButton/bell.ts +0 -67
  171. package/src/widgets/SubscriptionButton/constants.ts +0 -28
  172. package/src/widgets/SubscriptionButton/index.ts +0 -377
  173. package/src/widgets/SubscriptionButton/positioning.ts +0 -165
  174. package/src/widgets/SubscriptionButton/subscribe_widget.types.ts +0 -53
  175. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.constants.ts +0 -1
  176. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.helpers.ts +0 -110
  177. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.ts +0 -102
  178. package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.types.ts +0 -23
  179. package/src/widgets/SubscriptionPrompt/constants.ts +0 -22
  180. package/src/widgets/SubscriptionPrompt/helpers.ts +0 -42
  181. package/src/widgets/widgets.d.ts +0 -4
  182. package/src/worker/global.ts +0 -36
  183. package/src/worker/notification.ts +0 -34
  184. package/src/worker/worker.types.ts +0 -4
  185. package/test/__helpers__/apiHelpers.ts +0 -22
  186. package/test/__helpers__/keyValueHelpers.ts +0 -15
  187. package/test/__helpers__/platformHelpers.ts +0 -54
  188. package/test/__helpers__/sinonHelpers.ts +0 -7
  189. package/test/__helpers__/storageHelpers.ts +0 -56
  190. package/test/__mocks__/apiRequests.ts +0 -26
  191. package/test/__mocks__/idbMock.ts +0 -12
  192. package/test/__mocks__/idbObjectStoreMock.ts +0 -38
  193. package/test/__mocks__/inboxMessages.ts +0 -292
  194. package/test/__mocks__/models/inboxModel.ts +0 -71
  195. package/test/__mocks__/modules/apiClientModule.ts +0 -18
  196. package/test/__mocks__/modules/dateModule.ts +0 -34
  197. package/test/__mocks__/modules/inboxParamsModule.ts +0 -21
  198. package/test/__mocks__/modules/paramsBuilder.ts +0 -12
  199. package/test/__mocks__/modules/paramsModule.ts +0 -35
  200. package/test/__mocks__/modules/payloadBuilderModule.ts +0 -15
  201. package/test/__mocks__/modules/storageModule.ts +0 -58
  202. package/test/__mocks__/navigator.ts +0 -38
  203. package/test/__mocks__/notification.ts +0 -84
  204. package/test/__mocks__/pushwoosh.ts +0 -12
  205. package/test/__mocks__/userAgents +0 -8
  206. package/test/functions.test.ts +0 -22
  207. package/test/ignore-html.js +0 -6
  208. package/test/mocha.opts +0 -6
  209. package/test/modules/DateModule/unit.test.ts +0 -80
  210. package/test/modules/storage/Storage/unit.test.ts +0 -180
  211. package/test/modules/storage/Store/unit.test.ts +0 -192
  212. package/testRegister.js +0 -24
  213. package/tsconfig.json +0 -31
  214. package/webpack.config.js +0 -163
  215. /package/{src/core/modules/EventBus/index.ts → core/modules/EventBus/index.d.ts} +0 -0
  216. /package/{src/helpers/pwlogger/index.ts → helpers/pwlogger/index.d.ts} +0 -0
  217. /package/{src/modules/PlatformChecker/index.ts → modules/PlatformChecker/index.d.ts} +0 -0
  218. /package/{src/npm.ts → npm.d.ts} +0 -0
  219. /package/{src/services/PushService/PushService.ts → services/PushService/PushService.d.ts} +0 -0
  220. /package/{src/widget-inbox.ts → widget-inbox.d.ts} +0 -0
  221. /package/{src/widget-subscribe-popup.ts → widget-subscribe-popup.d.ts} +0 -0
  222. /package/{src/widget-subscription-button.ts → widget-subscription-button.d.ts} +0 -0
  223. /package/{src/widgets/Inbox/index.ts → widgets/Inbox/index.d.ts} +0 -0
  224. /package/{src/widgets/SubscribePopup/index.ts → widgets/SubscribePopup/index.d.ts} +0 -0
@@ -1,386 +0,0 @@
1
- import { type SetPurchaseAttributes } from './Api.types';
2
- import * as CONSTANTS from '../../core/constants';
3
- import { type EventBus } from '../../core/modules/EventBus';
4
- import { ApiClient } from '../ApiClient/ApiClient';
5
- import {
6
- type IMapRequest,
7
- type IMapResponse,
8
- type IRequest,
9
- } from '../ApiClient/ApiClient.types';
10
- import { Data } from '../Data/Data';
11
-
12
- export class Api {
13
- private readonly data: Data;
14
- private readonly apiClient: ApiClient;
15
- private readonly eventBus: EventBus;
16
-
17
- constructor(
18
- eventBus: EventBus,
19
- data: Data = new Data(),
20
- apiClient: ApiClient = new ApiClient(),
21
- ) {
22
- this.eventBus = eventBus;
23
- this.data = data;
24
- this.apiClient = apiClient;
25
- }
26
-
27
- public async checkDevice(): Promise<IMapResponse['checkDevice']> {
28
- const params = await this.getRequestParams();
29
-
30
- return await this.apiClient.checkDevice(params);
31
- }
32
-
33
- public async checkDeviceSubscribeForPushNotifications(useCache: boolean = true): Promise<boolean> {
34
- // get current subscription status from local storage
35
- let status = localStorage.getItem(CONSTANTS.KEY_DEVICE_REGISTRATION_STATUS);
36
-
37
- // check need force update
38
- if (typeof status === 'undefined' || !useCache) {
39
- const { exist, push_token_exist } = await this.checkDevice();
40
- localStorage.setItem(
41
- CONSTANTS.KEY_DEVICE_REGISTRATION_STATUS,
42
- (exist && push_token_exist)
43
- ? CONSTANTS.DEVICE_REGISTRATION_STATUS_REGISTERED
44
- : CONSTANTS.DEVICE_REGISTRATION_STATUS_UNREGISTERED,
45
- );
46
-
47
- status = localStorage.getItem(CONSTANTS.KEY_DEVICE_REGISTRATION_STATUS);
48
- }
49
-
50
- return status === CONSTANTS.DEVICE_REGISTRATION_STATUS_REGISTERED;
51
- }
52
-
53
- public async getConfig(features: string[]): Promise<IMapResponse['getConfig']> {
54
- const params = await this.getRequestParams();
55
-
56
- return this.apiClient.getConfig({
57
- ...params,
58
- features,
59
- });
60
- }
61
-
62
- public async applicationOpen(): Promise<IMapResponse['applicationOpen']> {
63
- const params = await this.getRequestParams();
64
-
65
- // set do database latest sending time
66
- await this.data.setLastOpenApplicationTime(Date.now());
67
-
68
- return this.apiClient.applicationOpen(params);
69
- }
70
-
71
- public async registerDevice(): Promise<IMapResponse['registerDevice']> {
72
- // check communication disabled
73
- const isCommunicationDisabled = await this.data.getStatusCommunicationDisabled();
74
-
75
- // if communication disabled -> can't register device
76
- if (isCommunicationDisabled) {
77
- throw new Error(`Can't register device: Communication is disabled!`);
78
- }
79
-
80
- const params = await this.getRequestParams();
81
- const tokens = await this.data.getTokens();
82
-
83
- // if have not pushToken -> user not get permission for send push notifications
84
- if (!tokens.pushToken) {
85
- throw new Error(`Can't register device: pushToken is not exist!`);
86
- }
87
-
88
- // register device into Pushwoosh
89
- const response = await this.apiClient.registerDevice({
90
- ...params,
91
- push_token: tokens.pushToken,
92
- auth_token: tokens.authToken,
93
- public_key: tokens.publicKey,
94
- });
95
-
96
- // set info to database, that the device IS NOT manual unsubscribed
97
- await this.data.setStatusManualUnsubscribed(false);
98
-
99
- // set info to local storage, that device is subscribed
100
- localStorage.setItem(CONSTANTS.KEY_DEVICE_REGISTRATION_STATUS, CONSTANTS.DEVICE_REGISTRATION_STATUS_REGISTERED);
101
-
102
- // emit event
103
- this.eventBus.dispatchEvent('register', {});
104
-
105
- return response;
106
- }
107
-
108
- public async unregisterDevice(): Promise<IMapResponse['unregisterDevice']> {
109
- const params = await this.getRequestParams();
110
- const response = this.apiClient.unregisterDevice(params);
111
-
112
- // set info to local storage, that device is unsubscribed
113
- localStorage.setItem(CONSTANTS.KEY_DEVICE_REGISTRATION_STATUS, CONSTANTS.DEVICE_REGISTRATION_STATUS_UNREGISTERED);
114
-
115
- // emit event
116
- this.eventBus.dispatchEvent('unsubscribe', {});
117
-
118
- return response;
119
- }
120
-
121
- public async deleteDevice(): Promise<IMapResponse['deleteDevice']> {
122
- const params = await this.getRequestParams();
123
-
124
- const response = this.apiClient.deleteDevice(params);
125
-
126
- // set info to database, that the device IS manual unsubscribed
127
- await this.data.setStatusManualUnsubscribed(true);
128
-
129
- // set info to local storage, that device is unsubscribed
130
- localStorage.setItem(CONSTANTS.KEY_DEVICE_REGISTRATION_STATUS, CONSTANTS.DEVICE_REGISTRATION_STATUS_UNREGISTERED);
131
-
132
- // emit event
133
- this.eventBus.dispatchEvent('unsubscribe', {});
134
-
135
- return response;
136
- }
137
-
138
- public async messageDeliveryEvent(hash: string, isTrackingLogOnFailure?: boolean, metaData: { [key: string]: any } = {}): Promise<IMapResponse['messageDeliveryEvent']> {
139
- const params = await this.getRequestParams();
140
-
141
- return await this.apiClient.messageDeliveryEvent({
142
- ...params,
143
- hash,
144
- metaData,
145
- });
146
- }
147
-
148
- public async pushStat(hash: string, isTrackingLogOnFailure?: boolean, metaData: { [key: string]: any } = {}): Promise<IMapResponse['pushStat']> {
149
- const params = await this.getRequestParams();
150
-
151
- return await this.apiClient.pushStat({
152
- ...params,
153
- hash,
154
- metaData,
155
- });
156
- }
157
-
158
- public async setTags(tags: { [key: string]: any }): Promise<IMapResponse['setTags']> {
159
- const { hwid, device_type, ...params } = await this.getRequestParams();
160
- const email = await this.data.getEmail();
161
-
162
- // set email tags in pushwoosh
163
- if (email) {
164
- await this.apiClient.setEmailTags({
165
- ...params,
166
- email,
167
- tags,
168
- });
169
- }
170
-
171
- return this.apiClient.setTags({
172
- ...params,
173
- hwid,
174
- device_type,
175
- tags,
176
- });
177
- }
178
-
179
- public async getTags(): Promise<IMapResponse['getTags']> {
180
- const params = await this.getRequestParams();
181
-
182
- return this.apiClient.getTags(params);
183
- }
184
-
185
- public async registerUser(userId: string | number): Promise<IMapResponse['registerUser']> {
186
- const {
187
- hwid,
188
- device_type: _device_type,
189
- ...params
190
- } = await this.getRequestParams();
191
- const deviceType = await this.data.getDeviceType();
192
-
193
- const id = `${userId}`;
194
- const email = await this.data.getEmail();
195
-
196
- // register user in pushwoosh
197
- const response = await this.apiClient.registerUser({
198
- ...params,
199
- hwid,
200
- userId: id,
201
- ts_offset: -(new Date()).getTimezoneOffset() * 60,
202
- device_type: deviceType,
203
- });
204
-
205
- // register user email in pushwoosh
206
- if (email) {
207
- await this.apiClient.registerEmailUser({
208
- ...params,
209
- email,
210
- userId: id,
211
- ts_offset: -(new Date()).getTimezoneOffset() * 60,
212
- });
213
- }
214
-
215
- // set user id to database
216
- await this.data.setUserId(`${userId}`);
217
-
218
- // set info to database that user id was change
219
- await this.data.setStatusUserIdWasChanged(true);
220
-
221
- return response;
222
- }
223
-
224
- public async registerEmail(email: string, params?: IMapRequest['registerEmail']): Promise<IMapResponse['registerEmail']> {
225
- const {
226
- hwid: _hwid,
227
- device_type: _device_type,
228
- ...requestParams
229
- } = await this.getRequestParams();
230
-
231
- // register user email in pushwoosh
232
- const response = await this.apiClient.registerEmail({
233
- ...requestParams,
234
- ...params,
235
- email,
236
- ts_offset: -(new Date()).getTimezoneOffset() * 60,
237
- });
238
-
239
- // set user email to database
240
- await this.data.setEmail(`${email}`);
241
-
242
- // set info to database that user email was change
243
- await this.data.setStatusEmailWasChanged(true);
244
-
245
- return response;
246
- }
247
-
248
- public async postEvent(event: string, attributes: { [key: string]: any }): Promise<IMapResponse['postEvent']> {
249
- const params = await this.getRequestParams();
250
-
251
- const date = new Date();
252
- const time = date.getTime();
253
- const timestampUTC = Math.floor(time / 1000);
254
- const timestampCurrent = timestampUTC - (date.getTimezoneOffset() / 60 * 3600);
255
-
256
- // if post event send after notification open:
257
- // need add message hash to event
258
- const lastOpenMessage = await this.data.getLastOpenMessage();
259
-
260
- if (lastOpenMessage && lastOpenMessage.expiry > Date.now()) {
261
- if (attributes['msgHash']) {
262
- return Promise.reject('attribute msgHash already defined');
263
- }
264
-
265
- attributes = {
266
- ...attributes,
267
- msgHash: lastOpenMessage.messageHash,
268
- };
269
- }
270
-
271
- // remove last open message
272
- await this.data.setLastOpenMessage(undefined);
273
-
274
- const response = await this.apiClient.postEvent({
275
- ...params,
276
- event,
277
- timestampUTC,
278
- timestampCurrent,
279
- attributes,
280
- });
281
-
282
- if (response && response.code) {
283
- this.eventBus.dispatchEvent('receive-in-app-code', {
284
- code: response.code,
285
- });
286
- }
287
-
288
- return response;
289
- }
290
-
291
- public async getInboxMessages(count: number = 0): Promise<IMapResponse['getInboxMessages']> {
292
- const params = await this.getRequestParams();
293
-
294
- const lastCode = await this.data.getInboxLastRequestCode();
295
- const lastRequestTime = await this.data.getInboxLastRequestTime();
296
-
297
- return this.apiClient.getInboxMessages({
298
- ...params,
299
- count,
300
- last_code: lastCode,
301
- last_request_time: lastRequestTime,
302
- });
303
- }
304
-
305
- public async inboxStatus(order: string, status: number): Promise<IMapResponse['inboxStatus']> {
306
- const params = await this.getRequestParams();
307
-
308
- return this.apiClient.inboxStatus({
309
- ...params,
310
- inbox_code: order,
311
- status,
312
- time: (new Date()).getTime(),
313
- });
314
- }
315
-
316
- public async triggerEvent(): Promise<void> {
317
- throw new Error(`Method has been deprecated, because we don't aggregate this statistics.`);
318
- }
319
-
320
- public async pageVisit(config: { title: string; url_path: string; url: string }): Promise<IMapResponse['pageVisit']> {
321
- const params = await this.getRequestParams();
322
- const features = await this.data.getFeatures();
323
-
324
- const url = features && features.page_visit && features.page_visit.entrypoint;
325
-
326
- if (!url) {
327
- return;
328
- }
329
-
330
- return this.apiClient.pageVisit({
331
- ...params,
332
- ...config,
333
- }, url);
334
- }
335
-
336
- public async setPurchase(attributes: SetPurchaseAttributes): Promise<IMapResponse['setPurchase']> {
337
- const params = await this.getRequestParams();
338
-
339
- return this.apiClient.setPurchase({
340
- ...params,
341
- ...attributes,
342
- });
343
- }
344
-
345
- public async getParams() {
346
- const applicationCode = await this.data.getApplicationCode();
347
- const hwid = await this.data.getHwid();
348
- const apiParams = await this.data.getTokens();
349
- const initParams = await this.data.getInitParams();
350
-
351
- return {
352
- applicationCode,
353
- hwid,
354
- ...apiParams,
355
- ...initParams,
356
- };
357
- }
358
-
359
- public get params(): void {
360
- throw new Error('Property "Pushwoosh.api.params" have been deprecated. Use the async method "Pushwoosh.api.getParams()"');
361
- }
362
-
363
- private async getRequestParams(): Promise<IRequest> {
364
- const applicationCode = await this.data.getApplicationCode();
365
- const hwid = await this.data.getHwid();
366
- const userId = await this.data.getUserId();
367
- const deviceType = await this.data.getDeviceType();
368
- const deviceModel = await this.data.getDeviceModel();
369
- const language = await this.data.getLanguage();
370
- const version = await this.data.getSdkVersion();
371
-
372
- const timezone = -(new Date()).getTimezoneOffset() * 60;
373
-
374
- return {
375
- application: applicationCode,
376
- hwid: hwid,
377
-
378
- userId: userId || hwid,
379
- device_type: deviceType,
380
- device_model: deviceModel,
381
- timezone: timezone,
382
- language: language,
383
- v: version,
384
- };
385
- };
386
- }
@@ -1,7 +0,0 @@
1
- export interface SetPurchaseAttributes {
2
- transactionDate: string;
3
- quantity: number;
4
- currency: string;
5
- productIdentifier: string;
6
- price: number;
7
- }
@@ -1,153 +0,0 @@
1
- import { type TMethod, type IMapRequest, type IMapResponse } from './ApiClient.types';
2
- import { getGlobal } from '../../core/functions';
3
- import { Logger } from '../../core/logger';
4
- import { Data } from '../Data/Data';
5
-
6
- export class ApiClient {
7
- private readonly data: Data;
8
- private readonly logger: typeof Logger;
9
-
10
- constructor(
11
-
12
- data: Data = new Data(),
13
- logger: typeof Logger = Logger,
14
- ) {
15
- this.data = data;
16
- this.logger = logger;
17
- }
18
-
19
- public checkDevice(options: IMapRequest['checkDevice']): Promise<IMapResponse['checkDevice']> {
20
- return this.createRequest('checkDevice', options);
21
- }
22
-
23
- public getConfig(options: IMapRequest['getConfig']): Promise<IMapResponse['getConfig']> {
24
- return this.createRequest('getConfig', options, '', true);
25
- }
26
-
27
- public applicationOpen(options: IMapRequest['applicationOpen']): Promise<IMapResponse['applicationOpen']> {
28
- return this.createRequest('applicationOpen', options);
29
- }
30
-
31
- public registerDevice(options: IMapRequest['registerDevice']): Promise<IMapResponse['registerDevice']> {
32
- return this.createRequest('registerDevice', options);
33
- }
34
-
35
- public unregisterDevice(options: IMapRequest['unregisterDevice']): Promise<IMapResponse['unregisterDevice']> {
36
- return this.createRequest('unregisterDevice', options);
37
- }
38
-
39
- public deleteDevice(options: IMapRequest['deleteDevice']): Promise<IMapResponse['deleteDevice']> {
40
- return this.createRequest('deleteDevice', options);
41
- }
42
-
43
- public messageDeliveryEvent(options: IMapRequest['messageDeliveryEvent']): Promise<IMapResponse['messageDeliveryEvent']> {
44
- return this.createRequest('messageDeliveryEvent', options);
45
- }
46
-
47
- public pushStat(options: IMapRequest['pushStat']): Promise<IMapResponse['pushStat']> {
48
- return this.createRequest('pushStat', options);
49
- }
50
-
51
- public setTags(options: IMapRequest['setTags']): Promise<IMapResponse['setTags']> {
52
- return this.createRequest('setTags', options);
53
- }
54
-
55
- public getTags(options: IMapRequest['getTags']): Promise<IMapResponse['getTags']> {
56
- return this.createRequest('getTags', options);
57
- }
58
-
59
- public registerUser(options: IMapRequest['registerUser']): Promise<IMapResponse['registerUser']> {
60
- return this.createRequest('registerUser', options);
61
- }
62
-
63
- public registerEmail(options: IMapRequest['registerEmail']): Promise<IMapResponse['registerEmail']> {
64
- return this.createRequest('registerEmail', options);
65
- }
66
-
67
- public registerEmailUser(options: IMapRequest['registerEmailUser']): Promise<IMapResponse['registerEmailUser']> {
68
- return this.createRequest('registerEmailUser', options);
69
- }
70
-
71
- public setEmailTags(options: IMapRequest['setEmailTags']): Promise<IMapResponse['setEmailTags']> {
72
- return this.createRequest('setEmailTags', options);
73
- }
74
-
75
- public postEvent(options: IMapRequest['postEvent']): Promise<IMapResponse['postEvent']> {
76
- return this.createRequest('postEvent', options);
77
- }
78
-
79
- public getInboxMessages(options: IMapRequest['getInboxMessages']): Promise<IMapResponse['getInboxMessages']> {
80
- return this.createRequest('getInboxMessages', options);
81
- }
82
-
83
- public inboxStatus(options: IMapRequest['inboxStatus']): Promise<IMapResponse['inboxStatus']> {
84
- return this.createRequest('inboxStatus', options);
85
- }
86
-
87
- public pageVisit(options: IMapRequest['pageVisit'], url: string): Promise<IMapResponse['pageVisit']> {
88
- return this.createRequest('pageVisit', options, url);
89
- }
90
-
91
- public setPurchase(options: IMapRequest['setPurchase']): Promise<IMapResponse['setPurchase']> {
92
- return this.createRequest('setPurchase', options);
93
- }
94
-
95
- private async createRequest<T extends TMethod>(
96
- methodName: T,
97
- request: IMapRequest[T],
98
- customUrl?: string,
99
- ignoreAuth?: boolean,
100
- ): Promise<IMapResponse[T]> {
101
- const entrypoint = await this.data.getApiEntrypoint();
102
- const apiToken = ignoreAuth ? '' : await this.data.getApiToken();
103
- const url = customUrl || entrypoint + methodName;
104
-
105
- const authRequestOptions = apiToken ? {
106
- headers: {
107
- Authorization: `Token ${apiToken}`,
108
- 'Content-Type': 'text/plain;charset=UTF-8',
109
- Origin: getGlobal().location.origin,
110
- },
111
- credentials: 'include' as RequestCredentials,
112
- } : {};
113
-
114
- const response = await fetch(url, {
115
- method: 'POST',
116
- headers: {
117
- 'Content-Type': 'text/plain;charset=UTF-8',
118
- },
119
- body: JSON.stringify({
120
- request,
121
- }),
122
- ...authRequestOptions,
123
- });
124
-
125
- const result = await this.checkResponse(response);
126
-
127
- // reset api entrypoint if need
128
- if (result.base_url) {
129
- await this.data.setApiEntrypoint(result.base_url);
130
- }
131
-
132
- await this.logger.write(
133
- 'apirequest',
134
- `${methodName} call with arguments: ${JSON.stringify(request)} to Pushwoosh has been successful. Result: ${JSON.stringify(result.response)}`,
135
- );
136
-
137
- return result.response as IMapResponse[T];
138
- }
139
-
140
- private async checkResponse(response: Response): Promise<any> {
141
- if (response.status !== 200) {
142
- throw new Error(`Error code: ${response.status}. Error text: ${response.statusText}`);
143
- }
144
-
145
- const data = await response.json();
146
-
147
- if (data.status_code !== 200) {
148
- throw new Error(`Error code: ${data.status_code}. Error text: ${data.status_message}`);
149
- }
150
-
151
- return data;
152
- }
153
- }