npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

v1.0.7

Published

Ensures we have consistent responses from APIs

Downloads

3,509

Readme

@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.