Help us shape the future of Go! Take the 2026 Go Developer Survey

api

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package api holds REST helpers on top of web and orm: resource transformers, pagination envelopes, allowlisted query conventions (filter, sort, fields, include, pagination) and deprecation headers. See docs/api.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collection

func Collection[T, R any](c *web.Ctx, items []T, fn func(T) R) error

Collection answers 200 with {"data": [...]}.

func Cursor

func Cursor[T, R any](c *web.Ctx, p orm.CursorPage[T], fn func(T) R) error

Cursor answers 200 with a keyset page: meta.next_cursor and links.next (null on the last page).

func Deprecate

func Deprecate(since, sunset time.Time, link string) func(http.Handler) http.Handler

Deprecate marks routes as deprecated (RFC 9745 Deprecation and RFC 8594 Sunset headers) and links to the replacement or migration guide. Apply it to a version group: g.Use(api.Deprecate(since, sunset, "/docs/v2")).

func Map

func Map[T, R any](items []T, fn func(T) R) []R

Map transforms every item with fn; resources use it to turn models into their wire format: func Products(ps []models.Product) []ProductJSON { return api.Map(ps, Product) }.

func Page

func Page[T, R any](c *web.Ctx, p orm.Page[T], fn func(T) R) error

Page answers 200 with an offset page: data, meta (page, per_page, total, last_page) and links (self, next, prev).

Types

type Envelope

type Envelope[R any] struct {
	Data  []R    `json:"data"`
	Meta  *Meta  `json:"meta,omitempty"`
	Links *Links `json:"links,omitempty"`
}

Envelope is the JSON body of collections and pages.

type Links struct {
	Self string  `json:"self"`
	Next *string `json:"next"`
	Prev *string `json:"prev"`
}

Links point to neighbour pages; nil encodes as null.

type Meta

type Meta struct {
	Page       int    `json:"page,omitempty"`
	PerPage    int    `json:"per_page"`
	Total      *int64 `json:"total,omitempty"`
	LastPage   int    `json:"last_page,omitempty"`
	NextCursor string `json:"next_cursor,omitempty"`
}

Meta describes a page.

type Option

type Option func(*spec)

Option allowlists one query convention for an endpoint.

func Fields

func Fields(cols ...string) Option

Fields allows ?fields=col,col (sparse fieldsets).

func Filters

func Filters(cols ...string) Option

Filters allows ?filter[col]=value (comma-separated values mean IN).

func Includes

func Includes(rels ...string) Option

Includes allows ?include=rel,rel; handlers eager-load Request.Includes.

func PerPage

func PerPage(def, maxPerPage int) Option

PerPage sets the default and maximum ?per_page (larger values are capped).

func Sorts

func Sorts(cols ...string) Option

Sorts allows ?sort=col,-col. The last entry is the default sort; a leading "-" means descending.

type Request

type Request[T any] struct {
	Query    orm.Query[T]
	Page     int
	PerPage  int
	Cursor   string
	Includes []string
}

Request is a query refined by the allowlisted conventions.

func Query

func Query[T any](c *web.Ctx, q orm.Query[T], opts ...Option) (Request[T], error)

Query applies ?filter, ?sort, ?fields, ?include, ?page, ?per_page and ?cursor to q. Every column must be allowlisted: an unknown filter, sort, field or include is a 400 problem naming the allowed values, so request input never chooses columns freely.

func (Request[T]) Paginate

func (r Request[T]) Paginate(ctx context.Context) (orm.Page[T], error)

Paginate runs an offset page of the refined query.

Jump to

Keyboard shortcuts

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