@sasonarik/nextapi-swagger
Version:
CLI tool to generate Next.js API routes and types from Swagger/OpenAPI specs
94 lines (60 loc) • 2.56 kB
Markdown
# nextapi-swagger
> CLI tool to generate Next.js API routes, types, and clients from Swagger/OpenAPI specs
[](https://www.npmjs.com/package/@sasonarik/nextapi-swagger)
[](./LICENSE)
## 🚀 What is this?
`nextapi-swagger` is a developer-friendly CLI tool designed to **automatically generate Next.js App Router API routes, TypeScript types, and API client utilities** based on your Swagger/OpenAPI JSON specifications.
- Streamline your Next.js backend API development
- Avoid repetitive manual coding of API types and routes
- Stay in sync with your API spec effortlessly
## 💡 Why use this?
- Supports **multiple Swagger/OpenAPI JSON URLs**
- Generates **Next.js 13+ App Router API routes**
- Produces **strongly-typed TypeScript interfaces**
- Builds a reusable **API client helper** for requests
- Highly customizable prompts via CLI
- Written in TypeScript for type safety and maintainability
## 🛠️ Installation
You can install the CLI via npm:
```bash
npm install @sasonarik/nextapi-swagger
```
## Usage
You can run the CLI directly using `npx` without installing:
```bash
npx @sasonarik/nextapi-swagger
```
## You will be prompted to:
- Provide one or more Swagger/OpenAPI JSON URL
- Choose whether to generate TypeScript types (types/api.ts)
- Choose whether to generate Next.js API routes
- Choose whether to generate the API client helper
## Generated Files Structure
The generated files will be placed inside your Next.js project directory structure:
```
my-nextjs-project/
├── app/
│ └── api/ # Generated Next.js API routes
├── types/
│ └── api.ts # Generated TypeScript types from OpenAPI
├── utils/
│ └── apiRequests.ts # Generated API client helper
| └── apiBaseUrls.ts # Swagger/OpenAPI API urls
├── ...
```
## Example
```
$ nextapi-swagger
? Enter one Swagger/OpenAPI JSON URL: https://petstore.swagger.io/v2/swagger.json
? Generate types/api.ts? Yes
? Generate Next.js App Router API routes? Yes
? Generate utils/apiRequests.ts client? Yes
✅ Types generated at types/api.ts
✅ API routes generated under app/api
✅ API client helper generated at utils/apiRequests.ts
🎉 Generation complete!
```
### This CLI works best with Swagger 3.0 or higher for improved results.