- Latest Release:
- Build-Status:
- Official Site: http://dracoblue.net/
hateoas-client.js is copyright 2011-2015 by DracoBlue http://dracoblue.net
hateoas-client.js is a library (for browser+nodejs) to communicate with RESTful services. It uses
jQuery as ajax library. It's aim is to provide a very simple API to follow
the links
defined in a request response, thus achieving
level 3 in Richardson Maturity Model
.
Requirements:
- jQuery 1.5+
- On the browser:
$ bower install hateoas-client
- In nodejs:
$ npm install hateoas-client
If you include hateoas-client.js
after your jQuery.js
, you'll have the ability
to make such requests:
var a = new HttpAgent('/api');
a.navigate(['coffee', {'name': 'Small'}, "buy"]);
a.post(function(response) {
// response.getValue() contains what the POST /api/orders?product_id=5 returned
});
This example assumes that the responses contain
GET /api
{
"links": [ { "rel": "coffee", "href": "/api/coffee" } ]
}
GET /api/coffee
[
{
"id": 5,
"name": "Small",
"links": [ { "rel": "buy", "href": "/api/orders?product_id=5" } ]
}
]
This example retrieves the most viewed videos from youtube, navigates 2x next, chooses the
first element (because of empty filter{}
), navigates to it's self
link and finally:
returns the <title>
element's text.
var a = new HttpAgent('http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?max-results=5', {}, {
'proxy_script': 'proxy.php?url='
});
a.navigate(['next', 'next', {}, 'self']);
a.get(function(response) {
var title = jQuery(response.getValue()).find('title').text();
});
That example also shows, how one can use the proxy_script
-option to use a
.php
-script to retrieve contents from a remote site.
If you want to retrieve the HttpAgent in your require.js script use (ensure that hateoas-client
maps on hateoas-client.js
in your requirejs config file):
require('hateoas-client', function(hateoasClient) {
var a = new hateoasClient.HttpAgent('/api');
});
application/json
(detected byJsonHttpResponse
), supported features:getValue()
: Returns the entire response as json objectgetValues()
: If the entire response was an array, it will return each element asJsonHttpResponse[]
.getMatchingValue()
: Filters ongetValues()
getLinks()
: Expects alinks
-property as array with{rel: REL, "href": URL}
elements and returns them asHttpLink[]
application/atom+xml
(detected byAtomXmlHttpResponse
), supported features:getValue()
: Returns the entire response as xml objectgetValues()
: Returns all<entry>
children asAtomXmlHttpResponse[]
.getMatchingValue()
: Filters ongetValues()
getLinks()
: Returns all<link>
children asHttpLink[]
application/hal+json
(detected byJsonHalHttpResponse
), supported features:getValue()
: Returns the entire response as json objectgetValues()
: Returns all_embedded
objects asJsonHalHttpResponse[]
.getMatchingValue()
: Filters ongetValues()
getLinks()
: Returns all_links
and_embedded
links asHttpLink[]
according to HAL specification
application/hc+json
(detected byJsonHcHttpResponse
), supported features:getValue()
: Returns the entire response as json objectgetValues()
: Returns all embedded objects on root level withself
-link asJsonHalHttpResponse[]
.getMatchingValue()
: Filters ongetValues()
getLinks()
: Returns all embedded objects (withself
-link), obvious links (with iana registration) or properties starting withhttp:
/https:
on root level asHttpLink[]
application/xml
(detected byXmlHttpResponse
), supported features:getValue()
: Returns the entire response as xml object
- test and extend support for other responses (xml, maybe a generic converter system or usage of the one from jQuery)
- handle status codes other then 200 (currently only 200 is
JsonHttpResponse#isOk() == true
and 201 is interpreted) - add documentation for
HttpAgent
,JsonHttpResponse
,AtomXmlHttpResponse
andXmlHttpResponse
- ... more as soon as I get to that!
See CHANGLOG.md for more information.
hateoas-client.js is licensed under the terms of MIT. See LICENSE for more information.