refactoring: server actions => tmdb proxy
This commit is contained in:
1 parent
dfdfb79169
commit
97d55b24a1
13 files changed
+131
-113
No files matched your search
@@ -1,3 +1,7 @@
|
||||
import { isServer } from '@tanstack/react-query';
|
||||
|
||||
import { SITE_URL } from '@/env';
|
||||
|
||||
type Search = Record<string, string | number | boolean | null | undefined>;
|
||||
|
||||
class Http {
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import { isServer } from '@tanstack/react-query';
|
||||
|
||||
import Http from './Http';
|
||||
|
||||
const BASE =
|
||||
process.env.NEXT_PUBLIC_VERCEL_ENV === 'production'
|
||||
? process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL
|
||||
: process.env.NEXT_PUBLIC_VERCEL_URL;
|
||||
process.env.NEXT_VERCEL_ENV === 'production'
|
||||
? process.env.NEXT_VERCEL_PROJECT_PRODUCTION_URL
|
||||
: process.env.NEXT_VERCEL_URL;
|
||||
|
||||
const movieAdvisor = new Http(`${BASE?.startsWith('http') ? BASE : `https://${BASE}`}/api/tmdb`);
|
||||
const ROOT = isServer ? `${BASE?.startsWith('http') ? BASE : `https://${BASE}`}` : '';
|
||||
|
||||
const movieAdvisor = new Http(`${ROOT}/api/tmdb`);
|
||||
|
||||
export default movieAdvisor;
|
||||
@@ -1,7 +1,7 @@
|
||||
import { MOVIE_DB_TOKEN } from '@/env';
|
||||
import { MOVIE_DB_API_URL, MOVIE_DB_TOKEN } from '@/env';
|
||||
|
||||
import Http from './Http';
|
||||
|
||||
const theMovieDb = new Http('https://api.themoviedb.org/3', `Bearer ${MOVIE_DB_TOKEN}`);
|
||||
const theMovieDB = new Http(MOVIE_DB_API_URL, `Bearer ${MOVIE_DB_TOKEN}`);
|
||||
|
||||
export default theMovieDb;
|
||||
export default theMovieDB;
|
||||
+8
-60
@@ -2,74 +2,22 @@
|
||||
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import http from './clients/theMovieDb';
|
||||
import mapMoviesSeriesResponseToShows from './dto/Show';
|
||||
import theMovieDB from './clients/theMovieDB';
|
||||
|
||||
export const session = async () => cookies().get('session')?.value;
|
||||
|
||||
export const createRequestToken = async () => {
|
||||
return http.get<RequestToken>('/authentication/token/new', { preventCache: true }).then(({ request_token }) => ({
|
||||
redirectUrl: `https://www.themoviedb.org/authenticate/${request_token}`,
|
||||
requestToken: request_token
|
||||
}));
|
||||
return theMovieDB
|
||||
.get<RequestToken>('/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<Session>('/authentication/session/new', {
|
||||
return theMovieDB.post<Session>('/authentication/session/new', {
|
||||
params: { request_token: requestToken },
|
||||
preventCache: true
|
||||
});
|
||||
};
|
||||
|
||||
export const account = async () => {
|
||||
return http.get<Account>('/account/null', {
|
||||
params: { session_id: await session() },
|
||||
preventCache: true
|
||||
});
|
||||
};
|
||||
|
||||
export const accountStates = async ({ showId, showType }: { showId: Show['id']; showType: Show['type'] }) => {
|
||||
return http.get<ShowState>(`/${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<Pagination<Show>> => {
|
||||
return http
|
||||
.get<TMDBPagination<Movie>>(`/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));
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
import movieAdvisor from '../clients/movieAdvisor';
|
||||
|
||||
export type StatesMutationProps = {
|
||||
session?: string;
|
||||
};
|
||||
|
||||
export type StatesMutationFnProps = {
|
||||
showId: Show['id'];
|
||||
showType: Show['type'];
|
||||
list: 'favorite' | 'watchlist';
|
||||
value: boolean;
|
||||
};
|
||||
|
||||
const statesMutation = ({ session }: StatesMutationProps) => ({
|
||||
retry: 2,
|
||||
mutationFn: ({ showId, showType, list, value }: StatesMutationFnProps) =>
|
||||
movieAdvisor.post(
|
||||
`/account/null/${list}`,
|
||||
{ params: { session_id: session }, preventCache: true },
|
||||
{
|
||||
media_id: showId,
|
||||
media_type: showType,
|
||||
[list]: value
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
export default statesMutation;
|
||||
@@ -0,0 +1,16 @@
|
||||
import movieAdvisor from '../clients/movieAdvisor';
|
||||
|
||||
export type StatesQueryProps = { showId?: Show['id']; showType?: Show['type']; session?: string };
|
||||
|
||||
const statesQuery = ({ showId, showType, session }: StatesQueryProps) => ({
|
||||
enabled: Boolean(showId),
|
||||
queryKey: ['states', showId, showType, session],
|
||||
staleTime: Infinity,
|
||||
queryFn: () =>
|
||||
movieAdvisor.get<ShowState>(`/${showType}/${showId}/account_states`, {
|
||||
params: { session_id: session },
|
||||
preventCache: true
|
||||
})
|
||||
});
|
||||
|
||||
export default statesQuery;
|
||||
Reference in new issue
Block a user