notifly-sdk 1.0.3 → 1.0.5

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 (3) hide show
  1. package/.prettierrc.yml +14 -0
  2. package/index.js +49 -17
  3. package/package.json +1 -1
@@ -0,0 +1,14 @@
1
+ # .prettierrc or .prettierrc.yaml
2
+ proseWrap: 'preserve'
3
+ trailingComma: 'es5'
4
+ tabWidth: 4
5
+ semi: true
6
+ singleQuote: true
7
+ quoteProps: 'preserve'
8
+ printWidth: 120
9
+ overrides:
10
+ - files:
11
+ - '*.json'
12
+ - '*.yml'
13
+ options:
14
+ tabWidth: 2
package/index.js CHANGED
@@ -10,9 +10,20 @@ exports.trackEvent = logEvent;
10
10
 
11
11
  exports.setUserProperties = _setUserProperties
12
12
 
13
- exports.setUserId = async userId => await _setUserProperties({
14
- external_user_id: userId,
15
- })
13
+ exports.setUserId = async function (userID) {
14
+ try {
15
+ if (userID) {
16
+ await _setUserProperties({
17
+ external_user_id: userID,
18
+ })
19
+ return;
20
+ }
21
+ await _removeUserId();
22
+ return;
23
+ } catch (err) {
24
+ console.warn('[Notifly] setUserId Failed.');
25
+ }
26
+ }
16
27
 
17
28
  exports.sessionStart = async function () {
18
29
  const apiLevel = rnDeviceInfo.getApiLevel(); // only android
@@ -83,13 +94,13 @@ exports.initialize = async function (prjId, userName, password) {
83
94
  try {
84
95
  await logEvent("push_delivered", { "campaign_id": campaign_id, "status": "background-notifee" }, null, true);
85
96
  } catch (err) {
86
- console.error('[Notifly]: ', err);
97
+ console.warn('[Notifly]: ', err);
87
98
  }
88
99
  break;
89
100
  }
90
101
  });
91
102
  } catch (err) {
92
- console.error("[Notifly]: ", err);
103
+ console.warn("[Notifly]: ", err);
93
104
  }
94
105
  }
95
106
 
@@ -118,7 +129,7 @@ exports.foregroundSetting = function () {
118
129
  }
119
130
  await logEvent("push_click", { "campaign_id": campaign_id, "status": "foreground" }, null, true);
120
131
  } catch (err) {
121
- console.error('[Notifly]: ', err);
132
+ console.warn('[Notifly]: ', err);
122
133
  }
123
134
  }
124
135
  });
@@ -144,7 +155,7 @@ async function getCognitoIdToken(userName, password) {
144
155
  const tokens = JSON.parse(result).AuthenticationResult.IdToken
145
156
  return tokens;
146
157
  })
147
- .catch(error => console.error('[Notifly]: ', error));
158
+ .catch(error => console.warn('[Notifly]: ', error));
148
159
 
149
160
  return token;
150
161
  }
@@ -165,18 +176,40 @@ async function _getNotiflyUserId() {
165
176
  }
166
177
 
167
178
  async function _setUserProperties(params) {
168
- if (params.external_user_id) {
169
- params['previous_notifly_user_id'] = await _getNotiflyUserId();
170
- await Promise.all([
171
- AsyncStorage.setItem('notiflyExternalUserId', params.external_user_id),
172
- AsyncStorage.removeItem('notiflyUserId'),
173
- ]);
179
+ try {
180
+ if (params.external_user_id) {
181
+ const [previousNotiflyUserID, previousExternalUserID] = await Promise.all([
182
+ _getNotiflyUserId(),
183
+ AsyncStorage.getItem('notiflyExternalUserId')
184
+ ]);
185
+ params['previous_notifly_user_id'] = previousNotiflyUserID
186
+ params['previous_external_user_id'] = previousExternalUserID
187
+ await Promise.all([
188
+ AsyncStorage.setItem('notiflyExternalUserId', params.external_user_id),
189
+ AsyncStorage.removeItem('notiflyUserId'),
190
+ ]);
191
+ }
192
+ return (await logEvent("set_user_properties", params, null, true));
193
+ } catch (err) {
194
+ console.warn('[Notifly] Failed to remove userID');
195
+ }
196
+ }
197
+
198
+ async function _removeUserId() {
199
+ try {
200
+ await Promise.all([AsyncStorage.removeItem('notiflyExternalUserId'), AsyncStorage.removeItem('notiflyUserId')]);
201
+ return await logEvent('remove_external_user_id', {}, null, true);
202
+ } catch (err) {
203
+ console.warn('[Notifly] Failed to remove userID');
174
204
  }
175
- return (await logEvent("set_user_properties", params, null, true));
176
205
  }
177
206
 
178
207
  async function logEvent(eventName, eventParams, segmentation_event_param_keys = null, isInternalEvent = false) {
179
208
  try {
209
+ if (!eventName) {
210
+ console.warn('[Notifly]event_name must be provided.');
211
+ return;
212
+ }
180
213
  let token;
181
214
  const [savedCognitoToken, notiflyUserId, externalUserId, prjId, externalDeviceId, osVersion, appVersion] = await Promise.all([
182
215
  AsyncStorage.getItem('notiflyCognitoIdToken'),
@@ -252,7 +285,6 @@ async function logEvent(eventName, eventParams, segmentation_event_param_keys =
252
285
 
253
286
  const response = await _apiCall("https://12lnng07q2.execute-api.ap-northeast-2.amazonaws.com/prod/records", requestOptions);
254
287
  const result = JSON.parse(response)
255
- console.log(result)
256
288
  if (result.message == "The incoming token has expired") {
257
289
  const [userName, password] = await Promise.all([AsyncStorage.getItem('notiflyUserName'), AsyncStorage.getItem('notiflyUserPassword')])
258
290
  const newToken = await getCognitoIdToken(userName, password);
@@ -261,9 +293,9 @@ async function logEvent(eventName, eventParams, segmentation_event_param_keys =
261
293
  }
262
294
 
263
295
  } catch (err) {
264
- console.log(err)
296
+ console.warn('[Notifly]Error: err')
265
297
  if (err == 'Error: null value') {
266
- throw new Error("[Notifly] Fail to log the event.");
298
+ throw new Error("[Notifly] Fail to log the event. Please retry the initialization.");
267
299
  }
268
300
  }
269
301
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notifly-sdk",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {