1.0.7 • Published 2 years ago

@spiderdan98/json-api-normalizer v1.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

json-api-normalizer

Utility package to normalize JSON API response data

Description

json-api-normalizer helps to normalize JSON API. Its supports all valid JSON API responses.

Install

$ npm install @spiderdan98/json-api-normalizer

Example

import jsonApiNormalizer from '@spiderdan98/json-api-normalizer';

const json = {
    "meta": {
        "page": {
            "currentPage": 1,
            "from": 1,
            "lastPage": 1,
            "perPage": 3,
            "to": 3,
            "total": 3
        }
    },
    "jsonapi": {
        "version": "1.0"
    },
    "links": {
        "first": "http://localhost/api/v1/games",
        "last": "http://localhost/api/v1/games"
    },
    "data": [
        {
            "type": "games",
            "id": "1",
            "attributes": {
                "name": "Game 1"
            },
            "relationships": {
                "tournament": {
                    "data": {
                        "type": "tournaments",
                        "id": "1"
                    }
                }
            },
            "links": {
                "self": "http://localhost/api/v1/games/1"
            },
            "meta": {
                "foo": "game"
            }
        },
    ],
    "included": [
        {
            "type": "tournaments",
            "id": "1",
            "attributes": {
                "name": "Tournament 1"
            },
            "relationships": {
                "creator": {
                    "links": {
                        "related": "http://localhost/api/v1/tournaments/1/creator",
                        "self": "http://localhost/api/v1/tournaments/1/relationships/creator"
                    },
                    "data": {
                        "type": "users",
                        "id": "1"
                    }
                },
                "location": {
                    "links": {
                        "related": "http://localhost/api/v1/tournaments/1/location",
                        "self": "http://localhost/api/v1/tournaments/1/relationships/location"
                    },
                    "data": {
                        "type": "locations",
                        "id": "1"
                    }
                },
                "games": {
                    "links": {
                        "related": "http://localhost/api/v1/tournaments/1/games",
                        "self": "http://localhost/api/v1/tournaments/1/relationships/games"
                    }
                }
            },
            "links": {
                "self": "http://localhost/api/v1/tournaments/1"
            },
            "meta": {
                "foo": "tournament"
            }
        },
        {
            "type": "users",
            "id": "1",
            "attributes": {
                "username": "User 1"
            },
            "links": {
                "self": "http://localhost/api/v1/users/1"
            },
            "meta": {
                "foo": "user"
            }
        },
        {
            "type": "locations",
            "id": "1",
            "attributes": {
                "name": "Location 1"
            },
            "links": {
                "self": "http://localhost/api/v1/locations/1"
            },
            "meta": {
                "foo": "location"
            }
        }
    ]
}

console.log(jsonApiNormalizer(json));
/* Output:
{
  "data": [
    {
      "name": "Game 1",
      "id": "1",
      "type": "games",
      "links": {
        "self": "http://localhost/api/v1/games/1"
      },
      "meta": {
        "foo": "game"
      },
      "tournament": {
        "name": "Tournament 1",
        "id": "1",
        "type": "tournaments",
        "links": {
          "self": "http: //localhost/api/v1/tournaments/1"
        },
        "meta": {
          "foo": "tournament"
        },
        "creator": {
          "username": "User 1",
          "id": "1",
          "type": "users",
          "links": {
            "self": "http://localhost/api/v1/users/1"
          },
          "meta": {
            "foo": "user"
          }
        },
        "location": {
          "name": "Location1",
          "id": "1",
          "type": "locations",
          "links": {
            "self": "http: //localhost/api/v1/locations/1"
          },
          "meta": {
            "foo": "location"
          }
        }
      }
    }
  ],
  "jsonapi": {
    "version": "1.0"
  },
  "links": {
    "first": "http://localhost/api/v1/games",
    "last": "http://localhost/api/v1/games"
  },
  "meta": {
    "page": {
      "currentPage": 1,
      "from": 1,
      "lastPage": 1,
      "perPage": 3,
      "to": 3,
      "total": 3
    }
  }
}
*/

Config

OptionDescriptionDefault
onlyDataIf true it will return only the data contentfalse
onlyErrorIf true and data is empty it will return only the errorstrue
includeErrorsIf true it will append errorstrue
includeJsonapiIf true it will append jsonapitrue
includeLinksIf true it will append linkstrue
includeMetaIf true it will append metatrue
includeIdIf true it will append the id to resourcestrue
includeTypeIf true it will append the type to resourcestrue
ignorePointerPathThe path to ignore by creating error object'/data/attributes'
beforeResourceTriggers before a resource will be normalizednone
afterResourceTriggers after a resource is normalizednone
beforeDataResourceTriggers before a root resource will be normalizednone
afterDataResourceTriggers after a root resource is normalizednone
beforeRelationshipTriggers before a relationship will be normalizednone
afterRelationshipTriggers after a relationship is normalizednone
beforeErrorTriggers before a error is normalizednone
afterErrorTriggers after a error is normalizednone
1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago