hardywu / restbee

JSON data normalizer/cacher. Currently support JSONAPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NPM version Codecov Travis

Restbee

JSON data normalizer/cacher based on axios. Currently support JSONAPI.

Quick start

import { JsonApiNormalizer } from 'restbee/dist/lib/restbee'
import axios from 'axios'

const cachedRecords = {}
const normalizer = new JsonApiNormalizer(cachedRecords)
const apiService = axios.create({
  baseURL: 'http://localhost/api/endpoint', 
})
  
apiService.interceptors.response.use(normalizer.parse,  normalizer.parErrors)

... ...

await postKey = apiService.get('posts/123') # posts_123
const post = cachedRecords[postKey]
# {
#   id: 123,
#   type: 'post',
#   title: 'test title',
#   content: 'post content',
#   userKey: 'user_3'
# } 

How JsonApiNormalizer serialize/deserialize json

use parseResObject to parse (flatten) a JSONAPI data

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON:API paints my bikeshed!"
    },
    "relationships": {
      "author": {
        "data": {
          "id": "3",
          "type": "users"
        }
      }
    }
  }
}

the result const flattened = normalizer.parseResObject(data)

# flattened
{
  "type": "articles",
  "id": "1",
  "title": "JSON:API paints my bikeshed!",
  "authorKey": "users_3",
}

use normalizer.toJsonApi(flattened) to serialize the flattened json back to JSONAPI data

About

JSON data normalizer/cacher. Currently support JSONAPI

License:MIT License


Languages

Language:TypeScript 100.0%