@boldvideo/bold-js 0.1.0 → 0.3.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 CHANGED
@@ -77,33 +77,82 @@ var import_axios = __toESM(require("axios"), 1);
77
77
  // src/lib/fetchers.ts
78
78
  function get(client, url) {
79
79
  return __async(this, null, function* () {
80
- const res = yield client.get(url);
81
- return res.data;
80
+ try {
81
+ const res = yield client.get(url);
82
+ if (res.status !== 200) {
83
+ throw new Error(`Unexpected response status: ${res.status}`);
84
+ }
85
+ return res.data;
86
+ } catch (error) {
87
+ console.error(`Error fetching data from URL: ${url}`, error);
88
+ throw error;
89
+ }
82
90
  });
83
91
  }
84
92
  function fetchSettings(client) {
85
93
  return (videoLimit = 12) => __async(this, null, function* () {
86
- return get(client, `settings?limit=${videoLimit}`);
94
+ try {
95
+ return yield get(
96
+ client,
97
+ `settings?limit=${videoLimit}`
98
+ );
99
+ } catch (error) {
100
+ console.error(`Error fetching settings with limit: ${videoLimit}`, error);
101
+ throw error;
102
+ }
87
103
  });
88
104
  }
89
105
  function fetchVideos(client) {
90
106
  return (videoLimit = 12) => __async(this, null, function* () {
91
- return get(client, `videos/latest?limit=${videoLimit}`);
107
+ try {
108
+ return yield get(
109
+ client,
110
+ `videos/latest?limit=${videoLimit}`
111
+ );
112
+ } catch (error) {
113
+ console.error(`Error fetching videos with limit: ${videoLimit}`, error);
114
+ throw error;
115
+ }
116
+ });
117
+ }
118
+ function searchVideos(client) {
119
+ return (term) => __async(this, null, function* () {
120
+ try {
121
+ return yield get(client, `videos?query=${term}`);
122
+ } catch (error) {
123
+ console.error(`Error searching for videos with term: ${term}`, error);
124
+ throw error;
125
+ }
92
126
  });
93
127
  }
94
128
  function fetchVideo(client) {
95
129
  return (id) => __async(this, null, function* () {
96
- return get(client, `videos/${id}`);
130
+ try {
131
+ return yield get(client, `videos/${id}`);
132
+ } catch (error) {
133
+ console.error(`Error fetching video with ID: ${id}`, error);
134
+ throw error;
135
+ }
97
136
  });
98
137
  }
99
138
  function fetchPlaylists(client) {
100
139
  return () => __async(this, null, function* () {
101
- return get(client, "playlists");
140
+ try {
141
+ return yield get(client, "playlists");
142
+ } catch (error) {
143
+ console.error("Error fetching playlists", error);
144
+ throw error;
145
+ }
102
146
  });
103
147
  }
104
148
  function fetchPlaylist(client) {
105
149
  return (id) => __async(this, null, function* () {
106
- return get(client, `playlists/${id}`);
150
+ try {
151
+ return yield get(client, `playlists/${id}`);
152
+ } catch (error) {
153
+ console.error(`Error fetching playlist with ID: ${id}`, error);
154
+ throw error;
155
+ }
107
156
  });
108
157
  }
109
158
 
@@ -158,6 +207,7 @@ function trackEvent(client, userId, options) {
158
207
  videoId: video.id,
159
208
  title: video.title,
160
209
  videoDuration: video.duration,
210
+ // TODO: change to currentTime of HTMLMediaElement
161
211
  timeStamp: event.timeStamp
162
212
  });
163
213
  if (event.type == "timeupdate") {
@@ -208,6 +258,9 @@ function basicInfos() {
208
258
  // src/lib/client.ts
209
259
  function createClient(apiKey, options = { debug: false }) {
210
260
  var _a;
261
+ if (!apiKey || typeof apiKey !== "string") {
262
+ throw new Error("API key is missing or invalid");
263
+ }
211
264
  const { debug } = options;
212
265
  const apiClientOptions = {
213
266
  baseURL: (_a = options.baseURL) != null ? _a : "https://app.boldvideo.io/api/v1/",
@@ -215,13 +268,20 @@ function createClient(apiKey, options = { debug: false }) {
215
268
  Authorization: apiKey
216
269
  }
217
270
  };
218
- const apiClient = import_axios.default.create(apiClientOptions);
271
+ let apiClient;
272
+ try {
273
+ apiClient = import_axios.default.create(apiClientOptions);
274
+ } catch (error) {
275
+ console.error("Error creating API client", error);
276
+ throw error;
277
+ }
219
278
  const userId = [...Array(30)].map(() => Math.random().toString(36)[2]).join("");
220
279
  return {
221
280
  settings: fetchSettings(apiClient),
222
281
  videos: {
223
282
  list: fetchVideos(apiClient),
224
- get: fetchVideo(apiClient)
283
+ get: fetchVideo(apiClient),
284
+ search: searchVideos(apiClient)
225
285
  },
226
286
  playlists: {
227
287
  list: fetchPlaylists(apiClient),
package/dist/index.d.ts CHANGED
@@ -59,6 +59,9 @@ declare function createClient(apiKey: string, options?: ClientOptions): {
59
59
  get: (id: string) => Promise<{
60
60
  data: Video;
61
61
  }>;
62
+ search: (term: string) => Promise<{
63
+ data: Video[];
64
+ }>;
62
65
  };
63
66
  playlists: {
64
67
  list: () => Promise<{
package/dist/index.js CHANGED
@@ -44,33 +44,82 @@ import axios from "axios";
44
44
  // src/lib/fetchers.ts
45
45
  function get(client, url) {
46
46
  return __async(this, null, function* () {
47
- const res = yield client.get(url);
48
- return res.data;
47
+ try {
48
+ const res = yield client.get(url);
49
+ if (res.status !== 200) {
50
+ throw new Error(`Unexpected response status: ${res.status}`);
51
+ }
52
+ return res.data;
53
+ } catch (error) {
54
+ console.error(`Error fetching data from URL: ${url}`, error);
55
+ throw error;
56
+ }
49
57
  });
50
58
  }
51
59
  function fetchSettings(client) {
52
60
  return (videoLimit = 12) => __async(this, null, function* () {
53
- return get(client, `settings?limit=${videoLimit}`);
61
+ try {
62
+ return yield get(
63
+ client,
64
+ `settings?limit=${videoLimit}`
65
+ );
66
+ } catch (error) {
67
+ console.error(`Error fetching settings with limit: ${videoLimit}`, error);
68
+ throw error;
69
+ }
54
70
  });
55
71
  }
56
72
  function fetchVideos(client) {
57
73
  return (videoLimit = 12) => __async(this, null, function* () {
58
- return get(client, `videos/latest?limit=${videoLimit}`);
74
+ try {
75
+ return yield get(
76
+ client,
77
+ `videos/latest?limit=${videoLimit}`
78
+ );
79
+ } catch (error) {
80
+ console.error(`Error fetching videos with limit: ${videoLimit}`, error);
81
+ throw error;
82
+ }
83
+ });
84
+ }
85
+ function searchVideos(client) {
86
+ return (term) => __async(this, null, function* () {
87
+ try {
88
+ return yield get(client, `videos?query=${term}`);
89
+ } catch (error) {
90
+ console.error(`Error searching for videos with term: ${term}`, error);
91
+ throw error;
92
+ }
59
93
  });
60
94
  }
61
95
  function fetchVideo(client) {
62
96
  return (id) => __async(this, null, function* () {
63
- return get(client, `videos/${id}`);
97
+ try {
98
+ return yield get(client, `videos/${id}`);
99
+ } catch (error) {
100
+ console.error(`Error fetching video with ID: ${id}`, error);
101
+ throw error;
102
+ }
64
103
  });
65
104
  }
66
105
  function fetchPlaylists(client) {
67
106
  return () => __async(this, null, function* () {
68
- return get(client, "playlists");
107
+ try {
108
+ return yield get(client, "playlists");
109
+ } catch (error) {
110
+ console.error("Error fetching playlists", error);
111
+ throw error;
112
+ }
69
113
  });
70
114
  }
71
115
  function fetchPlaylist(client) {
72
116
  return (id) => __async(this, null, function* () {
73
- return get(client, `playlists/${id}`);
117
+ try {
118
+ return yield get(client, `playlists/${id}`);
119
+ } catch (error) {
120
+ console.error(`Error fetching playlist with ID: ${id}`, error);
121
+ throw error;
122
+ }
74
123
  });
75
124
  }
76
125
 
@@ -125,6 +174,7 @@ function trackEvent(client, userId, options) {
125
174
  videoId: video.id,
126
175
  title: video.title,
127
176
  videoDuration: video.duration,
177
+ // TODO: change to currentTime of HTMLMediaElement
128
178
  timeStamp: event.timeStamp
129
179
  });
130
180
  if (event.type == "timeupdate") {
@@ -175,6 +225,9 @@ function basicInfos() {
175
225
  // src/lib/client.ts
176
226
  function createClient(apiKey, options = { debug: false }) {
177
227
  var _a;
228
+ if (!apiKey || typeof apiKey !== "string") {
229
+ throw new Error("API key is missing or invalid");
230
+ }
178
231
  const { debug } = options;
179
232
  const apiClientOptions = {
180
233
  baseURL: (_a = options.baseURL) != null ? _a : "https://app.boldvideo.io/api/v1/",
@@ -182,13 +235,20 @@ function createClient(apiKey, options = { debug: false }) {
182
235
  Authorization: apiKey
183
236
  }
184
237
  };
185
- const apiClient = axios.create(apiClientOptions);
238
+ let apiClient;
239
+ try {
240
+ apiClient = axios.create(apiClientOptions);
241
+ } catch (error) {
242
+ console.error("Error creating API client", error);
243
+ throw error;
244
+ }
186
245
  const userId = [...Array(30)].map(() => Math.random().toString(36)[2]).join("");
187
246
  return {
188
247
  settings: fetchSettings(apiClient),
189
248
  videos: {
190
249
  list: fetchVideos(apiClient),
191
- get: fetchVideo(apiClient)
250
+ get: fetchVideo(apiClient),
251
+ search: searchVideos(apiClient)
192
252
  },
193
253
  playlists: {
194
254
  list: fetchPlaylists(apiClient),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@boldvideo/bold-js",
3
3
  "license": "MIT",
4
- "version": "0.1.0",
4
+ "version": "0.3.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -12,10 +12,10 @@
12
12
  },
13
13
  "devDependencies": {
14
14
  "@changesets/cli": "^2.26.0",
15
- "tsup": "^6.6.3",
15
+ "tsup": "^6.7.0",
16
16
  "typescript": "^4.9.5"
17
17
  },
18
18
  "dependencies": {
19
- "axios": "^1.3.4"
19
+ "axios": "^1.3.6"
20
20
  }
21
21
  }
package/src/lib/client.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import axios, { AxiosInstance } from "axios";
2
2
 
3
- import { fetchVideo, fetchVideos, fetchSettings, fetchPlaylist, fetchPlaylists } from './fetchers'
3
+ import { fetchVideo, fetchVideos, searchVideos, fetchSettings, fetchPlaylist, fetchPlaylists } from './fetchers'
4
4
  import { trackEvent, trackPageView } from './tracking'
5
5
 
6
6
  type ClientOptions = {
@@ -9,6 +9,10 @@ type ClientOptions = {
9
9
  }
10
10
 
11
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');
14
+ }
15
+
12
16
  const { debug } = options;
13
17
  const apiClientOptions = {
14
18
  baseURL: options.baseURL ?? "https://app.boldvideo.io/api/v1/",
@@ -17,7 +21,14 @@ function createClient(apiKey: string, options: ClientOptions = {debug: false}) {
17
21
  },
18
22
  };
19
23
 
20
- const apiClient: AxiosInstance = axios.create(apiClientOptions);
24
+ let apiClient: AxiosInstance;
25
+
26
+ try {
27
+ apiClient = axios.create(apiClientOptions);
28
+ } catch (error) {
29
+ console.error("Error creating API client", error);
30
+ throw error;
31
+ }
21
32
 
22
33
  const userId = [...Array(30)]
23
34
  .map(() => Math.random().toString(36)[2])
@@ -28,6 +39,7 @@ function createClient(apiKey: string, options: ClientOptions = {debug: false}) {
28
39
  videos: {
29
40
  list: fetchVideos(apiClient),
30
41
  get: fetchVideo(apiClient),
42
+ search: searchVideos(apiClient),
31
43
  },
32
44
  playlists: {
33
45
  list: fetchPlaylists(apiClient),
@@ -1,33 +1,96 @@
1
- import { Video, Playlist, Settings } from './types'
2
- import { AxiosInstance } from 'axios'
1
+ import { Video, Playlist, Settings } from "./types";
2
+ import { AxiosInstance } from "axios";
3
3
 
4
4
  type Response<T> = {
5
- data: T
6
- }
5
+ data: T;
6
+ };
7
7
 
8
8
  type ApiClient = AxiosInstance;
9
9
 
10
- async function get<TResponse>(client: ApiClient, url: string): Promise<TResponse> {
11
- const res = await client.get(url);
12
- return res.data as TResponse;
10
+ async function get<TResponse>(
11
+ client: ApiClient,
12
+ url: string
13
+ ): Promise<TResponse> {
14
+ try {
15
+ const res = await client.get(url);
16
+ if (res.status !== 200) {
17
+ throw new Error(`Unexpected response status: ${res.status}`);
18
+ }
19
+ return res.data as TResponse;
20
+ } catch (error) {
21
+ console.error(`Error fetching data from URL: ${url}`, error);
22
+ throw error;
23
+ }
13
24
  }
14
25
 
15
26
  export function fetchSettings(client: ApiClient) {
16
- return async (videoLimit = 12) => get<Response<Settings>>(client, `settings?limit=${videoLimit}`);
27
+ return async (videoLimit = 12) => {
28
+ try {
29
+ return await get<Response<Settings>>(
30
+ client,
31
+ `settings?limit=${videoLimit}`
32
+ );
33
+ } catch (error) {
34
+ console.error(`Error fetching settings with limit: ${videoLimit}`, error);
35
+ throw error;
36
+ }
37
+ };
17
38
  }
18
39
 
19
40
  export function fetchVideos(client: ApiClient) {
20
- return async (videoLimit = 12) => get<Response<Video[]>>(client, `videos/latest?limit=${videoLimit}`);
41
+ return async (videoLimit = 12) => {
42
+ try {
43
+ return await get<Response<Video[]>>(
44
+ client,
45
+ `videos/latest?limit=${videoLimit}`
46
+ );
47
+ } catch (error) {
48
+ console.error(`Error fetching videos with limit: ${videoLimit}`, error);
49
+ throw error;
50
+ }
51
+ };
52
+ }
53
+
54
+ export function searchVideos(client: ApiClient) {
55
+ return async (term: string) => {
56
+ try {
57
+ return await get<Response<Video[]>>(client, `videos?query=${term}`);
58
+ } catch (error) {
59
+ console.error(`Error searching for videos with term: ${term}`, error);
60
+ throw error;
61
+ }
62
+ };
21
63
  }
22
64
 
23
65
  export function fetchVideo(client: ApiClient) {
24
- return async (id: string) => get<Response<Video>>(client, `videos/${id}`);
66
+ return async (id: string) => {
67
+ try {
68
+ return await get<Response<Video>>(client, `videos/${id}`);
69
+ } catch (error) {
70
+ console.error(`Error fetching video with ID: ${id}`, error);
71
+ throw error;
72
+ }
73
+ };
25
74
  }
26
75
 
27
76
  export function fetchPlaylists(client: ApiClient) {
28
- return async () => get<Response<Playlist[]>>(client, "playlists");
77
+ return async () => {
78
+ try {
79
+ return await get<Response<Playlist[]>>(client, "playlists");
80
+ } catch (error) {
81
+ console.error("Error fetching playlists", error);
82
+ throw error;
83
+ }
84
+ };
29
85
  }
30
86
 
31
87
  export function fetchPlaylist(client: ApiClient) {
32
- return async (id: string) => get<Response<Playlist>>(client, `playlists/${id}`);
88
+ return async (id: string) => {
89
+ try {
90
+ return await get<Response<Playlist>>(client, `playlists/${id}`);
91
+ } catch (error) {
92
+ console.error(`Error fetching playlist with ID: ${id}`, error);
93
+ throw error;
94
+ }
95
+ };
33
96
  }
@@ -58,6 +58,7 @@ export function trackEvent(
58
58
  videoId: video.id,
59
59
  title: video.title,
60
60
  videoDuration: video.duration,
61
+ // TODO: change to currentTime of HTMLMediaElement
61
62
  timeStamp: event.timeStamp,
62
63
  };
63
64
  // debounce fast hitting timeupdate event