You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Axios does not support response streaming in browser, only in Node (there is intent, but no visible progress so far.). A readable "stream" can be acquired from Blob response, but the whole response body is buffered in memory anyways. Fetch, on the other had, supports streaming, although not in every browser.
Typed request builder can also be useful to developers who do not use Axios.
TL;DR
Current problems are:
We can not be independent of axios, because we need AxiosResponse generic.
Axios can not be peer dependency because TypeScript does not work well with peer dependencies. Not a problem anymore, was solved by also having it as dev dependency.
Details
To be independent of axios, we have to be able to arbitrarily wrap response types (e.g. Pet into AxiosResponse<Pet>).
But TypeScript does not support:
First class generics, e.g. generic as parameter for another generic.
typeWrap<T>={data: T};consttaxios=newTaxios<Api,Wrap>(agent);// error: Wrap is not a type
ReturnType of generic function.
typeWrap<T>={data: T};typeWrapper=<T>()=>Wrap<T>;typeWrappedNumber=ReturnType<Wrapper<number>>;// error: Wrapper is not a generic
So far I could not find a way around that.
The text was updated successfully, but these errors were encountered:
TLDR: I think axios is slowly becoming a "jQuery".
I think people use axios mostly because:
XMLHttpRequest is awful to work with. Fetch feels much better in comparison (but not perfect), and axios feels even better (but at a cost of not supporting web streaming).
Axios has interceptors for "global" behaviors, like logging, authentication and retry.
Native Response is too uncomfortable to work with.
I would like to argue that it's not really. Response and AxiosResponse have mostly the same interface, with only notable exceptions being:
AxiosResponse has config: AxiosConfig, which is mostly used in interceptors.
Response is returned before all data has arrived, so it has to be { json(): Promise<T> } instead of { data: T }. But I personally don't see much difference between taxios.get('/users').then(r => r.data) and taxios.get('/users').then(r => r.json()).
Motivation:
TL;DR
Current problems are:
AxiosResponse
generic.Axios can not be peer dependency because TypeScript does not work well with peer dependencies.Not a problem anymore, was solved by also having it as dev dependency.Details
To be independent of axios, we have to be able to arbitrarily wrap response types (e.g.
Pet
intoAxiosResponse<Pet>
).But TypeScript does not support:
ReturnType
of generic function.So far I could not find a way around that.
The text was updated successfully, but these errors were encountered: