'use server'; import { cookies } from 'next/headers'; import http from './clients/theMovieDb'; import mapMoviesSeriesResponseToShows from './dto/Show'; export const session = async () => cookies().get('session')?.value; export const createRequestToken = async () => { return http.get('/authentication/token/new', { preventCache: true }).then(({ request_token }) => ({ redirectUrl: `https://www.themoviedb.org/authenticate/${request_token}`, requestToken: request_token })); }; export const createSession = async ({ requestToken }: { requestToken: string }) => { return http.post('/authentication/session/new', { params: { request_token: requestToken }, preventCache: true }); }; export const account = async () => { return http.get('/account/null', { params: { session_id: await session() }, preventCache: true }); }; export const accountStates = async ({ showId, showType }: { showId: Show['id']; showType: Show['type'] }) => { return http.get(`/${showType}/${showId}/account_states`, { params: { session_id: await session() }, preventCache: true }); }; export const updateAccountStates = async ({ showId, showType, list, value }: { showId: Show['id']; showType: Show['type']; list: 'favorite' | 'watchlist'; value: boolean; }) => { http.post( `/account/null/${list}`, { params: { session_id: await session() }, preventCache: true }, { media_id: showId, media_type: showType, [list]: value } ); }; export const usersShows = async ({ showType, list, page }: { showType: Show['type']; list: 'favorite' | 'watchlist'; page: string; }): Promise> => { return http .get>(`/account/null/${list}/${showType === 'tv' ? 'tv' : 'movies'}`, { params: { page, sort_by: 'created_at.desc', session_id: await session() }, preventCache: true }) .then((response) => mapMoviesSeriesResponseToShows(response, showType)); };