A javascript module designed to make it really easy to consume a json:api
service.
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.
<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>
var JsonapiClient = require("jsonapi-client");
var client = new JsonapiClient("http://localhost:16006/rest", {
header: {
authToken: "2ad1d6f7-e1d0-480d-86b2-dfad8af4a5b3"
}
});
var article = client.create("articles");
article.set("title", "foobar");
article.sync(function(err) {
console.log("Resource created");
});
client.find("articles", function(err, resources) {
resources.map(function(resource) {
console.log(resource.toJSON());
});
});
client.get("articles", 5, { include: [ "author" ] }, function(err, article) {
console.log(article.toJSONTree());
});
article.fetch("author", function(err) {
console.log(article.author.toJSON());
});
article.relationships("comments").add(comment);
article.sync(function(err) {
console.log("Resource's relation updated");
});
article.delete(function(err) {
console.log("Resource deleted");
});