All Projects → yury-dymov → Json Api Normalizer

yury-dymov / Json Api Normalizer

Licence: mit
Normalize JSON API data for redux applications

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Json Api Normalizer

Mojojson
A simple and fast JSON parser.
Stars: ✭ 271 (-49.63%)
Mutual labels:  json-api
Polr
🚡 A modern, powerful, and robust URL shortener
Stars: ✭ 4,147 (+670.82%)
Mutual labels:  json-api
Aimeos Laravel
Laravel ecommerce package for professional, ultra fast online shops, complex B2B applications and #gigacommerce
Stars: ✭ 5,204 (+867.29%)
Mutual labels:  json-api
Laravel5 Jsonapi
Laravel 5 JSON API Transformer Package
Stars: ✭ 313 (-41.82%)
Mutual labels:  json-api
Redux Json Api
Redux actions, action creators and reducers to make life with JSON APIs a breeze.
Stars: ✭ 374 (-30.48%)
Mutual labels:  json-api
Json.h
🗄️ single header json parser for C and C++
Stars: ✭ 387 (-28.07%)
Mutual labels:  json-api
Vertigo
Blog engine in Go (golang)
Stars: ✭ 266 (-50.56%)
Mutual labels:  json-api
Getty
a netty like asynchronous network I/O library based on tcp/udp/websocket; a bidirectional RPC framework based on JSON/Protobuf; a microservice framework based on zookeeper/etcd
Stars: ✭ 532 (-1.12%)
Mutual labels:  json-api
W.i.l.l
A python written personal assistant
Stars: ✭ 377 (-29.93%)
Mutual labels:  json-api
Jsonapidotnetcore
JSON:API Framework for ASP.NET Core
Stars: ✭ 465 (-13.57%)
Mutual labels:  json-api
Json api client
Build client libraries compliant with specification defined by jsonapi.org
Stars: ✭ 321 (-40.33%)
Mutual labels:  json-api
Pyjfuzz
PyJFuzz - Python JSON Fuzzer
Stars: ✭ 342 (-36.43%)
Mutual labels:  json-api
Para
Open source back-end server for web, mobile and IoT. The backend for busy developers. (self-hosted or hosted)
Stars: ✭ 389 (-27.7%)
Mutual labels:  json-api
Quotable
Random Quotes API
Stars: ✭ 293 (-45.54%)
Mutual labels:  json-api
Element Api
Create a JSON API/Feed for your elements in Craft.
Stars: ✭ 493 (-8.36%)
Mutual labels:  json-api
Spine
A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
Stars: ✭ 267 (-50.37%)
Mutual labels:  json-api
Devour
Don't just consume your JSON API, Devour it...
Stars: ✭ 386 (-28.25%)
Mutual labels:  json-api
Simpla
Open, modular, and serverless content management for a modern web
Stars: ✭ 534 (-0.74%)
Mutual labels:  json-api
Security Apis
A collective list of public JSON APIs for use in security. Contributions welcome
Stars: ✭ 508 (-5.58%)
Mutual labels:  json-api
Json Api Php
JSON-API (http://jsonapi.org) responses in PHP.
Stars: ✭ 426 (-20.82%)
Mutual labels:  json-api

json-api-normalizer

Utility to normalize JSON API data for redux applications

npm version Downloads Build Status Coverage Status

Description

json-api-normalizer helps awesome JSON API and redux work together. Unlike normalizr json-api-normalizer supports JSON API specification, which means that you don't have to care about schemes. It also converts collections into maps, which is a lot more suitable for redux.

Demo - https://yury-dymov.github.io/json-api-react-redux-example/

Demo sources - https://github.com/yury-dymov/json-api-react-redux-example

Works great together with redux-object, which helps to fetch and denormalize data from the store.

json-api-normalizer was recently featured in SmashingMagazine: https://www.smashingmagazine.com/2017/05/json-api-normalizer-redux/

Install

$ npm install json-api-normalizer

Example

import normalize from 'json-api-normalizer';

const json = {
  data: [{
    "type": "post-block",
    "relationships": {
      "question": {
        "data": {
          "type": "question",
          "id": "295"
        }
      }
    },
    "id": "2620",
    "attributes": {
      "text": "I am great!",
      "id": 2620
    }
  }],
  included: [{
    "type": "question",
    "id": "295",
    "attributes": {
      "text": "How are you?",
      id: 295
    }
  }]
};

console.log(normalize(json));
/* Output:
{
  question: {
    "295": {
      id: 295,
      type: "question"
      attributes: {
        text: "How are you?"
      }
    }
  },
  postBlock: {
    "2620": {
      id: 2620,
      type: "postBlock",
      attributes: {
        text: "I am great!"
      },
      relationships: {
        question: {
          type: "question",
          id: "295"
        }
      }
    }
  }
}
*/

Options

Endpoint And Metadata

While using redux, it is supposed that cache is incrementally updated during the application lifecycle. However, you might face an issue if two different requests are working with the same data objects, and after normalization, it is not clear how to distinguish, which data objects are related to which request. json-api-normalizer can handle such situations by saving the API response structure as metadata, so you can easily get only data corresponding to the certain request.

console.log(normalize(json, { endpoint: '/post-block/2620' }));
/* Output:
{
  question: {
    ...
  },
  postBlock: {
    ...
  },
  meta: {
    "/post-block/2620": {
      data: [{
        type: "postBlock",
        id: 2620,
        relationships: {
          "question": {
            type: "question",
            id: "295"
          }
      }]
    }
  }
}
*/

Endpoint And Query Options

By default request query options are ignored as it is supposed that data is incrementally updated. You can override this behavior by setting filterEndpoint option value to false.

const d1 = normalize(json, { endpoint: '/post-block/2620?page[cursor]=0' });
const d2 = normalize(json, { endpoint: '/post-block/2620?page[cursor]=20' });
console.log(Object.assign({}, d1, d2));
/* Output:
{
  question: {
    ...
  },
  postBlock: {
    ...
  },
  meta: {
    "/post-block/2620": {
      ...
    }
  }
}
*/

const d1 = normalize(json, { endpoint: '/post-block/2620?page[cursor]=0', filterEndpoint: false });
const d2 = normalize(json, { endpoint: '/post-block/2620?page[cursor]=20', filterEndpoint: false });
console.log(someFunctionWhichMergesStuff({}, d1, d2));
/* Output:
{
  question: {
    ...
  },
  postBlock: {
    ...
  },
  meta: {
    "/post-block/2620: {
      "?page[cursor]=0": {
        ...
      },
      "?page[cursor]=20": {
        ...
      }
    }
  }
}
*/

Pagination And Links

If JSON API returns links section and you define the endpoint, then links are also stored in metadata.

const json = {
  data: [{
    ...
  }],
  included: [{
    ...
  }],
  links: {
    first: "http://example.com/api/v1/post-block/2620?page[cursor]=0",
    next: "http://example.com/api/v1/post-block/2620?page[cursor]=20"
  }
};

console.log(normalize(json, { endpoint: '/post-block/2620?page[cursor]=0'}));
/* Output:
{
  question: {
    ...
  },
  postBlock: {
    ...
  },
  meta: {
    "/post-block/2620": {
      data: [{
        ...
      }],
      links: {
        first: "http://example.com/api/v1/post-block/2620?page[cursor]=0",
        next: "http://example.com/api/v1/post-block/2620?page[cursor]=20"
      }
    }
  }
}
*/

Lazy Loading

If you want to lazy load nested objects, json-api-normalizer will store links for that

const json = {
  data: [{
    attributes: {
      ...
    },
    id: "29",
    relationships: {
      "movie": {
        "links": {
          "self": "http://...",
          "related": "http://..."
        }
      },
    },
    type: "question"
  }]
};

console.log(normalize(json));
/* Output:
{
  question: {
    "29": {
      attributes: {
        ...
      },
      relationships: {
        movie: {
          links: {
            "self": "http://...",
            "related": "http://..."
          }
        }
      }
    }
  }
}
*/

Camelize Keys

By default all object keys and type names are camelized, however, you can disable this with camelizeKeys option.

const json = {
  data: [{
    type: "post-block",
    id: "1",
    attributes: {
      "camel-me": 1,
      id: 1
    }
  }]
}

console.log(normalize(json));
/* Output:
{
  postBlock: {
    "1": {
      id: 1,
      type: "postBlock",
      attributes: {
        camelMe: 1
      }
    }
  }
}
*/

console.log(normalize(json, { camelizeKeys: false }));
/* Output:
{
  "post-block": {
    "1": {
      id: 1,
      type: "postBlock",
      attributes: {
        "camel-me": 1
      }
    }
  }
}
*/

Camelize Type Values

By default propagated type values are camelized but original value may be also preserved

const json = {
  data: [{
    type: "post-block",
    id: "1",
    attributes: {
      "camel-me": 1,
      id: 1
    }
  }]
}

console.log(normalize(json, { camelizeTypeValues: false }));
/* Output:
{
  postBlock: {
    "1": {
      id: 1,
      type: "post-block", // <-- this
      attributes: {
        camelMe: 1
      }
    }
  }
}
*/

Copyright

MIT (c) Yury Dymov

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].