-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.example.ts
More file actions
54 lines (48 loc) · 1.74 KB
/
search.example.ts
File metadata and controls
54 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {
TMDB,
type MovieSearchResults,
type TvSearchResults,
type PeopleSearchResults,
type MultiSearchResults,
} from "@vo1x/tmdb";
/**
* A minimal example demonstrating the search functionality of the TMDB SDK.
*/
async function main() {
const tmdb = new TMDB({
apiKey: process.env.TMDB_TOKEN!,
language: "en-US",
});
// Movie search
const movieRes: MovieSearchResults = await tmdb.search.movies("batman");
console.log(`Found ${movieRes.totalResults} movies.`);
movieRes.results?.slice(0, 2).forEach((movie) => {
console.log(`- [Movie] "${movie.title}" (${movie.releaseDate})`);
});
// TV search
const tvRes: TvSearchResults = await tmdb.search.tv("batman");
console.log(`\nFound ${tvRes.totalResults} TV shows.`);
tvRes.results?.slice(0, 2).forEach((show) => {
console.log(`- [TV] "${show.name}" (${show.firstAirDate})`);
});
// People search
const peopleRes: PeopleSearchResults = await tmdb.search.people("christian bale");
console.log(`\nFound ${peopleRes.totalResults} people.`);
peopleRes.results?.slice(0, 2).forEach((person) => {
console.log(`- [Person] ${person.name} (${person.knownForDepartment})`);
});
// Multi search
const multiRes: MultiSearchResults = await tmdb.search.multi("batman");
console.log(`\nFound ${multiRes.totalResults} mixed results.`);
multiRes.results?.slice(0, 5).forEach((result) => {
// Using the discriminated union 'mediaType' to determine the result's shape
if (result.mediaType === "movie") {
console.log(`- [Multi/Movie] "${result.title}"`);
} else if (result.mediaType === "tv") {
console.log(`- [Multi/TV] "${result.title}"`);
}
});
}
main().catch((err) => {
console.error("TMDB search example failed:", err.message);
});