-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor lib/actions to only use transforms (instead of callbacks)
- Loading branch information
Showing
12 changed files
with
257 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var minilog = require('minilog') | ||
, log = minilog('traverson') | ||
, isContinuation = require('../is_continuation'); | ||
|
||
/* | ||
* This transform covers the case of a follow() call *without any links* after | ||
* a continue(). Actually, there is nothing to do here since we should have | ||
* fetched everything last time. | ||
*/ | ||
module.exports = function continuationToDoc(t) { | ||
if (isContinuation(t)) { | ||
log.debug('continuing from last traversal process (actions)'); | ||
t.continuation = null; | ||
t.callback(null, t.step.doc); | ||
return false; | ||
} | ||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
var minilog = require('minilog') | ||
, log = minilog('traverson') | ||
, convertEmbeddedDocToResponse = | ||
require('./convert_embedded_doc_to_response') | ||
, isContinuation = require('../is_continuation'); | ||
|
||
/* | ||
* follow() call without links after continue(). Actually, there is nothing | ||
* to do here since we should have fetched everything last time. | ||
*/ | ||
module.exports = function continuationToResponse(t) { | ||
if (isContinuation(t)) { | ||
log.debug('continuing from last traversal process (actions)'); | ||
t.continuation = null; | ||
// Hm, a transform using another transform. This feels a bit fishy. | ||
convertEmbeddedDocToResponse(t); | ||
t.callback(null, t.step.response); | ||
return false; | ||
} | ||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
var minilog = require('minilog') | ||
, log = minilog('traverson') | ||
, abortTraversal = require('../abort_traversal') | ||
, httpRequests = require('../http_requests'); | ||
|
||
/* | ||
* Execute the last http request in a traversal that ends in | ||
* post/put/patch/delete. | ||
*/ | ||
// TODO Why is this different from when do a GET at the end of the traversal? | ||
// Probably only because the HTTP method is configurable here (with | ||
// t.lastMethod), we might be able to unify this with the | ||
// fetch_resource/fetch_last_resource transform. | ||
function executeLastHttpRequest(t, callback) { | ||
// always check for aborted before doing an HTTP request | ||
if (t.aborted) { | ||
return abortTraversal.callCallbackOnAbort(t); | ||
} | ||
httpRequests.executeHttpRequest( | ||
t, t.requestModuleInstance, t.lastMethod, t.callback); | ||
} | ||
|
||
executeLastHttpRequest.isAsync = true; | ||
|
||
module.exports = executeLastHttpRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var minilog = require('minilog') | ||
, log = minilog('traverson'); | ||
|
||
/* | ||
* This transform is meant to be run at the very end of a getResource call. It | ||
* just extracts the last doc from the step and calls t.callback with it. | ||
*/ | ||
module.exports = function extractDoc(t) { | ||
log.debug('walker.walk has finished'); | ||
/* | ||
TODO Breaks a lot of tests although it seems to make perfect sense?!? | ||
if (!t.doc) { | ||
t.callback(new Error('No document available')); | ||
return false; | ||
} | ||
*/ | ||
t.callback(null, t.step.doc); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
var minilog = require('minilog') | ||
, log = minilog('traverson'); | ||
|
||
/* | ||
* This transform is meant to be run at the very end of a get/post/put/patch/ | ||
* delete call. It just extracts the last response from the step and calls | ||
* t.callback with it. | ||
*/ | ||
module.exports = function extractDoc(t) { | ||
log.debug('walker.walk has finished'); | ||
/* | ||
TODO Breaks a lot of tests although it seems to make perfect sense?!? | ||
if (!t.response) { | ||
t.callback(new Error('No response available')); | ||
return false; | ||
} | ||
*/ | ||
t.callback(null, t.step.response); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var minilog = require('minilog') | ||
, log = minilog('traverson') | ||
, url = require('url'); | ||
|
||
/* | ||
* 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(new Error('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\"')); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
// TODO Only difference to lib/transform/fetch_resource is the continuation | ||
// checking, which is missing here. Maybe we can delete this transform and use | ||
// fetch_resource in its place everywhere? | ||
|
||
var minilog = require('minilog') | ||
, log = minilog('traverson') | ||
, abortTraversal = require('../abort_traversal') | ||
, httpRequests = require('../http_requests'); | ||
|
||
/* | ||
* Execute the last step in a traversal that ends with an HTTP GET. | ||
*/ | ||
// This is similar to lib/transforms/fetch_resource.js - refactoring potential? | ||
function fetchLastResource(t, callback) { | ||
// always check for aborted before doing an HTTP request | ||
if (t.aborted) { | ||
return abortTraversal.callCallbackOnAbort(t); | ||
} | ||
httpRequests.fetchResource(t, function(err, t) { | ||
log.debug('fetchResource returned (fetchLastResource).'); | ||
if (err) { | ||
if (!err.aborted) { | ||
log.debug('error while processing step ', t.step); | ||
log.error(err); | ||
} | ||
return t.callback(err); | ||
} | ||
callback(t); | ||
}); | ||
} | ||
|
||
fetchLastResource.isAsync = true; | ||
|
||
module.exports = fetchLastResource; |
Oops, something went wrong.