Documentation
¶
Overview ¶
Package jsonapi encodes rest responses as JSON:API documents (application/vnd.api+json) with github.com/hashicorp/jsonapi. It is a separate package so that the library reaches only the services that serve JSON:API, not every importer of rest.
The DTO is tagged for github.com/hashicorp/jsonapi, which assembles the document (data, attributes, relationships, included) from the tags:
type WidgetResource struct {
ID string `jsonapi:"primary,widgets"`
Name string `jsonapi:"attr,name"`
}
Usage ¶
// one resource, or a collection without pagination
return jsonapi.Payload(&WidgetResource{ID: id, Name: name})
return jsonapi.Payload(resources) // []*WidgetResource
// a page of a list: the pagination goes to meta.pagination
return jsonapi.NewResult(resources, total, pg)
API ¶
- Payload: one resource or a collection, as rest.ResponseEncoder.
- Result / NewResult: a list document with meta.pagination {page, size, total_pages, total_results}; on an empty result page and size are zero.
A file that also imports github.com/hashicorp/jsonapi names one of the two packages, e.g. hjsonapi.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Payload ¶
func Payload(model any) rest.ResponseEncoder
Payload wraps model as a JSON:API rest.ResponseEncoder: model is a pointer to a tagged struct (one resource) or a slice of such pointers (a collection). A marshal error surfaces from Encode, and rest.Respond maps it to 500.
Example ¶
Payload encodes one resource, or a collection, as a JSON:API document.
package main
import (
"fmt"
"strings"
"github.com/assanoff/skit/rest/jsonapi"
)
// widgetResource is the DTO, tagged for github.com/hashicorp/jsonapi.
type widgetResource struct {
ID string `jsonapi:"primary,widgets"`
Name string `jsonapi:"attr,name"`
}
func main() {
resp := jsonapi.Payload(&widgetResource{ID: "42", Name: "gadget"})
body, contentType, _ := resp.Encode()
fmt.Println(contentType)
fmt.Println(strings.TrimSpace(string(body)))
}
Output: application/vnd.api+json {"data":{"type":"widgets","id":"42","attributes":{"name":"gadget"}}}
Types ¶
type Pagination ¶
type Pagination struct {
Page int `json:"page"`
Size int `json:"size"`
TotalPages int `json:"total_pages"`
TotalResults int `json:"total_results"`
}
Pagination is the paging metadata of a list document, under meta.pagination. Its keys follow the JSON:API convention (page/size), not rest.Pagination.
type Result ¶
Result is a paginated JSON:API list document: the items as resources and the pagination in the top-level meta. It implements rest.ResponseEncoder.
func NewResult ¶
NewResult builds a list document from the page's items, the total row count, and the page request that produced them (see rest.NewPagination).
Example ¶
NewResult puts a page of a list into a document and its pagination into meta.
package main
import (
"fmt"
"github.com/assanoff/skit/page"
"github.com/assanoff/skit/rest/jsonapi"
)
// widgetResource is the DTO, tagged for github.com/hashicorp/jsonapi.
type widgetResource struct {
ID string `jsonapi:"primary,widgets"`
Name string `jsonapi:"attr,name"`
}
func main() {
items := []*widgetResource{{ID: "3", Name: "gear"}, {ID: "4", Name: "bolt"}}
resp := jsonapi.NewResult(items, 5, page.MustParse("2", "2"))
body, _, _ := resp.Encode()
fmt.Println(string(body))
}
Output: {"data":[{"type":"widgets","id":"3","attributes":{"name":"gear"}},{"type":"widgets","id":"4","attributes":{"name":"bolt"}}],"meta":{"pagination":{"page":2,"size":2,"total_pages":3,"total_results":5}}}