Repository navigation
✅ Per-endpoint status codes without duplicating the factory chain #3571
marco-carvalho
started this conversation in
Ideas
Replies: 1 comment 2 replies
|
Hello @marco-carvalho |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
The gap
Status codes are declared on the
ResultHandler, so they describe a factory, not an endpoint. When endpoints built on one factory can fail in different ways, the generated document cannot be accurate for all of them at once.Minimal example on
express-zod-api@28.7.6,zod@4.4.3:Generated document versus observed runtime:
GET /items/{id}200,400200,400,404,500The
404is thrown by the handler itself and the500arises fromOutputValidationErroror any unexpected throw, yet neither appears in the spec. A generated client has no branch for either, so the document is not merely incomplete, it makes a false claim about the operation.Why the existing answers do not quite close it
I found #1193 and the multiple-status-code support released in 16.2.0, so this is deliberately narrower than that request. Two things remain:
Array status codes are per handler, so accuracy trades against precision. Declaring
statusCode: [400, 404, 500]on one shared handler fixes the under-declaration, but every endpoint built on that factory then advertises a404, including collection endpoints that can never produce one. The document stops lying by omission and starts lying by inclusion.The
makeResultHandlerworkaround does not compose with middleware. The suggestion in #1193 (a function producing handlers, which is what I ended up shipping) works well until the factory carries anything else.resultHandleris only settable through the constructor and there is no method to derive a factory with a different one:So a factory that has accumulated auth, caching, and rate limiting must have that whole chain rebuilt from a fresh
new EndpointsFactory(...)for every distinct set of status codes. Factories then multiply as the cross product of middleware chains and failure shapes, and the duplicated chains are the kind of thing that drifts apart over time.For three endpoints I was happy to write two factories. The concern is how that scales.
Possible directions
Roughly in order of how small they look from the outside:
Let
build()narrow what the handler declares. The handler stays the single source of truth for runtime behaviour and declares the full union, say[400, 404, 500]; an individual endpoint documents a subset of it. Nothing about response transmission changes, only which of the already-declared codes appear on that operation. Narrowing rather than extending keeps the handler authoritative and can be validated at the type level.factory.withResultHandler(handler), returning a factory that keeps the existing middlewares, input schema, and scopes. This leaves status codes a handler concern, exactly as designed, and just makes the documented workaround compose.A
statusCodesfield onBuildProps. The most direct expression of intent, but it splits responsibility for status codes across two places, so I suspect it is the least appealing of the three.Happy to prototype whichever direction seems reasonable, or to be told this is working as intended and that the two-factory split is simply the expected shape.
All reactions