Overview
Thetmdb() function is a cached, authenticated HTTP client for interacting with The Movie Database (TMDB) API. It wraps the TMDB API with automatic authentication, error handling, and React cache optimization.
Configuration
The client requires two environment variables:VITE_PUBLIC_TMDB_ACCESS_TOKEN- Your TMDB API bearer tokenVITE_PUBLIC_TMDB_API_URL- TMDB API base URL (typicallyhttps://api.themoviedb.org/3)
Function Signature
Parameters
The API endpoint path. Can start with or without a leading slash (e.g.,
/movie/popular or movie/popular).Optional configuration for the request.
Cache revalidation time in milliseconds. When set, uses
cache: "default", otherwise uses cache: "no-store".Return Type
{ data: T }- Successful response with typed data{ error: string }- Error response with descriptive message
Caching Behavior
Thetmdb() function is wrapped with React’s cache() function, providing automatic request deduplication during server-side rendering:
- Same endpoint, same render - Only one network request is made
- Different renders - Fresh requests are made
- Revalidate option - Controls HTTP cache behavior (not React cache)
Error Handling
The client handles two types of errors:HTTP Errors
When the TMDB API returns a non-OK status:Network Errors
When the fetch request fails:Usage Examples
Basic Request
With Caching
Error Handling Pattern
Multiple Concurrent Requests
Implementation Details
Authentication
All requests include the bearer token in the Authorization header:Request Method
All requests use theGET method. The TMDB API primarily uses GET endpoints for data retrieval.
Endpoint Normalization
Endpoints are automatically normalized to include a leading slash:Related Functions
Thetmdb() client is used by all query functions in the library. See:
- Query Functions - Higher-level functions that use this client
- Response validation utilities in
/lib/utils.ts
Source Code
Location:src/lib/tmdb.ts:18-61