Overview

Traverson – the Hypermedia API Client For JavaScript

3 Comments

A lot of REST APIs offer links between their individual resources. A well constructed client then only needs to know the URI of the root resource and can find its way from there to every other resource by following links with well defined relations – without hardcoding any URI except one, thus decoupling client and server. This pattern is commonly known as HATEOAS – Hypertext As The Engine Of Application State. APIs that do this are also referred to as Hypermedia APIs recently, although according to Mr REST himself, this it not even optional but mandatory for any decent REST API.

Traverson is a JavaScript module for Node.js and the browser that makes working with such APIs much easier.

GitHub’s API at https://api.github.com/ is an example for the HATEOAS pattern. Let’s see what a walk from resource to resource could look like with this API:

Starting at https://api.github.com/,

{
  "current_user_url": "https://api.github.com/user",
  "gists_url": "https://api.github.com/gists{/gist_id}",
  "repository_url": "https://api.github.com/repos/{owner}/{repo}",
  ...
}

… then following the link "repository_url",
using the parameters { owner: "basti1302", repo: "traverson" },
thus going to https://api.github.com/repos/basti1302/traverson

{
  "id": 13181035,
  "name": "traverson",
  "full_name": "basti1302/traverson",
  "owner": {
    "login": "basti1302",
    ...
  },
  "url": "https://api.github.com/repos/basti1302/traverson",
  "commits_url": "https://api.github.com/repos/basti1302/traverson/commits{/sha}",
  "issues_url": "https://api.github.com/repos/basti1302/traverson/issues{/number}",
  ...
}

… then following the link "commits_url",
using the parameter { sha: 5c82c74583ee67eae727466179dd66c91592dd4a },
thus going to https://api.github.com/repos/basti1302/traverson/commits/5c82c74583ee67eae727466179dd66c91592dd4a

{
  "sha": "5c82c74583ee67eae727466179dd66c91592dd4a",
  "commit": {
    "author": {
      "name": "Bastian Krol",
      ...
    },
    "committer": {
      "name": "Bastian Krol",
      "date": "2013-10-25T11:41:09Z",
      ...
    },
    "message": "test hal support against @mikekelly's haltalk server"
    ...
  },
  "url": "https://api.github.com/repos/basti1302/traverson/commits/5c82c74583ee67eae727466179dd66c91592dd4a",
  "comments_url": "https://api.github.com/repos/basti1302/traverson/commits/5c82c74583ee67eae727466179dd66c91592dd4a/comments",
  ...
}

… then, at last, following the link "comments_url",
thus finally arriving at
https://api.github.com/repos/basti1302/traverson/commits/5c82c74583ee67eae727466179dd66c91592dd4a/comments

[
  {
    "id": 4432531,
    "user": {
      "login": "basti1302",
      ...
    },
    "created_at": "2013-10-25T22:50:59Z",
    "body": "This commit is a commit is a commit.",
    ...
  }
]

So we followed three consecutive links, hopping from resource to resource, to reach the one resource we were actually interested in (the commit comments, in this contrived example).

Introducing Traverson

There a lot of useful modules and libraries that make working with REST APIs easier. Most of them focus on working with one resource or one request. But, as far as I know, there is no help with the typical pattern of following multiple links, walking from resource to resource, as outlined above – at least not in Node.js, or, more generally in JavaScript. Say hello to Traverson. Traverson centers around the idea of following a sequence of links or walking along a path of link relations.

Let’s return to the GitHub API example to show how this works. If you wanted to make use of the links this API provides, using, for example, the request module for Node.js directly, you could write something like this:

var request = require('request')
var rootUri = 'https://api.github.com/'
 
function nextUri(response, link) {
  var resource = JSON.parse(response.body)
  return resource[link]
}
 
request.get(rootUri, function(err, response) {
  if (err) { console.log(err); return; }
  var uri = nextUri(response, 'repository_url')
  uri = uri.replace(/{owner}/, 'basti1302')
  uri = uri.replace(/{repo}/, 'traverson')
  request.get(uri, function(err, response) {
    if (err) { console.log(err); return; }
    uri = nextUri(response, 'commits_url')
    uri = uri.replace(/{\/sha}/,
        '/5c82c74583ee67eae727466179dd66c91592dd4a')
    request.get(uri, function(err, response) {
      if (err) { console.log(err); return; }
      uri = nextUri(response, 'comments_url')
      request.get(uri, function(err, response) {
        if (err) { console.log(err); return; }
        var resource = JSON.parse(response.body)
        console.log(resource)
      })
    })
  })
})

There it is, the infamous callback pyramid. Also, the code is repetitive. Now, with Traverson, the basic pattern for following a sequence of links to a target resource looks like this:

var api = require('traverson').json.from('https://api.github.com/')
api.newRequest()
  .follow('repository_url', 'commits_url', 'comments_url')
  .getResource(function(err, resource) {
    // do something with the resource...
  })

