I'm designing an API client with angular
and the $http
service using a Hypermedia API. Since I have resources that build on top of each other I wanted the client user to be able to do this declaratively:
apiRoot
.getPeopleRoot()
.getPerson(1)
.addJob(apiRoot.getJobsRoot().getRandomJob())
.updatePerson()
But I want to also be able to tap in $then
methods to use intermediate results:
$scope.job = apiRoot.getJobsRoot().getRandomJob()
apiRoot.getPeopleRoot().getPerson(1).then(function (person) {
$scope.person = person
}).addJob($scope.job).updatePerson()
I managed to do each one of these separately by returning the promise or an object that contains the functions to operate with each of these resouces but I'm not sure how to achieve both at the same time, or if it's a good idea.
EDIT: I managed to progress using the following mixin to my resource objects
function Actualizable() {}
Actualizable.prototype.actual = function (value) {
this.realizedValue = value
return this
}
Then checking if I've resolved the value on each function or calling a promise to actually resolve it. I'm not sure if there's a mechanism to do this with promises still.