@boldvideo/bold-js 0.3.5 → 0.4.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/dist/index.cjs +37 -10
- package/dist/index.js +37 -10
- package/package.json +1 -1
- package/src/lib/client.ts +39 -12
- package/src/lib/fetchers.ts +11 -1
- package/src/lib/types.ts +2 -0
package/dist/index.cjs
CHANGED
|
@@ -72,9 +72,11 @@ __export(src_exports, {
|
|
|
72
72
|
module.exports = __toCommonJS(src_exports);
|
|
73
73
|
|
|
74
74
|
// src/lib/client.ts
|
|
75
|
-
var
|
|
75
|
+
var import_axios2 = __toESM(require("axios"), 1);
|
|
76
|
+
var import_js_cookie = __toESM(require("js-cookie"), 1);
|
|
76
77
|
|
|
77
78
|
// src/lib/fetchers.ts
|
|
79
|
+
var import_axios = __toESM(require("axios"), 1);
|
|
78
80
|
function get(client, url) {
|
|
79
81
|
return __async(this, null, function* () {
|
|
80
82
|
try {
|
|
@@ -84,7 +86,16 @@ function get(client, url) {
|
|
|
84
86
|
}
|
|
85
87
|
return res.data;
|
|
86
88
|
} catch (error) {
|
|
87
|
-
|
|
89
|
+
if (import_axios.default.isAxiosError(error)) {
|
|
90
|
+
console.error("fetching error details:", {
|
|
91
|
+
message: error.message,
|
|
92
|
+
code: error.code,
|
|
93
|
+
config: error.config,
|
|
94
|
+
response: error.response
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
console.error(`Error fetching data from URL: ${url}`, error);
|
|
98
|
+
}
|
|
88
99
|
throw error;
|
|
89
100
|
}
|
|
90
101
|
});
|
|
@@ -202,14 +213,15 @@ function sendEvent(client, eventName, data, debug) {
|
|
|
202
213
|
var [throttledSendEvent] = throttle(sendEvent, 5e3);
|
|
203
214
|
function trackEvent(client, userId, options) {
|
|
204
215
|
return (video, event) => {
|
|
216
|
+
var _a;
|
|
205
217
|
const eventDetails = __spreadProps(__spreadValues({}, basicInfos()), {
|
|
206
218
|
userId,
|
|
207
219
|
videoId: video.id,
|
|
208
220
|
title: video.title,
|
|
209
221
|
videoDuration: video.duration,
|
|
210
|
-
currentTime: event.target.currentTime || 0
|
|
222
|
+
currentTime: ((_a = event.target) == null ? void 0 : _a.currentTime) || 0
|
|
211
223
|
});
|
|
212
|
-
if (event.type == "timeupdate") {
|
|
224
|
+
if (event.type == "timeupdate" || event.type == "time-update") {
|
|
213
225
|
throttledSendEvent(
|
|
214
226
|
client,
|
|
215
227
|
getEventName(event),
|
|
@@ -230,14 +242,16 @@ function trackPageView(client, userId, options) {
|
|
|
230
242
|
sendEvent(client, "page_view", eventDetails, options.debug);
|
|
231
243
|
};
|
|
232
244
|
}
|
|
233
|
-
function getEventName(
|
|
234
|
-
switch (type) {
|
|
245
|
+
function getEventName(event) {
|
|
246
|
+
switch (event.type) {
|
|
235
247
|
case "pause":
|
|
236
248
|
return "video_pause";
|
|
237
249
|
case "play":
|
|
238
250
|
return "video_resume";
|
|
239
251
|
case "loadedmetadata":
|
|
252
|
+
case "loaded-metadata":
|
|
240
253
|
return "video_load";
|
|
254
|
+
case "time-update":
|
|
241
255
|
case "timeupdate":
|
|
242
256
|
return "video_progress";
|
|
243
257
|
default:
|
|
@@ -247,7 +261,7 @@ function getEventName({ type }) {
|
|
|
247
261
|
function basicInfos() {
|
|
248
262
|
return {
|
|
249
263
|
url: location.href,
|
|
250
|
-
domain:
|
|
264
|
+
domain: location.hostname,
|
|
251
265
|
referrer: document.referrer || null,
|
|
252
266
|
deviceWidth: window.innerWidth,
|
|
253
267
|
userAgent: navigator.userAgent
|
|
@@ -255,26 +269,39 @@ function basicInfos() {
|
|
|
255
269
|
}
|
|
256
270
|
|
|
257
271
|
// src/lib/client.ts
|
|
272
|
+
var generateUserId = () => {
|
|
273
|
+
return [...Array(30)].map(() => Math.random().toString(36)[2]).join("");
|
|
274
|
+
};
|
|
275
|
+
var getUserId = () => {
|
|
276
|
+
const localStorageKey = "boldvideo_user_id";
|
|
277
|
+
let userId = import_js_cookie.default.get(localStorageKey);
|
|
278
|
+
if (!userId) {
|
|
279
|
+
userId = generateUserId();
|
|
280
|
+
import_js_cookie.default.set(localStorageKey, userId, { expires: 365 });
|
|
281
|
+
}
|
|
282
|
+
return userId;
|
|
283
|
+
};
|
|
258
284
|
function createClient(apiKey, options = { debug: false }) {
|
|
259
285
|
var _a;
|
|
260
286
|
if (!apiKey || typeof apiKey !== "string") {
|
|
261
287
|
throw new Error("API key is missing or invalid");
|
|
262
288
|
}
|
|
289
|
+
const userId = getUserId();
|
|
263
290
|
const { debug } = options;
|
|
264
291
|
const apiClientOptions = {
|
|
265
292
|
baseURL: (_a = options.baseURL) != null ? _a : "https://app.boldvideo.io/api/v1/",
|
|
266
293
|
headers: {
|
|
267
|
-
Authorization: apiKey
|
|
294
|
+
Authorization: apiKey,
|
|
295
|
+
"X-User-Id": userId
|
|
268
296
|
}
|
|
269
297
|
};
|
|
270
298
|
let apiClient;
|
|
271
299
|
try {
|
|
272
|
-
apiClient =
|
|
300
|
+
apiClient = import_axios2.default.create(apiClientOptions);
|
|
273
301
|
} catch (error) {
|
|
274
302
|
console.error("Error creating API client", error);
|
|
275
303
|
throw error;
|
|
276
304
|
}
|
|
277
|
-
const userId = [...Array(30)].map(() => Math.random().toString(36)[2]).join("");
|
|
278
305
|
return {
|
|
279
306
|
settings: fetchSettings(apiClient),
|
|
280
307
|
videos: {
|
package/dist/index.js
CHANGED
|
@@ -39,9 +39,11 @@ var __async = (__this, __arguments, generator) => {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
// src/lib/client.ts
|
|
42
|
-
import
|
|
42
|
+
import axios2 from "axios";
|
|
43
|
+
import Cookies from "js-cookie";
|
|
43
44
|
|
|
44
45
|
// src/lib/fetchers.ts
|
|
46
|
+
import axios from "axios";
|
|
45
47
|
function get(client, url) {
|
|
46
48
|
return __async(this, null, function* () {
|
|
47
49
|
try {
|
|
@@ -51,7 +53,16 @@ function get(client, url) {
|
|
|
51
53
|
}
|
|
52
54
|
return res.data;
|
|
53
55
|
} catch (error) {
|
|
54
|
-
|
|
56
|
+
if (axios.isAxiosError(error)) {
|
|
57
|
+
console.error("fetching error details:", {
|
|
58
|
+
message: error.message,
|
|
59
|
+
code: error.code,
|
|
60
|
+
config: error.config,
|
|
61
|
+
response: error.response
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
console.error(`Error fetching data from URL: ${url}`, error);
|
|
65
|
+
}
|
|
55
66
|
throw error;
|
|
56
67
|
}
|
|
57
68
|
});
|
|
@@ -169,14 +180,15 @@ function sendEvent(client, eventName, data, debug) {
|
|
|
169
180
|
var [throttledSendEvent] = throttle(sendEvent, 5e3);
|
|
170
181
|
function trackEvent(client, userId, options) {
|
|
171
182
|
return (video, event) => {
|
|
183
|
+
var _a;
|
|
172
184
|
const eventDetails = __spreadProps(__spreadValues({}, basicInfos()), {
|
|
173
185
|
userId,
|
|
174
186
|
videoId: video.id,
|
|
175
187
|
title: video.title,
|
|
176
188
|
videoDuration: video.duration,
|
|
177
|
-
currentTime: event.target.currentTime || 0
|
|
189
|
+
currentTime: ((_a = event.target) == null ? void 0 : _a.currentTime) || 0
|
|
178
190
|
});
|
|
179
|
-
if (event.type == "timeupdate") {
|
|
191
|
+
if (event.type == "timeupdate" || event.type == "time-update") {
|
|
180
192
|
throttledSendEvent(
|
|
181
193
|
client,
|
|
182
194
|
getEventName(event),
|
|
@@ -197,14 +209,16 @@ function trackPageView(client, userId, options) {
|
|
|
197
209
|
sendEvent(client, "page_view", eventDetails, options.debug);
|
|
198
210
|
};
|
|
199
211
|
}
|
|
200
|
-
function getEventName(
|
|
201
|
-
switch (type) {
|
|
212
|
+
function getEventName(event) {
|
|
213
|
+
switch (event.type) {
|
|
202
214
|
case "pause":
|
|
203
215
|
return "video_pause";
|
|
204
216
|
case "play":
|
|
205
217
|
return "video_resume";
|
|
206
218
|
case "loadedmetadata":
|
|
219
|
+
case "loaded-metadata":
|
|
207
220
|
return "video_load";
|
|
221
|
+
case "time-update":
|
|
208
222
|
case "timeupdate":
|
|
209
223
|
return "video_progress";
|
|
210
224
|
default:
|
|
@@ -214,7 +228,7 @@ function getEventName({ type }) {
|
|
|
214
228
|
function basicInfos() {
|
|
215
229
|
return {
|
|
216
230
|
url: location.href,
|
|
217
|
-
domain:
|
|
231
|
+
domain: location.hostname,
|
|
218
232
|
referrer: document.referrer || null,
|
|
219
233
|
deviceWidth: window.innerWidth,
|
|
220
234
|
userAgent: navigator.userAgent
|
|
@@ -222,26 +236,39 @@ function basicInfos() {
|
|
|
222
236
|
}
|
|
223
237
|
|
|
224
238
|
// src/lib/client.ts
|
|
239
|
+
var generateUserId = () => {
|
|
240
|
+
return [...Array(30)].map(() => Math.random().toString(36)[2]).join("");
|
|
241
|
+
};
|
|
242
|
+
var getUserId = () => {
|
|
243
|
+
const localStorageKey = "boldvideo_user_id";
|
|
244
|
+
let userId = Cookies.get(localStorageKey);
|
|
245
|
+
if (!userId) {
|
|
246
|
+
userId = generateUserId();
|
|
247
|
+
Cookies.set(localStorageKey, userId, { expires: 365 });
|
|
248
|
+
}
|
|
249
|
+
return userId;
|
|
250
|
+
};
|
|
225
251
|
function createClient(apiKey, options = { debug: false }) {
|
|
226
252
|
var _a;
|
|
227
253
|
if (!apiKey || typeof apiKey !== "string") {
|
|
228
254
|
throw new Error("API key is missing or invalid");
|
|
229
255
|
}
|
|
256
|
+
const userId = getUserId();
|
|
230
257
|
const { debug } = options;
|
|
231
258
|
const apiClientOptions = {
|
|
232
259
|
baseURL: (_a = options.baseURL) != null ? _a : "https://app.boldvideo.io/api/v1/",
|
|
233
260
|
headers: {
|
|
234
|
-
Authorization: apiKey
|
|
261
|
+
Authorization: apiKey,
|
|
262
|
+
"X-User-Id": userId
|
|
235
263
|
}
|
|
236
264
|
};
|
|
237
265
|
let apiClient;
|
|
238
266
|
try {
|
|
239
|
-
apiClient =
|
|
267
|
+
apiClient = axios2.create(apiClientOptions);
|
|
240
268
|
} catch (error) {
|
|
241
269
|
console.error("Error creating API client", error);
|
|
242
270
|
throw error;
|
|
243
271
|
}
|
|
244
|
-
const userId = [...Array(30)].map(() => Math.random().toString(36)[2]).join("");
|
|
245
272
|
return {
|
|
246
273
|
settings: fetchSettings(apiClient),
|
|
247
274
|
videos: {
|
package/package.json
CHANGED
package/src/lib/client.ts
CHANGED
|
@@ -1,23 +1,54 @@
|
|
|
1
1
|
import axios, { AxiosInstance } from "axios";
|
|
2
|
+
import Cookies from "js-cookie";
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
4
|
+
import {
|
|
5
|
+
fetchVideo,
|
|
6
|
+
fetchVideos,
|
|
7
|
+
searchVideos,
|
|
8
|
+
fetchSettings,
|
|
9
|
+
fetchPlaylist,
|
|
10
|
+
fetchPlaylists,
|
|
11
|
+
} from "./fetchers";
|
|
12
|
+
import { trackEvent, trackPageView } from "./tracking";
|
|
5
13
|
|
|
6
14
|
type ClientOptions = {
|
|
7
|
-
baseURL?: string
|
|
8
|
-
debug: boolean
|
|
9
|
-
}
|
|
15
|
+
baseURL?: string;
|
|
16
|
+
debug: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const generateUserId = (): string => {
|
|
20
|
+
return [...Array(30)].map(() => Math.random().toString(36)[2]).join("");
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getUserId = (): string => {
|
|
24
|
+
const localStorageKey = "boldvideo_user_id";
|
|
10
25
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
26
|
+
let userId = Cookies.get(localStorageKey);
|
|
27
|
+
|
|
28
|
+
if (!userId) {
|
|
29
|
+
userId = generateUserId();
|
|
30
|
+
Cookies.set(localStorageKey, userId, { expires: 365 }); // 1-year expiration
|
|
14
31
|
}
|
|
15
32
|
|
|
33
|
+
return userId;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function createClient(
|
|
37
|
+
apiKey: string,
|
|
38
|
+
options: ClientOptions = { debug: false }
|
|
39
|
+
) {
|
|
40
|
+
if (!apiKey || typeof apiKey !== "string") {
|
|
41
|
+
throw new Error("API key is missing or invalid");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const userId = getUserId();
|
|
45
|
+
|
|
16
46
|
const { debug } = options;
|
|
17
47
|
const apiClientOptions = {
|
|
18
48
|
baseURL: options.baseURL ?? "https://app.boldvideo.io/api/v1/",
|
|
19
49
|
headers: {
|
|
20
50
|
Authorization: apiKey,
|
|
51
|
+
"X-User-Id": userId,
|
|
21
52
|
},
|
|
22
53
|
};
|
|
23
54
|
|
|
@@ -30,10 +61,6 @@ function createClient(apiKey: string, options: ClientOptions = {debug: false}) {
|
|
|
30
61
|
throw error;
|
|
31
62
|
}
|
|
32
63
|
|
|
33
|
-
const userId = [...Array(30)]
|
|
34
|
-
.map(() => Math.random().toString(36)[2])
|
|
35
|
-
.join("");
|
|
36
|
-
|
|
37
64
|
return {
|
|
38
65
|
settings: fetchSettings(apiClient),
|
|
39
66
|
videos: {
|
package/src/lib/fetchers.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import axios from "axios";
|
|
1
2
|
import { Video, Playlist, Settings } from "./types";
|
|
2
3
|
import { AxiosInstance } from "axios";
|
|
3
4
|
|
|
@@ -18,7 +19,16 @@ async function get<TResponse>(
|
|
|
18
19
|
}
|
|
19
20
|
return res.data as TResponse;
|
|
20
21
|
} catch (error) {
|
|
21
|
-
|
|
22
|
+
if (axios.isAxiosError(error)) {
|
|
23
|
+
console.error("fetching error details:", {
|
|
24
|
+
message: error.message,
|
|
25
|
+
code: error.code,
|
|
26
|
+
config: error.config,
|
|
27
|
+
response: error.response,
|
|
28
|
+
});
|
|
29
|
+
} else {
|
|
30
|
+
console.error(`Error fetching data from URL: ${url}`, error);
|
|
31
|
+
}
|
|
22
32
|
throw error;
|
|
23
33
|
}
|
|
24
34
|
}
|