FabienHenon/jsonapi/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

JsonApi

JsonApi exposes the ResourceInfo type and functions to get and set information for your resources.

Type

type alias ResourceInfo =
ResourceInfo

The ResourceInfo represents a resource. It is passed to your resource decoders, but you can also use it to encode resources to json api. It contains useful information for decoding and encoding your resource: resource id, links, attributes, relationships, ...

_Example of json api resource:_

{
    "data": [
        {
            "type": "users",
            "id": "13608770-76dd-47e5-a1c4-4d0d9c2483ad",
            "links": {
                "self": "http://link-to-user",
                "profile": "http://link-to-user-profile"
            },
            "attributes": {
                "firstname": "John",
                "lastname": "Doe",
                "gender": "male"
            },
            "relationships": {}
        }
    ]
}

And how to build it with the JsonApi module:

build "users"
    |> withId "13608770-76dd-47e5-a1c4-4d0d9c2483ad"
    |> withLinks
        (Dict.fromList
            [ ( "self", "http://link-to-user" )
            , ( "profile", "http://link-to-user-profile" )
            ]
        )
    |> withAttributes
        [ ( "firstname", string "John" )
        , ( "lastname", string "Doe" )
        , ( "gender", string "male" )
        ]
OneOrManyRelationships

This type is used to represent either or or many relationships in your ResourceInfo object.

See withRelationship function for more information

New resource

build : String -> ResourceInfo

Builds a new ResourceInfo with the specified type name

You can build your resources like this:

myResource : Post -> ResourceInfo
myResource post =
    build "posts"
        |> withId "post-1"
        |> withLinks (Dict.fromList [ ( "self", "http://url-to-post/1" ) ])

Getter functions

id : ResourceInfo -> String

Returns the id of your resource.

From the json example above, id will return 13608770-76dd-47e5-a1c4-4d0d9c2483ad

Setter functions

withId : String -> ResourceInfo -> ResourceInfo

Sets the id of the ResourceInfo object

myResource : Post -> ResourceInfo
myResource post =
    build "posts"
        |> withId "post-1"
withAttributes : List ( String, Value ) -> ResourceInfo -> ResourceInfo

Sets the attributes of the ResourceInfo object. This is the payload of your resource.

myResource : Post -> ResourceInfo
myResource post =
    build "posts"
        |> withAttributes
            [ ( "firstname", string "John" )
            , ( "lastname", string "Doe" )
            , ( "gender", string "male" )
            ]
withRelationship : String -> OneOrManyRelationships -> ResourceInfo -> ResourceInfo

Adds a relationship in the ResourceInfo object. You have to pass it the name of the relationship and a description of the relationship resource (See relationship and relationships)

myResource : Post -> ResourceInfo
myResource post =
    build "posts"
        |> withRelationship "creators" (relationship creator.id (creatorResource creator))

Relationships

relationship : String -> ResourceInfo -> OneOrManyRelationships

Defines a relationship that can then be added to its parent ResourceInfo. It takes the id of the resource and the resource.

creatorResource : Creator -> ResourceInfo
creatorResource creator =
    build "creator"
        |> withAttributes [ ( "firstname", string creator.firstname ) ]


myResource : Post -> ResourceInfo
myResource post =
    build "posts"
        |> withRelationship "creators" (relationship creator.id (creatorResource creator))
relationships : List ( String, ResourceInfo ) -> OneOrManyRelationships

Defines a list of relationships that can then be added to a parent ResourceInfo. It takes a List of Tuples with the id of the resource and the resource.

commentResource : Comment -> ResourceInfo
commentResource comment =
    build "comment"
        |> withAttributes [ ( "content", string comment.content ) ]


myResource : Post -> ResourceInfo
myResource post =
    build "posts"
        |> withRelationship "comments" (relationships (List.map (\comment -> ( "comment", commentResource comment )) comments))