traverson

A Hypermedia API/HATEOAS Client for Node.js and the Browser

Github星跟蹤圖

Traverson Logo

Traverson

A Hypermedia API/HATEOAS Client for Node.js and the Browser

Introduction

Traverson comes in handy when consuming REST APIs that follow the HATEOAS principle, that is, REST APIs that have links between their resources. Such an API (also sometimes referred to as hypermedia or hypertext-driven API) typically has a root resource/endpoint, which publishes links to other resources. These resources in turn might also have, as part of their metadata, links to related resources. Sometimes you need to follow multiple consecutive links to get to the resource you want. This pattern makes it unnecessary for the client to hardcode all endpoint URLs of the API it uses, which in turn reduces the coupling between the API provider and the API consumer. This makes it easier for the API provider to change the structure of the API without breaking existing client implementations.

To follow a path of links you typically start at one URL (most often the root URL of the API), then look for the link you are interested in, fetch the document from there and repeat this process until you have reached the end of this path.

Traverson does that for you. You just need to tell Traverson where it can find the link to follow in each consecutive document and Traverson will happily execute the hops from document to document for you and when it's done, hand you the final http response or document, the one you really wanted to have in the first place.

Traverson works in Node.js and in the browser. For now, Traverson only supports JSON APIs. Support for other specialized JSON hypermedia types can be added with plug-ins (for example JSON-HAL).

The most basic thing you can do with traverson is to let it start at the root URL of an API, follow some links and pass the resource that is found at the end of this journey back to you. We call this procedure a "link traversal process". Here's how:

var traverson = require('traverson');

traverson
.from('http://api.example.com')
.follow('link_to', 'resource')
.getResource(function(error, document) {
  if (error) {
    console.error('No luck :-)')
  } else {
    console.log('We have followed the path and reached the target resource.')
    console.log(JSON.stringify(document))
  }
});

The User Guide has a ton of examples of what else Traverson can do for you. Here are some highlights:

  • resolve URI Templates
  • use JSONPath
  • manage headers, query strings and authentication (including OAuth)
  • content type detection
  • work with different media types by using plug-ins
  • use a custom JSON parser
  • continuing link traversals

License

MIT

主要指標

概覽
名稱與所有者traverson/traverson
主編程語言JavaScript
編程語言JavaScript (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2013-09-28 21:18:17
推送於2025-07-23 03:45:01
最后一次提交2024-12-03 04:17:36
發布數49
最新版本名稱v8.0.3 (發布於 )
第一版名稱0.8.0 (發布於 2014-06-04 13:05:17)
用户参与
星數456
關注者數17
派生數50
提交數0.9k
已啟用問題?
問題數88
打開的問題數10
拉請求數218
打開的拉請求數18
關閉的拉請求數51
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?