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

yayson

v3.0.0

Published

A library for serializing and reading JSON API standardized data in JavaScript.

Downloads

8,544

Readme

YAYSON

A library for serializing and reading JSON API data in JavaScript.

From version 3 we now support native JavaScript classes. YAYSON has zero dependencies and works in the browser and in node 14 and up.

NPM

Installing

Install yayson by running:



$ npm i yayson

Presenting data

A basic Presenter can look like this:

const yayson = require('yayson')
const { Presenter } = yayson()

class BikePresenter extends Presenter {
  static type = 'bikes'
}

const bike = {
  id: 5,
  name: 'Monark'
};

BikePresenter.render(bike);

This would produce:




{
  data: {
    id: 5,
    type: 'bikes',
    attributes: {
      id: 5,
      name: 'Monark'
    }
  }
}

It also works with arrays, so if you send an array to render, "data" will be an array.

A bit more advanced example:


const yayson = require('yayson')
const { Presenter } = yayson()

class WheelPresenter extends Presenter {
  static type = 'wheels'

  relationships() {
    return { bike: BikePresenter }
  }
}

class BikePresenter extends Presenter {
  static type = 'bikes'
  relationships() {
    return { wheels: WheelPresenter }
  }
}

Sequelize support

By default it is set up to handle standard JS objects. You can also make it handle Sequelize.js models like this:


const yayson = require('yayson')
{Presenter} = yayson({adapter: 'sequelize'})

You can also define your own adapter globally:



const yayson = require('yayson')
{Presenter} = yayson(adapter: {
  id: function(model){ return 'omg' + model.id},
  get: function(model, key){ return model[key] }
})

Take a look at the SequelizeAdapter if you want to extend YAYSON to your ORM. Pull requests are welcome. :)

Metadata

You can add metadata to the top level object.



  ItemsPresenter.render(items, {meta: count: 10})

This would produce:



{
  meta: {
    count: 10
  }
  data: {
    id: 5,
    type: 'items',
    attributes: {
      id: 5,
      name: 'First'
    }
  }
}

Parsing data

You can use a Store can like this:

  const {Store} = require('yayson')();
  const store = new Store();

  const data = await adapter.get({path: '/events/' + id});
  const event = store.sync(data);

This will give you the parsed event with all its relationships.

Its also possible to find in the synched data:

  const event = this.store.find('events', id)

  const images = this.store.findAll('images')

Use in the browser

Recommended way is to use it via webpack or similar build system wich lets you just require the package as usual.

If you just want to try it out, copy the file dist/yayson.js to your project. Then simply include it:


    <script src="./lib/yayson.js"></script>

Then you can var yayson = window.yayson() use the yayson.Presenter and yayson.Store as usual.

Browser support

Tested

  • Chrome
  • Firefox
  • Safari
  • Safari iOS

Untested, but should work

  • IE 11
  • Android

Legacy support

Earlier versions of JSON API worked a bit different from 1.0. Therefore YAYSON provides legacy presenters and stores in order to have interoperability between the versions. Its used similar to the standard presenters:


const yayson = require('yayson/legacy')
const { Presenter } = yayson()