PeaBox Lab › PeaLang › Learn › Chapter 28 › Lesson 3

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:

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

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.

← Previous Next →