Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HATEOAS ¶
type HATEOAS[T babyapi.Resource] struct { // LinkKey will override the default "links" key that the link map is represented by LinkKey string // CustomLinks runs as part of the Render process for responses and allows adding or overwriting links CustomLinks func(*http.Request) map[string]string }
HATEOAS is an babyapi Extension that wraps any babyapi Resource with automatic HATEOAS style links. By default, it can automatically add links for "self" and child resources when getting a resource by ID. This will add the "links" field to the JSON response, so be wary of conflicting with that key. Set the LinkKey to an alternative option if "links" would conflict.
type HATEOASResponse ¶
type HATEOASResponse[T babyapi.Resource] struct { Resource T Links map[string]string `json:"links"` // contains filtered or unexported fields }
HATEOASResponse wraps a babyapi Resource with additional links. The custom MarshalJSON function will handle flattening the Resource so the added "links" field is at the same level as other fields of the Resource
func (*HATEOASResponse[T]) MarshalJSON ¶
func (h *HATEOASResponse[T]) MarshalJSON() ([]byte, error)
MarshalJSON will handle flattening the Links and Resource by marshalling separately and then combining. If the Resource has its own links field, this will not work unless the LinkKey is overridden
func (*HATEOASResponse[T]) Render ¶
func (h *HATEOASResponse[T]) Render(w http.ResponseWriter, r *http.Request) error
Render will populate the HATEOAS response with the appropriate links
type HTMX ¶
HTMX is a shortcut to apply HTMX compatibility. Currently this just sets a 200 response on Delete
type KVConnectionConfig ¶
type KVConnectionConfig struct {
// Filename to write JSON data to
Filename string
// Host of Redis instance
RedisHost string
// Password for Redis instance
RedisPassword string
// If other configurations are empty, this will not return an error and skips setting api Storage.
// This is useful if using env vars as the values for configs
Optional bool
}
type KeyValueStorage ¶
type KeyValueStorage[T babyapi.Resource] struct { // Optional key to use as a prefix when storing in key-value store. If empty, api Name is used StorageKeyPrefix string // KVConnectionConfig has connection data for KV store. Optional if DB is provided KVConnectionConfig // DB is the database connection. It is created if not provided. This is useful if multiple APIs share // a storage backend DB hord.Database }
KeyValueStorage sets up a connection to Redis or local file storage and applies to the API's Storage Client If you pass environment variables for configurations, this can dynamically determine filesystem or Redis storage based on the available configs