@apicase/jsonapi

JSON API plugin for Apicase (jsonapi.org)

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
11Jul 13, 2018Apr 13, 2018Minified + gzip package size for @apicase/jsonapi in KB

Readme

Apicase JSON API

Apicase plugin that helps you work with JSON API (based on json-api-normalize)

Read JSON API specification

click here

Installation

npm install @apicase/jsonapi
import fetch from "@apicase/adapter-fetch"
import { ApiService } from "@apicase/core"
import { jsonApiPlugin } from "@apicase/jsonapi"

const Root = new ApiService(fetch, {
  url: "/api"
}).use(jsonApiPlugin) // <- Add this line

Usage

Imagine that we have API route with the following result.body:

{
  "data": [
    {
      "id": "1",
      "type": "post",
      "attributes": {
        "title": "Hello world",
        "text": "Lorem Ipsum Dolor ..."
      },
      "relationships": {
        "author": {
          "data": {
            "id": "1",
            "type": "user"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": "1",
      "type": "user",
      "attributes": {
        "name": "Anton",
        "surname": "Kosykh"
      }
    }
  ]
}

We'll create a service with meta.normalize options:

const GetPosts = Root.extend({
  meta: {
    normalize: ["id", "title", "text", "author.id", "author.name"]
  }
})

GetPosts
  .doRequest()
  .then(({ result }) => {
    console.log(result.body)
  })

It will convert result.body to:

[
  {
    "id": "1",
    "title": "Hello world",
    "text": "Lorem Ipsum Dolor ...",
    "author": {
      "id": "1",
      "name": "Anton"
    }
  }
]

If you need links or another options from original body, it's stored in result.rawBody:

console.log(result.rawBody.included)
/*
  [{
    "id": "1",
    "type": "user",
    "attributes": {
      "name": "Anton",
      "surname": "Kosykh"
    }
  }]
*/

License

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.