nuxt-generate-api
v1.2.0
Published
Generates composables and types from swagger.json
Maintainers
Readme
nuxt-generate-api
⚡ Instantly generate typed composables and API types from a Swagger/OpenAPI spec for Nuxt projects.
📦 What is this?
nuxt-generate-api is a CLI tool that takes a swagger.json file and generates:
- Fully-typed composables:
composables/useApi.ts - API type definitions:
types/api.types.ts - A typed fetch wrapper:
composables/useApiService.ts
Built for Nuxt 3, using $fetch, useRuntimeConfig, and TypeScript.
🛠 Installation
npm install -D nuxt-generate-apiOr use via npx:
npx nuxt-generate-api🚀 Usage
- Download your Swagger spec:
💡 Tip: If using Swagger UI, open DevTools → Network → refresh → find a
.jsonfile likeopenapi.json, right-click → "Save as" →swagger.json
Place the
swagger.jsonfile in the root of your Nuxt project.Run:
npx nuxt-generate-apiThis will generate:
/types/api.types.ts ← All request & response types
/composables/useApi.ts ← Typed composables (e.g., createUser, getPosts)
/composables/useApiService.ts ← Reusable fetch helper📁 File Outputs
types/api.types.ts
- Types generated from
components.schemas - Request/response interfaces per endpoint
composables/useApi.ts
- Auto-generated composables for each endpoint
- Uses
useApiService<T>()under the hood
composables/useApiService.ts
- A wrapper around
$fetchwith built-in support for:- Query params
- Request body
- Runtime API base URL from
useRuntimeConfig
🧩 Example
// In your Nuxt 3 component or composable
const { createUser, getPosts } = useApi();
const result = await createUser({
body: {
name: 'John Doe',
email: '[email protected]'
}
});📌 Requirements
- Node.js v16+
- Nuxt 3 (uses
useRuntimeConfigand$fetch) - A valid
swagger.jsonfile
💡 Roadmap
- [ ] CLI arguments (
--input,--output) - [ ] Support OpenAPI 3.1
- [ ] Optional
zodvalidation output - [ ] GitHub Action support for CI type generation
📝 License
MIT
🙌 Acknowledgements
Created by Fayzulla Rahmatullayev
Inspired by openapi-typescript, swagger-typescript-api, and Nuxt DX best practices.
