Menu

Typed API Contracts from NestJS to Next.js with OpenAPI: openapi-typescript, openapi-fetch, and Where the Types Lie — Field Notes
📰

Typed API Contracts from NestJS to Next.js with OpenAPI: openapi-typescript, openapi-fetch, and Where the Types Lie — Field Notes

DEV Community: nestjs·Ahmed Mahmoud·3 days ago
#vbSTLcJj
#devtonestjs#devto#dev#openapi#nestjs
Long article (1989 words) — no AI summary yet.
Reading 0:00
15s threshold

Headline: The most reliable way I have found to keep a Next.js frontend in sync with a NestJS REST API is to treat the OpenAPI spec as the contract: the @nestjs/swagger CLI plugin generates the spec at build time, openapi-typescript turns it into TypeScript types, and openapi-fetch makes every call type-checked. A CI step that regenerates both files and runs git diff --exit-code turns an unannounced contract change into a failed build instead of a production bug.

For a long time my Next.js apps talked to NestJS backends through types I copied by hand, or at best through a shared types package in the monorepo. Both drifted. A renamed field compiled fine on the frontend and rendered undefined in production.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.