Return

Automatic Type Generation for OpenAPI

INFRASTRUCTURE

Situation

The workplace project uses dozens of backend APIs documented via Swagger UI. Manual TypeScript type definitions were error-prone and became stale as backend APIs evolved. Knowing Prisma can auto-generate types from databases, explored whether similar automation could work for HTTP API specifications.

Task

  • Automation of type generation for backend APIs
  • Integrate with existing @juun/api/fetcher architecture

Action

1. Attempted Prisma type generation (workplace)

  • Explored Prisma's introspection feature with multiple datasources
  • Generated types across multiple schemas, but the result types were representing only database tables, differed from actual HTTP API specs

2. Evaluated swagger-typescript-api

  • Generated both types and API client code (unified solution)
  • Workplace: Sufficient when starting from scratch with no HTTP abstractions
  • This project: Conflicts with existing @juun/api/fetcher architecture
  • Didn't support type-only generation - couples type definitions with HTTP client implementation

3. Selected openapi-typescript

  • Generates TypeScript types only (.d.ts files)
  • Zero runtime code - purely compile-time type safety
  • Configuration-driven: openapi.json array for multiple APIs

4. Architecture Integration

  • Types exported via package subpath: @juun/api/openapi/types/*
  • Keeps HTTP logic (@juun/api/fetcher) separate from type definitions
  • Generated types consume ~0 KB bundle size (declaration files only)
  • ESLint configured to ignore generated files

Result

Automated type generation, enabling type-safe API client development.