-
Notifications
You must be signed in to change notification settings - Fork 338
feat(ajax): use fetch as a fundament instead of axios #1147
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
Conversation
🦋 Changeset detectedLatest commit: 5bfa13a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@jorenbroekema this fetch-based http client implements transformers as a way to modify the requests/responses, while The main difference is that interceptors are asynchronous and transformers are synchronous, which may break some use cases around |
|
@rodrigo-garcia-leon good point. We have decided to rename transformers to interceptors. Since request / requestJson are both already async (because fetch is async), we can easily make it a requirement for the interceptors to also be async functions without much effect for the end user. Then it's closer to axios interceptors and makes migration easier as well. Thoughts? |
|
Sounds like a good approach, by making the interceptors async I think all possible use cases should be covered |
|
Making them async makes sense. Note that there is another difference in that axios interceptors can actually avoid doing the actual fetch and return a response directly. Currently the request transformers don't cater for that. Probably it makes sense to have a separate property for "replacing" the actual fetch call... because for those you do still want both the request and response transforms to apply I think. |
|
@LarsDenBakker https://github.com/ing-bank/lion/pull/1147/files#diff-e854d17b9a6c1d34ce659f4bb1e4c650fb31bade36d8a7d582e8ee42272f2f9aR101 I thought this was returning the Response directly and not doing the fetch call? |
|
oh.. you're right. I forgot we did that. 👍 |
77f842d to
5d5542c
Compare
6f8e09c to
e0d09bb
Compare
02f38e4 to
5bfa13a
Compare
What I did
TODOs
Rationale
Redaxios :(
We wanted to use redaxios since it's smaller, uses fetch and keeps the axios API, prevents breaking change.
However:
Interceptors
Interceptors are functions that get executed before the request is sent or before the response is resolved. This is actually precisely what Lars' response/requestTransformers do in this package 🎉 . So it's supported and with a pretty nice API.
Extras
Some cool extra features that I don't think we had before (but not sure)
Response.text()+fetchall into one call instead of doing double await like you normally have to do with fetchdatato requestJson calls directly, also handles content-type. Parses the Response as JSON as well. Very handy for APIs that expect/return JSON encoded data, don't have to do the encoding/decoding yourself.