dart_swagger_to_api_client 1.1.1 copy "dart_swagger_to_api_client: ^1.1.1" to clipboard
dart_swagger_to_api_client: ^1.1.1 copied to clipboard

Generate type-safe HTTP API clients from OpenAPI/Swagger specs for Dart and Flutter.

dart_swagger_to_api_client #

Dart License

Generate type-safe HTTP API clients from OpenAPI/Swagger specifications

dart_swagger_to_api_client is a code generator that creates fully type-safe, production-ready HTTP clients for Dart and Flutter applications. It works seamlessly with dart_swagger_to_models to generate a complete stack: models + API client.

✨ Features #

  • 🎯 Type-safe API calls β€” Strongly typed methods generated from OpenAPI specs
  • πŸ”„ Multiple HTTP adapters β€” Support for http, dio, and custom adapters
  • πŸ›‘οΈ Middleware system β€” Logging, retries, rate limiting, circuit breakers, and more
  • πŸ” Flexible authentication β€” API keys, bearer tokens, environment variables
  • 🌍 Environment profiles β€” Easy switching between dev/staging/prod
  • πŸ“¦ Model integration β€” Automatic integration with dart_swagger_to_models
  • ⚑ Watch mode β€” Auto-regenerate on spec changes
  • πŸš€ CI/CD ready β€” Templates for GitHub Actions and GitLab CI
  • πŸ“š State management β€” Examples for Riverpod and BLoC

πŸš€ Quick Start #

Installation #

Add to your pubspec.yaml:

dev_dependencies:
  dart_swagger_to_models: ^0.9.0
  dart_swagger_to_api_client: ^1.0.0

Basic Usage #

  1. Generate models (using dart_swagger_to_models):
dart run dart_swagger_to_models:dart_swagger_to_models \
  --input swagger/api.yaml \
  --output-dir lib/models \
  --style json_serializable
  1. Generate API client:
dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
  --input swagger/api.yaml \
  --output-dir lib/api_client
  1. Use in your code:
import 'package:my_app/api_client/api_client.dart';
import 'package:my_app/models/user.dart';

final config = ApiClientConfig(
  baseUrl: Uri.parse('https://api.example.com'),
  auth: AuthConfig(
    bearerToken: 'your-token-here',
  ),
);

final client = ApiClient(config);

try {
  // Type-safe API call
  final List<User> users = await client.defaultApi.getUsers();
  print('Users: $users');
} finally {
  await client.close();
}

πŸ“– Documentation #

🎯 Key Concepts #

HTTP Adapters #

Choose your HTTP implementation:

// Default: package:http
final config = ApiClientConfig(
  baseUrl: Uri.parse('https://api.example.com'),
);

// Dio adapter
import 'package:dio/dio.dart';
final dio = Dio();
final adapter = DioHttpClientAdapter(dio: dio);
final config = ApiClientConfig(
  baseUrl: Uri.parse('https://api.example.com'),
  httpClientAdapter: adapter,
);

// Custom adapter
class MyCustomAdapter implements HttpClientAdapter {
  @override
  Future<HttpResponse> send(HttpRequest request) async {
    // Your implementation
  }
}

Middleware #

Add powerful middleware to your client:

final config = ApiClientConfig(
  baseUrl: Uri.parse('https://api.example.com'),
  requestInterceptors: [
    RateLimitInterceptor(maxRequests: 100, window: Duration(minutes: 1)),
    LoggingInterceptor.console(),
  ],
  responseInterceptors: [
    RetryInterceptor(maxRetries: 3),
    CircuitBreakerInterceptor(failureThreshold: 5),
  ],
);

Environment Profiles #

Configure different environments:

# dart_swagger_to_api_client.yaml
client:
  baseUrl: https://api.example.com

environments:
  dev:
    baseUrl: https://dev-api.example.com
  prod:
    baseUrl: https://api.example.com
    auth:
      bearerTokenEnv: PROD_BEARER_TOKEN
dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
  --input swagger/api.yaml \
  --output-dir lib/api_client \
  --config dart_swagger_to_api_client.yaml \
  --env prod

πŸ“š Examples #

See the example/ directory for complete examples:

  • complete_example.dart β€” Full end-to-end example
  • auth_example.dart β€” Authentication methods
  • error_handling_example.dart β€” Error handling and retries
  • middleware_example.dart β€” Middleware usage
  • circuit_breaker_example.dart β€” Circuit breaker pattern
  • transformer_example.dart β€” Request/response transformations
  • riverpod_integration_example.dart β€” Riverpod integration
  • bloc_integration_example.dart β€” BLoC integration

πŸ› οΈ CLI Options #

dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
  --input swagger/api.yaml \
  --output-dir lib/api_client \
  --config dart_swagger_to_api_client.yaml \
  --env prod \
  --watch \
  --verbose

Options:

  • --input / -i β€” OpenAPI/Swagger spec path (required)
  • --output-dir β€” Output directory (required)
  • --config / -c β€” Configuration file path
  • --env β€” Environment profile name
  • --watch / -w β€” Watch mode for auto-regeneration
  • --verbose / -v β€” Verbose output
  • --quiet / -q β€” Quiet mode (errors only)
  • --help / -h β€” Show help

πŸ”„ Watch Mode #

Automatically regenerate on spec changes:

dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
  --input swagger/api.yaml \
  --output-dir lib/api_client \
  --watch

πŸ€– CI/CD Integration #

Ready-to-use templates for automatic regeneration:

  • GitHub Actions β€” .github/workflows/regenerate-client.yml
  • GitLab CI β€” .gitlab-ci.yml

See ci/README.md for setup instructions.

🎨 State Management Integration #

Examples for popular state management solutions:

  • Riverpod β€” example/riverpod_integration_example.dart
  • BLoC β€” example/bloc_integration_example.dart

πŸ“‹ Requirements #

  • Dart SDK: ^3.11.0
  • dart_swagger_to_models (for model generation)

🀝 Contributing #

Contributions are welcome! Please see DEVELOPERS.md for guidelines.

πŸ“„ License #

MIT License β€” see LICENSE file for details.

πŸ“ž Support #


Made with ❀️ for the Dart/Flutter community

1
likes
70
points
31
downloads
screenshot

Documentation

API reference

Publisher

verified publishergoodwin.website

Weekly Downloads

Generate type-safe HTTP API clients from OpenAPI/Swagger specs for Dart and Flutter.

Repository (GitHub)
View/report issues

Topics

#openapi #swagger #code-generator #codegen #http-client

License

MIT (license)

Dependencies

args, crypto, dio, http, meta, path, yaml

More

Packages that depend on dart_swagger_to_api_client