request

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 21 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoTx                = fmt.Errorf("no transaction set")
	ErrNotModel            = fmt.Errorf("propery must be of type models.Model")
	ErrCompositePrimaryKey = fmt.Errorf("can't use models with composite primary keys")
)

Functions

func HandleErrors

func HandleErrors(handlers ...func(err error)) router.MiddlewareFunc

func Run

func Run(requestHttp *http.Request, requestStruct any) error

func SetTestTx

func SetTestTx(r *http.Request, tx *sqlx.Tx) *http.Request

func UseDB

func UseDB(r *http.Request) *sqlx.DB

func UseTx

func UseTx(r *http.Request) *sqlx.Tx

func Validate

func Validate(request *http.Request, keys []string, v any) error

func WithDB

func WithDB(db *sqlx.DB) router.MiddlewareFunc

Types

type ErrResponse

type ErrResponse struct {
	Error      string          `json:"error"`
	Status     int             `json:"status"`
	StatusText string          `json:"-"`
	Fields     ValidationError `json:"fields,omitempty"`
}

type HTMLResponse

type HTMLResponse struct {
	Response
}

func NewHTMLResponse

func NewHTMLResponse(data []byte) *HTMLResponse

type HTTPError

type HTTPError struct {
	// contains filtered or unexported fields
}

func NewHTTPError

func NewHTTPError(err error, status int) *HTTPError

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Respond

func (e *HTTPError) Respond(w http.ResponseWriter, r *http.Request) error

type JSONResponse

type JSONResponse struct {
	// contains filtered or unexported fields
}

func NewJSONResponse

func NewJSONResponse(data any) *JSONResponse

func (*JSONResponse) AddHeader

func (r *JSONResponse) AddHeader(key, value string) *JSONResponse

func (*JSONResponse) Respond

func (r *JSONResponse) Respond(w http.ResponseWriter, _ *http.Request) error

func (*JSONResponse) SetStatus

func (r *JSONResponse) SetStatus(status int) *JSONResponse

type Message

type Message struct {
	Array   string `json:"array"`
	String  string `json:"string"`
	Numeric string `json:"numeric"`
}

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(b []byte) error

type MessageOptions

type MessageOptions struct {
	Attribute string
	Value     any
	Arguments []string
}

type RequestHandler

type RequestHandler[TRequest, TResponse any] func(r *TRequest) (TResponse, error)

func Handler

func Handler[TRequest, TResponse any](callback func(r *TRequest) (TResponse, error)) RequestHandler[TRequest, TResponse]

Handler is a helper to create http handlers with built in input validation and error handling.

func (RequestHandler[TRequest, TResponse]) ServeHTTP

func (h RequestHandler[TRequest, TResponse]) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Responder

type Responder interface {
	Respond(w http.ResponseWriter, r *http.Request) error
}

type Response

type Response struct {
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(body io.Reader) *Response

func (*Response) AddHeader

func (r *Response) AddHeader(key, value string) *Response

func (*Response) Respond

func (r *Response) Respond(w http.ResponseWriter, _ *http.Request) error

func (*Response) SetStatus

func (r *Response) SetStatus(status int) *Response

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	Status int
}

func (*ResponseWriter) OK

func (w *ResponseWriter) OK() bool

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(statusCode int)

type ValidationError

type ValidationError map[string][]string

func (ValidationError) AddError

func (e ValidationError) AddError(key string, message string)

func (ValidationError) Error

func (e ValidationError) Error() string

func (ValidationError) HasErrors

func (e ValidationError) HasErrors() bool

func (ValidationError) Merge

func (e ValidationError) Merge(vErr ValidationError)

type Validator

type Validator interface {
	Valid() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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