@tdanks2000/tmdb-wrapper 1.3.4 → 1.5.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.mjs CHANGED
@@ -113,9 +113,32 @@ let ReleaseDateType = /* @__PURE__ */ function(ReleaseDateType$1) {
113
113
  //#endregion
114
114
  //#region src/utils/api.ts
115
115
  const BASE_URL_V3 = "https://api.themoviedb.org/3";
116
+ /**
117
+ * Lightweight TMDB v3 API client.
118
+ *
119
+ * - Sends requests to `https://api.themoviedb.org/3`.
120
+ * - Supports authentication via:
121
+ * - v4 API Read Access Token (Bearer), and/or
122
+ * - v3 API key via `api_key` query parameter.
123
+ *
124
+ * Notes:
125
+ * - Many endpoints accept either method depending on your TMDB settings.
126
+ * - When an API key is present, it is appended as `api_key` in the query string.
127
+ */
116
128
  var API = class {
129
+ /**
130
+ * Optional v3 API key (sent as `api_key` query param).
131
+ */
117
132
  apiKey;
133
+ /**
134
+ * Optional v4 read access token (sent as `Authorization: Bearer ...`).
135
+ */
118
136
  accessToken;
137
+ /**
138
+ * Create a new API client.
139
+ *
140
+ * @param {TokenType} auth - Authentication information.
141
+ */
119
142
  constructor(auth) {
120
143
  if (typeof auth === "string") this.accessToken = auth;
121
144
  else {
@@ -124,9 +147,16 @@ var API = class {
124
147
  }
125
148
  }
126
149
  /**
127
- * Generic GET:
128
- * @template T — response type
129
- * @template O options (query params) type
150
+ * Generic HTTP GET request.
151
+ *
152
+ * @template T - Response JSON type.
153
+ * @template O - Query options (query params) type.
154
+ *
155
+ * @param {string} path - The TMDB path beginning with `/`, e.g. `/movie/550`.
156
+ * @param {O} [options] - Query parameters to serialize.
157
+ *
158
+ * @returns {Promise<T>} Resolves with parsed JSON typed as `T`.
159
+ * @throws {ErrorResponse} Rejects with parsed TMDB error payload when `response.ok` is false.
130
160
  */
131
161
  async get(path, options) {
132
162
  const rawOptions = {
@@ -148,6 +178,16 @@ var API = class {
148
178
  return await response.json();
149
179
  }
150
180
  };
181
+ /**
182
+ * Serializes an options object into a query string.
183
+ *
184
+ * - Skips `undefined` and `null`.
185
+ * - Arrays are expanded into repeated keys:
186
+ * `{ with_genres: ["12", "16"] }` -> `with_genres=12&with_genres=16`
187
+ *
188
+ * @param {Record<string, unknown>} [options] - Options to serialize.
189
+ * @returns {string} Query string without the leading `?`.
190
+ */
151
191
  const parseOptions = (options) => {
152
192
  if (!options) return "";
153
193
  const entries = [];
@@ -161,31 +201,7 @@ const parseOptions = (options) => {
161
201
 
162
202
  //#endregion
163
203
  //#region src/utils/getimagePath.ts
164
- /**
165
- * Utility method to construct full url for image
166
- * based on configuration
167
- *
168
- * https://developers.themoviedb.org/3/getting-started/images
169
- * @param {string} baseUrl base image url (e.g., 'https://image.tmdb.org/t/p/')
170
- * @param {string} fileSize file size (e.g., 'original', 'w500')
171
- * @param {string} imagePath raw image path
172
- * @param {string} format override image format (e.g., 'svg', 'png', 'jpg')
173
- * @returns {string} The complete image URL
174
- */
175
- const getFullImagePath = (baseUrl, fileSize, imagePath, format) => {
176
- if (!imagePath) return "";
177
- const hasExtension = imagePath.includes(".");
178
- if (hasExtension) {
179
- const imagePathArr = imagePath.split(".");
180
- const imageFormat$1 = format || imagePathArr[1];
181
- return `${baseUrl}${fileSize}${imagePathArr[0]}.${imageFormat$1}`;
182
- }
183
- const imageFormat = format || "jpg";
184
- return `${baseUrl}${fileSize}${imagePath}.${imageFormat}`;
185
- };
186
- /**
187
- * Common image sizes available in TMDB
188
- */
204
+ const TMDB_IMAGE_BASE_URL = "https://image.tmdb.org/t/p/";
189
205
  const ImageSizes = {
190
206
  ORIGINAL: "original",
191
207
  W500: "w500",
@@ -194,19 +210,67 @@ const ImageSizes = {
194
210
  W92: "w92",
195
211
  H632: "h632"
196
212
  };
197
- /**
198
- * Image formats supported by TMDB
199
- */
200
213
  const ImageFormats = {
201
214
  JPG: "jpg",
202
215
  PNG: "png",
203
216
  SVG: "svg"
204
217
  };
218
+ const isAbsoluteUrl = (value) => /^https?:\/\//i.test(value);
219
+ const normalizeBaseUrl = (baseUrl) => baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
220
+ const normalizeSize = (size) => size.replace(/^\/+|\/+$/g, "");
221
+ const normalizePath = (path) => path.startsWith("/") ? path : `/${path}`;
222
+ const setExtension = (path, format) => {
223
+ const lastSlash = path.lastIndexOf("/");
224
+ const lastDot = path.lastIndexOf(".");
225
+ const hasExt = lastDot > lastSlash;
226
+ if (!hasExt) return `${path}.${format}`;
227
+ return `${path.slice(0, lastDot + 1)}${format}`;
228
+ };
229
+ /**
230
+ * Constructs a TMDB image URL.
231
+ *
232
+ * - Keeps paths as-is unless `format` is provided (then it replaces/appends
233
+ * the extension safely using the last dot after the last slash).
234
+ * - Handles leading/trailing slashes robustly.
235
+ * - If `imagePath` is already an absolute URL, it is returned unchanged.
236
+ */
237
+ const getFullImagePath = (baseUrl, fileSize, imagePath, format) => {
238
+ if (!imagePath) return "";
239
+ if (isAbsoluteUrl(imagePath)) return imagePath;
240
+ const base = normalizeBaseUrl(baseUrl);
241
+ const size = normalizeSize(fileSize);
242
+ let path = normalizePath(imagePath);
243
+ if (format) path = setExtension(path, format);
244
+ return `${base}${size}${path}`;
245
+ };
246
+ /**
247
+ * Convenience helper for TMDB `Image` objects.
248
+ */
249
+ const formImage = (image, fileSize, format) => {
250
+ const path = image?.file_path;
251
+ if (!path) return void 0;
252
+ return getFullImagePath(TMDB_IMAGE_BASE_URL, fileSize, path, format);
253
+ };
205
254
 
206
255
  //#endregion
207
256
  //#region src/@types/models/baseEndpoint.ts
257
+ /**
258
+ * Base class for all TMDB API endpoints.
259
+ *
260
+ * Provides a configured {@link API} client instance to subclasses.
261
+ */
208
262
  var BaseEndpoint = class {
263
+ /**
264
+ * Low-level HTTP client wrapper used by all endpoints.
265
+ */
209
266
  api;
267
+ /**
268
+ * Create a new endpoint instance.
269
+ *
270
+ * @param {TokenType} auth - Authentication information.
271
+ * - If a string: treated as a v4 API Read Access Token (Bearer token).
272
+ * - If an object: can include an API key and/or access token, depending on your {@link TokenType}.
273
+ */
210
274
  constructor(auth) {
211
275
  this.auth = auth;
212
276
  this.api = new API(auth);
@@ -217,11 +281,20 @@ var BaseEndpoint = class {
217
281
  //#region src/endpoints/account.ts
218
282
  /**
219
283
  * Represents an endpoint for retrieving account details.
284
+ *
285
+ * TMDB v3 reference:
286
+ * - GET /account/{account_id}
287
+ *
288
+ * Note:
289
+ * TMDB does not expose a generic "GET /account" for account details. You must
290
+ * provide an `account_id`. Most apps obtain it from an auth flow and then cache
291
+ * it for subsequent requests.
220
292
  */
221
293
  var AccountEndpoint = class extends BaseEndpoint {
222
294
  /**
223
295
  * Constructs a new AccountEndpoint instance.
224
- * @param {string} access_token - The access token used for authentication.
296
+ *
297
+ * @param {TokenType} access_token - The access token used for authentication.
225
298
  */
226
299
  constructor(access_token) {
227
300
  super(access_token);
@@ -229,10 +302,14 @@ var AccountEndpoint = class extends BaseEndpoint {
229
302
  }
230
303
  /**
231
304
  * Retrieves account details asynchronously.
305
+ *
306
+ * TMDB: GET /account/{account_id}
307
+ *
308
+ * @param {number} accountId - The TMDB account ID.
232
309
  * @returns {Promise<AccountDetails>} A Promise that resolves with the account details.
233
310
  */
234
- async details() {
235
- return await this.api.get("/account");
311
+ details(accountId) {
312
+ return this.api.get(`/account/${accountId}`);
236
313
  }
237
314
  };
238
315
 
@@ -240,11 +317,16 @@ var AccountEndpoint = class extends BaseEndpoint {
240
317
  //#region src/endpoints/certification.ts
241
318
  /**
242
319
  * Represents an endpoint for retrieving certifications for movies and TV shows.
320
+ *
321
+ * TMDB v3 reference:
322
+ * - GET /certification/movie/list
323
+ * - GET /certification/tv/list
243
324
  */
244
325
  var CertificationEndpoint = class extends BaseEndpoint {
245
326
  /**
246
327
  * Constructs a new CertificationEndpoint instance.
247
- * @param {string} access_token - The access token used for authentication.
328
+ *
329
+ * @param {TokenType} access_token - The access token used for authentication.
248
330
  */
249
331
  constructor(access_token) {
250
332
  super(access_token);
@@ -252,17 +334,23 @@ var CertificationEndpoint = class extends BaseEndpoint {
252
334
  }
253
335
  /**
254
336
  * Retrieves certifications for movies asynchronously.
337
+ *
338
+ * TMDB: GET /certification/movie/list
339
+ *
255
340
  * @returns {Promise<Certifications>} A Promise that resolves with the certifications for movies.
256
341
  */
257
- async movies() {
258
- return await this.api.get("/certification/movie/list");
342
+ movies() {
343
+ return this.api.get("/certification/movie/list");
259
344
  }
260
345
  /**
261
346
  * Retrieves certifications for TV shows asynchronously.
347
+ *
348
+ * TMDB: GET /certification/tv/list
349
+ *
262
350
  * @returns {Promise<Certifications>} A Promise that resolves with the certifications for TV shows.
263
351
  */
264
- async tv() {
265
- return await this.api.get("/certification/tv/list");
352
+ tv() {
353
+ return this.api.get("/certification/tv/list");
266
354
  }
267
355
  };
268
356
 
@@ -270,11 +358,17 @@ var CertificationEndpoint = class extends BaseEndpoint {
270
358
  //#region src/endpoints/changes.ts
271
359
  /**
272
360
  * Represents an endpoint for retrieving changes in movies, TV shows, and persons.
361
+ *
362
+ * TMDB v3 reference:
363
+ * - GET /movie/changes
364
+ * - GET /tv/changes
365
+ * - GET /person/changes
273
366
  */
274
367
  var ChangeEndpoint = class extends BaseEndpoint {
275
368
  /**
276
369
  * Constructs a new ChangeEndpoint instance.
277
- * @param {string} access_token - The access token used for authentication.
370
+ *
371
+ * @param {TokenType} access_token - The access token used for authentication.
278
372
  */
279
373
  constructor(access_token) {
280
374
  super(access_token);
@@ -282,27 +376,36 @@ var ChangeEndpoint = class extends BaseEndpoint {
282
376
  }
283
377
  /**
284
378
  * Retrieves changes in movies asynchronously.
379
+ *
380
+ * TMDB: GET /movie/changes
381
+ *
285
382
  * @param {ChangeOption} [options] - Optional parameters for filtering the changes.
286
383
  * @returns {Promise<MediaChanges>} A Promise that resolves with the changes in movies.
287
384
  */
288
- async movies(options) {
289
- return await this.api.get("/movie/changes", options);
385
+ movies(options) {
386
+ return this.api.get("/movie/changes", options);
290
387
  }
291
388
  /**
292
389
  * Retrieves changes in TV shows asynchronously.
390
+ *
391
+ * TMDB: GET /tv/changes
392
+ *
293
393
  * @param {ChangeOption} [options] - Optional parameters for filtering the changes.
294
394
  * @returns {Promise<MediaChanges>} A Promise that resolves with the changes in TV shows.
295
395
  */
296
- async tv(options) {
297
- return await this.api.get("/tv/changes", options);
396
+ tv(options) {
397
+ return this.api.get("/tv/changes", options);
298
398
  }
299
399
  /**
300
400
  * Retrieves changes related to persons asynchronously.
401
+ *
402
+ * TMDB: GET /person/changes
403
+ *
301
404
  * @param {ChangeOption} [options] - Optional parameters for filtering the changes.
302
405
  * @returns {Promise<MediaChanges>} A Promise that resolves with the changes related to persons.
303
406
  */
304
- async person(options) {
305
- return await this.api.get("/person/changes", options);
407
+ person(options) {
408
+ return this.api.get("/person/changes", options);
306
409
  }
307
410
  };
308
411
 
@@ -446,11 +549,16 @@ var CreditsEndpoint = class extends BaseEndpoint {
446
549
  const BASE_DISCOVER = "/discover";
447
550
  /**
448
551
  * Represents an endpoint for discovering movies and TV shows based on various criteria.
552
+ *
553
+ * TMDB v3 reference:
554
+ * - GET /discover/movie
555
+ * - GET /discover/tv
449
556
  */
450
557
  var DiscoverEndpoint = class extends BaseEndpoint {
451
558
  /**
452
559
  * Constructs a new DiscoverEndpoint instance.
453
- * @param {string} access_token - The access token used for authentication.
560
+ *
561
+ * @param {TokenType} access_token - The access token used for authentication.
454
562
  */
455
563
  constructor(access_token) {
456
564
  super(access_token);
@@ -458,19 +566,25 @@ var DiscoverEndpoint = class extends BaseEndpoint {
458
566
  }
459
567
  /**
460
568
  * Retrieves a list of movies based on the provided query options asynchronously.
569
+ *
570
+ * TMDB: GET /discover/movie
571
+ *
461
572
  * @param {MovieQueryOptions} [options] - Optional parameters for refining the movie discovery.
462
573
  * @returns {Promise<MovieDiscoverResult>} A Promise that resolves with the movie discovery results.
463
574
  */
464
- async movie(options) {
465
- return await this.api.get(`${BASE_DISCOVER}/movie`, options);
575
+ movie(options) {
576
+ return this.api.get(`${BASE_DISCOVER}/movie`, options);
466
577
  }
467
578
  /**
468
579
  * Retrieves a list of TV shows based on the provided query options asynchronously.
580
+ *
581
+ * TMDB: GET /discover/tv
582
+ *
469
583
  * @param {TvShowQueryOptions} [options] - Optional parameters for refining the TV show discovery.
470
584
  * @returns {Promise<TvShowDiscoverResult>} A Promise that resolves with the TV show discovery results.
471
585
  */
472
- async tvShow(options) {
473
- return await this.api.get(`${BASE_DISCOVER}/tv`, options);
586
+ tvShow(options) {
587
+ return this.api.get(`${BASE_DISCOVER}/tv`, options);
474
588
  }
475
589
  };
476
590
 
@@ -1557,5 +1671,5 @@ var TMDB = class {
1557
1671
  };
1558
1672
 
1559
1673
  //#endregion
1560
- export { API, BackdropSizes, BaseEndpoint, ChangeKeys, ImageFormats, ImageSizes, LogoSizes, PosterSizes, ProfileSizes, ReleaseDateType, StillSizes, TMDB, getFullImagePath, parseOptions };
1674
+ export { API, BackdropSizes, BaseEndpoint, ChangeKeys, ImageFormats, ImageSizes, LogoSizes, PosterSizes, ProfileSizes, ReleaseDateType, StillSizes, TMDB, TMDB_IMAGE_BASE_URL, formImage, getFullImagePath, parseOptions };
1561
1675
  //# sourceMappingURL=index.mjs.map