- How do I install spatie/laravel-openapi-cli in my Laravel project?
- Run `composer require spatie/laravel-openapi-cli` in your project directory. The package requires Laravel 10.x+ and PHP 8.1+. No additional configuration is needed for basic usage, but you’ll need to define your OpenAPI spec path in `config/openapi.php` or register it programmatically.
- Can I use this package with Laravel Zero for standalone CLI tools?
- Yes, the package is explicitly designed to work with Laravel Zero. It generates Artisan commands from OpenAPI specs, making it ideal for building lightweight, API-focused CLI tools without a full Laravel frontend.
- What OpenAPI versions does this package support (Swagger 2.0, OpenAPI 3.x)?
- The package supports OpenAPI 3.x (including 3.0, 3.1) and Swagger 2.0 specs. It relies on `zircote/swagger-php` for parsing, so ensure your spec adheres to one of these versions. Complex specs with `$ref` or nested schemas should work, but validate them pre-integration.
- How do I configure authentication (Bearer tokens, API keys) for generated commands?
- Use the fluent `register()` method to set auth. For Bearer tokens, chain `->bearer('your_token')` or `->bearer(env('TOKEN_KEY'))`. For API keys, use `->header('X-API-Key', 'value')`. You can also inject auth dynamically via the `onBeforeRequest` callback.
- Will generated commands cache API responses, or do they hit live endpoints every time?
- By default, commands hit live endpoints. Enable caching with `->cache(ttl: 300)` to store responses for a specified time (in seconds). Cached responses are stored in Laravel’s default cache driver (e.g., file, Redis).
- Can I filter which OpenAPI endpoints become CLI commands? For example, only generate commands for `/users` endpoints.
- Yes, use the `--filter` flag during generation, e.g., `php artisan openapi:generate --filter '/users'`. You can also filter by HTTP method (e.g., `--filter-method GET`) or operation ID if your spec uses them.
- How do I handle custom error responses or rate limiting in generated commands?
- Use the `onError` callback in the `register()` method to define custom logic. For example, detect 429 responses and show a retry message: `->onError(fn($response) => $response->status() === 429 ? 'Rate limited' : false)`. You can also throw exceptions or log errors.
- Does this package work with Laravel packages (e.g., for reusable API contracts)?
- Yes, the package is built with Laravel packages in mind. You can publish the OpenAPI config or register specs programmatically in a service provider, making it reusable across microservices or monoliths sharing the same API contracts.
- What’s the best way to test generated commands before deploying to production?
- Use the `openapi:generate` command with `--dry-run` to preview commands without executing them. For integration tests, mock HTTP responses with Laravel’s `Http::fake()` or use `symfony/process` to test CLI output. Validate edge cases like auth failures or malformed requests.
- Are there alternatives to this package for generating CLI tools from OpenAPI specs?
- Alternatives include `openapi-generator` (multi-language CLI tool) or custom scripts using `zircote/swagger-php` to parse specs and generate PHP code. However, this package is Laravel-native, offering seamless integration with Artisan, caching, and Laravel Zero, which alternatives lack.