respond

package
v0.37.0 Latest Latest
Warning

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

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

Documentation

Overview

Package respond provides HTTP response helpers for HTMX-first applications.

It renders templ components or JSON payloads via Echo. Use respond.HTML for templ components and respond.JSON for API responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTML

func HTML(c echo.Context, status int, component templ.Component) error

HTML renders a templ component with the given status code.

The component is rendered into a buffer first: if rendering fails, no status or body has been committed, so the returned error reaches Echo's error handler and a proper error page can be sent. (Writing directly to the response would leave a committed 2xx with a truncated body.)

func JSON

func JSON(c echo.Context, status int, data any) error

JSON sends a JSON response with the given status code.

func ParsePagination

func ParsePagination(c echo.Context, defaultSize int) (page, size int)

ParsePagination reads page and size query params from the request. It defaults page to 1 and clamps size to [1, 100].

func Redirect

func Redirect(c echo.Context, url string) error

Redirect sends an HTMX-aware redirect. For HTMX requests it sets the HX-Redirect header and returns 200; for regular requests it returns a 303.

Types

type Page

type Page struct {
	Number     int  `json:"number"`
	Size       int  `json:"size"`
	Total      int  `json:"total"`
	TotalPages int  `json:"total_pages"`
	HasNext    bool `json:"has_next"`
	HasPrev    bool `json:"has_prev"`
}

Page holds pagination metadata.

func NewPage

func NewPage(page, size, total int) Page

NewPage computes pagination metadata from the given parameters.

type PagedResponse

type PagedResponse[T any] struct {
	Data []T  `json:"data"`
	Page Page `json:"page"`
}

PagedResponse wraps a slice of items with pagination metadata.

Jump to

Keyboard shortcuts

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