Skip to content

confetti/yayson

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

YAYSON

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

As of 2.0.0 YAYSON aims to support JSON API version 1. In version 3.0.0 we now support native JavaScript classes.

NPM

Installing

Install yayson by running:


$ npm i yayson

Presenting data

A basic Presenter can look like this:

  const Presenter = require('yayson')({
    adapter: 'default'
  }).Presenter;

  class ItemsPresenter extends Presenter {};
  ItemsPresenter.prototype.type = 'items';

  var item = {
    id: 5,
    name: 'First'
  };

  ItemsPresenter.render(item);

This would produce:

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

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

A bit more advanced example:

var Presenter = require('yayson')().Presenter;

class ItemsPresenter extends Presenter {
  attributes() {
    var attrs = super.attributes(...arguments);
    attrs.start = moment.utc(attrs.start).toDate();
    return attrs;
  }

  relationships() {
    return {
      event: EventsPresenter
    }
  }
};
ItemsPresenter.prototype.type = 'items'


ItemsPresenter.render(item)

Sequelize support

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

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

You can also define your own adapter globally:

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

Or at Presenter level:

ItemPresenter.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();

    adapter.get({path: '/events/' + id}).then(function(data) {
      let event;
      return event = store.sync(data);
    });

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

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published