@outfit.io/normalize-json-api-response

1.0.7 • Public • Published

@outfit.io/normalize-json-api-response

Ensures we have consistent responses from APIs

import normalizeJsonApiResponse from '@outfit.io/normalize-json-api-response'

Usage

Fast-json-api can be very fast, but it returns an entity that is sometimes hard to navigate or requires some manipulation to get the entities redux store ready. Given the following json api response, we want to do a few things:

  • Create a primary data-centric normalized response, with an array of ids and a id indexed object that would be suitable for a slice in a redux store
  • Index included entities in to their own id resource object
  • Flatten each entity, such that attributes and relationships exist on root of the entity
  • Camel case entity keys to play nicer with common liniting rules
const testPayload = {
    data: {
        id: '3',
        type: 'movie',
        attributes: {
            name: 'test movie',
            year: 2000,
            original_year: null
        },
        relationships: {
            actors: {
                data: [
                    {
                        id: '1',
                        type: 'actor'
                    },
                    {
                        id: '2',
                        type: 'actor'
                    }
                ]
            },
            owner: {
                data: {
                    id: '3',
                    type: 'user'
                }
            }
        }
    },
    included: [
        {
            id: '1',
            type: 'actor',
            attributes: {
                first_name: 'test',
                last_name: 'mcgee'
            }
        },
        {
            id: '2',
            type: 'actor',
            attributes: {
                first_name: 'victoria',
                last_name: 'tset'
            }
        },
        {
            id: '3',
            type: 'user',
            attributes: {
                username: 'test_user'
            }
        }
    ]
};

turns in to

{
   "ids":[
      "3"
   ],
   "entities":{
      "movie":{
         "3":{
            "name":"test movie",
            "year":2000,
            "originalYear":null,
            "actors":[
               {
                  "id":"1",
                  "type":"actor"
               },
               {
                  "id":"2",
                  "type":"actor"
               }
            ],
            "owner":{
               "id":"3",
               "type":"user"
            },
            "id":"3"
         }
      },
      "actor":{
         "1":{
            "firstName":"test",
            "lastName":"mcgee",
            "id":"1"
         },
         "2":{
            "firstName":"victoria",
            "lastName":"tset",
            "id":"2"
         }
      },
      "user":{
         "3":{
            "username":"test_user",
            "id":"3"
         }
      }
   }
}

Contribution

This repo uses the changeset action to deploy changes. The main branch is protected, meaning you can not push directly to it. All changes must be made via Pull Request. You should also include a change set file; you can use the changeset cli to generate one using npm run changeset:add. You can use the prompts to create a changeset file, but don't worry, you can also add to it later.

Readme

Keywords

none

Package Sidebar

Install

npm i @outfit.io/normalize-json-api-response

Weekly Downloads

147

Version

1.0.7

License

ISC

Unpacked Size

11.8 kB

Total Files

7

Last publish

Collaborators

  • samueljim
  • jamesrplee
  • outfit-npm
  • phillycheese
  • omnihound