62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
"use server";
|
|
|
|
import { SearchResult } from "./types";
|
|
|
|
const url = "https://api.themoviedb.org/3";
|
|
const fetchTmbd = async (path: string) => {
|
|
const response = await fetch(url + path, {
|
|
headers: {
|
|
Authorization: `Bearer ${process.env.TMDB_BEARER}`,
|
|
},
|
|
});
|
|
const data = await response.json();
|
|
return data;
|
|
};
|
|
|
|
type SearchOptions = {
|
|
query: string;
|
|
include_adult?: boolean;
|
|
language?: string;
|
|
primary_release_year?: string;
|
|
page?: number;
|
|
region?: string;
|
|
year?: number;
|
|
};
|
|
|
|
export async function search(options: SearchOptions): Promise<SearchResult> {
|
|
const params = new URLSearchParams();
|
|
|
|
options.language = "pl-PL";
|
|
options.region = "PL";
|
|
|
|
Object.entries(options).forEach(([key, value]) => {
|
|
if (value) {
|
|
params.set(key, value.toString());
|
|
}
|
|
});
|
|
|
|
return await fetchTmbd(`/search/movie?${params.toString()}`);
|
|
}
|
|
|
|
export async function getPopularMovies(
|
|
page: number = 1
|
|
): Promise<SearchResult> {
|
|
return await fetchTmbd(`/movie/popular?language=pl-PL&page=${page}`);
|
|
}
|
|
|
|
export async function getTrendingMovies(): Promise<SearchResult> {
|
|
return await fetchTmbd("/trending/movie/day?language=pl-PL");
|
|
}
|
|
|
|
export async function getNowPlayingMovies(
|
|
page: number = 1
|
|
): Promise<SearchResult> {
|
|
return await fetchTmbd(
|
|
`/movie/now_playing?language=pl-PL®ion=PL&page=${page}`
|
|
);
|
|
}
|
|
|
|
export async function getUpcomingMovies(): Promise<SearchResult> {
|
|
return await fetchTmbd("/movie/upcoming?language=pl-PL®ion=PL");
|
|
}
|