@vrplatform/log 2.0.0-alpha.9 → 2.0.1
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/build/main/log/baselog.d.ts +0 -1
- package/build/main/log/baselog.js +1 -16
- package/build/main/log/baselog.js.map +1 -1
- package/build/main/log/common.d.ts +1 -10
- package/build/main/log/common.js +3 -13
- package/build/main/log/common.js.map +1 -1
- package/build/main/log/index.d.ts +2 -2
- package/build/main/log/index.js +7 -6
- package/build/main/log/index.js.map +1 -1
- package/build/main/log/index.spec.d.ts +0 -0
- package/build/main/log/index.spec.js +2 -0
- package/build/main/log/index.spec.js.map +1 -0
- package/build/main/log/type.d.ts +21 -1
- package/build/main/log/type.js +12 -0
- package/build/main/log/type.js.map +1 -1
- package/build/main/tracking/_intercom.d.ts +109 -0
- package/build/main/tracking/_intercom.js +139 -0
- package/build/main/tracking/_intercom.js.map +1 -0
- package/build/main/tracking/eventTypes.d.ts +16 -0
- package/build/main/tracking/eventTypes.js +3 -0
- package/build/main/tracking/eventTypes.js.map +1 -0
- package/build/main/tracking/index.d.ts +106 -55
- package/build/main/tracking/index.js +180 -74
- package/build/main/tracking/index.js.map +1 -1
- package/build/main/tracking/intercom.d.ts +2 -1
- package/build/main/tracking/intercom.js.map +1 -1
- package/build/main/tracking/types.d.ts +509 -0
- package/build/main/tracking/types.js +3 -0
- package/build/main/tracking/types.js.map +1 -0
- package/build/main/utils/convertKeysToSnakeCase.d.ts +1 -0
- package/build/main/utils/convertKeysToSnakeCase.js +23 -0
- package/build/main/utils/convertKeysToSnakeCase.js.map +1 -0
- package/build/main/utils/convertValuesToString.d.ts +5 -0
- package/build/main/utils/convertValuesToString.js +11 -0
- package/build/main/utils/convertValuesToString.js.map +1 -0
- package/build/main/utils/index.d.ts +3 -0
- package/build/main/utils/index.js +20 -0
- package/build/main/utils/index.js.map +1 -0
- package/build/main/utils/isTest.d.ts +1 -0
- package/build/main/utils/isTest.js +18 -0
- package/build/main/utils/isTest.js.map +1 -0
- package/build/module/log/baselog.d.ts +0 -1
- package/build/module/log/baselog.js +1 -2
- package/build/module/log/baselog.js.map +1 -1
- package/build/module/log/common.d.ts +1 -10
- package/build/module/log/common.js +1 -11
- package/build/module/log/common.js.map +1 -1
- package/build/module/log/index.d.ts +2 -2
- package/build/module/log/index.js +2 -1
- package/build/module/log/index.js.map +1 -1
- package/build/module/log/index.spec.d.ts +0 -0
- package/build/module/log/index.spec.js +2 -0
- package/build/module/log/index.spec.js.map +1 -0
- package/build/module/log/type.d.ts +21 -1
- package/build/module/log/type.js +11 -1
- package/build/module/log/type.js.map +1 -1
- package/build/module/tracking/_intercom.d.ts +109 -0
- package/build/module/tracking/_intercom.js +136 -0
- package/build/module/tracking/_intercom.js.map +1 -0
- package/build/module/tracking/eventTypes.d.ts +16 -0
- package/build/module/tracking/eventTypes.js +2 -0
- package/build/module/tracking/eventTypes.js.map +1 -0
- package/build/module/tracking/index.d.ts +106 -55
- package/build/module/tracking/index.js +164 -72
- package/build/module/tracking/index.js.map +1 -1
- package/build/module/tracking/intercom.d.ts +2 -1
- package/build/module/tracking/intercom.js.map +1 -1
- package/build/module/tracking/types.d.ts +509 -0
- package/build/module/tracking/types.js +2 -0
- package/build/module/tracking/types.js.map +1 -0
- package/build/module/utils/convertKeysToSnakeCase.d.ts +1 -0
- package/build/module/utils/convertKeysToSnakeCase.js +19 -0
- package/build/module/utils/convertKeysToSnakeCase.js.map +1 -0
- package/build/module/utils/convertValuesToString.d.ts +5 -0
- package/build/module/utils/convertValuesToString.js +8 -0
- package/build/module/utils/convertValuesToString.js.map +1 -0
- package/build/module/utils/index.d.ts +3 -0
- package/build/module/utils/index.js +4 -0
- package/build/module/utils/index.js.map +1 -0
- package/build/module/utils/isTest.d.ts +1 -0
- package/build/module/utils/isTest.js +14 -0
- package/build/module/utils/isTest.js.map +1 -0
- package/package.json +22 -23
- package/src/log/baselog.ts +2 -3
- package/src/log/common.ts +1 -14
- package/src/log/index.ts +6 -6
- package/src/log/type.ts +28 -1
- package/src/tracking/index.ts +322 -167
- package/src/tracking/types.ts +622 -0
- package/src/utils/convertKeysToSnakeCase.ts +21 -0
- package/src/utils/convertValuesToString.ts +10 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/isTest.ts +16 -0
- package/src/tracking/intercom.ts +0 -257
package/src/tracking/intercom.ts
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import type { Log } from '../log';
|
|
2
|
-
|
|
3
|
-
export class IntercomAPI {
|
|
4
|
-
constructor(token: string, log?: Log) {
|
|
5
|
-
this.headers = {
|
|
6
|
-
'Content-Type': 'application/json',
|
|
7
|
-
Accept: 'application/json',
|
|
8
|
-
'Intercom-Version': '2.11',
|
|
9
|
-
Authorization: `Bearer ${token}`,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
this.log = log;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
private headers: Record<string, string>;
|
|
16
|
-
private log?: Log;
|
|
17
|
-
|
|
18
|
-
private async fetch<T>(
|
|
19
|
-
path: string,
|
|
20
|
-
options?: RequestInit & { data?: any }
|
|
21
|
-
): Promise<T | undefined> {
|
|
22
|
-
if (options?.data) {
|
|
23
|
-
options.body = JSON.stringify(options.data);
|
|
24
|
-
options.data = undefined;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
const result = await fetch(`https://api.intercom.io${path}`, {
|
|
29
|
-
method: 'GET',
|
|
30
|
-
headers: this.headers,
|
|
31
|
-
...options,
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
if (!result.ok) {
|
|
35
|
-
this.log?.info('Failed to fetch from Intercom', {
|
|
36
|
-
result: {
|
|
37
|
-
status: result.status,
|
|
38
|
-
statusText: result.statusText,
|
|
39
|
-
body: await result.text(),
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
if (result.status === 404) return undefined;
|
|
44
|
-
|
|
45
|
-
if (result.status === 429) {
|
|
46
|
-
console.log('Intercom rate limit exceeded, waiting for 1 minute');
|
|
47
|
-
// X-RateLimit-Reset: 1487332520 => timestamp from header response when resetting
|
|
48
|
-
|
|
49
|
-
const reset = Number(result.headers.get('X-RateLimit-Reset'));
|
|
50
|
-
|
|
51
|
-
if (reset) {
|
|
52
|
-
const now = Math.floor(Date.now() / 1000);
|
|
53
|
-
const wait = reset - now + 1;
|
|
54
|
-
console.log(`Waiting for ${wait} seconds`);
|
|
55
|
-
await new Promise((resolve) => setTimeout(resolve, wait * 1000));
|
|
56
|
-
|
|
57
|
-
return await this.fetch(path, options);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return result.status === 202 ? ({} as T) : ((await result.json()) as T);
|
|
62
|
-
} catch (error: any) {
|
|
63
|
-
this.log?.error(error);
|
|
64
|
-
return undefined;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// User
|
|
69
|
-
async getUserByUserId(userId: string) {
|
|
70
|
-
return await this.fetch<{
|
|
71
|
-
type: 'list';
|
|
72
|
-
data: { id: string }[] | undefined;
|
|
73
|
-
total_count: 0;
|
|
74
|
-
pages: {
|
|
75
|
-
type: 'pages';
|
|
76
|
-
page: 1;
|
|
77
|
-
per_page: 10;
|
|
78
|
-
total_pages: 0;
|
|
79
|
-
};
|
|
80
|
-
}>('/contacts/search', {
|
|
81
|
-
method: 'POST',
|
|
82
|
-
data: {
|
|
83
|
-
query: {
|
|
84
|
-
field: 'external_id',
|
|
85
|
-
operator: '=',
|
|
86
|
-
value: userId,
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
async createUser({
|
|
92
|
-
data,
|
|
93
|
-
userId,
|
|
94
|
-
}: {
|
|
95
|
-
userId: string;
|
|
96
|
-
data: {
|
|
97
|
-
email: string;
|
|
98
|
-
name: string;
|
|
99
|
-
custom_attributes: Record<string, any>;
|
|
100
|
-
};
|
|
101
|
-
}) {
|
|
102
|
-
return await this.fetch('/contacts', {
|
|
103
|
-
method: 'POST',
|
|
104
|
-
data: { ...data, external_id: userId },
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
async updateUser({
|
|
108
|
-
data,
|
|
109
|
-
contactId,
|
|
110
|
-
}: {
|
|
111
|
-
contactId: string;
|
|
112
|
-
data: {
|
|
113
|
-
email?: string;
|
|
114
|
-
name?: string;
|
|
115
|
-
custom_attributes: Record<string, any>;
|
|
116
|
-
};
|
|
117
|
-
}) {
|
|
118
|
-
return await this.fetch(`/contacts/${contactId}`, {
|
|
119
|
-
method: 'PUT',
|
|
120
|
-
data,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
async deleteUser(intercom_contact_id: string) {
|
|
124
|
-
return await this.fetch<{
|
|
125
|
-
id: '6657adf56abd0167d9419d1c';
|
|
126
|
-
external_id: '70';
|
|
127
|
-
type: 'contact';
|
|
128
|
-
deleted: true;
|
|
129
|
-
}>(`/contacts/${intercom_contact_id}`, {
|
|
130
|
-
method: 'DELETE',
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
async createCompanyUser(data: {
|
|
134
|
-
intercom_contact_id: string;
|
|
135
|
-
intercom_company_id: string;
|
|
136
|
-
}) {
|
|
137
|
-
return await this.fetch(`/contacts/${data.intercom_contact_id}/companies`, {
|
|
138
|
-
method: 'POST',
|
|
139
|
-
data: {
|
|
140
|
-
id: data.intercom_company_id,
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
async deleteCompanyUser({
|
|
145
|
-
intercom_company_id,
|
|
146
|
-
intercom_contact_id,
|
|
147
|
-
}: {
|
|
148
|
-
intercom_contact_id: string;
|
|
149
|
-
intercom_company_id: string;
|
|
150
|
-
}) {
|
|
151
|
-
return await this.fetch<{
|
|
152
|
-
id: '6657adc96abd0167d9419ca7';
|
|
153
|
-
}>(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
|
|
154
|
-
method: 'DELETE',
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
async trackEvent(data: {
|
|
158
|
-
user_id: string;
|
|
159
|
-
event_name: string;
|
|
160
|
-
metadata: Record<string, any>;
|
|
161
|
-
}) {
|
|
162
|
-
return await this.fetch('/events', {
|
|
163
|
-
method: 'POST',
|
|
164
|
-
data: {
|
|
165
|
-
created_at: Math.floor(new Date().getTime() / 1000), // to seconds
|
|
166
|
-
...data,
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Company
|
|
172
|
-
async getCompanyByTenantId(tenantId: string) {
|
|
173
|
-
return await this.fetch<{
|
|
174
|
-
type: 'list';
|
|
175
|
-
data:
|
|
176
|
-
| [
|
|
177
|
-
{
|
|
178
|
-
type: 'company';
|
|
179
|
-
company_id: 'remote_companies_scroll_2';
|
|
180
|
-
id: '664df5436abd01f7945e1a78';
|
|
181
|
-
app_id: 'this_is_an_id111_that_should_be_at_least_';
|
|
182
|
-
name: 'IntercomQATest1';
|
|
183
|
-
remote_created_at: 1716385091;
|
|
184
|
-
created_at: 1716385091;
|
|
185
|
-
updated_at: 1716385091;
|
|
186
|
-
monthly_spend: 0;
|
|
187
|
-
session_count: 0;
|
|
188
|
-
user_count: 4;
|
|
189
|
-
tags: {
|
|
190
|
-
type: 'tag.list';
|
|
191
|
-
tags: [];
|
|
192
|
-
};
|
|
193
|
-
segments: {
|
|
194
|
-
type: 'segment.list';
|
|
195
|
-
segments: [];
|
|
196
|
-
};
|
|
197
|
-
custom_attributes: Record<string, string>;
|
|
198
|
-
},
|
|
199
|
-
]
|
|
200
|
-
| undefined;
|
|
201
|
-
pages: {
|
|
202
|
-
type: 'pages';
|
|
203
|
-
next: null;
|
|
204
|
-
page: 1;
|
|
205
|
-
per_page: 15;
|
|
206
|
-
total_pages: 1;
|
|
207
|
-
};
|
|
208
|
-
total_count: 1;
|
|
209
|
-
}>(`/companies?company_id=${tenantId}`, {
|
|
210
|
-
method: 'GET',
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
async createCompany({
|
|
214
|
-
data,
|
|
215
|
-
tenantId,
|
|
216
|
-
}: {
|
|
217
|
-
tenantId: string;
|
|
218
|
-
data: {
|
|
219
|
-
name: string;
|
|
220
|
-
remote_created_at: string;
|
|
221
|
-
custom_attributes: Record<string, any>;
|
|
222
|
-
};
|
|
223
|
-
}) {
|
|
224
|
-
return await this.fetch('/companies', {
|
|
225
|
-
method: 'POST',
|
|
226
|
-
data: {
|
|
227
|
-
...data,
|
|
228
|
-
company_id: tenantId,
|
|
229
|
-
},
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
async updateCompany({
|
|
233
|
-
data,
|
|
234
|
-
companyId,
|
|
235
|
-
}: {
|
|
236
|
-
companyId: string;
|
|
237
|
-
data: Record<string, any>;
|
|
238
|
-
}) {
|
|
239
|
-
return await this.fetch(`/companies/${companyId}`, {
|
|
240
|
-
method: 'PUT',
|
|
241
|
-
data,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
async deleteCompany(intercom_company_id: string) {
|
|
245
|
-
return await this.fetch<{
|
|
246
|
-
id: '6657adc96abd0167d9419ca7';
|
|
247
|
-
object: 'company';
|
|
248
|
-
deleted: true;
|
|
249
|
-
}>(`/companies/${intercom_company_id}`, {
|
|
250
|
-
method: 'DELETE',
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export function useIntercom(token: string, log?: Log) {
|
|
256
|
-
return new IntercomAPI(token, log);
|
|
257
|
-
}
|