Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type ContextResponseEncoder ¶
type ContextResponseEncoder string
ContextResponseEncoder is a type used to identify encoders in Contexts
const ResponseEncoderKey ContextResponseEncoder = "encoder"
ResponseEncoderKey is the common key used by this package to store an EncodeDecoder in a Context
type HTTPWritter ¶
type HTTPWritter interface { // WriteHTTP writes the contents of the object to the http.ResponseWriter `w` WriteHTTP(ctx context.Context, w http.ResponseWriter) }
HTTPWriter writes an object to a http.ResponseWriter as a HTTP response
type HttpResponse ¶
type HttpResponse[T any] struct { Success bool `json:"success,omitempty"` Message string `json:"message,omitempty"` Error string `json:"error,omitempty"` Data *T `json:"data,omitempty"` Status int `json:"-"` }
httpResponse is a generic type for HTTP responses, both OK and not-OK
func NewResponse ¶
func NewResponse[T any](status int, message string, err error, data *T) HttpResponse[T]
newResponse builds a httpResponse object for type T derived from the input data (*T)
It takes in an int `status`, a string `message` and either an error or data.
- if the error is nil, it is assumed that it is an OK response and the provided *T `data` is stored in the response, and the error ignored
- if the error is not nil, it is assumed that the response is not-OK, so the *T `data` is ignored, and the error stored in the response
func (HttpResponse[T]) WriteHTTP ¶
func (r HttpResponse[T]) WriteHTTP(ctx context.Context, w http.ResponseWriter)
WriteHTTP writes the contents of the object to the http.ResponseWriter `w`
Click to show internal directories.
Click to hide internal directories.