- How do I generate Laravel Artisan commands from an OpenAPI spec?
- Use the `OpenApiCli::register()` method in your `AppServiceProvider` or a dedicated service provider. Pass the OpenAPI spec URL (local file or remote) and a namespace (e.g., 'bookstore'). The package automatically generates commands for each endpoint with typed options for path/query/body inputs.
- Does this package work with Laravel Zero for standalone CLI tools?
- Yes, it’s fully compatible with Laravel Zero. Register the API in your `AppServiceProvider` or `bootstrap/app.php`, then use the generated commands in your CLI tool. The package avoids Laravel’s web dependencies, making it lightweight for standalone tools.
- What Laravel versions does spatie/laravel-openapi-cli support?
- The package supports Laravel 10+ and requires PHP 8.1+. It’s tested against the latest stable Laravel releases and leverages Laravel’s HTTP client, Artisan, and service provider bootstrapping for seamless integration.
- Can I use multiple OpenAPI specs in the same Laravel app?
- Absolutely. Register each API with a unique namespace (e.g., `bookstore:`, `payments:`). Commands are namespaced to avoid conflicts, and you can configure each API independently with its own base URL, auth, and caching settings.
- How do I handle authentication for API endpoints?
- Use methods like `bearer()`, `basic()`, or `custom()` during registration. For dynamic auth (e.g., OAuth tokens), pass a closure to `custom()` that resolves the token at runtime. Static auth (e.g., `bearer(env('API_TOKEN'))`) is simpler but less flexible.
- What happens if my OpenAPI spec changes or is invalid?
- The package doesn’t enforce spec validation, so poorly structured specs may generate unusable commands. Test your spec locally first (e.g., with Swagger UI) and ensure it includes clear `operationId`s and path parameters. Cache remote specs to avoid repeated fetches during development.
- Can I customize error handling for API responses?
- Yes, use the `onError()` method during registration to define custom behavior. For example, handle rate limits (429) with warnings or retry logic. Default error handling logs failures but doesn’t retry, so extend it for production resilience (e.g., exponential backoff).
- How do I test commands generated from OpenAPI specs?
- Mock Laravel’s HTTP client (e.g., with `Http::fake()`) to simulate API responses. Test individual commands using PHPUnit’s `Artisan::call()` method. For integration tests, use real APIs in a staging environment or with API mocking tools like VCR.
- Are there performance concerns with remote OpenAPI specs?
- Remote specs are fetched once on boot and cached (configurable TTL). Command generation is dynamic, so there’s no runtime penalty beyond HTTP calls to the API. For large specs, cache locally or use a CDN to reduce fetch latency.
- What alternatives exist for CLI-driven API interactions in Laravel?
- For lightweight CLI tools, consider manually writing Artisan commands with Guzzle or Symfony’s HTTP client. For OpenAPI-first approaches, tools like OpenAPI Generator (Node.js) or Spectral CLI can generate SDKs, but they lack Laravel’s Artisan integration. This package bridges the gap by natively embedding OpenAPI specs into Laravel’s CLI ecosystem.