Documentation
¶
Overview ¶
Package inertiaframe provides a high-level, message-oriented API for building Inertia.js applications.
It abstracts Inertia.js protocol details, providing automatic request parsing, validation, session management, and response handling. Build type-safe endpoints with minimal boilerplate.
Index ¶
- Constants
- Variables
- func DefaultValidationErrorHandler(w http.ResponseWriter, r *http.Request, errorer inertia.ValidationErrorer)
- func Mount[M any](mux Mux, endpoint Endpoint[M], opts *MountOpts[M])
- func RedirectBack(w http.ResponseWriter, r *http.Request)
- func WithProps(r *http.Request, props inertia.Proper) *http.Request
- type Endpoint
- type Handler
- type HandlerFunc
- type Meta
- type Middleware
- type MiddlewareFunc
- type MountOpts
- type Mux
- type RawRequestExtractor
- type RawResponseWriter
- type Request
- type Response
- func NewExternalRedirectResponse(url string) Response
- func NewRawResponse(h Handler) Response
- func NewRedirectBackResponse() Response
- func NewRedirectResponse(url string) Response
- func NewResponse(component string, proper inertia.Proper, opts ...ResponseOption) Response
- func NewStructResponse(component string, m any, opts ...ResponseOption) (Response, error)
- type ResponseOption
- type ResponseOptioner
- type ResponseOptions
- type Validator
- type ValidatorFunc
Constants ¶
const ( SessionCookieName = "_inertiaframe" SessionPath = "/" )
Variables ¶
var DefaultErrorHandler httphandler.ErrorHandler = httphandler.ErrorHandlerFunc( func(w http.ResponseWriter, r *http.Request, err error) { var errorer inertia.ValidationErrorer if errors.As(err, &errorer) { DefaultValidationErrorHandler(w, r, errorer) return } httphandler.DefaultErrorHandler(w, r, err) }, )
var DefaultFormDecoder = form.NewDecoder() //nolint:gochecknoglobals
var ErrEmptyResponse = errors.New("inertiaframe: empty response")
Functions ¶
func DefaultValidationErrorHandler ¶
func DefaultValidationErrorHandler(w http.ResponseWriter, r *http.Request, errorer inertia.ValidationErrorer)
DefaultValidationErrorHandler handles validation errors by storing them in the session and redirecting back to the previous page where they can be displayed.
func Mount ¶
Mount registers an Endpoint on a Mux, creating an HTTP handler that:
- Automatically parses JSON and form data into the message type M
- Validates requests using the configured Validator
- Executes the endpoint and renders the Response
The endpoint's Meta() defines the HTTP method and path pattern.
func RedirectBack ¶
func RedirectBack(w http.ResponseWriter, r *http.Request)
RedirectBack redirects to the previous page using the Referer header. Falls back to the session-stored path if the header is missing, or "/" if no session.
func WithProps ¶
WithProps attaches shared props to the request context for later merging with response props. Useful in middleware to provide global data (e.g., auth user, flash messages) to all pages.
Response props take precedence over shared props when keys overlap. Prefer setting props directly in responses when possible; use this for cross-cutting concerns.
Types ¶
type Endpoint ¶
type Endpoint[M any] interface { // Execute processes the validated request and returns a Response. // Errors are automatically handled based on type (e.g., ValidationErrorer). Execute(context.Context, *Request[M]) (Response, error) // Meta returns routing metadata (HTTP method and path pattern). Meta() Meta }
Endpoint represents a type-safe, message-oriented HTTP handler.
type Handler ¶
type Handler = httphandler.Handler
type HandlerFunc ¶
type HandlerFunc = httphandler.HandlerFunc
type Meta ¶
type Meta struct {
// Method is the HTTP method (GET, POST, PUT, DELETE, etc.).
Method string
// Path is the URL pattern, following http.ServeMux syntax (e.g., "/users/{id}").
Path string
}
Meta contains endpoint routing metadata used during mounting.
type Middleware ¶
type Middleware = httpmiddleware.Middleware
type MiddlewareFunc ¶
type MiddlewareFunc = httpmiddleware.MiddlewareFunc
type MountOpts ¶
type MountOpts[M any] struct { // Validator validates requests before execution. If nil, no validation is performed. Validator Validator[M] // FormDecoder parses form-urlencoded and multipart requests. // Defaults to DefaultFormDecoder if nil. FormDecoder *form.Decoder // ErrorHandler handles execution errors. Defaults to DefaultErrorHandler if nil. ErrorHandler httphandler.ErrorHandler // JSONUnmarshalOptions customizes JSON parsing (e.g., for protobuf). JSONUnmarshalOptions []json.Options }
MountOpts configures endpoint mounting behavior.
type Mux ¶
type Mux interface {
// Handle registers a handler for a pattern (e.g., "POST /users/{id}").
Handle(pattern string, h http.Handler)
}
Mux represents an HTTP router compatible with http.ServeMux.
type RawRequestExtractor ¶
type RawRequestExtractor interface {
// Extract parses and populates fields from the raw HTTP request.
Extract(*http.Request) error
}
RawRequestExtractor allows custom request parsing logic. When a request message implements this interface, it bypasses the default JSON/form decoder and calls Extract instead.
type RawResponseWriter ¶
type RawResponseWriter interface {
Write(http.ResponseWriter, *http.Request) error
}
RawResponseWriter allows custom response writing logic. When a Response implements this interface, it bypasses normal Inertia rendering and calls Write directly. Useful for non-Inertia responses (downloads, APIs, etc.).
type Request ¶
type Request[M any] struct { // Message is the decoded request payload (from JSON or form data). // If M implements RawRequestExtractor, custom extraction logic is used. Message M }
Request represents a parsed and validated client request.
type Response ¶
type Response interface {
// Component returns the frontend component name to render.
// Must be non-empty unless the response implements RawResponseWriter.
Component() string
// Proper returns the props to send to the component.
// Can be nil for redirect responses or when no props are needed.
Proper() inertia.Proper
}
Response represents an endpoint's response, instructing the client to render a component or redirect.
If a Response implements RawResponseWriter, it bypasses normal Inertia rendering and writes directly to http.ResponseWriter (useful for downloads, APIs, etc.).
func NewExternalRedirectResponse ¶
NewExternalRedirectResponse creates a Response that redirects to an external URL (outside the Inertia app). Uses the Location protocol for proper client handling.
func NewRawResponse ¶
NewRawResponse creates a Response that bypasses Inertia rendering. The provided handler has full control over the HTTP response. Useful for file downloads, API endpoints, or custom authentication flows.
func NewRedirectBackResponse ¶
func NewRedirectBackResponse() Response
NewRedirectBackResponse creates a Response that redirects to the previous page. Uses the Referer header or session-stored path.
func NewRedirectResponse ¶
NewRedirectResponse creates a Response that redirects to the specified URL within the Inertia app.
func NewResponse ¶
func NewResponse(component string, proper inertia.Proper, opts ...ResponseOption) Response
NewResponse creates a Response with the specified component and props. Optional ResponseOption functions can customize history and concurrency behavior.
func NewStructResponse ¶
func NewStructResponse(component string, m any, opts ...ResponseOption) (Response, error)
NewStructResponse creates a Response by parsing inertia struct tags on m. See inertia.ParseStruct for tag documentation. Returns an error if the struct tags are invalid.
type ResponseOption ¶
type ResponseOption func(*ResponseOptions)
ResponseOption is used to configure inertia response.
type ResponseOptioner ¶
type ResponseOptioner interface {
Options() ResponseOptions
}
ResponseOptioner is an optional interface for Responses that need custom options (history management, concurrency). If implemented, Options() is called to configure the response.
type ResponseOptions ¶
type ResponseOptions struct {
// ClearHistory instructs the client to clear its history stack.
ClearHistory bool
// EncryptHistory instructs the client to encrypt the history state.
EncryptHistory bool
// Concurrency sets the maximum concurrent lazy prop resolutions for this response.
Concurrency int
}
ResponseOptions configures Inertia response behavior for a specific page.
type Validator ¶
type Validator[M any] interface { // Validate checks the request message for errors. // If validation fails, return a ValidationErrorer to send errors to the client, // or any other error to trigger error handling. Validate(M) error }
Validator validates parsed request messages before execution.
type ValidatorFunc ¶
ValidatorFunc is a function that implements the Validator interface.
func (ValidatorFunc[M]) Validate ¶
func (f ValidatorFunc[M]) Validate(v M) error