And the complete, working example for the GitHub API would be

var api = require('traverson').json.from('https://api.github.com/')
api.newRequest()
  .follow('repository_url', 'commits_url', 'comments_url')
  .withTemplateParameters({
    owner: 'basti1302',
    repo: 'traverson',
    sha: '5c82c74583ee67eae727466179dd66c91592dd4a'
  }).getResource(function(err, resource) {
    if (err) { console.log(err); return; }
    console.log(resource)
  })

I don’t know about you, but I like the version that uses Traverson better.

So what happens here? Traverson will access the provided root URI given to from(). It will then work its way through the API by following the links identified by the arguments to the follow method. When it has finished the journey specified by the follow arguments, it will hand the last resource back to the caller. Actually, all that only happens when getResource is called, all method calls before that only set things up.

Traverson Features

URI Templates

Besides following links, which is Traverson’s core feature, the example above already used another feature, namely working with URI templates. Often the link to the next resource is not an actual URI but an RFC 6570 URI template. If Traverson encounters such a template on its way along the resources and values for the template parameters have been provided with the withTemplateParameters method, they are automatically substituted.

Besides URI templates, Traverson offers a number of other handy features:

  • basic authentication,
  • OAuth,
  • custom HTTP headers,
  • JSONPath,
  • HAL (hypertext application language)

Let’s examine some of them in detail.

JSONPath

So far we told Traverson which links to follow by giving it a series of property names. Traverson looks for these properties in each resource to find out which URI to access next. What if the link is not a direct property of the resource? For example, if the resource looks like this:

{
  "deeply": {
    "nested": {
      "link: "http://api.io/congrats/you/have/found/me"
    }
  }
}

To follow the link to http://api.io/congrats/you/have/found/me you can pass JSONPath expressions to the follow method:

api.newRequest()
  .follow('$.deeply.nested.link')
  .getResource(function(error, document) {
    ...
  })

Of course, you can mix JSONPath expressions with plain vanilla property names.

Basic Authentication

Under the hood, Traverson uses the aforementioned request module, at least, when running in Node.js (in the browser, request is shimmed by using superagent). Therefore, you can use all of request‘s features, like basic authentication, OAuth and custom headers directly with Traverson. For example, basic authentication with the GitHub API would look like this:

var api = require('traverson').json.from('https://api.github.com/'),
    ghUser = 'basti1302',
    ghPass = '...'
 
api.newRequest()
  .follow(...)
  .withRequestOptions({
    auth: {
      user: ghUser,
      pass: ghPass,
      sendImmediately: true
    }
  }).getResource(function(err, resource) {
    ...
  })

Working with HAL

HAL, the Hypertext Application Language, “is a simple format that gives a consistent and easy way to hyperlink between resources in your API”. There is a formal spec for the media type application/hal+json. It does not say anything about how to structure the payload content of your JSON, but it defines where and how links between resources are presented to the client. This makes the life easier for clients because they know exactly where to look for links, even when talking to different APIs. You can play around with a live HAL demo at http://haltalk.herokuapp.com. For example, you could follow the path from the root resource via the links ht:users, ht:user and ht:posts to arrive at the list of all posts for a user.

Let’s do that with Traverson. To use HAL, you just use require('traverson').jsonHal instead of require('traverson').json.

var api = require('traverson').jsonHal.from('http://haltalk.herokuapp.com/')
 
api.newRequest()
  .withRequestOptions({
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }
  }).follow('ht:users', 'ht:user', 'ht:posts', 'ht:post')                                                                      
  .withTemplateParameters({name: 'mike'})                                   
  .getResource(function(error, document) {
     // document will represent the list of posts for user mike
  })

Or, to post a new entry for a user:

var body = { content: 'Hello there!' }
api.newRequest()
  .withRequestOptions({
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    auth: {
      user: 'traverson',
      pass: '...', // traverson's password would go here :-)
      sendImmediately: true
    }
  }).follow('ht:me', 'ht:posts')
  .withTemplateParameters({ name: 'traverson' })
  .post(body, function(error, respons) {
    if (err) { console.log(err); return; }
    if (response.statusCode === 201) {
      console.log('unexpected http status: ' + response.statusCode)
    }
  })

For both examples we needed to set the Accept and the Content-Type header to make the Haltalk server understand our requests. Since Haltalk requires authenication to post stuff, we also needed to pass our user and password along.

In the browser

Working with REST APIs is definitely a thing in server side JavaScript, which is why Traverson started out as a Node.js module. But it can also be useful when working with APIs directly from the browser via AJAX. That’s the reason why Traverson also works in the browser, thanks to browserify. The browser build is a single file with a UMD that can be either included with a script tag or loaded by an AMD loader like RequireJS.

