Move the trimmer from the route into a shared lib/tmdbTrim and apply it in the server-side client too (server hits TMDB directly, bypassing the proxy), so both paths return identical shapes. Show page HTML drops ~22% (88.4KB -> 68.8KB); TV details payload -90%.
25 lines
985 B
TypeScript
25 lines
985 B
TypeScript
import { isServer } from '@tanstack/react-query';
|
|
|
|
import { MOVIE_DB_API_URL, MOVIE_DB_TOKEN } from '@/env';
|
|
import trimTmdb from '@/lib/tmdbTrim';
|
|
|
|
import Http from './Http';
|
|
|
|
type Search = Record<string, string | number | boolean | null | undefined>;
|
|
|
|
const http = isServer ? new Http(MOVIE_DB_API_URL, `Bearer ${MOVIE_DB_TOKEN}`) : new Http('/api/tmdb');
|
|
|
|
// The browser goes through the /api/tmdb proxy, which trims responses; on the server we hit the
|
|
// API directly, so apply the same trim to keep both paths returning identical data.
|
|
const movieAdvisor = {
|
|
get: <T>(url: string, { params, preventCache }: { params?: Search; preventCache?: boolean } = {}) =>
|
|
http.get<T>(url, { params, preventCache }).then((data) => trimTmdb(url, data) as T),
|
|
post: <T, B = unknown>(
|
|
url: string,
|
|
{ params, preventCache }: { params?: Search; preventCache?: boolean } = {},
|
|
body?: B
|
|
) => http.post<T, B>(url, { params, preventCache }, body)
|
|
};
|
|
|
|
export default movieAdvisor;
|