Smart OpenAPI to Frontend Service Code Generator
Swag-Gen is a powerful CLI tool that automatically generates frontend service code from OpenAPI specifications. It creates a complete, production-ready service layer with built-in session management, multiple storage adapters, and flexible HTTP client support.
- Complete Project Structure - Generates a fully organized project with proper separation of concerns
- Intelligent Session Management - Automatic handling of cookies, tokens, and authentication state
- Cross-Platform Support - Works seamlessly with React Native, Expo, and Next.js
- Flexible Storage Layer - Supports Cookie, LocalStorage, and AsyncStorage (for Expo)
- Dual HTTP Clients - Choose between Fetch API or Axios
- Full TypeScript Support - Complete type safety with intelligent autocomplete
- Auto-generated Service Harmony - Perfect integration between all layers of your application
- Next.js Special Modules - Built-in support for SSR, ISR, and middleware
- Error Handling & Retry Logic - Production-ready error management
- Request Caching - Built-in performance optimization
Install globally using npm or yarn:
npm:
npm install @abolfask/swag-gen -gyarn:
yarn global add @abolfask/swag-genBasic command structure:
swag-gen [options]| Option | Description |
|---|---|
-i, --input <path/url> |
OpenAPI file path or URL |
-t, --ts, --typescript |
Generate TypeScript code with full type definitions |
-a, --axios |
Use Axios instead of Fetch API |
-u, --url <url> |
Direct URL specification |
1. Generate JavaScript services from local file:
swag-gen --input ./openapi.json2. Generate TypeScript services from URL:
swag-gen --url https://api.example.com/openapi.json --typescript3. Generate with Axios and cookie management for Expo:
swag-gen -i ./swagger.yaml -a4. Generate complete Next.js project with TypeScript:
swag-gen --url https://api.site.com/openapi.json --ts --axiosAfter execution, you'll get:
services/
βββ network.js/.ts # Core HTTP client and request handling
βββ apiClient.js/.ts # Generated API services
βββ cookie.js/.ts # Session storage modules (Cookie/LocalStorage/AsyncStorage)
Automatic AsyncStorage and cookie management - no more token storage headaches
Full server and client support with automatic request header management
Lightweight, reusable services for any frontend project
- Parse & Validate - Reads and validates your OpenAPI specification
- Generate Core - Creates the main HTTP client and request handlers
- Generate Services - Produces service functions/classes for each endpoint
- Integrate Storage - Implements the session storage layer
- Output - Writes all generated files to disk
- β 90% Less Boilerplate - Run one command instead of writing repetitive code
- β No Vendor Lock-in - Switch between Fetch and Axios anytime
- β Professional Auth Management - Supports multiple storage methods out of the box
- β Type Safe - Full TypeScript support with compile-time error checking
- β Extensible - Easy to add custom modules and adapters
Contributions are welcome!
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
ISC License - see LICENSE.md
Built with β€οΈ for the developer community