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

sarala-json-api-data-formatter

v2.0.0

Published

Simple and fluent framework agnostic javascript library to transform standard JSON API responses to simple JSON objects and vice versa.

Downloads

1,517

Readme

sarala-json-api-data-formatter

codecov npm version apm Build Status

A fluent, framework-agnostic, JavaScript library, that can be used simply, to transform standard JSON API responses to simple JSON objects and vice versa.

Install

$ npm i sarala-json-api-data-formatter --save
$ yarn add sarala-json-api-data-formatter

JSON-API response data sample

const data = {
  "data": {
    "type": "posts",
    "id": "1",
    "attributes": {
      "slug": "voluptates-laborum-non-voluptatem-ducimus-veniam-et",
      "title": "Voluptates laborum non voluptatem ducimus veniam et.",
      "subtitle": "Cumque aut laudantium repudiandae rem repellendus voluptatem. Sunt ipsa eum ea molestias.",
      "body": "Est quod itaque suscipit quidem dolor dolores velit. Nihil voluptas placeat ex consequatur quasi.\n\nEst nulla cupiditate ad beatae rerum veritatis vel. Quia ut doloribus consequatur porro. Eligendi sit et dignissimos qui voluptatem magnam mollitia labore.\n\nLibero saepe praesentium et sed. Exercitationem error rerum sit inventore provident laborum. Fuga pariatur dolor reiciendis. Quibusdam corrupti commodi ut quo non laboriosam quia. Nihil sit iste sit optio voluptas repellendus exercitationem.",
      "published_at": "2018-01-25"
    },
    "links": {
      "self": "https://sarala-demo.app/api/posts/1"
    },
    "relationships": {
      "tags": {
        "links": {
          "self": "https://sarala-demo.app/api/posts/1/relationships/tags",
          "related": "https://sarala-demo.app/api/posts/1/tags"
        },
        "data": [
          {
            "type": "tags",
            "id": "1"
          },
          {
            "type": "tags",
            "id": "15"
          }
        ]
      }
    }
  },
  "included": [
    {
      "type": "tags",
      "id": "1",
      "attributes": {
        "name": "voluptates"
      },
      "links": {
        "self": "https://sarala-demo.app/api/tags/1"
      }
    },
    {
      "type": "tags",
      "id": "15",
      "attributes": {
        "name": "dolorum"
      },
      "links": {
        "self": "https://sarala-demo.app/api/tags/15"
      }
    }
  ]
};

Simple object data sample

const simpleObject = {
  "id": "1",
  "type": "posts",
  "links": {
    "self": "https://sarala-demo.app/api/posts/1"
  },
  "slug": "voluptates-laborum-non-voluptatem-ducimus-veniam-et",
  "title": "Voluptates laborum non voluptatem ducimus veniam et.",
  "subtitle": "Cumque aut laudantium repudiandae rem repellendus voluptatem. Sunt ipsa eum ea molestias.",
  "body": "Est quod itaque suscipit quidem dolor dolores velit. Nihil voluptas placeat ex consequatur quasi.\n\nEst nulla cupiditate ad beatae rerum veritatis vel. Quia ut doloribus consequatur porro. Eligendi sit et dignissimos qui voluptatem magnam mollitia labore.\n\nLibero saepe praesentium et sed. Exercitationem error rerum sit inventore provident laborum. Fuga pariatur dolor reiciendis. Quibusdam corrupti commodi ut quo non laboriosam quia. Nihil sit iste sit optio voluptas repellendus exercitationem.",
  "published_at": "2018-01-25",
  "relationships": [
    "tags"
  ],
  "tags": {
    "links": {
      "self": "https://sarala-demo.app/api/posts/1/relationships/tags",
      "related": "https://sarala-demo.app/api/posts/1/tags"
    },
    "data_collection": true,
    "data": [
      {
        "id": "1",
        "type": "tags",
        "links": {
          "self": "https://sarala-demo.app/api/tags/1"
        },
        "name": "voluptates"
      },
      {
        "id": "15",
        "type": "tags",
        "links": {
          "self": "https://sarala-demo.app/api/tags/15"
        },
        "name": "dolorum"
      }
    ]
  }
};

Deserialize

import { Formatter } from "sarala-json-api-data-formatter";

const formatter = new Formatter();

let data = this.deserialize(data);

Serialize

import { Formatter } from "sarala-json-api-data-formatter";

const formatter = new Formatter();

let data = this.serialize(data);

Filter relationships

Deserialize only root objects and skip all relationships

let data = this.includeOnly([]).deserialize(data);

Deserialize only specific relationships

When post has tags and comments, following will deserialize only root object and comments. Tags will be skipped.

let data = this.includeOnly(['comments']).deserialize(data);

Filter fields

Deserialize only specified fields

let data = this.filterFields({
    posts: ['title', 'subtitle'],
    tags: ['name']
}).deserialize(data);

The serialize method can be used similarly with includeOnly and filterFields.

Testing

npm run t

# run test with watch
npm run tw

Code coverage

npm run cc

Code Style

npm run cs