FabienHenon / jsonapi / JsonApi.Encode.Document

Provides a type alias and functions to create a new Json API document to be encoded via JsonApi.Encode.document.

Type


type alias Document =
JsonApi.Internal.Document.DocumentEncode

The Document represents the global Json API document. This version of the Document is to be encoded via JsonApi.Encode.document. Thus it has functions to create a new Document.

Example of json api document:

{
    "data": [
        // resources ...
    ],
    "meta": {
        "redirect": true
    },
    "jsonapi": {
        "version": "1.0"
    }
}

And how to build it with the JsonApi.Encode.Document module:

build
    |> withJsonApiVersion "1.0"
    |> withMeta (object [ ( "redirect", bool True ) ])
    |> withResources resources

New document

build : Document

Creates a new Json API Document with no meta, no resource and a default Json API version set to 1.0

myDocument : Document
myDocument =
    build

Getter functions

jsonApiVersion : Document -> String

Retrieves the Json API version.

From the json document above, jsonApiVersion will return "1.0"

meta : Document -> Maybe Json.Encode.Value

Retrieves the meta object of the Document if it exists.

resource : Document -> Maybe JsonApi.Resource.Resource

Retrieves the resource (in the data object) of the Document. If there is no resource Nothing is returned. If there are several resources, the first resource is returned.

resources : Document -> Maybe (List JsonApi.Resource.Resource)

Retrieves the resources (in the data array) of the Document. If there is no resource set Nothing is returned. If resources have been set but the list is empty, an empty list is also returned. If there is only a resouce, a list with this resource is returned.

links : Document -> Dict String String

Retrieves the links object of the Document.

Setter functions

withJsonApiVersion : String -> Document -> Document

Sets the Json API version of the document

myDocument : Document
myDocument =
    build
        |> withJsonApiVersion "1.0"

_The default version is 1.0

withMeta : Json.Encode.Value -> Document -> Document

Sets the meta value of the Document

myDocument : Document
myDocument =
    build
        |> withMeta (object [ ( "redirect", bool True ) ])

withResource : JsonApi.Resource.Resource -> Document -> Document

Sets the data value of the Json API Document to one resource.

myResource : Resource
myResource =
    Resource.build "posts"
        |> Resource.withId "post-1"

myDocument : Document
myDocument =
    build
        |> withResource myResource

withResources : List JsonApi.Resource.Resource -> Document -> Document

Sets the data value of the Json API Document to a list of resources.

myResources : List Resource
myResources =
    [ Resource.build "posts"
        |> Resource.withId "post-1"
    , Resource.build "posts"
        |> Resource.withId "post-2"
    ]

myDocument : Document
myDocument =
    build
        |> withResources myResources

withLinks : Dict String String -> Document -> Document

Sets the links value of the Document

myDocument : Document
myDocument =
    build
        |> withLinks (Dict.fromList [ ( "self", "http://self" ) ])