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

@leobagua/json-api-helper

v1.2.2

Published

JSON API Helper is a JS library that helps you to normalize and deserialize data within the JSON API specification.

Downloads

226

Readme

A simple solution to work with JSON API data in JS.

Installation

yarn add @leobagua/json-api-helper

or

npm install @leobagua/json-api-helper

Usage

import { Deserializer } from '@leobagua/json-api-helper'

Deserializer.deserialize(response)

Examples:

Collection

A JSON Api Response

 {
   "meta": {
     "page": 1,
     "resources_per_page": 5,
     "total_resources": 6
   },
   "data": [
     {
       "type": "authors",
       "id": "1",
       "attributes": {
         "name": "Leonardo da Rosa",
         "birthplace": "Brazil"
       },
       "relationships": {
         "photos": {
           "data": [
             {
               "type": "photos",
               "id": "7"
             },
             {
               "type": "photos",
               "id": "8"
             }
           ]
         },
         "books": {
           "data": [
             {
               "type": "books",
               "id": "1"
             },
             {
               "type": "books",
               "id": "33"
             }
           ]
         }
       },
       "links": {
         "self": "/authors/7"
       }
     }
   ],
   "included": [
     {
       "type": "photos",
       "id": "7",
       "attributes": {
         "title": "Photo 365",
         "uri": "https:\/\/picsum.photos\/id\/378\/400\/300.jpg"
       }
     },
     {
       "type": "photos",
       "id": "8",
       "attributes": {
         "title": "Photo 786",
         "uri": "https:\/\/picsum.photos\/id\/529\/400\/300.jpg"
       }
     },
     {
       "type": "books",
       "id": "1",
       "attributes": {
         "title": "O'Kon, Gorczany and Langworth",
         "date_published": "1987-01-25",
         "isbn": 4294967295
       }
     },
     {
       "type": "books",
       "id": "33",
       "attributes": {
         "title": "Simonis, Simonis and Mills",
         "date_published": "1982-11-30",
         "isbn": 1304049345
       }
     }
   ]
 }

Will be parsed as:

[
  {
    "id": 1,
    "name": "Leonardo da Rosa",
    "birthplace": "Brazil",
    "author_links": {
      "self": "/authors/7"
    },
    "author_meta": {},
    "photos": [
      {
        "id": 7,
        "title": "Photo 365",
        "uri": "https://picsum.photos/id/378/400/300.jpg",
        "photo_links": {},
        "photo_meta": {}
      },
      {
        "id": 8,
        "title": "Photo 786",
        "uri": "https://picsum.photos/id/529/400/300.jpg",
        "photo_links": {},
        "photo_meta": {}
      }
    ],
    "books": [
      {
        "id": 1,
        "title": "O'Kon, Gorczany and Langworth",
        "date_published": "1987-01-25",
        "isbn": 4294967295,
        "book_links": {},
        "book_meta": {}
      },
      {
        "id": 33,
        "title": "Simonis, Simonis and Mills",
        "date_published": "1982-11-30",
        "isbn": 1304049345,
        "book_links": {},
        "book_meta": {}
      }
    ]
  }
]

Object

A JSON Api Response

{
  "data": {
    "id": 1,
    "type": "company",
    "attributes": {
      "name": "My Company",
      "number_of_employers": 20
    },
    "relationships": {
      "config": {
        "data": {
          "id": 1,
          "type": "config"
        }
      },
      "addresses": {
        "data": [
          {
            "id": 1,
            "type": "address"
          }
        ]
      },
      "stages": {
        "data": [
          {
            "id": 1,
            "type": "stage"
          },
          {
            "id": 2,
            "type": "stage"
          },
          {
            "id": 3,
            "type": "stage"
          }
        ]
      }
    }
  },
  "included": [
    {
      "id": 1,
      "type": "address",
      "attributes": {
        "street": "Company street",
        "country": "Brazil"
      }
    },
    {
      "id": 1,
      "type": "config",
      "attributes": {
        "logo": "/assets/fallback/my-logo.png"
      },
      "relationships": {
        "benefits": {
          "data": [
            {
              "id": 1,
              "type": "benefit"
            },
            {
              "id": 2,
              "type": "benefit"
            }
          ]
        },
        "images": {
          "data": []
        }
      }
    },
    {
      "id": 1,
      "type": "benefit",
      "attributes": {
        "value_type": "Percentage",
        "periodicity": "Monthly",
        "extensible": true,
        "value": 80.00
      }
    },
    {
      "id": 2,
      "type": "benefit",
      "attributes": {
        "value_type": "Value",
        "periodicity": "Monthly",
        "extensible": false,
        "value": 299.90
      }
    }
  ]
}

Will be parsed as:

{
  id: 1,
  name: 'My Company',
  number_of_employers: 20,
  company_links: {},
  company_meta: {},
  config: {
    id: 1,
    logo: '/assets/fallback/my-logo.png',
    config_links: {},
    config_meta: {}
  },
  addresses: [
    {
      id: 1,
      street: 'Company street',
      country: 'Brazil',
      address_links: {},
      address_meta: {}
    }
  ]
}

Multi-case types

To prevent some tedious work with use of different types of the naming convention of the deserialized data, a layer of multi-case type was added to help to get and set each parsed attribute, accordantly with the proffered case-type. So, you can access each object attribute with your preferred case-type:

company = Deserializer.deserialize(response)

// Getter
company.company_name // 'My company'
company.companyName // 'My company'
company.CompanyName // 'My company'
company['company-name'] // 'My company'

// Setter
company.company_name = 'My company in snake_case'
company.companyName = 'My company in camelCase'
company.CompanyName = 'My company in PascalCase'
company['kebab-case'] = 'My company in kebab-case'