@suprsend/node-sdk 0.0.4 → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/event.js +205 -0
- package/dist/identity.js +417 -0
- package/dist/identity_helper.js +648 -0
- package/dist/index.js +12 -0
- package/dist/request_json/event.json +47 -0
- package/dist/request_json/workflow.json +49 -3
- package/dist/signature.js +1 -1
- package/dist/utils.js +42 -32
- package/dist/workflow.js +4 -2
- package/package.json +6 -5
- package/src/event.js +148 -0
- package/src/identity.js +281 -0
- package/src/identity_helper.js +463 -0
- package/src/index.js +8 -0
- package/src/request_json/event.json +47 -0
- package/src/request_json/workflow.json +49 -3
- package/src/signature.js +1 -4
- package/src/utils.js +28 -23
- package/src/workflow.js +5 -3
package/src/identity.js
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import {
|
|
2
|
+
is_object,
|
|
3
|
+
SuprsendError,
|
|
4
|
+
epoch_milliseconds,
|
|
5
|
+
uuid,
|
|
6
|
+
is_empty,
|
|
7
|
+
is_string,
|
|
8
|
+
} from "./utils";
|
|
9
|
+
import get_request_signature from "./signature";
|
|
10
|
+
import axios from "axios";
|
|
11
|
+
import _IdentityEventInternalHelper from "./identity_helper";
|
|
12
|
+
|
|
13
|
+
export default class UserIdentityFactory {
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.config = config;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
new_user(distinct_id) {
|
|
19
|
+
if (!is_string(distinct_id)) {
|
|
20
|
+
throw new SuprsendError(
|
|
21
|
+
"distinct_id must be a string. an Id which uniquely identify a user in your app"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
distinct_id = distinct_id.trim();
|
|
25
|
+
if (!distinct_id) {
|
|
26
|
+
throw new SuprsendError("distinct_id must be passed");
|
|
27
|
+
}
|
|
28
|
+
return new UserIdentity(this.config, distinct_id);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class UserIdentity {
|
|
33
|
+
constructor(config, distinct_id) {
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.distinct_id = distinct_id;
|
|
36
|
+
this.__url = this.__get_url();
|
|
37
|
+
this.__supr_props = this.__super_properties();
|
|
38
|
+
|
|
39
|
+
this.__errors = [];
|
|
40
|
+
this.__info = [];
|
|
41
|
+
this._append_count = 0;
|
|
42
|
+
this._remove_count = 0;
|
|
43
|
+
this._events = [];
|
|
44
|
+
this._helper = new _IdentityEventInternalHelper(
|
|
45
|
+
distinct_id,
|
|
46
|
+
config.env_key
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
__get_url() {
|
|
51
|
+
let url_template = "event/";
|
|
52
|
+
if (this.config.include_signature_param) {
|
|
53
|
+
if (this.config.auth_enabled) {
|
|
54
|
+
url_template = url_template + "?verify=true";
|
|
55
|
+
} else {
|
|
56
|
+
url_template = url_template + "?verify=false";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return `${this.config.base_url}${url_template}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
__get_headers() {
|
|
63
|
+
return {
|
|
64
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
65
|
+
Date: new Date().toUTCString(),
|
|
66
|
+
"User-Agent": this.config.user_agent,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
__super_properties() {
|
|
71
|
+
return {
|
|
72
|
+
$ss_sdk_version: this.config.user_agent,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
__get_events() {
|
|
77
|
+
let all_events = this._events;
|
|
78
|
+
for (let e of all_events) {
|
|
79
|
+
e["properties"] = this.__supr_props;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (this._append_count > 0) {
|
|
83
|
+
const user_identify_event = {
|
|
84
|
+
$insert_id: uuid(),
|
|
85
|
+
$time: epoch_milliseconds(),
|
|
86
|
+
env: this.config.env_key,
|
|
87
|
+
event: "$identify",
|
|
88
|
+
properties: {
|
|
89
|
+
$anon_id: this.distinct_id,
|
|
90
|
+
$identified_id: this.distinct_id,
|
|
91
|
+
...this.__super_properties,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
all_events.push(user_identify_event);
|
|
95
|
+
}
|
|
96
|
+
return all_events;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
__validate_body() {
|
|
100
|
+
if (!is_empty(this.__info)) {
|
|
101
|
+
console.log("WARNING: " + this.__info.join("\n"));
|
|
102
|
+
}
|
|
103
|
+
if (!is_empty(this.__errors)) {
|
|
104
|
+
throw new SuprsendError("ERROR: " + this.__errors.join("\n"));
|
|
105
|
+
}
|
|
106
|
+
if (is_empty(this._events)) {
|
|
107
|
+
throw new SuprsendError(
|
|
108
|
+
"ERROR: no user properties have been edited. Use user.append/remove/unset method to update user properties"
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async save() {
|
|
114
|
+
this.__validate_body();
|
|
115
|
+
const headers = this.__get_headers();
|
|
116
|
+
const events = this.__get_events();
|
|
117
|
+
const content_text = JSON.stringify(events);
|
|
118
|
+
if (this.config.auth_enabled) {
|
|
119
|
+
const signature = get_request_signature(
|
|
120
|
+
this.__url,
|
|
121
|
+
"POST",
|
|
122
|
+
content_text,
|
|
123
|
+
headers,
|
|
124
|
+
this.config.env_secret
|
|
125
|
+
);
|
|
126
|
+
headers["Authorization"] = `${this.config.env_key}:${signature}`;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
const response = await axios.post(this.__url, content_text, {
|
|
130
|
+
headers,
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
status_code: response.status,
|
|
134
|
+
success: true,
|
|
135
|
+
message: response.statusText,
|
|
136
|
+
};
|
|
137
|
+
} catch (err) {
|
|
138
|
+
return {
|
|
139
|
+
status_code: 400,
|
|
140
|
+
success: false,
|
|
141
|
+
message: err.message,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
_collect_event() {
|
|
147
|
+
const resp = this._helper.get_identity_events();
|
|
148
|
+
if (!is_empty(resp["errors"])) {
|
|
149
|
+
this.__errors = [...this.__errors, ...resp["errors"]];
|
|
150
|
+
}
|
|
151
|
+
if (!is_empty(resp["info"])) {
|
|
152
|
+
this.__info = [...this.__info, ...resp["info"]];
|
|
153
|
+
}
|
|
154
|
+
if (!is_empty(resp["event"])) {
|
|
155
|
+
this._events.push(resp["event"]);
|
|
156
|
+
this._append_count += resp["append"];
|
|
157
|
+
this._remove_count += resp["remove"];
|
|
158
|
+
this._unset_count += resp["unset"];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
append(key, value) {
|
|
163
|
+
const caller = "append";
|
|
164
|
+
if (!is_string(key) && !is_object(key)) {
|
|
165
|
+
this.__errors.push(`[${caller}] arg1 must be either string or a dict`);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (is_string(key)) {
|
|
169
|
+
if (!value) {
|
|
170
|
+
this.__errors.push(
|
|
171
|
+
`[${caller}] if arg1 is a string, then arg2 must be passed`
|
|
172
|
+
);
|
|
173
|
+
return;
|
|
174
|
+
} else {
|
|
175
|
+
this._helper._append_kv(key, value, {}, caller);
|
|
176
|
+
this._collect_event();
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
for (let item in key) {
|
|
180
|
+
this._helper._append_kv(item, key[item], key, caller);
|
|
181
|
+
}
|
|
182
|
+
this._collect_event();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
remove(key, value) {
|
|
187
|
+
const caller = "remove";
|
|
188
|
+
if (!is_string(key) && !is_object(key)) {
|
|
189
|
+
this.__errors.push(`[${caller}] arg1 must be either string or a dict`);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (is_string(key)) {
|
|
193
|
+
if (!value) {
|
|
194
|
+
this.__errors.push(
|
|
195
|
+
`[${caller}] if arg1 is a string, then arg2 must be passed`
|
|
196
|
+
);
|
|
197
|
+
return;
|
|
198
|
+
} else {
|
|
199
|
+
this._helper._remove_kv(key, value, {}, caller);
|
|
200
|
+
this._collect_event();
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
for (let item in key) {
|
|
204
|
+
this._helper._remove_kv(item, key[item], key, caller);
|
|
205
|
+
}
|
|
206
|
+
this._collect_event();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
add_email(email) {
|
|
211
|
+
const caller = "add_email";
|
|
212
|
+
this._helper._add_email(email, caller);
|
|
213
|
+
this._collect_event();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
remove_email(email) {
|
|
217
|
+
const caller = "remove_email";
|
|
218
|
+
this._helper._remove_email(email, caller);
|
|
219
|
+
this._collect_event();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
add_sms(mobile_no) {
|
|
223
|
+
const caller = "add_sms";
|
|
224
|
+
this._helper._add_sms(mobile_no, caller);
|
|
225
|
+
this._collect_event();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
remove_sms(mobile_no) {
|
|
229
|
+
const caller = "remove_sms";
|
|
230
|
+
this._helper._remove_sms(mobile_no, caller);
|
|
231
|
+
this._collect_event();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
add_whatsapp(mobile_no) {
|
|
235
|
+
const caller = "add_whatsapp";
|
|
236
|
+
this._helper._add_whatsapp(mobile_no, caller);
|
|
237
|
+
this._collect_event();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
remove_whatsapp(mobile_no) {
|
|
241
|
+
const caller = "remove_whatsapp";
|
|
242
|
+
this._helper._remove_whatsapp(mobile_no, caller);
|
|
243
|
+
this._collect_event();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
add_androidpush(push_token, provider = "fcm") {
|
|
247
|
+
const caller = "add_androidpush";
|
|
248
|
+
this._helper._add_androidpush(push_token, provider, caller);
|
|
249
|
+
this._collect_event();
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
remove_androidpush(push_token, provider = "fcm") {
|
|
253
|
+
const caller = "remove_androidpush";
|
|
254
|
+
this._helper._remove_androidpush(push_token, provider, caller);
|
|
255
|
+
this._collect_event();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
add_iospush(push_token, provider = "apns") {
|
|
259
|
+
const caller = "add_iospush";
|
|
260
|
+
this._helper._add_iospush(push_token, provider, caller);
|
|
261
|
+
this._collect_event();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
remove_iospush(push_token, provider = "apns") {
|
|
265
|
+
const caller = "remove_iospush";
|
|
266
|
+
this._helper._remove_iospush(push_token, provider, caller);
|
|
267
|
+
this._collect_event();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
add_webpush(push_token, provider = "vapid") {
|
|
271
|
+
const caller = "add_webpush";
|
|
272
|
+
this._helper._add_webpush(push_token, provider, caller);
|
|
273
|
+
this._collect_event();
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
remove_webpush(push_token, provider = "vapid") {
|
|
277
|
+
const caller = "remove_webpush";
|
|
278
|
+
this._helper._remove_webpush(push_token, provider, caller);
|
|
279
|
+
this._collect_event();
|
|
280
|
+
}
|
|
281
|
+
}
|