traverson
Version:
Hypermedia API/HATEOAS client for Node.js and the browser
33 lines (29 loc) • 1.05 kB
JavaScript
;
var minilog = require('minilog')
, log = minilog('traverson')
, url = require('url');
var errorModule = require('../errors')
, errors = errorModule.errors
, createError = errorModule.createError;
/*
* This transform is meant to be run at the very end of a get/post/put/patch/
* delete call. It just extracts the last accessed url from the step and calls
* t.callback with it.
*/
module.exports = function extractDoc(t) {
log.debug('walker.walk has finished');
if (t.step.url) {
return t.callback(null, t.step.url);
} else if (t.step.doc &&
// TODO actually this is very HAL specific :-/
t.step.doc._links &&
t.step.doc._links.self &&
t.step.doc._links.self.href) {
return t.callback(
null, url.resolve(t.startUrl, t.step.doc._links.self.href));
} else {
return t.callback(createError('You requested an URL but the last ' +
'resource is an embedded resource and has no URL of its own ' +
'(that is, it has no link with rel=\"self\"', errors.LinkError));
}
};