|
| ▲ | arghwhat 11 hours ago | parent | next [-] |
| Interceptors are just wrappers in disguise. const myfetch = async (req, options) => {
let options = options || {};
options.headers = options.headers || {};
options.headers['Authorization'] = token;
let res = await fetch(new Request(req, options));
if (res.status == 401) {
// do your thing
throw new Error("oh no");
}
return res;
}
Convenience is a thing, but it doesn't require a massive library. |
| |
| ▲ | nailer 10 hours ago | parent | next [-] | | That fetch requires so many users to rewrite the same code - that was already handled well by every existing node HTTP client- says something about the standards process. | | |
| ▲ | arghwhat 10 hours ago | parent [-] | | It could also be trivially written for XMLHttpRequest or any node client if needed. Would be nice if they had always been the same, but oh well - having a server and client version isn't that bad. Because it is so few lines it is much more sensible to have everyone duplicate that little snippet manually than import a library and write interceptors for that... (Not only because the integration with the library would likely be more lines of code, but also because a library is a significantly liability on several levels that must be justified by significant, not minor, recurring savings.) | | |
| ▲ | nailer 5 hours ago | parent [-] | | > Because it is so few lines it is much more sensible to have everyone duplicate that little snippet manually Mine's about 100 LOC. There's a lot you can get wrong. Having a way to use a known working version and update that rather than adding a hundred potentially unnecessary lines of code is a good thing. https://github.com/mikemaccana/fetch-unfucked/blob/master/sr... > import a library and write interceptors for that... What you suggesting people would have to intercept? Just import a library you trust and use it. | | |
| ▲ | reactordev 4 hours ago | parent [-] | | But you said so yourself they are necessary… otherwise you would just use fetch. This reasoning is going around in circles. | | |
| ▲ | nailer 4 hours ago | parent [-] | | Why the 'but'? Where is the circular reasoning? What are you suggesting we have to intercept? - Don't waste time rewriting and maintaining code unecessarily. Install a package and use it. - Have a minimum release age. I do not know what the issue is. |
|
|
|
| |
| ▲ | 11 hours ago | parent | prev | next [-] | | [deleted] | |
| ▲ | pixel_popping 11 hours ago | parent | prev [-] | | but it does for massive DDoS :p |
|
|
| ▲ | rjmunro 9 hours ago | parent | prev | next [-] |
| > Likewise, every response I get is JSON. fetch responses have a .json() method. It's literally the first example in MDN: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/U... It's literally easier than not using JSON because I have to think about if I want `repsponse.text()` or `response.body()`. |
|
| ▲ | abluecloud 9 hours ago | parent | prev | next [-] |
| that's such a weak argument. you can write about 20 lines of code to do exactly this without requiring a third party library. |
|
| ▲ | anon7000 14 hours ago | parent | prev | next [-] |
| Helper functions seem trivial and not like you’re reimplementing much. |
| |
| ▲ | creshal 11 hours ago | parent [-] | | Don't be silly, this is the JS ecosystem. Why use your brain for a minute and come up with a 50 byte helper function, if you can instead import a library with 3912726 dependencies and let the compiler spend 90 seconds on every build to tree shake 3912723 out again and give you a highly optimized bundle that's only 3 megabytes small? |
|
|
| ▲ | sayamqazi 13 hours ago | parent | prev | next [-] |
| > usage patterns IMO interceptors are bad. they hide what might get transformed with the API call at the place it is being used. > Likewise, every response I get is JSON. There's no reason to manually unwrap the response into JSON every time. This is not true unless you are not interfacing with your own backends. even then why not just make a helper that unwraps as json by default but can be passed an arg to parse as something else |
|
| ▲ | hiccuphippo 7 hours ago | parent | prev [-] |
| One more use case for Axios is it automatically follows redirects, forwarding headers, and more importantly, omiting or rewriting the headers that shouldn't be forwarded for security reasons. |
| |
| ▲ | reactordev 3 hours ago | parent [-] | | fetch automatically follows redirects, fetch will forward your headers, omitting or rewriting headers is how security breaks… now a scraper got through because it’s masquerading as Chrome. |
|