- How do I generate Laravel Artisan commands from an OpenAPI spec?
- Use the `OpenApiCli::register()` method in a service provider, pointing to your OpenAPI YAML/JSON file. The package automatically generates one command per endpoint, with typed options for path/query/body inputs. Example: `OpenApiCli::register('path/to/spec.yaml', 'api-name')`.
- Does this package work with Laravel Zero for standalone CLI tools?
- Yes, it’s explicitly designed for Laravel Zero. Generate API-specific commands (e.g., `php artisan bookstore:fetch-orders`) and distribute them as standalone tools without requiring a full Laravel stack. Just include the package and register your OpenAPI spec.
- What Laravel versions does `spatie/laravel-openapi-cli` support?
- The package supports Laravel 9.x and 10.x. Check the [GitHub repo](https://github.com/spatie/laravel-openapi-cli) for the latest compatibility notes, as minor updates may align with new Laravel releases. Laravel Zero compatibility is also maintained.
- How do I handle authentication (OAuth, API keys) in generated commands?
- Use the `auth()` method with a closure or static value. For OAuth, pass a token manager: `->auth(fn() => app(OAuthTokenManager::class)->token())`. For API keys, use `->auth('Bearer YOUR_KEY')`. Dynamic auth (e.g., token refresh) requires custom closures in `retryOn()` or `onError()`.
- Can I customize the output format (JSON, YAML, HTML) for API responses?
- Yes. Use chainable methods like `->jsonOutput()`, `->yamlOutput()`, or `->showHtmlBody()` during registration. For example, `->yamlOutput()->cache(ttl: 300)` ensures responses are formatted as YAML and cached for 5 minutes.
- What happens if my OpenAPI spec changes frequently? Will commands break?
- Commands rely on cached specs by default (configurable TTL). If specs change, update the cached file or disable caching (`->cache(false)`). For real-time sync, fetch specs dynamically in a custom closure during registration. Monitor spec updates to avoid stale commands.
- How do I test commands generated from OpenAPI specs?
- Unit test individual commands using Laravel’s `Artisan::call()` or mock HTTP responses with Guzzle/Symfony HTTP clients. For auth or error scenarios, use `retryOn()` or `onError()` closures with test doubles. Integration tests may require mocking external APIs.
- Are there alternatives to this package for OpenAPI-to-CLI generation?
- Alternatives include custom scripts using OpenAPI parsers (e.g., `zircote/swagger-php`) or tools like [OpenAPI Generator](https://openapi-generator.tech/). However, this package uniquely integrates with Laravel’s Artisan ecosystem, offering typed parameters, auth, and caching out of the box.
- Can I extend generated commands with custom logic (e.g., local file processing)?
- Yes. Override the generated command class or use Laravel’s command hooks (e.g., `handle()` method). For example, extend a command to save API responses to a file: `public function handle(): int { $response = $this->callApi(); file_put_contents('output.json', $response->getBody()); return 0; }`.
- How do I handle rate limits or retry logic for API failures?
- Use the `retryOn()` and `onError()` methods to define custom logic. Example: `->retryOn(fn($response) => $response->status() === 429, 3)` retries 3 times on rate limits. For OAuth token refreshes, combine with `auth()`: `->retryOn(fn($response) => $response->status() === 401, fn() => app(OAuthTokenManager::class)->refresh())`.