Middleware

middleware/paginate

github.com/gofiber/fiber/v3/middleware/paginate

import "github.com/gofiber/fiber/v3/middleware/paginate"

Constants

DefaultMaxLimit is the default maximum limit allowed.

Source: middleware/paginate/paginate.go:21

const DefaultMaxLimit = 100

Source: middleware/paginate/page_info.go:22

const (
	ASC  SortOrder = "asc"
	DESC SortOrder = "desc"
)

Variables

ConfigDefault is the default config.

Source: middleware/paginate/config.go:71

var ConfigDefault = Config{
	Next:         nil,
	PageKey:      "page",
	DefaultPage:  1,
	LimitKey:     "limit",
	DefaultLimit: 10,
	MaxLimit:     DefaultMaxLimit,
	DefaultSort:  "id",
	OffsetKey:    "offset",
	CursorKey:    "cursor",
}

ErrCursorEncode is returned when cursor values cannot be encoded.

Source: middleware/paginate/page_info.go:16

var ErrCursorEncode = errors.New("paginate: failed to encode cursor values")

Functions

func FromContext(ctx any) (*PageInfo, bool)

Source: middleware/paginate/paginate.go:97

FromContext returns the PageInfo from the request context. It accepts fiber.CustomCtx, fiber.Ctx, *fasthttp.RequestCtx, and context.Context. Returns nil and false if no PageInfo is stored.

func New(config ...Config) fiber.Handler

Source: middleware/paginate/paginate.go:27

New creates a new pagination middleware handler.

func NewPageInfo(page, limit, offset int, sort []SortField) *PageInfo

Source: middleware/paginate/page_info.go:55

NewPageInfo creates a new PageInfo.

func SortOrderFromString(s string) SortOrder

Source: middleware/paginate/page_info.go:33

SortOrderFromString returns a SortOrder from a string (case-insensitive).

Types

type Config

Source: middleware/paginate/config.go:10

Config defines the config for the pagination middleware.

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	Next func(c fiber.Ctx) bool

	// PageKey is the query string key for page number.
	//
	// Optional. Default: "page"
	PageKey string

	// LimitKey is the query string key for limit.
	//
	// Optional. Default: "limit"
	LimitKey string

	// SortKey is the query string key for sort.
	//
	// Optional. Default: ""
	SortKey string

	// DefaultSort is the default sort field.
	//
	// Optional. Default: "id"
	DefaultSort string

	// CursorKey is the query string key for cursor-based pagination.
	//
	// Optional. Default: "cursor"
	CursorKey string

	// OffsetKey is the query string key for offset.
	//
	// Optional. Default: "offset"
	OffsetKey string

	// CursorParam is an optional alias for the cursor query key.
	//
	// Optional. Default: ""
	CursorParam string

	// AllowedSorts is the list of allowed sort fields.
	//
	// Optional. Default: nil
	AllowedSorts []string

	// DefaultPage is the default page number.
	//
	// Optional. Default: 1
	DefaultPage int

	// DefaultLimit is the default items per page.
	//
	// Optional. Default: 10
	DefaultLimit int

	// MaxLimit is the maximum items per page.
	//
	// Optional. Default: 100
	MaxLimit int
}
Fields
  • Next func(c fiber.Ctx) bool

    Next defines a function to skip this middleware when returned true.

  • PageKey string

    PageKey is the query string key for page number.

    Optional. Default: "page"

  • LimitKey string

    LimitKey is the query string key for limit.

    Optional. Default: "limit"

  • SortKey string

    SortKey is the query string key for sort.

    Optional. Default: ""

  • DefaultSort string

    DefaultSort is the default sort field.

    Optional. Default: "id"

  • CursorKey string

    CursorKey is the query string key for cursor-based pagination.

    Optional. Default: "cursor"

  • OffsetKey string

    OffsetKey is the query string key for offset.

    Optional. Default: "offset"

  • CursorParam string

    CursorParam is an optional alias for the cursor query key.

    Optional. Default: ""

  • AllowedSorts []string

    AllowedSorts is the list of allowed sort fields.

    Optional. Default: nil

  • DefaultPage int

    DefaultPage is the default page number.

    Optional. Default: 1

  • DefaultLimit int

    DefaultLimit is the default items per page.

    Optional. Default: 10

  • MaxLimit int

    MaxLimit is the maximum items per page.

    Optional. Default: 100

