vt-elixir / ja_serializer

JSONAPI.org Serialization in Elixir.
Other
640 stars 148 forks source link

Links at top level of resource collection #222

Open lleger opened 7 years ago

lleger commented 7 years ago

I see there's an easy way to add links to an object via the location DSL or the links callback, but this only adds links to individual resources. Is there a way to add links to the top-level of a collection? For example, how can I do this:

{
  "links": {
    "self": "http://example.com/articles"
  },
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    }
  }, {
    "type": "articles",
    "id": "2",
    "attributes": {
      "title": "Rails is Omakase"
    }
  }]
}
alanpeabody commented 7 years ago

It looks like we either never had support for this or lost it at some point. I will get this in the next release.

beerlington commented 6 years ago

I'm pretty sure this never existed but here are a couple ideas:

For non-DSL version it'd probably be easy to do something like this:

def links(data, conn) when is_list(data) do
  # ...
end
def links(data, conn) do
  # ...
end

For DSL version we could either offer a second function:

location "/articles/:id"
collection_location "/articles"

or add a function that takes both:

locations collection: "/articles", resource: "/articles/:id"