Using Traverson in the browser is not any different from using it in Node.js:

<html>
  <head>
    <meta charset="utf-8">
    <title>Traverson in the Browser</title>
    <script src="http://code.jquery.com/jquery-2.0.2.min.js"></script>
    <script src="traverson.min.js"></script>
  </head>
  <body>
 
    <div id="response"/>
 
    <script>
      var api = traverson.json.from('https://api.github.com/')
      api.newRequest()
        .follow('repository_url', 'commits_url', 'comments_url')
        .withTemplateParameters({
          owner: 'basti1302',
          repo: 'traverson',
          sha: '5c82c74583ee67eae727466179dd66c91592dd4a'
        }).getResource(function(err, resource) {
        if (err) {
          $('#response').html(JSON.stringify(err))
          return
        }
        $('#response').html('Hooray! The commit comment is <br/>"' +
            resource[0].body + '"')
      })
    </script>
  </body>
</html>

Background Story: Shave More Yaks

How did Traverson come about? Well, I have been doing a bit of yak shaving lately. Actually, quite a lot of yak shaving. It all started a few weeks ago, when I set out to write a short blog post about Cucumber.js, the JavaScript port of Cucumber that runs on Node.js and in the browser. What would make a nice little example for using Cucumber.js? I decided to showcase testing a REST API with Cucumber.js and Mikeal Rogers’ request library for Node.js. So I started writing some “tests” for GitHub’s API. While doing so, I noticed that the GitHub API actually offers links between the resources.

As mentioned above, a cleverly constructed client only needs to know the URI of the root resource and needs no other hard coded URIs. Because my tests were not “cleverly constructed” and did not make use of the provided links, I needed to hard code the URI of each endpoint. This was clearly not satisfying. So I started looking around if there is a node module that makes working with link-driven APIs easier. There are a few, but none of them offered the functionality I wanted – making following a sequence of links as easy as a single method call.

So I started to implement a little library for working with HATEOAS APIs – Traverson. When Traverson had come far enough to work for the simple Cucumber.js/GitHub API showcase, I thought it would also be nice to have Traverson support HAL, the hypertext application language, which tries to establish a standard for JSON-based hypertext APIs. There was Halbert, a node module for working with HAL resources, so I thought HAL support for Traverson is probably a matter of hours. Turned out that Halbert had some issues and was not yet fully spec-compliant. So I gave it an overhaul and sent the author some pull requests. He merged them nice and fast and also fixed some of the problems I found himself. Hooray for open source. A while later I wrote my own HAL module Halfred anyway to be able to better align it with Traverson’s goals – or because of not-invented-here-syndrome, who knows. It also occured to me that Traverson would be as useful in the browser as in Node.js, so I started to turn it into a cross platform module, that works in Node.js as well as in the browser. Because that is not entirely trivial, I also wrote a quick project template for such cross platform JavaScript modules based on npm, browserify, Grunt, Mocha and some other good stuff. I’ll probably blog a bit or two about cross platform JavaScript later.

However, at that point I was already four to five yak shaving stack frames deep, starting from the Cucumber.js blog post I originally wanted to do. I solemnly swear to write this blog post sooner or later. The code is already there, I just need to write it up 🙂

Anyway, I in my humble opinion Traverson is actually more useful than the Cucumber.js blog post would have been, so maybe yak shaving is not always a bad thing per se. Plus, I own a very nice, yak hair stuffed pillow now.

Kommentare

  • Oliver Gierke

    22. December 2013 von Oliver Gierke

    Hi guys, this looks like really cool stuff. I’ve been thinking about adding similar capabilities to the Java library Spring HATEOAS. If you don’t mind, I’d like to exchange a few thoughts on what a reasonable port could look like and maybe coin a few additional feature ideas. Drop me an email in case you’re interested. Merry Christmas!

    Cheers,
    Ollie

  • Bastian Krol

    Hi Oliver, sounds great! E-Mail is coming 😀

  • Kuldeep Daftary

    26. May 2015 von Kuldeep Daftary

    Hi Bastian, Great post very helpful.

    Question :

    Service that I’m using returns array object (example below) how do you reckon can i use Traverson with it ?

    [{
    “_links”: {
    “user”: {
    “href”: “resourceUrlGoesHere”
    }
    },
    “resourceId”: “c54e3437-2c12-4718-80a6-328ccb954ba4”,
    “name”: “Blah”,
    “created”: “2015-05-26 15:28:12+0000”
    }, {
    “_links”: {
    “user”: {
    “href”: “resourceUrlGoesHere”
    }
    },
    “resourceId”: “c54e3437-2c12-4718-80a6-328ccb954ba4”,
    “name”: “Blah Blah”,
    “created”: “2015-05-26 15:28:12+0000”
    }]

    I looked into Traverson api and didnt find much.

Comment

Your email address will not be published. Required fields are marked *