TMDB API code generation workflow with selective Zod schemas using pnpm codegen:tmdb...
This project uses automatic code generation for TMDB API integration with selective Zod schema generation for optimal performance.
NEVER manually edit apps/web/src/_generated/tmdb-server-functions.ts - it is auto-generated.
Always use pnpm codegen:tmdb to regenerate after making changes to endpoint configurations.
The project uses a selective approach to Zod schema generation:
Zod schemas are only generated for endpoints marked with needsZodSchema: true in endpoints.js.
Why selective?
Location: packages/tmdb-codegen/src/endpoints.js
export const endpoints = [
{
path: "/3/search/movie",
functionName: "searchMovies",
needsZodSchema: true, // ✅ Zod schema generated for AI tools
},
{
path: "/3/movie/{movie_id}",
functionName: "getMovieDetails",
// ❌ No needsZodSchema flag = TypeScript types only
},
];
endpoints.jsneedsZodSchema: true only if needed for AI toolspnpm codegen:tmdb to regenerate{
path: "/3/discover/tv",
functionName: "discoverTvShows",
needsZodSchema: false, // Only TS types needed
}
# Full pipeline (TypeScript types + Zod schemas)
pnpm codegen
# Only regenerate TMDB server functions
pnpm codegen:tmdb
# Only regenerate Zod schemas (fast!)
pnpm codegen:zod
apps/web/src/_generated/tmdb-server-functions.ts - Server functions with TypeScript typesapps/web/src/_generated/tmdb-client-functions.ts - One client function per entry in api-routes.js, with its server function's signature, that calls the /api/tmdb/* route. The movie-database/tmdb/queries/* factories call these by default; a server prefetch passes the server functions insteadapps/web/src/_generated/tmdb-zod.ts - Selective Zod schemas (only for endpoints with needsZodSchema: true)These files are committed to the repository, so run codegen and commit the result whenever endpoints.js changes:
pnpm codegen:tmdb
Location: packages/tmdb-codegen/src/generate-schemas-from-source.ts
endpoints.js to find endpoints needing Zod schemas.nullable().optional())Automatically applies fixes for OpenAI Structured Outputs:
// Generated schema with OpenAI compatibility
export const movieSchema = z.object({
id: z.number(),
title: z.string().nullable().optional(), // OpenAI-compatible
overview: z.string().nullable().optional(),
});
import {
searchMovies,
getMovieDetails,
} from "#src/_generated/tmdb-server-functions.ts";
// Use in server components
const movies = await searchMovies({ query: "Inception" });
import { movieSearchSchema } from "#src/_generated/tmdb-zod.ts";
// Use with OpenAI Structured Outputs
const completion = await openai.chat.completions.create({
model: "gpt-4",
messages: [...],
response_format: zodResponseFormat(movieSearchSchema, "movies"),
});
Regenerate TMDB code when:
endpoints.js, then run pnpm codegen:tmdbendpoints.js, then regeneratepnpm codegen:tmdbneedsZodSchema: true when needed for AI toolsendpoints.js is version-controlled# 1. Edit configuration
# Add to packages/tmdb-codegen/src/endpoints.js
# 2. Regenerate
pnpm codegen:tmdb
# 3. Use in code
import { newFunction } from "#src/_generated/tmdb-server-functions.ts";
# 1. Edit configuration
# Set needsZodSchema: true in endpoints.js
# 2. Regenerate Zod schemas only (fast!)
pnpm codegen:zod
# 3. Use schema
import { newSchema } from "#src/_generated/tmdb-zod.ts";
# Regenerate from scratch (run from apps/web)
rm -rf src/_generated
pnpm codegen:tmdb
Check endpoints.js - ensure needsZodSchema: true is set for that endpoint, then regenerate.