Coder Social home page Coder Social logo

jsonapi.js's Introduction

jsonapi.js

jsonapi.js is a Javascript implement of jsonapi 1.0 version.

Installation

Install jsonapi.js by NPM

npm install jsonapi.js

Install jsonapi.js by Bower

bower install jsonapi.js

Usage

var jsonapi = require('jsonapi.js');
var Pool = jsonapi.Pool;
var Resource = jsonapi.Resource;
var Relationship = jsonapi.Relationship;

var pool = new Pool();
pool.addRemote('foo', '/api/foo');


/*=============================
  Fetching Data
  http://jsonapi.org/format/#fetching
  =============================*/

/*
  GET /api/foo HTTP/1.1
  Accept: application/vnd.api+json
*/
pool.fetch('foo');

/*
  GET /api/foo/1 HTTP/1.1
  Accept: application/vnd.api+json
*/
pool.fetch('foo', 1);


/*=============================
  Creating, Updating and Deleting Resources
  http://jsonapi.org/format/#crud
  =============================*/

/*
  POST /api/foo HTTP/1.1
  Content-Type: application/vnd.api+json
  Accept: application/vnd.api+json

  {
    "data": {
      "type": "foo",
      "attributes": {
        "content": "foo"
      }
    }
  }
*/
pool.create('foo', {
  attributes: {
    content: 'foo'
  }
});

/*
  PATCH /api/foo/1 HTTP/1.1
  Content-Type: application/vnd.api+json
  Accept: application/vnd.api+json

  {
    "data": {
      "type": "foo",
      "id": 1,
      "attributes": {
        "content": "bar"
      }
    }
  }
*/
pool.update('foo', 1, {
  attributes: {
    'content': 'bar'
  }
});

/*
  DELETE /api/foo/1 HTTP/1.1
  Accept: application/vnd.api+json
*/
pool.remove('foo', 1);

/*=============================
  Update To-One Relationships
  http://jsonapi.org/format/#crud-updating-to-one-relationships
  =============================*/

var toOneRelationship = new Relationship({
  data: null
});

/*
  PATCH /api/foo/1/relationships/baz HTTP/1.1
  Content-Type: application/vnd.api+json
  Accept: application/vnd.api+json

  {
    "data": {
      type: 'baz',
      id: 1
    }
  }
*/
pool.replaceLinkage(toOneRelationship, {
  type: 'baz',
  id: 1
});

/*
  PATCH /api/foo/1/relationships/baz HTTP/1.1
  Content-Type: application/vnd.api+json
  Accept: application/vnd.api+json

  {
    "data": null
  }
*/
pool.replaceLinkage(toOneRelationship, null);


/*=============================
  Update To-Many Relationships
  http://jsonapi.org/format/#crud-updating-to-many-relationships
  =============================*/

var toManyRelationship = new Relationship({
  data: []
});

/*
  PATCH /api/foo/1/relationships/bar HTTP/1.1
  Content-Type: application/vnd.api+json
  Accept: application/vnd.api+json

  {
    "data": [{
      type: 'bar',
      id: 1
    }]
  }
*/
pool.replaceLinkage(toManyRelationship, [{
  type: 'bar',
  id: 1
}]);

/*
  POST /api/foo/1/relationships/bar HTTP/1.1
  Content-Type: application/vnd.api+json
  Accept: application/vnd.api+json

  {
    "data": [{
      type: 'bar',
      id: 1
    }]
  }
*/
pool.addLinkage(toManyRelationship, [{
  type: 'bar',
  id: 1
}]);

/*
  DELETE /api/foo/1/relationships/bar HTTP/1.1
  Content-Type: application/vnd.api+json
  Accept: application/vnd.api+json

  {
    "data": [{
      type: 'bar',
      id: 1
    }]
  }
*/
pool.removeLinkage(toManyRelationship, [{
  type: 'bar',
  id: 1
}]);

Contributing

Install Dependencies: gulp

npm install -g gulp

watch

gulp watch

build

gulp build

test

gulp test

jsonapi.js's People

Contributors

lustdante avatar jeongsd avatar ironhee avatar

Stargazers

Steffen Müller avatar Seho Noh avatar  avatar  avatar  avatar

Watchers

James Cloos avatar Seho Noh avatar  avatar  avatar

jsonapi.js's Issues

ESDoc Hosting Service

Hi!
I am ESDoc developer and thanks for using ESDoc 😄

I released ESDoc Hosting Service 🚀
Register your repository Git URL, this service automatically generate documentation.
If you are interested, please try to use it.
Thanks

ESDoc Hosting Service

Hi!
I am ESDoc developer and thanks for using ESDoc 😄

I released ESDoc Hosting Service 🚀
Register your repository Git URL, this service automatically generate documentation.
If you are interested, please try to use it.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.