Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsonapi.js

Package Overview
Dependencies
Maintainers
3
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonapi.js

Javascript implementation of JSONAPI

  • 0.5.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
3
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 18 Sep 2015

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