OpenAPI & Clients
PAM HTTP knows every route you register, so it can describe them. $app->openApi() produces an OpenAPI 3.1
document and simple typed clients straight from the router, with no annotations to keep in sync.
Describing Routes
Section titled “Describing Routes”Add metadata to a route with the PendingRoute methods:
$app->post('/users', [UserController::class, 'store']) ->name('users.store') ->summary('Create a user') ->tags(['Users']) ->input(StoreUserRequest::class) ->output(UserResource::class);| Method | Effect on the operation |
|---|---|
name() |
operationId. Without a name, PAM derives one from the method and path, such as get.users.id. |
summary() |
summary. |
tags() |
tags. |
input() |
A required JSON requestBody referencing a component schema named after the class. |
output() |
A JSON schema reference on the 200 response. |
Route parameters become required path parameters. Every operation lists 200, 422 and 500 responses.
Generating the Document
Section titled “Generating the Document”$document = $app->openApi('Example API', '1.0.0');
file_put_contents(__DIR__ . '/../openapi.json', $document->toJson());generate() returns the document as an array and toJson() as pretty-printed JSON. Paths are sorted, so the
output is stable in version control.
Generating Clients
Section titled “Generating Clients”use Pam\Api\OpenApi\ClientLanguage;
$typescript = $document->client(ClientLanguage::TypeScript);$kotlin = $document->client(ClientLanguage::Kotlin);$swift = $document->client(ClientLanguage::Swift);The generated client is a thin class with one method per operation. You supply the transport. The TypeScript client looks like this:
export class PamApiClient { constructor(private readonly request: (method: string, path: string) => Promise<unknown>) {} get_admin_me = () => this.request('GET', `/admin/me`); delete_admin_posts_id = (id: string | number) => this.request('DELETE', `/admin/posts/${encodeURIComponent(String(id))}`);}ClientGenerator can also be used directly with any OpenAPI array: (new ClientGenerator($document))->generate($language).
Detecting Breaking Changes
Section titled “Detecting Breaking Changes”Compare the committed document with the current one in CI:
use Pam\Api\OpenApi\CompatibilityChecker;
$previous = json_decode(file_get_contents('openapi.json'), true, flags: JSON_THROW_ON_ERROR);$changes = (new CompatibilityChecker())->breakingChanges($previous, $app->openApi()->generate());
foreach ($changes as $change) { fwrite(STDERR, "[{$change->code->value}] {$change->location}: {$change->message}\n");}
exit($changes === [] ? 0 : 1);Each CompatibilityChange has a code, a location and a message. The checker currently reports removed paths
(CompatibilityChangeCode::PathRemoved = 1) and removed operations (OperationRemoved = 2).