@engage_so/core 2.0.0 → 2.0.2
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/dist/error.d.ts +4 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +10 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +257 -0
- package/package.json +2 -2
package/dist/error.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAI7B"}
|
package/dist/error.js
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.EngageError = void 0;
|
4
|
+
class EngageError extends Error {
|
5
|
+
constructor(message) {
|
6
|
+
super(message);
|
7
|
+
this.name = 'EngageError';
|
8
|
+
}
|
9
|
+
}
|
10
|
+
exports.EngageError = EngageError;
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
interface Key {
|
2
|
+
key?: string;
|
3
|
+
secret?: string;
|
4
|
+
}
|
5
|
+
interface EventParameter {
|
6
|
+
event: string;
|
7
|
+
value?: string | number | Date | boolean;
|
8
|
+
properties?: {
|
9
|
+
[key: string]: string | number | Date | boolean;
|
10
|
+
};
|
11
|
+
timestamp?: string | number | Date;
|
12
|
+
}
|
13
|
+
type UserAttrParams = {
|
14
|
+
[key: string]: string | number | Date | boolean;
|
15
|
+
};
|
16
|
+
type UserIdentifyParams = UserAttrParams & {
|
17
|
+
id: string;
|
18
|
+
};
|
19
|
+
type Methods = 'POST' | 'PUT' | 'DELETE';
|
20
|
+
export declare function request(url: string, params: Record<string, any> | null, method: Methods): object;
|
21
|
+
export declare function init(key: Key | string): void;
|
22
|
+
export declare function identify(user: UserIdentifyParams): Promise<any>;
|
23
|
+
export declare function addAttribute(uid: string, attributes: UserAttrParams): Promise<any>;
|
24
|
+
export declare function track(uid: string, data: EventParameter): Promise<any>;
|
25
|
+
export declare function merge(sourceUid: string, destinationUid: string): Promise<any>;
|
26
|
+
export declare function addToAccount(uid: string, accountId: string, role: string): Promise<any>;
|
27
|
+
export declare function removeFromAccount(uid: string, accountId: string): Promise<any>;
|
28
|
+
export declare function changeAccountRole(uid: string, accountId: string, role: string): Promise<any>;
|
29
|
+
export declare function convertToCustomer(uid: string): Promise<any>;
|
30
|
+
export declare function convertToAccount(uid: string): Promise<any>;
|
31
|
+
export {};
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAUA,UAAU,GAAG;IACX,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AACD,UAAU,cAAc;IACtB,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,CAAA;IACxC,UAAU,CAAC,EAAE;QACX,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,CAAA;KAChD,CAAA;IACD,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CACnC;AACD,KAAK,cAAc,GAAG;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,CAAA;CAChD,CAAA;AACD,KAAK,kBAAkB,GAAG,cAAc,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CAAA;AAQzD,KAAK,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAA;AA2CxC,wBAAgB,OAAO,CAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAEjG;AAED,wBAAgB,IAAI,CAAE,GAAG,EAAE,GAAG,GAAG,MAAM,QAwBtC;AAGD,wBAAsB,QAAQ,CAAE,IAAI,EAAE,kBAAkB,gBAqBvD;AACD,wBAAsB,YAAY,CAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc,gBAyB1E;AACD,wBAAsB,KAAK,CAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,gBAmB7D;AAED,wBAAsB,KAAK,CAAE,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,gBAYrE;AAGD,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,gBAiB9E;AACD,wBAAsB,iBAAiB,CAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,gBAQtE;AACD,wBAAsB,iBAAiB,CAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,gBAWpF;AACD,wBAAsB,iBAAiB,CAAE,GAAG,EAAE,MAAM,gBAKnD;AACD,wBAAsB,gBAAgB,CAAE,GAAG,EAAE,MAAM,gBAKlD"}
|
package/dist/index.js
ADDED
@@ -0,0 +1,257 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.convertToAccount = exports.convertToCustomer = exports.changeAccountRole = exports.removeFromAccount = exports.addToAccount = exports.merge = exports.track = exports.addAttribute = exports.identify = exports.init = exports.request = void 0;
|
16
|
+
const buffer_1 = require("buffer");
|
17
|
+
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
18
|
+
const error_1 = require("./error");
|
19
|
+
if (typeof btoa === 'undefined') {
|
20
|
+
global.btoa = function (str) {
|
21
|
+
return buffer_1.Buffer.from(str).toString('base64');
|
22
|
+
};
|
23
|
+
}
|
24
|
+
// type UserIdentifyParams = {
|
25
|
+
// id: string
|
26
|
+
// [key: string]: string | number | Date | boolean
|
27
|
+
// }
|
28
|
+
// type UserAttrParams = Omit<UserIdentifyParams, 'id'>
|
29
|
+
const rootURL = 'https://api.engage.so/v1';
|
30
|
+
let auth = '';
|
31
|
+
const notMeta = ['created_at', 'is_account', 'number', 'device_token', 'device_platform', 'email', 'first_name', 'last_name', 'tz', 'app_version', 'app_build', 'app_last_active'];
|
32
|
+
const apiRoot = 'https://api.engage.so/';
|
33
|
+
function _request(url, params, method) {
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
35
|
+
try {
|
36
|
+
const o = {
|
37
|
+
method,
|
38
|
+
headers: {
|
39
|
+
'Content-Type': 'application/json;charset=utf-8',
|
40
|
+
Authorization: `Basic ${auth}`
|
41
|
+
},
|
42
|
+
// throwHttpErrors: false,
|
43
|
+
// prefixUrl: rootURL
|
44
|
+
};
|
45
|
+
if (params) {
|
46
|
+
o.body = JSON.stringify(params);
|
47
|
+
}
|
48
|
+
// const response = await ky(url, o)
|
49
|
+
const response = yield (0, cross_fetch_1.default)(`${apiRoot}${url}`, o);
|
50
|
+
const body = yield response.json();
|
51
|
+
let error = 'API connection error';
|
52
|
+
if (!response.ok) {
|
53
|
+
if (body && body.error) {
|
54
|
+
error = body.error;
|
55
|
+
}
|
56
|
+
return { error };
|
57
|
+
}
|
58
|
+
return body;
|
59
|
+
}
|
60
|
+
catch (e) {
|
61
|
+
return { error: 'API connection error' };
|
62
|
+
}
|
63
|
+
});
|
64
|
+
}
|
65
|
+
// Alias of _request method
|
66
|
+
// Same with _request for now but can later have modifications
|
67
|
+
function request(url, params, method) {
|
68
|
+
return _request(url, params, method);
|
69
|
+
}
|
70
|
+
exports.request = request;
|
71
|
+
function init(key) {
|
72
|
+
if (!key) {
|
73
|
+
throw new error_1.EngageError('You need to pass in your API key(s).');
|
74
|
+
}
|
75
|
+
const options = {
|
76
|
+
key: undefined,
|
77
|
+
secret: ''
|
78
|
+
};
|
79
|
+
if (typeof key === 'string') {
|
80
|
+
options.key = key;
|
81
|
+
}
|
82
|
+
else {
|
83
|
+
if (!key.key) {
|
84
|
+
throw new error_1.EngageError('`key` missing in object.');
|
85
|
+
}
|
86
|
+
if (key.key) {
|
87
|
+
options.key = `${key.key}`;
|
88
|
+
}
|
89
|
+
if (key.secret) {
|
90
|
+
options.secret = `${key.secret}`;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
// Set auth
|
94
|
+
// auth = Buffer.from(`${options.key}:${options.secret}`).toString('base64')
|
95
|
+
auth = btoa(`${options.key}:${options.secret}`);
|
96
|
+
}
|
97
|
+
exports.init = init;
|
98
|
+
// Data tracking
|
99
|
+
function identify(user) {
|
100
|
+
return __awaiter(this, void 0, void 0, function* () {
|
101
|
+
if (!user) {
|
102
|
+
throw new error_1.EngageError('You need to pass an object with at least an id.');
|
103
|
+
}
|
104
|
+
if (!user.id) {
|
105
|
+
throw new error_1.EngageError('ID missing.');
|
106
|
+
}
|
107
|
+
if (user.email && (typeof user.email !== 'string' || !/^\S+@\S+$/.test(user.email))) {
|
108
|
+
throw new error_1.EngageError('Email invalid.');
|
109
|
+
}
|
110
|
+
const params = {};
|
111
|
+
params.meta = {};
|
112
|
+
for (const k in user) {
|
113
|
+
if (k === 'id' || notMeta.includes(k)) {
|
114
|
+
params[k] = user[k];
|
115
|
+
}
|
116
|
+
else {
|
117
|
+
params.meta[k] = user[k];
|
118
|
+
}
|
119
|
+
}
|
120
|
+
return _request(`users/${user.id}`, params, 'PUT');
|
121
|
+
});
|
122
|
+
}
|
123
|
+
exports.identify = identify;
|
124
|
+
function addAttribute(uid, attributes) {
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
126
|
+
if (!uid) {
|
127
|
+
throw new error_1.EngageError('User ID missing.');
|
128
|
+
}
|
129
|
+
if (!attributes) {
|
130
|
+
throw new error_1.EngageError('Attributes missing.');
|
131
|
+
}
|
132
|
+
if (!Object.keys(attributes).length) {
|
133
|
+
throw new error_1.EngageError('Attributes missing.');
|
134
|
+
}
|
135
|
+
const params = {};
|
136
|
+
params.meta = {};
|
137
|
+
for (const k in attributes) {
|
138
|
+
if (notMeta.includes(k)) {
|
139
|
+
params[k] = attributes[k];
|
140
|
+
}
|
141
|
+
else {
|
142
|
+
params.meta[k] = attributes[k];
|
143
|
+
Object.assign(params.meta, k, attributes[k]);
|
144
|
+
}
|
145
|
+
}
|
146
|
+
if (Object.keys(params.meta).length) {
|
147
|
+
delete params.meta;
|
148
|
+
}
|
149
|
+
return _request(`users/${uid}`, params, 'PUT');
|
150
|
+
});
|
151
|
+
}
|
152
|
+
exports.addAttribute = addAttribute;
|
153
|
+
function track(uid, data) {
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
155
|
+
if (!uid) {
|
156
|
+
throw new error_1.EngageError('User ID missing.');
|
157
|
+
}
|
158
|
+
if (!data) {
|
159
|
+
throw new error_1.EngageError('Event data missing.');
|
160
|
+
}
|
161
|
+
if (typeof data === 'string') {
|
162
|
+
data = {
|
163
|
+
event: data,
|
164
|
+
value: true
|
165
|
+
};
|
166
|
+
}
|
167
|
+
else {
|
168
|
+
if (!Object.keys(data).length) {
|
169
|
+
throw new error_1.EngageError('Attributes missing.');
|
170
|
+
}
|
171
|
+
}
|
172
|
+
return _request(`users/${uid}/events`, data, 'POST');
|
173
|
+
});
|
174
|
+
}
|
175
|
+
exports.track = track;
|
176
|
+
function merge(sourceUid, destinationUid) {
|
177
|
+
return __awaiter(this, void 0, void 0, function* () {
|
178
|
+
if (!sourceUid) {
|
179
|
+
throw new error_1.EngageError('Source ID missing.');
|
180
|
+
}
|
181
|
+
if (!destinationUid) {
|
182
|
+
throw new error_1.EngageError('Destination ID missing.');
|
183
|
+
}
|
184
|
+
return _request(`users/merge`, {
|
185
|
+
source: sourceUid,
|
186
|
+
destination: destinationUid
|
187
|
+
}, 'POST');
|
188
|
+
});
|
189
|
+
}
|
190
|
+
exports.merge = merge;
|
191
|
+
// Account functions
|
192
|
+
function addToAccount(uid, accountId, role) {
|
193
|
+
return __awaiter(this, void 0, void 0, function* () {
|
194
|
+
if (!uid) {
|
195
|
+
throw new error_1.EngageError('User ID missing.');
|
196
|
+
}
|
197
|
+
if (!accountId) {
|
198
|
+
throw new error_1.EngageError('Account ID missing.');
|
199
|
+
}
|
200
|
+
if (role && typeof role !== 'string') {
|
201
|
+
throw new error_1.EngageError('Role should be a text.');
|
202
|
+
}
|
203
|
+
const g = {
|
204
|
+
id: accountId
|
205
|
+
};
|
206
|
+
if (role) {
|
207
|
+
g.role = role;
|
208
|
+
}
|
209
|
+
return _request(`users/${uid}/accounts`, { accounts: [g] }, 'POST');
|
210
|
+
});
|
211
|
+
}
|
212
|
+
exports.addToAccount = addToAccount;
|
213
|
+
function removeFromAccount(uid, accountId) {
|
214
|
+
return __awaiter(this, void 0, void 0, function* () {
|
215
|
+
if (!uid) {
|
216
|
+
throw new error_1.EngageError('User ID missing.');
|
217
|
+
}
|
218
|
+
if (!accountId) {
|
219
|
+
throw new error_1.EngageError('Account ID missing.');
|
220
|
+
}
|
221
|
+
return _request(`users/${uid}/accounts/${accountId}`, null, 'DELETE');
|
222
|
+
});
|
223
|
+
}
|
224
|
+
exports.removeFromAccount = removeFromAccount;
|
225
|
+
function changeAccountRole(uid, accountId, role) {
|
226
|
+
return __awaiter(this, void 0, void 0, function* () {
|
227
|
+
if (!uid) {
|
228
|
+
throw new error_1.EngageError('User ID missing.');
|
229
|
+
}
|
230
|
+
if (!accountId) {
|
231
|
+
throw new error_1.EngageError('Account ID missing.');
|
232
|
+
}
|
233
|
+
if (!role) {
|
234
|
+
throw new error_1.EngageError('New role missing.');
|
235
|
+
}
|
236
|
+
return _request(`users/${uid}/accounts/${accountId}`, { role }, 'PUT');
|
237
|
+
});
|
238
|
+
}
|
239
|
+
exports.changeAccountRole = changeAccountRole;
|
240
|
+
function convertToCustomer(uid) {
|
241
|
+
return __awaiter(this, void 0, void 0, function* () {
|
242
|
+
if (!uid) {
|
243
|
+
throw new error_1.EngageError('User ID missing.');
|
244
|
+
}
|
245
|
+
return _request(`users/${uid}/convert`, { type: 'customer' }, 'POST');
|
246
|
+
});
|
247
|
+
}
|
248
|
+
exports.convertToCustomer = convertToCustomer;
|
249
|
+
function convertToAccount(uid) {
|
250
|
+
return __awaiter(this, void 0, void 0, function* () {
|
251
|
+
if (!uid) {
|
252
|
+
throw new error_1.EngageError('User ID missing.');
|
253
|
+
}
|
254
|
+
return _request(`users/${uid}/convert`, { type: 'account' }, 'POST');
|
255
|
+
});
|
256
|
+
}
|
257
|
+
exports.convertToAccount = convertToAccount;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@engage_so/core",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.2",
|
4
4
|
"description": "Engage JS core.",
|
5
5
|
"main": "./dist/index.js",
|
6
6
|
"types": "./dist/index.d.ts",
|
@@ -25,5 +25,5 @@
|
|
25
25
|
"dependencies": {
|
26
26
|
"cross-fetch": "^4.0.0"
|
27
27
|
},
|
28
|
-
"gitHead": "
|
28
|
+
"gitHead": "b3efe1705878e55156fe82fd8b9c1a3c52f4ed04"
|
29
29
|
}
|