Hi, there!
I've played with Restangular for a while now. I've come across an issue, though, that seems interesting and that could lead me to make an improvement pull-request, if anyone is interested and if @mgonto find it nice to go into Restangular's code.
So, I was dealing with pre-request validation. For us to have a case of study, let's imagine a simple password size validation:
function validPassword(password) {
return password.length >= 8;
}
Now, lets say we use Restangular for the authentication in the following way:
Restangular.one('login').doPOST({password: '12345678'});
Ok, that's a pretty simple example. Now, the password validation part would have to rely on controller logic (besides the server side validation, of course), so that the request is only made if a valid password is attempted. IMHO, that is very bad for a large application, that could have many of it's parts using login Restangular route.
Well, I thought we could arrange that via requestInterceptors. My idea was to make requestInterceptors be able to return a promise, when they need to deal with something before doing the actual request. That would allow, for instance, to write these following code:
// Reject the request promise when password is not valid.
Restangular.addRequestInterceptor(function (element, op, what, url) {
if (what === 'login' && op === 'post') {
return validPassword(element.password) ? element : $q.reject('Invalid password');
}
return element;
});
That would allow, among other things, for chained dependent requests and any asynchronous code validation, like the the following:
Restangular.addRequestInterceptor(function (element, op, what, url) {
if (what === 'login' && op === 'post') {
return $timeout(function () {
return validPassword(element.password);
}, 5000);
}
return element;
});
So, what do you guys think?
Hi, there!
I've played with Restangular for a while now. I've come across an issue, though, that seems interesting and that could lead me to make an improvement pull-request, if anyone is interested and if @mgonto find it nice to go into Restangular's code.
So, I was dealing with pre-request validation. For us to have a case of study, let's imagine a simple password size validation:
Now, lets say we use Restangular for the authentication in the following way:
Ok, that's a pretty simple example. Now, the password validation part would have to rely on controller logic (besides the server side validation, of course), so that the request is only made if a valid password is attempted. IMHO, that is very bad for a large application, that could have many of it's parts using
loginRestangular route.Well, I thought we could arrange that via
requestInterceptors. My idea was to makerequestInterceptorsbe able to return a promise, when they need to deal with something before doing the actual request. That would allow, for instance, to write these following code:That would allow, among other things, for chained dependent requests and any asynchronous code validation, like the the following:
So, what do you guys think?