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

normalize-json-api-response

v1.1.2

Published

Normalizing a JSON:API styled server response

Downloads

40

Readme

Normalize JSON:API Response

Logo

npm version npm downloads license

Normalize JSON:API Response (NJAR) is not only designed to simplify JSON:API responses but also to make them easy developer friendly.

Created by Sinestro White with :heart: !

Features

  • Easy installation and integration - No complicated actions
  • Front-end design friendly - Makes response data easy to display with a for-loop
  • JSON:API simplification
  • No need to correct the normalized response
  • No schema required
  • No dependencies 👌

Installation

Using npm:

  $ npm i normalize-json-api-response

Then, using a module bundler that supports either CommonJS or ES2015 modules, such as webpack:

  import normalize from 'normalize-json-api-response';

Usage

Basic Example

When working with the JSON:API specification, the response body of any request is optimized and it can get difficult to do computations. The main information is contained in the "data" property but if there are relations to other tables they are put in the "included" property.

    //Example server response
    const data = {
      data: {
        type: "articles",
        id: "1",
        attributes: {
          title: "JSON:API paints my bikeshed!"
        },
        relationships: {
          author: {
            data: { type: "people", id: "9" }
          }
        }
      },
      included: [
        {
          type: "people",
          id: "9",
          attributes: {
            firstName: "Dan",
            lastName: "Gebhardt",
            twitter: "dgeb"
          }
        }
      ]
    };

Normalize JSON:API Response (NJAR) solves the problem with the JSON:API response optimization by moving every item from "included" to the item from "data" it belongs to.

    import normalize from 'normalize-json-api-response';
    console.log(normalize(data));
    
    // Normalized object
    // {
    //   "people": [
    //     {
    //       "id": "9",
    //       "attributes": {
    //         "firstName": "Dan",
    //         "lastName": "Gebhardt",
    //         "twitter": "dgeb"
    //       }
    //     }
    //   ],
    //   "articles": [
    //     {
    //       "id": "1",
    //       "attributes": {
    //         "title": "JSON:API paints my bikeshed!"
    //       },
    //       "people": {
    //         "type": "people",
    //         "id": "9",
    //         "attributes": {
    //           "firstName": "Dan",
    //           "lastName": "Gebhardt",
    //           "twitter": "dgeb"
    //         }
    //       }
    //     }
    //   ]
    // }

As you can see the "articles" property is an array of objects so that every item can be accessed easily with a simple for-loop. The information from "included" has been moved in the "relationships" property of every item in "articles".

Why should I use this?

There are already a number of great JSON:API normalizing packages out there (for instance, json-api-normalizer is fantastic). However, most of those packages do not provide a simple way to access the included information from every "data" item, which has some severe limitations. In this case, you have to create additional functions to correct the normalized response.

FAQ

Dependencies

NJAR has no dependencies.

Reporting Issues

If believe you've found an issue, please report it along with any relevant details to reproduce it.

Asking for help

Please file an issue for personal support requests. Tag them with question.

Contributions

Yes please! Feature requests / pull requests are welcome.