这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -2048,7 +2048,7 @@ func convenience[I, O any](api API, method, path string, handler func(context.Co
// Body []Thing
// }) (*ListThingOutput, error) {
// // TODO: list things from DB...
// resp := &PostThingOutput{}
// resp := &ListThingOutput{}
// resp.Body = []Thing{{ID: "1", Name: "Thing 1"}}
// return resp, nil
// })
Expand Down Expand Up @@ -2079,6 +2079,26 @@ func Post[I, O any](api API, path string, handler func(context.Context, *I) (*O,
convenience(api, http.MethodPost, path, handler, operationHandlers...)
}

// Head HTTP operation handler for an API. The handler must be a function that
// takes a context and a pointer to the input struct and returns a pointer to the
// output struct and an error. The input struct must be a struct with fields
// for the request path/query/header/cookie parameters. The output struct must be a
// struct with fields for the output headers of the operation, if any.
//
// huma.Head(api, "/things/{thing-id}", func(ctx context.Context, input *struct{
// ID string `path:"thing-id"`
// Header string `header:"X-My-Header"`
// }) (*HeadThingOutput, error) {
// // TODO: get info from DB...
// resp := &HeadThingOutput{}
// return resp, nil
// })
//
// This is a convenience wrapper around `huma.Register`.
func Head[I, O any](api API, path string, handler func(context.Context, *I) (*O, error), operationHandlers ...func(o *Operation)) {
convenience(api, http.MethodHead, path, handler, operationHandlers...)
}

// Put HTTP operation handler for an API. The handler must be a function that
// takes a context and a pointer to the input struct and returns a pointer to the
// output struct and an error. The input struct must be a struct with fields
Expand Down
6 changes: 6 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2899,6 +2899,12 @@ func TestConvenienceMethods(t *testing.T) {
assert.Equal(t, []string{"Things"}, api.OpenAPI().Paths[path].Post.Tags)

path = path + "/{thing-id}"
huma.Head(api, path, func(ctx context.Context, input *Input) (*struct{}, error) {
return nil, nil
}, huma.OperationTags("Things"))
assert.Equal(t, "head-things-by-thing-id", api.OpenAPI().Paths[path].Head.OperationID)
assert.Equal(t, []string{"Things"}, api.OpenAPI().Paths[path].Head.Tags)

huma.Put(api, path, func(ctx context.Context, input *Input) (*struct{}, error) {
return nil, nil
}, huma.OperationTags("Things"))
Expand Down
Loading