next-oapi
v0.0.1
Published
CLI tool to generate OpenAPI schema from Next.js routes
Readme
next-oapi
A CLI tool to generate OpenAPI schema from Next.js routes. It analyzes your Next.js API routes and automatically generates comprehensive OpenAPI 3.0 specifications, including schemas for request/response models, enums, and more.
Installation
Install globally via npm:
npm install -g next-oapiOr using pnpm:
pnpm add -g next-oapiOr using bun:
bun add -g next-oapiUsage
After installation, you can use the CLI in your Next.js project directory:
next-oapi --project <path-to-next-project>For example:
next-oapi --project . --out ./custom-docsThis will generate an openapi.json file in the specified output directory (or the default src/docs/ or docs/ folder).
Options
--project <path>: Path to the Next.js project directory (required)--out <path>: Output directory for the generatedopenapi.json(optional, defaults tosrc/docs/ordocs/)--help: Show help message
Generated Output
The tool generates a complete OpenAPI 3.0 specification that includes:
- Automatic path detection from your API routes
- Schema inference from TypeScript types and expressions
- Enum generation for union types and literals
- Support for complex response types from NextResponse.json calls
- Cleanup of primitives (no unnecessary wrappers for strings, numbers, booleans)
- Stable schema names with deduplication
Limitations
⚠️ Important: This tool is designed to analyze pure Next.js API routes only. It does not support (and may produce incorrect or incomplete results for):
- Routes wrapped with other frameworks or libraries (e.g., tRPC, NextAuth, Better Auth, etc.)
- Custom handlers that don't use standard Next.js patterns
- Routes that rely on middleware or external schema definitions
For custom authentication, data validation, or RPC patterns, please use targeted libraries designed for those systems.
Development
Prerequisites
- Node.js (v18 or higher recommended)
- pnpm, npm, or bun
Setup
- Install dependencies:
pnpm install- Run the unit tests:
pnpm run test- Build the library:
pnpm run build- Run in development mode with watch:
pnpm run devScripts
pnpm run build: Build the CLI tool with tsdownpnpm run dev: Watch mode for developmentpnpm run test: Run unit tests with Vitestpnpm run typecheck: Type-check the TypeScript codepnpm run release: Bump version and publish to npm
Repository
License
MIT
