restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Allow fullRequestInterceptor to modify path

Open frogomatic opened this issue 13 years ago • 3 comments

My backend handles GET requests of the form /users/123/accounts to get all accounts from user 123, but to update one of those accounts, one would use PUT /accounts/456 as opposed to PUT /users/123/accounts/456. Rather than explicitly setting acct.parentResource = undefined every time I want to do a non-GET RESTful operation in my application, it would be nice if I could do this once, when configuring Restangular, by using the fullRequestInterceptor to set element.parentResource=undefined for all non-GET requests.

For this to work, however, either fullRequestInterceptor needs to be called before urlHandler.fetchUrl (in elemFunction), so that it can return a cloned object with no parentResource, or fullRequestInterceptor needs to be able to override the url somehow, presumably using urlHandler.

(Or am I missing something obvious here?)

frogomatic avatar Sep 16 '13 19:09 frogomatic

Hey,

You can use parentless requests for those :).

https://github.com/mgonto/restangular#setparentless

If you set parentless to accounts, it'd mean that after you got it, put, delete and all other interactions would actually be used using base URL.

Hope this works :).

Otherwise, please reopen!

mgonto avatar Sep 20 '13 18:09 mgonto

Unfortunately this seems to apply universally regardless of http method. My backend in question does use /users/123/accounts to get a list of all accounts for user 123, whereas /accounts would simply be a list of all accounts for all users. So, parentless won't work, unless I can set it before any PUT or DELETE, and clear it before any POST (e.g., POST /users/123/accounts would add a new account to user 123) or GET. Also, unless I'm mistaken, it's not possible to call setParentless in a request interceptor anyway, and have it make a difference, because by then the request path is already built.

Basically, it's too coarse-grained for my needs. Mind you I can accept that perhaps in my case it's not Restangular's responsibility to support this sort of backend semantics, but it would be nice.

frogomatic avatar Sep 22 '13 18:09 frogomatic

I understand.

Any suggestions on how you'd configure it or you'd do it?

mgonto avatar Sep 23 '13 17:09 mgonto