Replies: 1 comment
-
|
@danielgtaylor, what do you think of this idea? I'd be happy to work on an implementation if it sounds good. |
Beta Was this translation helpful? Give feedback.
0 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.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been using Huma for almost a year on production and it has largely been a pleasure.
While the initial draw for us was the OpenAPI generation, the type-safety of requests/responses ultimately turned out to be the biggest game-changer.
That said, there's a pain point I'm currently experiencing.
I make use of an envelope to wrap all response data for consistency and extensibility:
{ "data": { "name": "John Doe" } }This makes it easy to extend the response with extra information like pagination details:
{ "data": [ { "name": "John Doe" }, { "name": "Jane Doe" } ], "metadata": { "current_page": 1, "page_size": 2, "total_pages": 1, "total_records": 2 } }To simply that, I make use of a generic wrapper to enforce the envelope.
As mentioned in the docs, Huma automatically treats the
Bodyfield of requests/responses structs as the request/response body. So it needs to be explicitly part of the envelope.This works perfectly fine for most endpoints:
But it falls apart when I need to provide custom response headers as they can't be dynamically added to the generic.
Like with the
Body, it would be great to also have a special field for the headers, let's sayHeaderswhich is automatically handled. So the envelope can be extended to:My current workaround is having a general
Headersstruct which holds all possible headers in the system and inlining it into the envelope:This works but adds noise of unused headers in that endpoint's documentation, making it less accurate.

That brings me back to my original point. Treating
Headeras a special field for headers in the response would be great to aid reusability of wrappers.In the same vein, this can also be extended to requests. Like with responses,
Bodyfields in a request struct are automatically treated as the request body. The same can be done forHeaders,Paths,QueriesandCookies.One issue I see with this is the possibility of breaking existing code which have those fields already set for other purposes.
To limit that, they can be prepended with "Huma" i.e.
Headers->HumaHeaders. This doesn't make the chances of collision zero, but it should greatly reduce them.Ultimately, a more foolproof alternative would be having a
humastruct tag:This is my preferred approach as it has the advantage of being able to freely use any field names, and also gives a more intuitive (in my opinion, at least) model of the request/response.
Beta Was this translation helpful? Give feedback.
All reactions