Documentation
¶
Overview ¶
Package adapters offers common adapters for turing regular functions into HTTP Handlers There are three types of adapters
- Query adapters
- Action adapters
- Command adapters
Index ¶
- func Action[T any, Y any](f AdapterFunc[T, Y], ok int) errchain.HandlerFunc
- func ActionID[T any, Y any](param string, f IDFunc[T, Y], ok int) errchain.HandlerFunc
- func Command[T any](f CommandFunc[T], ok int) errchain.HandlerFunc
- func CommandID[T any](param string, f CommandIDFunc[T], ok int) errchain.HandlerFunc
- func DecodeBody[T any](r *http.Request) (T, error)
- func DecodeQuery[T any](r *http.Request) (T, error)
- func Query[T any, Y any](f AdapterFunc[T, Y], ok int) errchain.HandlerFunc
- func QueryID[T any, Y any](param string, f IDFunc[T, Y], ok int) errchain.HandlerFunc
- func RouteUUID(r *http.Request, key string) (uuid.UUID, error)
- type AdapterFunc
- type CommandFunc
- type CommandIDFunc
- type IDFunc
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Action ¶
func Action[T any, Y any](f AdapterFunc[T, Y], ok int) errchain.HandlerFunc
Action is a function that adapts a function to the server.Handler interface. 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.
Example:
type Body struct { Foo string `json:"foo"` } fn := func(r *http.Request, b Body) (any, error) { // do something with b return nil, nil }
r.Post("/foo", adapters.Action(fn, http.StatusCreated))
func ActionID ¶
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(r *http.Request, 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 Command ¶
func Command[T any](f CommandFunc[T], ok int) errchain.HandlerFunc
Command is an HandlerAdapter that returns a errchain.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(r *http.Request) (interface{}, error) { // do something return nil, nil } r.Get("/foo", adapters.Command(fn, http.NoContent))
func CommandID ¶
func CommandID[T any](param string, f CommandIDFunc[T], ok int) errchain.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(r *http.Request, id uuid.UUID) (interface{}, error) { // do something return nil, nil } r.Get("/foo/{id}", adapters.CommandID("id", fn, http.NoContent))
func Query ¶
func Query[T any, Y any](f AdapterFunc[T, Y], ok int) errchain.HandlerFunc
Query is a server.Handler that decodes a query from the request and calls the provided function.
Example:
type Query struct { Foo string `schema:"foo"` } fn := func(r *http.Request, q Query) (any, error) { // do something with q return nil, nil } r.Get("/foo", adapters.Query(fn, http.StatusOK))
func QueryID ¶
QueryID is a server.Handler that decodes a query and an ID from the request and calls the provided function.
Example:
type Query struct { Foo string `schema:"foo"` } fn := func(r *http.Request, ID uuid.UUID, q Query) (any, error) { // do something with ID and q return nil, nil } r.Get("/foo/{id}", adapters.QueryID(fn, http.StatusOK))