type PageInfo

Source: middleware/paginate/page_info.go:41

PageInfo contains pagination information.

type PageInfo struct {
	Cursor     string      `json:"cursor,omitempty"`
	NextCursor string      `json:"next_cursor,omitempty"`
	Sort       []SortField `json:"sort"`
	Page       int         `json:"page"`
	Limit      int         `json:"limit"`
	Offset     int         `json:"offset"`
	HasMore    bool        `json:"has_more,omitempty"`
	// contains filtered or unexported fields
}
Fields
  • Cursor string `json:"cursor,omitempty"`
  • NextCursor string `json:"next_cursor,omitempty"`
  • Sort []SortField `json:"sort"`
  • Page int `json:"page"`
  • Limit int `json:"limit"`
  • Offset int `json:"offset"`
  • HasMore bool `json:"has_more,omitempty"`

func (p *PageInfo) CursorValues() map[string]any

Source: middleware/paginate/page_info.go:176

CursorValues returns the decoded cursor key-value map. If the cursor was parsed by the middleware, the pre-parsed data is returned. Otherwise it decodes the opaque cursor string. Returns nil if cursor is empty or invalid.

func (p *PageInfo) NextCursorURL(baseURL string) string

Source: middleware/paginate/page_info.go:127

NextCursorURL returns the URL for the next cursor page. Returns empty string if HasMore is false.

func (p *PageInfo) NextCursorURLWithKeys(baseURL, cursorKey, limitKey string) string

Source: middleware/paginate/page_info.go:118

NextCursorURLWithKeys returns the URL for the next cursor page using custom query keys. Returns empty string if HasMore is false.

func (p *PageInfo) NextPageURL(baseURL string) string

Source: middleware/paginate/page_info.go:97

NextPageURL returns the URL for the next page.

func (p *PageInfo) NextPageURLWithKeys(baseURL, pageKey, limitKey string) string

Source: middleware/paginate/page_info.go:88

NextPageURLWithKeys returns the URL for the next page using custom query keys.

func (p *PageInfo) PreviousPageURL(baseURL string) string

Source: middleware/paginate/page_info.go:112

PreviousPageURL returns the URL for the previous page. Returns empty string if on page 1.

func (p *PageInfo) PreviousPageURLWithKeys(baseURL, pageKey, limitKey string) string

Source: middleware/paginate/page_info.go:103

PreviousPageURLWithKeys returns the URL for the previous page using custom query keys. Returns empty string if on page 1.

func (p *PageInfo) SetNextCursor(values map[string]any) error

Source: middleware/paginate/page_info.go:210

SetNextCursor encodes a key-value map into an opaque cursor token and sets both NextCursor and HasMore on the PageInfo.

func (p *PageInfo) SortBy(field string, order SortOrder) *PageInfo

Source: middleware/paginate/page_info.go:82

SortBy adds a sort field. Chainable.

func (p *PageInfo) Start() int

Source: middleware/paginate/page_info.go:65

Start returns the start index based on page/limit or offset.

type SortField

Source: middleware/paginate/page_info.go:27

SortField represents a sort field with direction.

type SortField struct {
	Field string    `json:"field"`
	Order SortOrder `json:"order"`
}
Fields
  • Field string `json:"field"`
  • Order SortOrder `json:"order"`

type SortOrder

Source: middleware/paginate/page_info.go:19

SortOrder represents sort order.

type SortOrder string