Miss any of our Open RFC calls?Watch the recordings here! »

normalize-json-api-response

1.1.2 • Public • Published

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 ❤️ !

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.

Install

npm i normalize-json-api-response

DownloadsWeekly Downloads

74

Version

1.1.2

License

MIT

Unpacked Size

81.1 kB

Total Files

22

Last publish

Collaborators

  • avatar