-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Ajax config & intercept #2686
Comments
I'm also interested in how this is solved? |
I'm planning on writing a simple http wrapper singleton service around ajax to solve this issue. Will share when I have it completed |
I am also very interested in this. I am really wondering if there's an ongoing plan for this issue. I am interested in contributing to adding this functionality. I might have a simple solution to it, and I can work on it very soon :) Cheers! |
So I am currently using Rxjs in one of my projects and I was using rxjs/ajax. However I realized that rxjs/ajax doesn't have interceptors. So out of curiosity I forked rxjs and implemented interceptors. The usage that I came up with (from my project :D) was that I added an operator ajaxInterceptors which can be called using array of (request interceptor array or a request interceptor or none), and (response interceptor array or a response interceptor or none), also more interceptors can be added using few methods for each request (interceptors for GET requests and so on). My question is: Is is an ideal implementation for ajax interceptors for rxjs and if so, will it be accepted, once unit tests and performance tests are complete? |
update? |
Would be great to have an ability to hook to/intercept all the ajax requests. |
No luck? |
any update on this? |
any update on this? is there a plan to add interceptors ? |
My application is using
Epics:
By doing this, all the request i send will automatically insert |
maybe using more pipe to make more interception.. |
You can still use Axios with interceptor and use |
In my case i created a function for that. export function ajaxObs(params: {
[key: string]: any;
url: string;
method?: string;
body?: any;
headers?: { [key: string]: string };
responseType?: string;
}) {
const {
url,
method,
body,
headers = {
"Content-Type": "application/json",
...(Cookies.get("accessToken") && {
Authorization: `Bearer ${Cookies.get("accessToken")}`,
}),
},
responseType,
} = params;
const apiBaseUrl: string = https://mycustomurl.com/
// creation observable
const ajax$: Observable<AjaxResponse | Observable<AjaxError>> = ajax({
url: `${apiBaseUrl}${url}`,
method: method ? method : "GET",
async: true,
headers: headers,
body: body,
...(responseType && { responseType: responseType }),
})
return ajax$;
} I hope this will help you |
I'm on the fence about this feature. I've seen interceptors be useful, however, it also means introducing global, mutable state (at least in the proposed solution, or in existing prior art). In other words, if a third party adds an interceptor like I'm more inclined to say a better design would be to have a factory that creates an instance of If we had use cases we all agreed on, then perhaps we could implement something. In the mean time, as a workaround, I'd recommend just wrapping it in your own function that amended the request/response as necessary. |
Core Team: General lack of interest in this feature as of right now. @benlesh: I'll think about the problem though, since it seems like something a lot of people are asking for. |
I tend to prefer these interceptors capable factories instead of a global solution. What could be provided instead is a generator of such factories. |
I have created a sample wrapper for both functional and class-based HTTP services over rxjs ajax to have both before and after interceptors. https://gist.github.com/maxgaurav/48e1f309fd2afb2ab55c558c3e63d1dc Hope this helps. |
Is there a way to create a pre configured ajax instance, so that I don't need to configure it over and over again ?
For example:
BTW how can I intercept all http requests or responses in a single place using rxjs' ajax api ?
Here's how the axios library intercepts http requests and responses:
Thanks !
The text was updated successfully, but these errors were encountered: