Part V — The Modern Experience Layer · Chapter 28 · Lesson 3 of 3
OpenAPI 3.0 generation and typed SDKs
The last serious ergonomic gap in hand-rolled APIs is the spec drifting away from the routes. PeaLang closes it: the spec is generated from the app object you already have.
Generate the spec
let spec = web.openapi(app, "Orders API", "1.2.0")
web.openapi(app_obj, title="PeaLang App", version="1.0.0") delegates to app.openapi(title, version). It walks the registered routes — paths, methods and path parameters — and emits an OpenAPI document. Path params in :id form become OpenAPI {id} form; route groups appear as path prefixes. The signature accepts a title and version string, not a description map, so enrich the returned document after generation if your pipeline needs extra metadata.
Serve it
app.get("/openapi.json", function(req) {
return web.json(spec)
})
Or use the CLI: pea openapi (and its alias web-openapi) generate the document from a source file without running the service — ideal for CI. Both commands accept --title and --version.
From routes to typed clients
The generators are registered in the analysis namespace. Of these, only generate_typescript_client(routes, ...) reads route descriptors; the others return static source strings/snippets and do not inspect routes:
analysis.generate_typescript_client(routes, base_url="http://localhost:8000", module_name="PeaClient")— a fetch-based typed client from route descriptors.analysis.generate_react_hooks()— a React hooks source string for PeaLang's browser WASM runtime; it does not generate route clients.analysis.sse_endpoint_source()/analysis.websocket_handler_source()— streaming endpoint source strings.analysis.jwt_middleware_source(),analysis.rate_limiter_source(),analysis.api_versioning_source()— source snippets you can review before adding them to a service.
Versioning without pain
app.group("/v1").get("/orders", list_orders_v1)
app.group("/v2").get("/orders", list_orders_v2)
analysis.api_versioning_source() returns path-based middleware that reads a /vN/ path prefix; it has no header-based mode.
Guided practice
- Generate
openapi.jsonfrom a two-route app and validate it with any OpenAPI validator; note what the generator could not infer (response schemas you should add by hand). - Generate a TypeScript client and call one endpoint from a scratch script; check the types flow (try a wrong id type — your editor should complain).
- Add a breaking change on a v2 route group and confirm both specs emit cleanly side by side.
Rule of thumb. If your spec is written by hand, it is already wrong. Generate it, diff it in CI, and treat it as a build artifact.