improve documentation

This commit is contained in:
Hayden 2023-03-05 20:17:53 -09:00
parent c2f432cb4d
commit e1f9c2997b
No known key found for this signature in database
GPG key ID: 17CF79474E257545
5 changed files with 83 additions and 2 deletions

View file

@ -11,6 +11,18 @@ import (
type CommandFunc[T any] func(context.Context) (T, error)
type CommandIDFunc[T any] func(context.Context, uuid.UUID) (T, error)
// Command is an HandlerAdapter that returns a server.HandlerFunc that
// The command adapters are used to handle commands that do not accept a body
// or a query. You can think of them as a way to handle RPC style Rest Endpoints.
//
// Example:
//
// fn := func(ctx context.Context) (interface{}, error) {
// // do something
// return nil, nil
// }
//
// r.Get("/foo", adapters.Command(fn, http.NoContent))
func Command[T any](f CommandFunc[T], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
res, err := f(r.Context())
@ -22,6 +34,17 @@ func Command[T any](f CommandFunc[T], ok int) server.HandlerFunc {
}
}
// CommandID is the same as the Command adapter but it accepts a UUID as a parameter
// in the URL. The parameter name is passed as the first argument.
//
// Example:
//
// fn := func(ctx context.Context, id uuid.UUID) (interface{}, error) {
// // do something
// return nil, nil
// }
//
// r.Get("/foo/{id}", adapters.CommandID("id", fn, http.NoContent))
func CommandID[T any](param string, f CommandIDFunc[T], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
ID, err := routeUUID(r, param)