Giter Site home page Giter Site logo

jsonapi's Introduction

JSONAPI Elixir

A project that will render your data models into JSONAPI Documents and parse/verify JSONAPI query strings.

Build Status

JSONAPI Support

  • Basic JSONAPI Document encoding
  • Basic support for compound documents
  • Links
  • Relationship links
  • Parsing of sort query parameter into Ecto Query order_by
  • Parsing and limiting of filter keywords.
  • Handling of sparse fieldsets
  • Handling of includes
  • Handling of pagination
  • Handling of top level meta data

How to use with Phoenix

Simply add use JSONAPI.View either to the top of your view, or to the web.ex view section and add the proper functions to your view like so.

defmodule MyApp.PostView do
  use JSONAPI.View, type: "posts"

  def fields do
    [:text, :body, :excerpt]
  end

  def excerpt(post, _conn) do
    String.slice(post.body, 0..5)
  end

  def meta(data, _conn) do
    # this will add meta to each record
    # To add meta as a top level property, pass as argument to render function (shown below)
    %{meta_text: "meta_#{data[:text]}"}
  end

  def relationships do
    # The post's author will be included by default
    [author: {MyApp.UserView, :include},
     comments: MyApp.CommentView]
  end
end

is an example of a basic view. You can now call render(conn, MyApp.PostView, "show.json", %{data: my_data, meta: meta}) or 'index.json normally.

If you'd like to use this without phoenix simply use the JSONAPI.View and call JSONAPI.Serializer.serialize(MyApp.PostView, data, conn, meta).

Parsing and validating a JSONAPI Request

plug JSONAPI.QueryParser,
  filter: ~w(name),
  sort: ~w(name title inserted_at),
  view: PostView

This will add a JSONAPI.Config struct called jsonapi_config to your conn.assigns. If a user tries to sort, filter, include, or sparse fieldset an invalid field it will raise a plug error that shows the proper error message.

The config holds the values parsed into things that are easy to pass into an Ecto query, for example

sort=-name will be parsed into sort: [desc: :name] which can be passed directly to the order_by in ecto.

This sort of behavior is consistent for includes. Sparse fieldsets happen in the view using Map.take but when Ecto gets more complex field selection support we will go further to only query the data we need.

You will need to handle filtering yourself, the filter is just a map with key=value.

Spec Enforcement

We include a set of Plugs to make enforcing the JSONAPI spec for requests easy. To add spec enforcement to your application, add JSONAPI.EnsureSpec to your pipeline:

plug JSONAPI.EnsureSpec

Under-the-hood JSONAPI.EnsureSpec relies on three individual plugs:

  • JSONAPI.ContentTypeNegotiation — Requires the Content-Type and Accept headers are set correctly.

  • JSONAPI.FormatRequired — Verifies that the JSON body matches the expected %{data: %{attributes: attributes}} format.

  • JSONAPI.IdRequired — Confirm the id key is present in %{data: data} and that it matches the resource's id in the URI.

Configuration

By default host and scheme are pulled from the provided conn but can be overridden via configuration like so:

config :jsonapi,
  host: "www.someotherhost.com",
  scheme: "https",
  underscore_to_dash: true,
  remove_links: false
  • underscore_to_dash

Additionally, JSONAPI now recommends the use of dashes (-) in place of underscore (_) as a word separator. Enabling this change is easy with the underscore_to_dash option, which handles the conversion for you. Defaults to false.

  • remove_links

links data can optionally be removed from the payload via setting the configuration above to true. Defaults to false.

Other

  • Feel free to make PR's. I will do my best to respond within a day or two.
  • If you want to take one of the TODO items just create an issue or PR and let me know so we avoid duplication.
  • If you need help, I am on irc and twitter.
  • Example project

jsonapi's People

Contributors

adamesque avatar bbhoss avatar bmrsny avatar doomspork avatar glejeune avatar gugl avatar jakerobers avatar jlvallelonga avatar mitchellhenke avatar mpriestman avatar safwankamarrudin avatar scrogson avatar snewcomer avatar stareintothebeard avatar sulphur avatar ybur-yug avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.