api_model_generator 1.0.0
api_model_generator: ^1.0.0 copied to clipboard
A powerful CLI tool to generate type-safe Models, Services, Repositories, and API Clients from JSON responses.
π API Model Generator (AMG) #
API Model Generator (AMG) is a professional CLI tool designed to supercharge your Flutter and Dart development. It automatically transforms JSON API responses into a complete, clean-architecture data layer including Models, Services, Repositories, and API Clients.
Stop wasting time on boilerplate. AMG handles nested objects, prevents duplicate models, syncs changes safely, and generates type-safe code in seconds.
β¨ Key Features #
- Recursive Model Generation: Automatically generates separate Dart files for deeply nested JSON objects.
- Model Registry (Deduplication): Smartly detects identical JSON structures and reuses existing models to keep your code DRY.
- Clean Architecture Layers:
- Models: Type-safe Dart classes with
fromJsonandtoJson. - Services: Network layer logic using the
httppackage. - Repositories: Data abstraction layer for your UI.
- API Client: A centralized client for all your defined endpoints.
- Models: Type-safe Dart classes with
- Model Sync: Safely update existing models when API responses changeβdetects new fields and updates constructors and serialization logic automatically.
- Batch Generation: Process an entire folder of JSON files in one command.
- YAML Configuration: standardise paths and settings for your project.
- Multi-Client Support: Generate API clients for both
httpanddio. - Interactive & Command Mode: Use guided prompts or fast CLI commands.
- Polished UX: Real-time progress indicators (spinners) and generation summaries.
π¦ Installation #
Add AMG to your dev_dependencies:
flutter pub add api_model_generator --dev
For global usage (recommended for the amg command):
dart pub global activate api_model_generator
π Quick Start #
- Place your API response in
response.jsonin the root of your project. - Run the interactive generator:
amg i - Follow the prompts to generate your Models, Services, and Repositories.
π οΈ CLI Commands & Aliases #
| Command | Alias | Description |
|---|---|---|
interactive |
i |
Start the guided interactive CLI (Default) |
generate |
g |
Generate models/services using api_model_generator.yaml |
sync |
s |
Sync an existing model file with a new JSON response |
batch <path> |
b |
Generate models for every .json file in a folder |
api |
a |
Generate a centralized ApiClient from your config |
init |
Create a default api_model_generator.yaml config file |
|
--help |
-h |
Show usage information |
--version |
-v |
Show current version |
π Updating Models (Sync) #
AMG allows you to safely evolve your models as your backend changes:
amg sync
The tool will compare your existing Dart model with the new JSON, show you a Diff Preview of new fields, and update the file without breaking your existing structure.
βοΈ Configuration (api_model_generator.yaml) #
Use a configuration file to automate paths and define API endpoints. Run amg init to create a template.
# Output Paths
models_path: lib/core/models
services_path: lib/core/services
repositories_path: lib/core/repositories
api_path: lib/core/api
# Generation Settings
generate:
service: true
repository: true
# API Client Settings
http_client: dio # or 'http'
api:
base_url: https://api.example.com
endpoints:
- name: getUsers
method: GET
path: /users
response_model: User
response_type: list
- name: createUser
method: POST
path: /users
request_model: User
response_model: User
response_type: object
After configuring, generate your API Client with:
amg api
π Generated Structure #
AMG follows a clean architecture pattern:
lib/
βββ api/
β βββ api_client.dart # Centralized API methods
βββ models/
β βββ user.dart # Type-safe model
β βββ address.dart # Reusable nested model
βββ services/
β βββ user_service.dart # Network calls
βββ repositories/
βββ user_repository.dart # Data abstraction
π§ͺ Running Tests #
Ensure the generator logic is working correctly in your environment:
dart test
π€ Contributing #
We welcome contributions! If you have ideas for new features (like Freezed support or OpenAPI integration), please open an issue or submit a pull request.
π License #
This project is licensed under the MIT License - see the LICENSE file for details.