@lorenzopant/tmdb 0.0.2

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/.env ADDED
@@ -0,0 +1 @@
1
+ TMDB_ACCESS_TOKEN=eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJlYWQ5ZGI4NTUwYmNlM2RmYmNmZTQ3YjRjNWU2ZGI3OSIsIm5iZiI6MTYxMjE5OTAwOC4xMDcsInN1YiI6IjYwMTgzNDYwYmIxMDU3MDAzZjk1ZThlZCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.-hv_iM_NKC9UWl3XgD8W-E6jzVo5bw8yUcW_rCCpJbU
package/.prettierrc ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "printWidth": 140,
3
+ "tabWidth": 4,
4
+ "useTabs": true
5
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "plugins": {
3
+ "release-it-pnpm": {}
4
+ },
5
+ "git": {
6
+ "commitMessage": "chore: release v${version}"
7
+ }
8
+ }
package/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # TMDB API TS
2
+
3
+ A **TypeScript-first** lightweight client for [The Movie Database (TMDB)](https://developer.themoviedb.org/docs/getting-started) API.
4
+
5
+ 🚧🚧🚧🚧🚧🚧 **WIP** This package is still cookin. It will be updated regularly with new features.
6
+ Feel free to open pull requests on the project repository here:
7
+ [tmdb-api-ts](https://github.com/lorenzopantano/tmdb-api-ts)
8
+
9
+ - 📦 Full TypeScript support
10
+ - 🔥 Simple and tiny wrapper
11
+ - 🚀 Designed for server-side and frontend use (React, Vue, etc.)
12
+ - 🛡️ Proper error handling with `TMDBError`
13
+
14
+ ---
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install tmdb-api-ts
20
+ ```
21
+
22
+ or
23
+
24
+ ```bash
25
+ yarn add tmdb-api-ts
26
+ ```
27
+
28
+ ---
29
+
30
+ ## Usage
31
+
32
+ ```typescript
33
+ import { TMDB } from 'tmdb-api-ts';
34
+
35
+ const tmdb = new TMDB('your_access_token');
36
+
37
+ async function searchMovies() {
38
+ try {
39
+ const movies = await tmdb.search.movies({ query: 'Fight Club' });
40
+ console.log(movies);
41
+ } catch (error) {
42
+ if (error instanceof TMDBError) {
43
+ console.error('TMDB Error:', error.message);
44
+ console.error('HTTP Status:', error.http_status_code);
45
+ console.error('TMDB Status Code:', error.tmdb_status_code);
46
+ } else {
47
+ console.error('Unknown error:', error);
48
+ }
49
+ }
50
+ }
51
+
52
+ searchMovies();
53
+ ```
54
+
55
+ ---
56
+
57
+ ## API
58
+
59
+ ### `TMDB`
60
+
61
+ The main client class. **Each API method supports all the parameters supported by TMDB API**, for example the search method supports: query, language, region, year, primary_release_year and so on...
62
+
63
+ #### Constructor
64
+
65
+ ```typescript
66
+ const tmdb = new TMDB(accessToken: string);
67
+ ```
68
+
69
+ - `accessToken`: **required**. Your TMDB API v4 access token.
70
+
71
+ ---
72
+
73
+ ### `Search`
74
+
75
+ Search for movies:
76
+
77
+ ```typescript
78
+ tmdb.search.movies("Fight Club");
79
+ ```
80
+
81
+ Returns a **typed response** containing movies.
82
+
83
+ ---
84
+
85
+ ### `Movie Lists`
86
+
87
+ Now Playing, Popular, Top Rated and Upcoming movies:
88
+
89
+ ```typescript
90
+ tmdb.movie_lists.now_playing();
91
+ tmdb.movie_lists.top_rated();
92
+ tmdb.movie_lists.popular();
93
+ tmdb.movie_lists.upcoming();
94
+ ```
95
+
96
+ Returns a **typed response** containing movies.
97
+
98
+ ---
99
+
100
+ ### `Movie`
101
+
102
+ Now Playing, Popular, Top Rated and Upcoming movies:
103
+
104
+ ```typescript
105
+ tmdb.movie.details(550);
106
+ tmdb.movie.alternative_titles(550);
107
+ tmdb.movie.changes(550);
108
+ tmdb.movie.credits(550);
109
+ tmdb.movie.external_ids(550);
110
+
111
+ ...
112
+ ```
113
+
114
+ Returns a **typed response** containing movies.
115
+
116
+ ---
117
+
118
+ ## Error Handling
119
+
120
+ All errors thrown from the TMDB API are wrapped in a `TMDBError` class.
121
+
122
+ You can catch them:
123
+
124
+ ```typescript
125
+ catch (error) {
126
+ if (error instanceof TMDBError) {
127
+ console.log(error.message);
128
+ console.log(error.http_status_code);
129
+ console.log(error.tmdb_status_code);
130
+ }
131
+ }
132
+ ```
133
+
134
+ Properties available:
135
+
136
+ - `message`
137
+ - `http_status_code`
138
+ - `tmdb_status_code`
139
+
140
+ If the TMDB service is unreachable or returns unexpected errors, `tmdb_status_code` is set to `-1`.
141
+
142
+ ---
143
+
144
+ ## Requirements
145
+
146
+ - Node.js 18+ recommended
147
+ - Works on frontend (React, Vue) but **don't expose sensitive access tokens** to users!
148
+
149
+ ---
150
+
151
+ ## License
152
+
153
+ MIT
@@ -0,0 +1,7 @@
1
+ export declare class ApiClient {
2
+ private accessToken;
3
+ private baseUrl;
4
+ constructor(accessToken: string);
5
+ request<T>(endpoint: string, params?: Record<string, any | undefined>): Promise<T>;
6
+ private handleError;
7
+ }
package/dist/client.js ADDED
@@ -0,0 +1,42 @@
1
+ import { TMDBError } from "./errors/tmdb";
2
+ export class ApiClient {
3
+ accessToken;
4
+ baseUrl = "https://api.themoviedb.org/3";
5
+ constructor(accessToken) {
6
+ this.accessToken = accessToken;
7
+ }
8
+ async request(endpoint, params = {}) {
9
+ const url = new URL(`${this.baseUrl}${endpoint}`);
10
+ for (const [key, value] of Object.entries(params)) {
11
+ if (value === undefined)
12
+ continue;
13
+ url.searchParams.append(key, String(value));
14
+ }
15
+ const res = await fetch(url.toString(), {
16
+ headers: {
17
+ Authorization: `Bearer ${this.accessToken}`,
18
+ "Content-Type": "application/json;charset=utf-8",
19
+ },
20
+ });
21
+ if (!res.ok)
22
+ await this.handleError(res);
23
+ return res.json();
24
+ }
25
+ async handleError(res) {
26
+ let errorMessage = res.statusText;
27
+ let tmdbStatusCode = -1;
28
+ try {
29
+ const errorBody = await res.json();
30
+ if (errorBody && typeof errorBody === "object") {
31
+ const err = errorBody;
32
+ errorMessage = err.status_message || errorMessage;
33
+ tmdbStatusCode = err.status_code || -1;
34
+ }
35
+ }
36
+ catch (_) {
37
+ // If response is not JSON, fallback to HTTP status text
38
+ }
39
+ const error = new TMDBError(errorMessage, res.status, tmdbStatusCode);
40
+ throw error;
41
+ }
42
+ }
@@ -0,0 +1,56 @@
1
+ import { ApiClient } from "../client";
2
+ import { MovieResultItem } from "../types/movies";
3
+ import { PaginatedResponse } from "../types/params";
4
+ export declare class MovieListsAPI {
5
+ private client;
6
+ constructor(client: ApiClient);
7
+ /**
8
+ * Fetch Movie List Wrapper
9
+ * @param endpoint Endpoint to call
10
+ * @param language Language (Defaults to en-US)
11
+ * @param page Page (Defaults to 1)
12
+ * @param region ISO-3166-1 code
13
+ * @returns Specific to endpoint (MovieListResult)
14
+ */
15
+ private fetch_movie_list;
16
+ /**
17
+ * Now Playing
18
+ * GET - https://api.themoviedb.org/3/movie/now_playing
19
+ *
20
+ * Get a list of movies that are currently in theatres.
21
+ * @param language Language (Defaults to en-US)
22
+ * @param page Page (Defaults to 1)
23
+ * @param region ISO-3166-1 code
24
+ */
25
+ now_playing(language?: string, page?: number, region?: string): Promise<PaginatedResponse<MovieResultItem>>;
26
+ /**
27
+ * Popular
28
+ * GET - https://api.themoviedb.org/3/movie/popular
29
+ *
30
+ * Get a list of movies ordered by popularity.
31
+ * @param language Language (Defaults to en-US)
32
+ * @param page Page (Defaults to 1)
33
+ * @param region ISO-3166-1 code
34
+ */
35
+ popular(language?: string, page?: number, region?: string): Promise<PaginatedResponse<MovieResultItem>>;
36
+ /**
37
+ * Top Rated
38
+ * GET - https://api.themoviedb.org/3/movie/top_rated
39
+ *
40
+ * Get a list of movies ordered by rating.
41
+ * @param language Language (Defaults to en-US)
42
+ * @param page Page (Defaults to 1)
43
+ * @param region ISO-3166-1 code
44
+ */
45
+ top_rated(language?: string, page?: number, region?: string): Promise<PaginatedResponse<MovieResultItem>>;
46
+ /**
47
+ * Upcoming
48
+ * GET - https://api.themoviedb.org/3/movie/upcoming
49
+ *
50
+ * Get a list of movies that are being released soon.
51
+ * @param language Language (Defaults to en-US)
52
+ * @param page Page (Defaults to 1)
53
+ * @param region ISO-3166-1 code
54
+ */
55
+ upcoming(language?: string, page?: number, region?: string): Promise<PaginatedResponse<MovieResultItem>>;
56
+ }
@@ -0,0 +1,73 @@
1
+ import { MOVIE_ENDPOINTS } from "./movies";
2
+ const MOVIE_LISTS_ENDPOINTS = {
3
+ NOW_PLAYING: "/now_playing",
4
+ POPULAR: "/popular",
5
+ TOP_RATED: "/top_rated",
6
+ UPCOMING: "/upcoming",
7
+ };
8
+ export class MovieListsAPI {
9
+ client;
10
+ constructor(client) {
11
+ this.client = client;
12
+ }
13
+ /**
14
+ * Fetch Movie List Wrapper
15
+ * @param endpoint Endpoint to call
16
+ * @param language Language (Defaults to en-US)
17
+ * @param page Page (Defaults to 1)
18
+ * @param region ISO-3166-1 code
19
+ * @returns Specific to endpoint (MovieListResult)
20
+ */
21
+ fetch_movie_list(endpoint, language, page, region) {
22
+ const params = { page, language, region };
23
+ return this.client.request(MOVIE_ENDPOINTS.MOVIE + endpoint, params);
24
+ }
25
+ /**
26
+ * Now Playing
27
+ * GET - https://api.themoviedb.org/3/movie/now_playing
28
+ *
29
+ * Get a list of movies that are currently in theatres.
30
+ * @param language Language (Defaults to en-US)
31
+ * @param page Page (Defaults to 1)
32
+ * @param region ISO-3166-1 code
33
+ */
34
+ async now_playing(language = "en-US", page = 1, region) {
35
+ return this.fetch_movie_list(MOVIE_LISTS_ENDPOINTS.NOW_PLAYING, language, page, region);
36
+ }
37
+ /**
38
+ * Popular
39
+ * GET - https://api.themoviedb.org/3/movie/popular
40
+ *
41
+ * Get a list of movies ordered by popularity.
42
+ * @param language Language (Defaults to en-US)
43
+ * @param page Page (Defaults to 1)
44
+ * @param region ISO-3166-1 code
45
+ */
46
+ async popular(language = "en-US", page = 1, region) {
47
+ return this.fetch_movie_list(MOVIE_LISTS_ENDPOINTS.POPULAR, language, page, region);
48
+ }
49
+ /**
50
+ * Top Rated
51
+ * GET - https://api.themoviedb.org/3/movie/top_rated
52
+ *
53
+ * Get a list of movies ordered by rating.
54
+ * @param language Language (Defaults to en-US)
55
+ * @param page Page (Defaults to 1)
56
+ * @param region ISO-3166-1 code
57
+ */
58
+ async top_rated(language = "en-US", page = 1, region) {
59
+ return this.fetch_movie_list(MOVIE_LISTS_ENDPOINTS.TOP_RATED, language, page, region);
60
+ }
61
+ /**
62
+ * Upcoming
63
+ * GET - https://api.themoviedb.org/3/movie/upcoming
64
+ *
65
+ * Get a list of movies that are being released soon.
66
+ * @param language Language (Defaults to en-US)
67
+ * @param page Page (Defaults to 1)
68
+ * @param region ISO-3166-1 code
69
+ */
70
+ async upcoming(language = "en-US", page = 1, region) {
71
+ return this.fetch_movie_list(MOVIE_LISTS_ENDPOINTS.UPCOMING, language, page, region);
72
+ }
73
+ }
@@ -0,0 +1,202 @@
1
+ import { ApiClient } from "../client";
2
+ import { Changes } from "../types/common";
3
+ import { MovieAlternativeTitles, MovieCredits, MovieDetails, MovieExternalIDs, MovieImages, MovieKeywords, MovieReleaseDates, MovieResultItem, MovieTranslations, MovieVideos, MovieWatchProvider } from "../types/movies";
4
+ import { PaginatedResponse } from "../types/params";
5
+ export declare const MOVIE_ENDPOINTS: {
6
+ MOVIE: string;
7
+ ALTERNATIVE_TITLES: string;
8
+ CREDITS: string;
9
+ EXTERNAL_IDS: string;
10
+ KEYWORDS: string;
11
+ CHANGES: string;
12
+ IMAGES: string;
13
+ LATEST: string;
14
+ RECOMMENDATIONS: string;
15
+ RELEASE_DATES: string;
16
+ SIMILAR: string;
17
+ TRANSLATIONS: string;
18
+ VIDEOS: string;
19
+ WATCH_PROVIDERS: string;
20
+ };
21
+ export declare class MoviesAPI {
22
+ private client;
23
+ constructor(client: ApiClient);
24
+ /**
25
+ * Details
26
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}
27
+ *
28
+ * Get the top level details of a movie by ID.
29
+ * @param movie_id The ID of the movie.
30
+ * @param append_to_response A comma-separated list of the fields to include in the response.
31
+ * @param language The language to use for the response.
32
+ * @returns A promise that resolves to the movie details.
33
+ * @reference https://developer.themoviedb.org/reference/movie-details
34
+ */
35
+ details(movie_id: number, append_to_response?: string[], language?: string): Promise<MovieDetails>;
36
+ /**
37
+ * Alternative Titles
38
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/alternative_titles
39
+ *
40
+ * Get the alternative titles for a movie (ex. Russian name, Indian names etc..).
41
+ * @param movie_id The ID of the movie.
42
+ * @param country The ISO 3166-1 code of the country to get alternative titles for.
43
+ * @returns A promise that resolves to the movie alternative titles.
44
+ * @reference https://developer.themoviedb.org/reference/movie-alternative-titles
45
+ */
46
+ alternative_titles(movie_id: number, country?: string): Promise<MovieAlternativeTitles>;
47
+ /**
48
+ * Credits
49
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/credits
50
+ *
51
+ * Get the cast and crew members details for a movie.
52
+ * @param movie_id The ID of the movie.
53
+ * @param language The ISO 639-1 code of the language to use for the response. Default is "en-US".
54
+ * @returns A promise that resolves to the movie credits.
55
+ * @reference https://developer.themoviedb.org/reference/movie-credits
56
+ */
57
+ credits(movie_id: number, language?: string): Promise<MovieCredits>;
58
+ /**
59
+ * External IDs
60
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/external_ids
61
+ *
62
+ * Get the external IDs for a movie (ex. IMDB, Facebook, Instagram etc..).
63
+ * Supported external IDs are: IMDB, Facebook, Instagram, Twitter, and Wikidata.
64
+ *
65
+ * @param movie_id The ID of the movie.
66
+ * @returns A promise that resolves to the movie external IDs.
67
+ * @reference https://developer.themoviedb.org/reference/movie-external-ids
68
+ */
69
+ external_ids(movie_id: number): Promise<MovieExternalIDs>;
70
+ /**
71
+ * Keywords
72
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/keywords
73
+ *
74
+ * Get the keywords that have been added to a movie.
75
+ * This is a list of keywords that have been added to the movie.
76
+ * @param movie_id The ID of the movie.
77
+ * @returns A promise that resolves to the movie keywords.
78
+ * @reference https://developer.themoviedb.org/reference/movie-keywords
79
+ */
80
+ keywords(movie_id: number): Promise<MovieKeywords>;
81
+ /**
82
+ * Changes
83
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/changes
84
+ *
85
+ * Get the changes for a movie. This is a list of all the changes that have been made to a movie.
86
+ * By default, only the last 24 hours are returned.
87
+ * You can query up to 14 days in a single query by using the start_date and end_date query parameters.
88
+ * @param movie_id The ID of the movie.
89
+ * @param page Page number of the results to return. Defaults to 1.
90
+ * @param start_date Optional start date for the changes. Format: YYYY-MM-DD.
91
+ * @param end_date Optional end date for the changes. Format: YYYY-MM-DD.
92
+ * @returns A promise that resolves to the changes made to the movie.
93
+ * @reference https://developer.themoviedb.org/reference/movie-changes
94
+ */
95
+ changes(movie_id: number, page?: number, start_date?: string, end_date?: string): Promise<Changes>;
96
+ /**
97
+ * Images
98
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/images
99
+ *
100
+ * Fetches images related to a specific movie, such as posters and backdrops.
101
+ * The images are returned in various sizes and formats.
102
+ * @param movie_id - The unique identifier of the movie.
103
+ * @param language - (Optional) The language code to filter the images by language.
104
+ * @param include_image_language - (Optional) A comma-separated list of language codes to include images for.
105
+ * @returns A promise that resolves to a `MovieImages` object containing the movie's images.
106
+ * @reference https://developer.themoviedb.org/reference/movie-images
107
+ */
108
+ images(movie_id: number, language?: string, include_image_language?: string): Promise<MovieImages>;
109
+ /**
110
+ * Latest
111
+ * GET - https://api.themoviedb.org/3/movie/latest
112
+ *
113
+ * Get the newest movie ID.
114
+ * This is the most recent movie that has been added to TMDB. This is a live response will continuously change as new movies are added.
115
+ * @returns A promise that resolves to the latest movie details.
116
+ * @reference https://developer.themoviedb.org/reference/movie-latest-id
117
+ */
118
+ latest(): Promise<MovieDetails>;
119
+ /**
120
+ * Recommendations
121
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/recommendations
122
+ *
123
+ * Get a list of recommended movies for a specific movie.
124
+ * This is based on the movie's popularity and user ratings.
125
+ * @param movie_id The ID of the movie.
126
+ * @param page Page number of the results to return. Defaults to 1.
127
+ * @param language Language code to filter the results. Default is "en-US".
128
+ * @returns A promise that resolves to a paginated response of similar movies.
129
+ * @reference https://developer.themoviedb.org/reference/movie-recommendations
130
+ */
131
+ recommendations(movie_id: number, page?: number, language?: string): Promise<PaginatedResponse<MovieResultItem>>;
132
+ /**
133
+ * Release Dates
134
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/release_dates
135
+ *
136
+ * Get the release dates and certifications for a movie. For different countries and release types.
137
+ * The release types and statuses used on TMDB are the following:
138
+ * - 1: Premiere
139
+ * - 2: Theatrical (Limited)
140
+ * - 3: Theatrical
141
+ * - 4: Digital
142
+ * - 5: Physical
143
+ * - 6: TV
144
+ * @param movie_id The ID of the movie.
145
+ * @returns A promise that resolves to the release dates for the movie.
146
+ * @reference https://developer.themoviedb.org/reference/movie-release-dates
147
+ */
148
+ release_dates(movie_id: number): Promise<MovieReleaseDates>;
149
+ /**
150
+ * Similar
151
+ * GET -https://api.themoviedb.org/3/movie/{movie_id}/similar
152
+ *
153
+ * Get the similar movies based on genres and keywords.
154
+ * This method only looks for other items based on genres and plot keywords.
155
+ * As such, the results found here are not always going to be 💯. Use it with that in mind.
156
+ * @param movie_id The ID of the movie
157
+ * @param page Page number of the results to return. Defaults to 1.
158
+ * @param language Language code to filter the results. Default is "en-US".
159
+ * @returns A promise that resolves to a paginated response of similar movies.
160
+ * @reference https://developer.themoviedb.org/reference/movie-similar
161
+ */
162
+ similar(movie_id: number, language?: string, page?: number): Promise<PaginatedResponse<MovieResultItem>>;
163
+ /**
164
+ * Translations
165
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/translations
166
+ *
167
+ * Get the translations for a movie.
168
+ * Take a read through our language documentation for more information about languages on TMDB.
169
+ * https://developer.themoviedb.org/docs/languages
170
+ * @param movie_id The ID of the movie
171
+ * @returns A promise that resolves to the translations of the movie.
172
+ * @reference https://developer.themoviedb.org/reference/movie-translations
173
+ */
174
+ translations(movie_id: number): Promise<MovieTranslations>;
175
+ /**
176
+ * Videos
177
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/videos
178
+ *
179
+ * Get the available videos for a movie.
180
+ * @param movie_id The ID of the movie
181
+ * @returns A promise that resolves to a list of videos for the movie.
182
+ * @reference https://developer.themoviedb.org/reference/movie-videos
183
+ */
184
+ videos(movie_id: number, language?: string): Promise<MovieVideos>;
185
+ /**
186
+ * Watch Providers
187
+ * GET - https://api.themoviedb.org/3/movie/{movie_id}/watch/providers
188
+ *
189
+ * Get the list of streaming providers we have for a movie.
190
+ * Powered by our partnership with JustWatch, you can query this method to get a list of the streaming/rental/purchase availabilities per country by provider.
191
+ * This is not going to return full deep links, but rather, it's just enough information to display what's available where.
192
+ * You can link to the provided TMDB URL to help support TMDB and provide the actual deep links to the content.
193
+ *
194
+ * JustWatch ATTRIBUTION REQUIRED
195
+ * In order to use this data you must attribute the source of the data as JustWatch.
196
+ * If we find any usage not complying with these terms we will revoke access to the API.
197
+ * @param movie_id The ID of the movie
198
+ * @returns A promise that resolves to a list of videos for the movie.
199
+ * @reference https://developer.themoviedb.org/reference/movie-videos
200
+ */
201
+ watch_providers(movie_id: number): Promise<MovieWatchProvider>;
202
+ }