@boldvideo/bold-js 0.4.0 → 0.4.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/package.json +1 -1
- package/src/lib/client.ts +12 -39
- package/src/lib/fetchers.ts +1 -11
package/package.json
CHANGED
package/src/lib/client.ts
CHANGED
|
@@ -1,54 +1,23 @@
|
|
|
1
1
|
import axios, { AxiosInstance } from "axios";
|
|
2
|
-
import Cookies from "js-cookie";
|
|
3
2
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
fetchVideos,
|
|
7
|
-
searchVideos,
|
|
8
|
-
fetchSettings,
|
|
9
|
-
fetchPlaylist,
|
|
10
|
-
fetchPlaylists,
|
|
11
|
-
} from "./fetchers";
|
|
12
|
-
import { trackEvent, trackPageView } from "./tracking";
|
|
3
|
+
import { fetchVideo, fetchVideos, searchVideos, fetchSettings, fetchPlaylist, fetchPlaylists } from './fetchers'
|
|
4
|
+
import { trackEvent, trackPageView } from './tracking'
|
|
13
5
|
|
|
14
6
|
type ClientOptions = {
|
|
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";
|
|
25
|
-
|
|
26
|
-
let userId = Cookies.get(localStorageKey);
|
|
27
|
-
|
|
28
|
-
if (!userId) {
|
|
29
|
-
userId = generateUserId();
|
|
30
|
-
Cookies.set(localStorageKey, userId, { expires: 365 }); // 1-year expiration
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return userId;
|
|
34
|
-
};
|
|
7
|
+
baseURL?: string
|
|
8
|
+
debug: boolean
|
|
9
|
+
}
|
|
35
10
|
|
|
36
|
-
function createClient(
|
|
37
|
-
apiKey
|
|
38
|
-
|
|
39
|
-
) {
|
|
40
|
-
if (!apiKey || typeof apiKey !== "string") {
|
|
41
|
-
throw new Error("API key is missing or invalid");
|
|
11
|
+
function createClient(apiKey: string, options: ClientOptions = {debug: false}) {
|
|
12
|
+
if (!apiKey || typeof apiKey !== 'string') {
|
|
13
|
+
throw new Error('API key is missing or invalid');
|
|
42
14
|
}
|
|
43
15
|
|
|
44
|
-
const userId = getUserId();
|
|
45
|
-
|
|
46
16
|
const { debug } = options;
|
|
47
17
|
const apiClientOptions = {
|
|
48
18
|
baseURL: options.baseURL ?? "https://app.boldvideo.io/api/v1/",
|
|
49
19
|
headers: {
|
|
50
20
|
Authorization: apiKey,
|
|
51
|
-
"X-User-Id": userId,
|
|
52
21
|
},
|
|
53
22
|
};
|
|
54
23
|
|
|
@@ -61,6 +30,10 @@ function createClient(
|
|
|
61
30
|
throw error;
|
|
62
31
|
}
|
|
63
32
|
|
|
33
|
+
const userId = [...Array(30)]
|
|
34
|
+
.map(() => Math.random().toString(36)[2])
|
|
35
|
+
.join("");
|
|
36
|
+
|
|
64
37
|
return {
|
|
65
38
|
settings: fetchSettings(apiClient),
|
|
66
39
|
videos: {
|
package/src/lib/fetchers.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
1
|
import { Video, Playlist, Settings } from "./types";
|
|
3
2
|
import { AxiosInstance } from "axios";
|
|
4
3
|
|
|
@@ -19,16 +18,7 @@ async function get<TResponse>(
|
|
|
19
18
|
}
|
|
20
19
|
return res.data as TResponse;
|
|
21
20
|
} catch (error) {
|
|
22
|
-
|
|
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
|
-
}
|
|
21
|
+
console.error(`Error fetching data from URL: ${url}`, error);
|
|
32
22
|
throw error;
|
|
33
23
|
}
|
|
34
24
|
}
|