Documentation
¶
Overview ¶
inertiaframe implements an opinionated framework around Go's HTTP and Inertia library, abstracting out protocol-level details and providing a simple message-based API.
Index ¶
- Constants
- Variables
- func DefaultTranslator(_ context.Context) ut.Translator
- func DefaultValidationErrorHandler(w http.ResponseWriter, r *http.Request, errorer inertia.ValidationErrorer)
- func Mount[M any](mux Mux, e Endpoint[M], opts *MountOpts)
- func RedirectBack(w http.ResponseWriter, r *http.Request)
- func WithProps(r *http.Request, props inertia.Proper) *http.Request
- type Endpoint
- type InertiaError
- type Message
- type Meta
- type MountOpts
- type Mux
- type RawRequestExtractor
- type RawResponseWriter
- type Request
- type Response
- type ResponseConfig
Constants ¶
const ( SessionCookieName = "_inertiaframe" SessionPath = "/" )
Variables ¶
var ( DefaultValidator = validator.New(validator.WithRequiredStructEnabled()) //nolint:gochecknoglobals DefaultFormDecoder = form.NewDecoder() //nolint:gochecknoglobals )
var DefaultErrorHandler httperror.ErrorHandler = httperror.ErrorHandlerFunc( func(w http.ResponseWriter, r *http.Request, err error) { var errorer inertia.ValidationErrorer if errors.As(err, &errorer) { DefaultValidationErrorHandler(w, r, errorer) return } errorer, ok := inertiavalidationerrors.FromValidationErrors(err, defaultTranslator) if ok { DefaultValidationErrorHandler(w, r, errorer) return } httperror.DefaultErrorHandler(w, r, err) }, )
Functions ¶
func DefaultTranslator ¶
func DefaultTranslator(_ context.Context) ut.Translator
DefaultTranslator returns the default translator that always uses the default locale - English (en).
func DefaultValidationErrorHandler ¶
func DefaultValidationErrorHandler(w http.ResponseWriter, r *http.Request, errorer inertia.ValidationErrorer)
DefaultValidationErrorHandler is a default error handler for validation errors.
It saves flash messages and redirects back to the previous page.
func Mount ¶
Mount mounts the executor on the given mux.
Endpoint must specify the HTTP method and path via Endpoint.Meta(). The mounted endpoint automatically handles requests with JSON and form data.
The message M is validated using the validator specified in the MountOpts. Validation errors are automatically handled and passed to the client according to Inertia protocol.
func RedirectBack ¶
func RedirectBack(w http.ResponseWriter, r *http.Request)
RedirectBack redirects the user back to the previous page.
The previous page is determined from the Referer header and falls back to the session if the header is not present.
func WithProps ¶
WithProps sets the props on the request context and returns the updated request.
WithProps can be used to gather props in multiple places, e.g., in middleware.
Any overlapping props between the shared context and the response props will be replaced with the response props.
Prefer to use the response props directly instead of using this function, and opt in only when necessary.
Types ¶
type Endpoint ¶
type Endpoint[R any] interface { // Execute executes the endpoint for the given request. // // If the returned error can automatically be converted to an Inertia // error, it will be converted and passed down to the client. Execute(context.Context, *Request[R]) (*Response, error) // Meta returns the metadata of the endpoint. It is used to configure // the endpoint's behavior when mounted on a given http.ServeMux. Meta() *Meta }
type InertiaError ¶
type InertiaError struct {
Cause error `json:"-"`
}
func (*InertiaError) Error ¶
func (e *InertiaError) Error() string
func (*InertiaError) Unwrap ¶
func (e *InertiaError) Unwrap() error
type Message ¶
type Message interface {
// Component returns the component name to be rendered.
//
// Executor panics if Component returns an empty string,
// unless the message implements RawResponseWriter.
//
// If the message is implementing RawResponseWriter, the default
// behavior is prevented and the writer is used instead to
// write the response data.
Component() string
}
Message is used to send a message to the client. It can be used to guide the client to render a component or redirect to a specific URL.
If the Message implements a RawResponseWriter, the default behavior is prevented and the writer is used instead to write the response data.
The Component() method must return a non-empty string.
type Meta ¶
type Meta struct {
// HTTP method of the endpoint.
Method string
// HTTP path of the endpoint. It supports the same path pattern as
// the http.ServeMux.
Path string
}
Meta is the metadata of an endpoint.
type MountOpts ¶
type MountOpts struct {
// Middleware is the middleware used to handle requests.
// If Middleware is nil, no middleware will be used.
Middleware httpmiddleware.Middleware
// Validator is the validator used to validate the request data.
// If Validator is nil, the DefaultValidator will be used.
Validator *validator.Validate
// FormDecoder is the decoder used to parse incoming request data
// when the request type is application/x-www-form-urlencoded or
// multipart/form-data.
// If FormDecoder is nil, the DefaultFormDecoder will be used.
FormDecoder *form.Decoder
// ErrorHandler is the error handler used to handle errors.
// If ErrorHandler is nil, the DefaultErrorHandler will be used.
ErrorHandler httperror.ErrorHandler
}
type Mux ¶
type Mux interface {
// Handle handles the given HTTP request at the specified path.
//
// The pattern is a string following the http.ServeMux format:
// "<http-method> <path>".
Handle(pattern string, h http.Handler)
}
Mux is a universal interface for routing HTTP requests.
type RawRequestExtractor ¶
type RawRequestExtractor interface {
// Extract extracts data from the raw http.Request.
Extract(*http.Request) error
}
RawRequestExtractor allows to extract data from the raw http.Request. If a request message implements RawRequestExtractor, the default behavior is prevented and the extractor is used instead to extract the request data.
type RawResponseWriter ¶
type RawResponseWriter interface {
Write(http.ResponseWriter, *http.Request) error
}
RawResponseWriter allows to write data to the http.ResponseWriter. If a response message implements RawResponseWriter, the default behavior is prevented and the writer is used instead to write the response data.
type Request ¶
type Request[M any] struct { // Message is a decoded message sent by a client. // // Message can implement RawRequestExtractor to intercept request data extraction. Message *M }
Request is a request sent by a client.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is a response sent by a server to a client.
Use NewResponse to create a new response.
func NewExternalRedirectResponse ¶
NewExternalRedirectResponse creates a new response that redirects the client to an external URL.
External URL is any URL that is not powered by Inertia.js.
func NewRedirectBackResponse ¶
func NewRedirectBackResponse() *Response
NewRedirectBackResponse creates a new response that redirects the client back to the previous page.
func NewRedirectResponse ¶
NewRedirectResponse creates a new response that redirects the client to the specified URL.
func NewResponse ¶
func NewResponse(msg Message, config *ResponseConfig) *Response
NewResponse creates a new inertia response.
The msg can be a struct with props tagged with `inertia:"key"`, a set of props, or a struct implementing RawResponseWriter for custom response handling.
An optional config can be passed to customize the response behavior. If config is nil, default values will be used.
type ResponseConfig ¶
type ResponseConfig struct {
// ClearHistory determines whether the history should be cleared by
// the client.
ClearHistory bool
// EncryptHistory determines whether the history should be encrypted by
// the client.
EncryptHistory bool
// Concurrency determines the maximum number of concurrent resolutions of lazy
// props that can be made during response resolution.
Concurrency int
}
ResponseConfig is a configuration for inertia response.