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

jsonapi-relate

v2.0.0

Published

Helpers for Interacting with a JSON API server

Downloads

1,471

Readme

JSON API Relate

npm version Build Status Coverage Status

This library provides several helper methods for navigating JSON API payloads

Get a Relationship

For starters, let's take the official example: getting articles from a blog.

Let's say you just want to clean up that up into something usable for your template.

MyAPI.get("/articles").then(function (payload) {
  return payload.data.map(function (article) {
    var author = payload.included.find(function(include) {
      return include.id === article.relationships.author.data.id
        && include.type === article.relationships.author.data.type
    })
    return {
      title: article.attributes.title,
      author: author && author.attributes,
      comments: article.relationships.comments.data.map(function (relationship, i) {
        var comment = payload.included.find(function(include) {
          return include.id === relationship.id
            && include.type === relationship.type
        })
        var author = payload.included.find(function(include) {
          return include.id === comment.relationships.author.data.id
            && include.type === comment.relationships.author.data.type
        })
        return {
          body: comment.attributes.body,
          author: author && author.attributes
        }
      })
    }
  })
})

That works, but it's messy and hard to follow.. this is where relate helps you out. Notice getRelationship deals with singular, or plural relationships just fine.

var relate = require("jsonapi-relate")
MyAPI.get("/articles").then(function (payload) {
  return payload.data.map(function (article) {
    var author = relate.getRelationship(payload, article, "author")
    return {
      title: article.attributes.title,
      author: author && author.attributes,
      comments: relate.getRelationship(payload, article, "comments")
        .map(function(comment) {
          var author = relate.getRelationship(payload, comment, "author")
          return {
            body: comment.attributes.body,
            author: author && author.attributes
          }
        })
    }
  })
})

We've also got a few more helpers...

Get all Relationships

Sometimes you have a lot of relationships, and you want to get them all. Let's update our first example above to get author and comments at once.

var relate = require("jsonapi-relate")
MyAPI.get("/articles").then(function (payload) {
  return payload.data.map(function (article) {
    var relationships = relate.getRelationships(payload, article)
    return {
      title: article.attributes.title,
      author: relationships.author && relationships.author.attributes,
      comments: relationships.comments
        .map(function(comment) {
          var author = relate.getRelationship(payload, comment, "author")
          return {
            body: comment.attributes.body,
            author: author && author.attributes
          }
        })
    }
  })
})

Get deep Relationships

Using our first example. Let's say that I just want to get Comment authors, and don't care about the comments themselves. Now, I'll need to traverse through two relationships.

var relate = require("jsonapi-relate")
MyAPI.get("/articles").then(function (payload) {
  return payload.data.map(function (article) {
    var author = relate.getRelationship(payload, article, "author")
    return {
      title: article.attributes.title,
      author: author && author.attributes,
      commentAuthors: relate.getDeepRelationship(payload, article, "comments.author")
        .filter(function(author) {
          return author
        })
        .attributes
      }
    }
  })
})

Installation

npm install jsonapi-relate --save

Development

Testing

Using Mocha/Chai for testing.

npm t

Code Coverage

Generate a report using Istanbul to make sure your tests are touching everything!

npm t && npm run view-coverage