resources

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collection

func Collection[T any](req *http.Request, models []T, transformer Resource[T]) []map[string]any

Collection applies a resource transformer to a slice of models.

func Created

func Created(w http.ResponseWriter, data any)

Created sends a created response

func Error

func Error(w http.ResponseWriter, status int, message string)

Error sends an error response

func JSON

func JSON(w http.ResponseWriter, status int, data any)

JSON sends a JSON response

func Merge

func Merge(maps ...map[string]any) map[string]any

Merge merges multiple maps

func Paginated

func Paginated(w http.ResponseWriter, data []map[string]any, links *Links, meta *Meta)

Paginated sends a paginated 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

func RespondWithMeta(w http.ResponseWriter, data any, meta, links map[string]any, status int)

RespondWithMeta writes a resource or collection with custom meta and links.

func Success

func Success(w http.ResponseWriter, data any)

Success sends a success response

func Transform

func Transform[T any](req *http.Request, model T, transformer Resource[T]) map[string]any

Transform applies a resource transformer to a single model.

func When

func When(condition bool, key string, value any) map[string]any

When adds a field conditionally

Types

type CollectionResponse

type CollectionResponse struct {
	Data []map[string]any `json:"data"`
}

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

type Resource

type Resource[T any] interface {
	ToMap(req *http.Request, model T) map[string]any
}

Resource defines how a model should be transformed into JSON.

type ResourceResponse

type ResourceResponse struct {
	Data  any            `json:"data"`
	Meta  map[string]any `json:"meta,omitempty"`
	Links map[string]any `json:"links,omitempty"`
}

ResourceResponse represents the standard JSON envelope.

Jump to

Keyboard shortcuts

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