New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

yayson

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yayson

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

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.9K
increased by4.15%
Maintainers
2
Weekly downloads
 
Created
Source

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()

Keywords

FAQs

Package last updated on 02 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc