{"modulePath":"github.com/Beyond-Technology/cozy-stack","version":"v0.0.0-20211207222605-4b98c26c30b6","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package jsonapi\n\nPackage jsonapi is for using the JSON-API format: parsing, serialization, checking the content-type, etc.\n\n## Constants\n\n```go\nconst ContentType = \"application/vnd.api+json\"\n```\nContentType is the official mime-type for JSON-API\n\n## Functions\n\n```go\nfunc BindRelations(req *http.Request) ([]couchdb.DocReference, error)\n```\nBindRelations extracts a Relationships request ( a list of ResourceIdentifier)\n\n```go\nfunc Data(c echo.Context, statusCode int, o Object, links *LinksList) error\n```\nData can be called to send an answer with a JSON-API document containing a single object as data\n\n```go\nfunc DataError(c echo.Context, err *Error) error\n```\nDataError can be called to send an error answer with a JSON-API document containing a single value error.\n\n```go\nfunc DataErrorList(c echo.Context, errs ...*Error) error\n```\nDataErrorList can be called to send an error answer with a JSON-API document containing multiple errors.\n\n```go\nfunc DataList(c echo.Context, statusCode int, objs []Object, links *LinksList) error\n```\nDataList can be called to send an multiple-value answer with a JSON-API document contains multiple objects.\n\n```go\nfunc DataListWithTotal(c echo.Context, statusCode, total int, objs []Object, links *LinksList, executionStats *couchdb.ExecutionStats) error\n```\nDataListWithTotal can be called to send a list of Object with a different meta:count, useful to indicate total number of results with pagination.\n\n```go\nfunc DataRelations(c echo.Context, statusCode int, refs []couchdb.DocReference, meta *Meta, links *LinksList, included []Object) error\n```\nDataRelations can be called to send a Relations page, a list of ResourceIdentifier\n\n```go\nfunc ExtractPaginationCursor(c echo.Context, defaultLimit, maxLimit int) (couchdb.Cursor, error)\n```\nExtractPaginationCursor creates a Cursor from context Query.\n\n```go\nfunc MarshalObject(o Object) (json.RawMessage, error)\n```\nMarshalObject serializes an Object to JSON. It returns a json.RawMessage that can be used a in Document.\n\n```go\nfunc PaginationCursorToParams(cursor couchdb.Cursor) (url.Values, error)\n```\nPaginationCursorToParams transforms a Cursor into url.Values the url.Values contains only keys page\\[limit] \u0026 page\\[cursor] if the cursor is Done, the values will be empty.\n\n```go\nfunc WriteData(w io.Writer, o Object, links *LinksList) error\n```\nWriteData can be called to write an answer with a JSON-API document containing a single object as data into an io.Writer.\n\n## Types\n\n```go\ntype Document struct {\n\tData     *json.RawMessage `json:\"data,omitempty\"`\n\tErrors   ErrorList        `json:\"errors,omitempty\"`\n\tLinks    *LinksList       `json:\"links,omitempty\"`\n\tMeta     *Meta            `json:\"meta,omitempty\"`\n\tIncluded []interface{}    `json:\"included,omitempty\"`\n}\n```\nDocument is JSON-API document, identified by the mediatype application/vnd.api+json See [http://jsonapi.org/format/#document-structure](http://jsonapi.org/format/#document-structure)\n\n```go\ntype Error struct {\n\tStatus int         `json:\"status,string\"`\n\tTitle  string      `json:\"title\"`\n\tCode   string      `json:\"code,omitempty\"`\n\tDetail string      `json:\"detail,omitempty\"`\n\tSource SourceError `json:\"source,omitempty\"`\n\tLinks  *LinksList  `json:\"links,omitempty\"`\n}\n```\nError objects provide additional information about problems encountered while performing an operation. See [http://jsonapi.org/format/#error-objects](http://jsonapi.org/format/#error-objects)\n\n```go\nfunc BadGateway(err error) *Error\n```\nBadGateway returns a 502 formatted error\n\n```go\nfunc BadJSON() *Error\n```\nBadJSON returns a 400 formatted error meaning the json input is malformed.\n\n```go\nfunc BadRequest(err error) *Error\n```\nBadRequest returns a 400 formatted error\n\n```go\nfunc Conflict(err error) *Error\n```\nConflict returns a 409 formatted error representing a conflict\n\n```go\nfunc Errorf(status int, format string, args ...interface{}) *Error\n```\nErrorf creates a new generic Error with detail build as Sprintf\n\n```go\nfunc Forbidden(err error) *Error\n```\nForbidden returns a 403 Forbidden error formatted when an action is fobidden.\n\n```go\nfunc InternalServerError(err error) *Error\n```\nInternalServerError returns a 500 formatted error\n\n```go\nfunc InvalidAttribute(attribute string, err error) *Error\n```\nInvalidAttribute returns a 422 formatted error when an attribute is invalid\n\n```go\nfunc InvalidParameter(parameter string, err error) *Error\n```\nInvalidParameter returns a 422 formatted error when an HTTP or Query-String parameter is invalid\n\n```go\nfunc MethodNotAllowed(method string) *Error\n```\nMethodNotAllowed returns a 405 formatted error\n\n```go\nfunc NewError(status int, detail string) *Error\n```\nNewError creates a new generic Error\n\n```go\nfunc NotFound(err error) *Error\n```\nNotFound returns a 404 formatted error\n\n```go\nfunc PreconditionFailed(parameter string, err error) *Error\n```\nPreconditionFailed returns a 412 formatted error when an expectation from an HTTP header is not matched\n\n```go\nfunc (e *Error) Error() string\n```\n\n```go\ntype ErrorList []*Error\n```\nErrorList is just an array of error objects\n\n```go\ntype LinksList struct {\n\tSelf    string `json:\"self,omitempty\"`\n\tRelated string `json:\"related,omitempty\"`\n\tPrev    string `json:\"prev,omitempty\"`\n\tNext    string `json:\"next,omitempty\"`\n\tIcon    string `json:\"icon,omitempty\"`\n\tPerms   string `json:\"permissions,omitempty\"`\n\tWebhook string `json:\"webhook,omitempty\"`\n\t// Thumbnails\n\tSmall  string `json:\"small,omitempty\"`\n\tMedium string `json:\"medium,omitempty\"`\n\tLarge  string `json:\"large,omitempty\"`\n\t// Preview for PDF\n\tPreview string `json:\"preview,omitempty\"`\n}\n```\nLinksList is the common links used in JSON-API for the top-level or a resource object See [http://jsonapi.org/format/#document-links](http://jsonapi.org/format/#document-links)\n\n```go\ntype Meta struct {\n\tRev            string                  `json:\"rev,omitempty\"`\n\tCount          *int                    `json:\"count,omitempty\"`\n\tExecutionStats *couchdb.ExecutionStats `json:\"execution_stats,omitempty\"`\n}\n```\nMeta is a container for the couchdb revision and the total number of items, in JSON-API land\n\n```go\ntype Object interface {\n\tcouchdb.Doc\n\tLinks() *LinksList\n\tRelationships() RelationshipMap\n\tIncluded() []Object\n}\n```\nObject is an interface to serialize something to a JSON-API Object\n\n```go\ntype ObjectMarshalling struct {\n\tType          string           `json:\"type\"`\n\tID            string           `json:\"id\"`\n\tAttributes    *json.RawMessage `json:\"attributes\"`\n\tMeta          Meta             `json:\"meta\"`\n\tLinks         *LinksList       `json:\"links,omitempty\"`\n\tRelationships RelationshipMap  `json:\"relationships,omitempty\"`\n}\n```\nObjectMarshalling is a JSON-API object See [http://jsonapi.org/format/#document-resource-objects](http://jsonapi.org/format/#document-resource-objects)\n\n```go\nfunc Bind(body io.Reader, attrs interface{}) (*ObjectMarshalling, error)\n```\nBind is used to unmarshal an input JSONApi document. It binds an incoming request to a attribute type.\n\n```go\nfunc BindCompound(body io.Reader) ([]*ObjectMarshalling, error)\n```\nBindCompound is used to unmarshal an compound input JSONApi document.\n\n```go\nfunc (o *ObjectMarshalling) GetRelationship(name string) (*Relationship, bool)\n```\nGetRelationship returns the relationship with the given name from the relationships map.\n\n```go\ntype Relationship struct {\n\tLinks *LinksList  `json:\"links,omitempty\"`\n\tMeta  *Meta       `json:\"meta,omitempty\"`\n\tData  interface{} `json:\"data\"`\n}\n```\nRelationship is a resource linkage, as described in JSON-API See [http://jsonapi.org/format/#document-resource-object-relationships](http://jsonapi.org/format/#document-resource-object-relationships)\n\nData can be a single ResourceIdentifier for to-one relationships, or an array of them for to-many relationships.\n\n```go\nfunc (r *Relationship) ResourceIdentifier() (*couchdb.DocReference, bool)\n```\nResourceIdentifier returns the resource identifier of the relationship.\n\n```go\ntype RelationshipMap map[string]Relationship\n```\nRelationshipMap is a map of relationships See [http://jsonapi.org/format/#document-resource-object-relationships](http://jsonapi.org/format/#document-resource-object-relationships)\n\n```go\ntype SourceError struct {\n\tPointer   string `json:\"pointer,omitempty\"`\n\tParameter string `json:\"parameter,omitempty\"`\n}\n```\nSourceError contains references to the source of the error\n\n","path":"github.com/Beyond-Technology/cozy-stack/pkg/jsonapi","name":"jsonapi","synopsis":"Package jsonapi is for using the JSON-API format: parsing, serialization, checking the content-type, etc.","isRedistributable":true}
