adapters

package
v0.0.0-...-0041c27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

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

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

func ActionID[T any, Y any](param string, f IDFunc[T, Y], ok int) errchain.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(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 DecodeBody

func DecodeBody[T any](r *http.Request) (T, error)

func DecodeQuery

func DecodeQuery[T any](r *http.Request) (T, error)

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

func QueryID[T any, Y any](param string, f IDFunc[T, Y], ok int) errchain.HandlerFunc

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))

func RouteUUID

func RouteUUID(r *http.Request, key string) (uuid.UUID, error)

Types

type AdapterFunc

type AdapterFunc[T any, Y any] func(*http.Request, T) (Y, error)

type CommandFunc

type CommandFunc[T any] func(*http.Request) (T, error)

type CommandIDFunc

type CommandIDFunc[T any] func(*http.Request, uuid.UUID) (T, error)

type IDFunc

type IDFunc[T any, Y any] func(*http.Request, uuid.UUID, T) (Y, error)

type Validator

type Validator interface {
	Validate() error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL