Socket
Socket
Sign inDemoInstall

@apicase/jsonapi

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @apicase/jsonapi

JSON API plugin for Apicase (jsonapi.org)


Version published
Maintainers
1
Install size
128 kB
Created

Readme

Source

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

FAQs

Last updated on 13 Jul 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc