extensions

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: Apache-2.0 Imports: 11 Imported by: 3

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.

func (HATEOAS[T]) Apply

func (h HATEOAS[T]) Apply(api *babyapi.API[T]) error

Apply the custom ResponseWrapper

func (HATEOAS[T]) ResponseWrapper

func (h HATEOAS[T]) ResponseWrapper(api *babyapi.API[T]) func(resource T) render.Renderer

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

type HTMX[T babyapi.Resource] struct{}

HTMX is a shortcut to apply HTMX compatibility. Currently this just sets a 200 response on Delete

func (HTMX[T]) Apply

func (HTMX[T]) Apply(api *babyapi.API[T]) error

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
}

func (KVConnectionConfig) CreateDB

func (h KVConnectionConfig) CreateDB() (hord.Database, error)

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

func (KeyValueStorage[T]) Apply

func (h KeyValueStorage[T]) Apply(api *babyapi.API[T]) error

Jump to

Keyboard shortcuts

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