Overview
Sponsors
A big thank you to our new sponsor:
Groups
Huma now supports groups, which port over much of the functionality from @cardinalby's excellent https://github.com/cardinalby/hureg library (thank you for that work!). This enables creating groups of operations with the same path prefixes, middleware, operation modifiers, and transformers. Typical usage might look like this:
grp := huma.NewGroup(api, "/v1")
grp.UseMiddleware(authMiddleware)
// Register a `GET /v1/users` route that requires auth.
huma.Get(grp, "/users", func(ctx context.Context, input *struct{}) (*UsersResponse, error) {
// ...
})
See https://huma.rocks/features/groups/ for more details.
Context Unwrapping
Due to many user requests, it is now possible to "unwrap" a router-specific context into its constituent router-specific representation. Each adapter package now has an Unwrap(huma.Context) T
function that will return either a request/response pair or that router's own context type, allowing you to effectively escape Huma in router-agnostic middleware & resolvers.
Caution
You must use the same adapter package to create the API and call Unwrap
or Huma will panic!
Example usage:
router := http.NewServeMux()
api := humago.New(router, huma.DefaultConfig("My API", "1.0.0"))
api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
r, w := humago.Unwrap(ctx)
// Do something with the request/response.
// ...
next(ctx)
})
While generally not recommended, this can help you to use router-specific middleware as you migrate large existing projects to Huma, or just escape Huma's abstractions when they no longer make sense for your use-case. Sometimes the best library is the one that gets out of the way.
See https://huma.rocks/features/middleware/#unwrapping for more details.
What's Changed
- fix docs pointer by @iliadmitriev in #735
- feat: support context unwrapping by @danielgtaylor in #736
- feat: groups by @danielgtaylor in #728
- chore(deps): bump golang.org/x/net from 0.30.0 to 0.33.0 in /examples by @dependabot in #738
New Contributors
- @iliadmitriev made their first contribution in #735
Full Changelog: v2.29.0...v2.30.0