Effect HttpApi Derived Client
A content-publishing HTTP API declared once as an Effect 4 HttpApi, so the server, the client, and the OpenAPI document all come from a single source. The problem it replaces is the usual REST service with three artefacts that are supposed to agree and do not: the server routes, a client SDK, and an OpenAPI file that a generator produces from annotations nobody keeps current, the doc being the first to rot because nothing breaks when it is wrong. Here one declaration lists groups of endpoints, each naming its method, path, path params, query, payload, success, and errors as schemas, and from that one value three things are derived: the server implements handlers against it, a client is produced from it with no codegen step, and OpenApi.fromApi turns it into an OpenAPI document, so the doc cannot describe a route the API does not declare and the client cannot call a path the server does not expose. Everything crossing the boundary is validated by its schema at runtime before a handler runs and encoded on the way out, so a params.id declared Schema.Int arrives at the handler as a number because the URL string was decoded and a non-integer was rejected with a 400 first. Status codes live on the error schema rather than the route, so the AlreadyPublished error is mapped to a conflict wherever it is raised, and the prebuilt HttpApiError.NotFound and Forbidden carry their statuses already. Enforcement is two layers: the handler and client types are derived from the declaration, so a handler that returns the wrong shape or a call with the wrong params does not compile, and the schema validates the same shapes at runtime as an independent guard, which is why the handler reads its decoded request under the params and query keys the framework passes. The server file wires HttpApiBuilder.layer with the OpenAPI document mounted as a route, and the derived client calls endpoints by group and name from the same declaration. Verified against [email protected], the HttpApi layer largely Tim Smart's work.
npx shadcn@latest add https://ui.aryank.space/r/effect-httpapi-derived-client.jsonInstalls from ui.aryank.space. To add it by hand, copy the files in Files below, or register the @compronents namespace via the docs.
The server routes, the client SDK, and the OpenAPI file are three artefacts that are supposed to agree. The server renamed title to headline last sprint; the SDK still types title. It compiles, ships, and returns undefined in production, the drift no build step can see.