Documentation
¶
Index ¶
- func Collection[T any](req *http.Request, models []T, transformer Resource[T]) []map[string]any
- func Created(w http.ResponseWriter, data any)
- func Error(w http.ResponseWriter, status int, message string)
- func JSON(w http.ResponseWriter, status int, data any)
- func Merge(maps ...map[string]any) map[string]any
- func Paginated(w http.ResponseWriter, data []map[string]any, links *Links, meta *Meta)
- func Respond[T any](w http.ResponseWriter, req *http.Request, model T, transformer Resource[T], ...)
- func RespondCollection[T any](w http.ResponseWriter, req *http.Request, models []T, transformer Resource[T], ...)
- func RespondWithMeta(w http.ResponseWriter, data any, meta, links map[string]any, status int)
- func Success(w http.ResponseWriter, data any)
- func Transform[T any](req *http.Request, model T, transformer Resource[T]) map[string]any
- func When(condition bool, key string, value any) map[string]any
- type CollectionResponse
- type JsonResponse
- type Links
- type Meta
- type PaginatedResponse
- type Resource
- type ResourceResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Collection ¶
Collection applies a resource transformer to a slice of models.
func Error ¶
func Error(w http.ResponseWriter, status int, message string)
Error sends an error response
func Respond ¶
func Respond[T any](w http.ResponseWriter, req *http.Request, model T, transformer Resource[T], status int)
Respond writes a single resource out as JSON to the response writer inside a standard envelope.
func RespondCollection ¶
func RespondCollection[T any](w http.ResponseWriter, req *http.Request, models []T, transformer Resource[T], status int)
RespondCollection writes a collection out as JSON inside a standard envelope.
func RespondWithMeta ¶
RespondWithMeta writes a resource or collection with custom meta and links.
Types ¶
type CollectionResponse ¶
CollectionResponse wraps a collection of resources
func NewCollectionResponse ¶
func NewCollectionResponse(data []map[string]any) *CollectionResponse
NewCollectionResponse creates a collection response from maps
type JsonResponse ¶
type JsonResponse struct {
Data any `json:"data"`
Message string `json:"message,omitempty"`
Status string `json:"status"`
}
JsonResponse represents a standard JSON API response
type Links ¶
type Links struct {
Self string `json:"self"`
First string `json:"first"`
Last string `json:"last"`
Next string `json:"next,omitempty"`
Prev string `json:"prev,omitempty"`
}
Links contains pagination links
type Meta ¶
type Meta struct {
CurrentPage int `json:"current_page"`
LastPage int `json:"last_page"`
PerPage int `json:"per_page"`
Total int `json:"total"`
}
Meta contains pagination metadata
type PaginatedResponse ¶
type PaginatedResponse struct {
Data []map[string]any `json:"data"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
PaginatedResponse represents a paginated JSON response
Click to show internal directories.
Click to hide internal directories.