mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-23 11:00:28 +00:00
improve documentation
This commit is contained in:
parent
c2f432cb4d
commit
e1f9c2997b
5 changed files with 83 additions and 2 deletions
|
@ -10,7 +10,18 @@ import (
|
|||
// It decodes the request body into a value of type T and passes it to the function f.
|
||||
// The function f is expected to return a value of type Y and an error.
|
||||
//
|
||||
// Note: Action differs from Query in that it decodes the request body.
|
||||
// Example:
|
||||
//
|
||||
// type Body struct {
|
||||
// Foo string `json:"foo"`
|
||||
// }
|
||||
//
|
||||
// fn := func(ctx context.Context, b Body) (any, error) {
|
||||
// // do something with b
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Post("/foo", adapters.Action(fn, http.StatusCreated))
|
||||
func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
v, err := decode[T](r)
|
||||
|
@ -28,6 +39,19 @@ func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
|||
}
|
||||
|
||||
// ActionID functions the same as Action, but it also decodes a UUID from the URL path.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Body struct {
|
||||
// Foo string `json:"foo"`
|
||||
// }
|
||||
//
|
||||
// fn := func(ctx context.Context, ID uuid.UUID, b Body) (any, error) {
|
||||
// // do something with ID and b
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Post("/foo/{id}", adapters.ActionID(fn, http.StatusCreated))
|
||||
func ActionID[T any, Y any](param string, f IDFunc[T, Y], ok int) server.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
ID, err := routeUUID(r, param)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue