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 🙏

© 2025 – Pkg Stats / Ryan Hefner

simple-jsonapi

v0.0.4

Published

A framework agnostic, flexible library to convert javascript objects to and from JSONAPI formats.

Downloads

7

Readme

Build Status Coverage Status

NPM

simple-jsonapi

A node.js module for serializing objects to JSON API compliant documents. Very flexible whilst not caring about your choice of framework or database layer. Aims to cover the latest published version of the spec, which is currently 1.0.

Inspired by the great work of these modules:

Note: this is a very young module, and as such, has no proven track record...yet. Please feel free to give it a spin in your project and send me some feedback, but take it with a pinch of salt for now.

Why another JSON API module?

There's a couple of reasons:

  • JSON API is a relatively young specification, and I believe that having a strong variety of modules available to developers will help in increasing it's adoption.
  • I wanted a module that keeps the power and flexibility in my hands without hiding behind any magic. This also means keeping to itself, and not worrying about my choices of frameworks, databases and other modules.
  • I wanted a module that speaks the same language as the specification, namely by using concepts such as document, resource, relationship etc.

These all boil down to a matter of personal opinion, but I hope you enjoy the approach I've taken.

Installation

Grab it from the NPM repository:

npm install simple-jsonapi

Quick start

Import the module:

var api = require('simple-jsonapi');

Create a serializer:

var serializer = new api.Serializer({
    getType: function(resource, options, callback) {
        callback(null, 'person')
    },
    getId: function(resource, options, callback) {
        callback(null, resource.id);
    },
    getAttributes: function(resource, options, callback) {
        var attributes = {
            firstName: resource.firstName,
            lastName: resource.lastName
        };

        callback(null, attributes);
    }
});

Create a resource with some data and your new serializer:

var user = {
    id: 1,
    firstName: 'John',
    lastName: 'Smith'
};

var resource = new api.Resource(user, serializer);

Create a document using your new resource:

var document = new api.Document(resource);

Serialize that baby:

var output = document.toJSON();

And you should be left with output being:

{
    data: {
        type: 'person',
        id: 1,
        attributes: {
            firstName: 'John',
            lastName: 'Smith'
        }
    }
}

Pop that into the normal JSON.stringify() or res.json() methods, and you're good to go.

Detailed usage

Coming soon...

Testing

Running the tests is easy - just use:

npm test

Still to do

  • [ ] allow the use of promises as an alternative to callbacks
  • [ ] add a helper method for deserializing
  • [ ] add more robust parameter checking and error handling
  • [ ] add more edge cases to tests

Contributing

Coming soon...