Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise based request interceptors #893

Conversation

lucasconstantino
Copy link

This pull-request closes #891, which is an issue created by myself. It intends to make fullRequestInterceptors be able to make asynchronous transformations before the actual request happens.

Because it is a promise based approach, it also makes it possible for a interceptor to reject the user request before any data travels. This is specially useful on situations where you want to validate requests in the client side before even bothering requesting the server. That could, of course, be implemented on a controller level of an application, but that makes things like per-route validation much more centralized and organized.

I've run all tests and everything seems to continue working after this major change. Besides, I'm already using the code on a project in production. Anyway, I hope more people can give it a try before we make it to the master. An alternative to introduce such a change is to actually implement this idea only when Restangular gets build from ground up to 2.0, which I hope to help make it happen soon.

P.s.: If I find some time I'll write tests for this pull-request.

@lucasconstantino lucasconstantino changed the title Closes #891 Promise based request interceptors Sep 30, 2014
@jack-kerouac
Copy link

I have a use case that would make use of this pull request:
We have an API for getting the currently logged in user and its ID. I also want to have services that query details of the user with URL pattern /user/<ID>/something. The something service should return a Restangular instance (or decoupled service) that I can use the POST/PUT/... to the above URL,without the invoker needing to know the user ID.

I would like to implement that using a request interceptor that prepends /home/<ID> to a request. But since the ID is retrieved from the server, it would need to return a promise that awaits the resolution of the ID.

@geotry
Copy link

geotry commented Oct 17, 2014

Thank you, i'm currently using your code and it works quite well! Here's my use case:

I'm using a API that uses OAuth2, so before each request I'll check that a valid access token is present in the local storage and if not, I'll use a refresh token to eventually get a new valid access token (this is the asynchronous part). This way, I'm avoiding the extra call saying my access token is not valid anymore and that I need to refresh it.

@tomjmul
Copy link

tomjmul commented Jan 20, 2016

Exactly the same use case as @Schunk. Is this coming anytime soon?

@javierquevedo
Copy link

Pretty similar use case over here. Please consider evaluating this merge request which is about 1.5 years old now 👍

@daviesgeek
Copy link
Collaborator

@lucasconstantino Can you rebase and then I'll take a look at this?

@gauravarora
Copy link
Contributor

I have exactly similar use case. I would like to refresh my access token and only then let the request through. Right now, I have no choice but to make the API call while still refreshing the access token in the background and then re-request when the API call errors out.

@lukeggchapman
Copy link

@daviesgeek I've rebased this and created a new PR #1433

@lucasconstantino The main changes I made was to change urlHandler.resource.apply(window, ... and config.fullRequestInterceptor.apply(window, ... to be applied with the context of itself rather than window.

All tests are passing and I should be using this over the next 2 days and hopefully not find any bugs.

@lucasconstantino
Copy link
Author

lucasconstantino commented Oct 26, 2016

Thanks, @lukeggchapman! I don't really use Restangular anymore, so I wouldn't make this rebase so soon. I guess we can close this one.

@gauravarora
Copy link
Contributor

@lucasconstantino if I may ask, what do you use instead?

@lucasconstantino
Copy link
Author

@gauravarora I don't have any projects in angular, beside some old ones. React took my heart :)

By the way: even in React environment I still miss a lot of the Restangular functionalities... once I though about extracting Restangular core from Angular. I think @mgonto also thought about this a while ago.

@daviesgeek
Copy link
Collaborator

Sorry all :/ It's been really hard keeping up with this project, since I'm really the only active one going through PRs and issues.

@gauravarora Take a look at https://github.com/daviesgeek/restjs I basically re-wrote Restangular in vanilla JS with a few changes. It's still a 1.0 for sure, but I can assure you, I will be maintaining it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pre-request validation on a Restangular level.
8 participants