api_generator 0.2.0
api_generator: ^0.2.0 copied to clipboard
Generate Flutter-friendly Dio clients and repository layers from Swagger/OpenAPI sources.
api_generator #
api_generator is a Dart CLI package that turns Swagger/OpenAPI sources into a Flutter-friendly data layer.
It can also scaffold a Clean Architecture Flutter feature by combining Swagger/OpenAPI analysis with Figma metadata.
It supports:
- raw OpenAPI JSON or YAML URLs
- Swagger docs URLs
- local
openapi.json - local
swagger-ui-init.js - no auth, basic auth, and bearer auth
- Figma file URLs and selected Figma node URLs for feature scaffolding
The generator writes:
- OpenAPI Generator
dart-diooutput tolib/core/generated/openapi - API constants to
lib/core/api_constants.dart - Retrofit API client to
lib/core/network/api_client.dart - feature models to
lib/features/<feature>/data/model - repository interfaces by tag to
lib/features/<feature>/domain/repositories - repository implementations by tag to
lib/features/<feature>/data/repositories - per-operation cubits to
lib/features/<feature>/presentation/cubits - feature reports, request/response models, repositories, use cases, Cubits, pages, and widgets for
generate-feature
Install #
dart pub global activate api_generator
Install openapi-generator-cli separately and make sure it is available on your PATH.
Usage #
api_generator generate https://example.com/swagger-ui/index.html
Run it from your Flutter project root and output defaults to ./lib.
Custom output:
api_generator generate https://example.com/docs /path/to/flutter_app/lib
If the Swagger page is protected with basic auth, the CLI prompts for username and password automatically.
Advanced non-interactive auth:
api_generator generate \
--input https://example.com/docs \
--output /path/to/flutter_app/lib \
--auth-type bearer \
--token your-token
Feature Scaffolding #
Generate a Flutter feature from Swagger and Figma:
api_generator generate-feature \
--swagger-url https://example.com/swagger.json \
--figma-url https://www.figma.com/file/FILE_KEY/Auth?node-id=1%3A2 \
--feature-name auth \
--project-name agro_cosmos \
--output /path/to/flutter_app/lib
Optional Figma access:
export FIGMA_ACCESS_TOKEN=your-token
Or pass it directly:
api_generator generate-feature \
--swagger-url https://example.com/swagger.json \
--figma-url https://www.figma.com/file/FILE_KEY/Auth \
--feature-name auth \
--project-name agro_cosmos \
--figma-token your-token
The feature generator:
- reuses the generated OpenAPI Dio client in
lib/core/generated/openapi - detects endpoints related to the feature name
- inspects the selected Figma node when
node-idis present - falls back to Swagger-driven screens when Figma metadata is not accessible
- writes a
feature_generation_report.mdwith mapping, assumptions, and integration notes
Notes #
application/jsonrequest bodies are mapped first.- Multipart request handling is designed to be extended later.
- Repository methods are grouped by OpenAPI tags and named from
operationId. - Generated
api_client.dartusesretrofit,dio, andflutter/foundation.dart. - After generation, run your Flutter app's Retrofit build step for
api_client.g.dart. generate-featurealso expects your Flutter app to provideget_it,flutter_bloc,freezed_annotation, andjson_annotation, then runbuild_runner.