Coder Social home page Coder Social logo

jsonapi-client's Introduction

Coverage Status Build Status Code Climate Codacy Badge Dependencies Status

Sauce Test Status

jsonapi-client

A javascript module designed to make it really easy to consume a json:api service.

Motivation / Justification / Rationale

Consuming a json:api service from within Javascript is a non-trivial affair. Setting up a transport mechanism, authentication, making requests to standardised HTTP routes, error handling, pagination and expanding an inclusion tree... All of these things represent barriers to consuming an API. This module takes away all the hassle and lets developers focus on interacting with a rich API without wasting developer time focusing on anything other than shipping valuable features.

This module is tested against the example json:api server provided by jsonapi-server.

Full documentation

The tl;dr

In a browser

<script src="/dist/jsonapi-client.min.js"></script>
<script type="text/javascript">
  var client = new JsonapiClient("http://localhost:16006/rest", {
    header: {
      authToken: "2ad1d6f7-e1d0-480d-86b2-dfad8af4a5b3"
    }
  });
</script>

Creating a new Client

var JsonapiClient = require("jsonapi-client");
var client = new JsonapiClient("http://localhost:16006/rest", {
  header: {
    authToken: "2ad1d6f7-e1d0-480d-86b2-dfad8af4a5b3"
  }
});

Creating a new Resource

var article = client.create("articles");
article.set("title", "foobar");
article.sync(function(err) {
  console.log("Resource created");
});

Finding Resources

client.find("articles", function(err, resources) {
  resources.map(function(resource) {
    console.log(resource.toJSON());
  });
});

Getting a specific Resource

client.get("articles", 5, { include: [ "author" ] }, function(err, article) {
  console.log(article.toJSONTree());
});

Fetching a Resource's related Resource

article.fetch("author", function(err) {
  console.log(article.author.toJSON());
});

Updating a Resource's primary relationships

article.relationships("comments").add(comment);
article.sync(function(err) {
  console.log("Resource's relation updated");
});

Deleting a Resource

article.delete(function(err) {
  console.log("Resource deleted");
});

jsonapi-client's People

Contributors

duncanfenning avatar pmcnr-hx avatar theninj4 avatar

Watchers

 avatar  avatar

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.