representor

0.2.0 • Public • Published

Representor

Library for building and using representors.

Install

npm install representor --save

Usage

Creating a Representor

Create a new instance of the representor.

var representor = new Representor;

Adding Attributes

The attributes property is a JavaScript object literal.

representor.attributes.email = 'john@example.com';

Adding links

representor.links.add({
  rel: 'next',
  href: 'http://example.com/users/2',
});

Adding Forms

let changePasswordForm = representor.forms.add({
  name: 'change-password',
  href: 'http://example.com/user/2/password',
  method: 'POST',
});
 
form.fields.add({
  name: 'password',
  value: 'foobar',
});

Adding Embedded Representors

let embedded = representor.embeddeds.add({
  rel: 'next',
  href: 'http://example.com/users/2',
});
 
embedded.links.add({
  rel: 'next',
  href: 'http://example.com/users/2',
});
 
let changePasswordForm = embedded.forms.add({
  name: 'change-password',
  href: 'http://example.com/user/2/password',
  method: 'POST',
});
 
changePasswordForm.fields.add(function(field) {
  name: 'password',
  value: 'foobar',
});

Converting to an Object Literal

Each object has its own #toValue function that may be called to output an object literal.

const rep = new Representor;
 
rep.links.add({
  rel: 'next',
  href: 'http://example.com/user/2',
});
 
const changePasswordForm = rep.forms.add({
  name: 'change-password',
  href: 'http://example.com/user/2/password',
  method: 'POST',
});
 
changePasswordForm.fields.add({
  name: 'password',
  value: 'foobar',
});
 
rep.embeddeds.add({
  rel: 'next',
  href: 'http://example.com/user/3',
});
 
repValue = rep.toValue();
 
// repValue above deep equals objLiteral here
const objLiteral = {
  attributes: {},
  links: [
    {
      rel: 'next',
      href: 'http://example.com/user/2',
    },
  ],
  forms: [
    {
      name: 'change-password',
      href: 'http://example.com/user/2/password',
      method: 'POST',
      fields: [
        {
          name: 'password',
          value: 'foobar',
        }
      ]
    },
  ],
  embeddeds: [
    {
      rel: 'next',
      href: 'http://example.com/user/3',
      attributes: {},
      links: [],
      forms: [],
      embeddeds: [],
    },
  ],
};

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.2.0
    0
  • 0.1.0
    0
  • 0.0.1
    0

Package Sidebar

Install

npm i representor

Weekly Downloads

0

Version

0.2.0

License

MIT

Unpacked Size

17.5 kB

Total Files

15

Last publish

Collaborators

  • smizell