api

package
v0.0.0-...-22a3027 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package api wraps Huma with Soro routing, errors, resources, and middleware.

Index

Constants

View Source
const RequestIDHeader = "X-Request-ID"

Variables

This section is empty.

Functions

func HTTPError

func HTTPError(ctx context.Context, err error) error

func Register

func Register[I, O any](router *Router, operation huma.Operation, handler func(context.Context, *I) (*O, error))

func RequestID

func RequestID(ctx context.Context) string

Types

type API

type API struct {
	// contains filtered or unexported fields
}

func New

func New(config Config, options ...Option) (*API, error)

func (*API) Config

func (api *API) Config() Config

func (*API) Handler

func (api *API) Handler() http.Handler

Handler returns the complete HTTP middleware chain.

func (*API) Huma

func (api *API) Huma() huma.API

func (*API) Mux

func (api *API) Mux() *http.ServeMux

func (*API) OpenAPI

func (api *API) OpenAPI() *huma.OpenAPI

func (*API) Routes

func (api *API) Routes() []Route

func (*API) Version

func (api *API) Version(name string, configure func(*Router)) error

type Action

type Action string

Action identifies one of the standard resource operations.

const (
	Index   Action = "index"
	Show    Action = "show"
	Create  Action = "create"
	Update  Action = "update"
	Destroy Action = "destroy"
)

type Config

type Config struct {
	Title        string
	Version      string
	BasePath     string
	OpenAPIPath  string
	DocsPath     string
	SchemasPath  string
	MaxBodyBytes int64
}

func DefaultConfig

func DefaultConfig() Config

func (Config) Validate

func (config Config) Validate() error

type ErrorBody

type ErrorBody struct {
	Code      string              `json:"code"`
	Message   string              `json:"message"`
	RequestID string              `json:"request_id,omitempty"`
	Fields    map[string][]string `json:"fields,omitempty"`
}

type ErrorEnvelope

type ErrorEnvelope struct {
	Error ErrorBody `json:"error"`
}

type Middleware

type Middleware func(http.Handler) http.Handler

type Option

type Option func(*settings)

func WithLogger

func WithLogger(logger *slog.Logger) Option

func WithMiddleware

func WithMiddleware(middleware ...Middleware) Option

func WithRequestIDGenerator

func WithRequestIDGenerator(generator func() string) Option

type PaginationMeta

type PaginationMeta struct {
	Page    int `json:"page"`
	PerPage int `json:"per_page"`
	Total   int `json:"total"`
	Pages   int `json:"pages"`
}

type Registrar

type Registrar interface {
	Register(*Router, string)
}

type Resource

type Resource[T, C, U, R any] struct {
	// contains filtered or unexported fields
}

Resource implements the five conventional REST operations for one model.

func NewResource

func NewResource[T, C, U, R any](config ResourceConfig[T, C, U, R]) (*Resource[T, C, U, R], error)

func (*Resource[T, C, U, R]) Register

func (resource *Resource[T, C, U, R]) Register(router *Router, path string)

type ResourceConfig

type ResourceConfig[T, C, U, R any] struct {
	Name            string
	Repository      *repository.Repository[T]
	Serializer      serializer.Serializer[T, R]
	CreateEntity    func(context.Context, C) (*T, error)
	UpdateEntity    func(context.Context, *T, U) error
	Query           query.Definition
	Disabled        []Action
	Authorize       ResourceHook[T]
	Before          ResourceHook[T]
	After           ResourceHook[T]
	Scope           func(context.Context, *bun.SelectQuery) *bun.SelectQuery
	ModifyOperation map[Action]func(*huma.Operation)
}

ResourceConfig describes a typed REST resource. CreateEntity and UpdateEntity are deliberately explicit so API input can never be reflectively assigned to a persistence model.

type ResourceHook

type ResourceHook[T any] func(context.Context, Action, *T) error

type Route

type Route struct {
	Method      string
	Path        string
	OperationID string
	Tags        []string
}

type Router

type Router struct {
	// contains filtered or unexported fields
}

func (*Router) Huma

func (router *Router) Huma() huma.API

func (*Router) Prefix

func (router *Router) Prefix() string

func (*Router) Resource

func (router *Router) Resource(path string, resource Registrar) error

type StatusError

type StatusError struct {
	ErrorEnvelope
	// contains filtered or unexported fields
}

func (*StatusError) ContentType

func (response *StatusError) ContentType(contentType string) string

func (*StatusError) Error

func (response *StatusError) Error() string

func (*StatusError) GetStatus

func (response *StatusError) GetStatus() int

Jump to

Keyboard shortcuts

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