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
[FEATURE] http requests snippets #514
Conversation
Added handling for `onerror` to log on the `error` stream of the console by default, maybe not the best possible behavior, but good for starters. If you have a better idea for this, please change it accordingly.
hopefully last typos I made
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On first sight, these seem completely fine. I will run a couple of tests tomorrow morning with live APIs that I have keys for, as well as a custom implementation I have set up locally and tell you if anything is broken (which should not be). Otherwise, we are ready to merge. Thanks a bunch for adding these in!
tag_database
Outdated
httpDelete:utility,url | ||
httpGet:utility,url | ||
httpPost:utility,url | ||
httpPut:utility,url |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Added browser to every http request
Why are we implementing these? |
@skatcat31 Everyone makes requests 💯 The new people in JS world might find Promises and |
@fejes713 not disagreeing everyone will need to make remote requests at some point. I do however want to point out that both libraries are confusing becasue they are trying to abstract the network layer and that promises have nothing to do with the complexity of learning network communications. If they can't understand promises that is a totally different issue. After all I can implement the same footprint with fetch too: const httpGET = (url, callback, err = console.error) =>
fetch(url, { method: 'GET' }).then( (res,error) => error ? err(error) : callback(res)) and neither implementation actually fixes the problem of having to learn how to handle the response. Your example makes an assumption, but truth is that it's still a request fulfillment object. To handle the response you would need to learn either library to know what the next step is which your code doesn't address and expects them to know. |
@skatcat31 Handling the response is a bit of an issue, agreed. Providing a link to the MDN documentation could help some people that grab a snippet from here kickstart their development process and not get lost. It's either that, explaining in depth how to do so (which is way out of scope for 30 seconds) or not adding the snippets altogether. I think providing links and adding an |
@Chalarangelo that's the same as implementing it with fetch and tell them to go read the fetch documentation. Either way our code hasn't actually solved anything. They still need to go read the documentation... This is a hard subject to approach due to the complex nature of network requests and how many infinite ways there are to implement it, most ending in full blown libraries due to response handling. Hence why I'm for the third option since we can't even scratch the surface without going past the scope of the repo. After all if they're misunderstanding promises we have solved nothing since now they're still confused about how to do network requests and have to read a library to advance, and still confused on how promises work. |
I am putting |
I don't know why but I feel like implementing these are wrong. I think maybe It's related to that we may confuse people as to why they should use third party packages like |
ExplanationI was in the same boat a few months ago, I had to make a get request but I didn't know how to do it, back then the first thing that I saw when I googled it was Not all users that come to our website will have the same skill level, however, we should provide learning material for everyone. Personally, I wouldn't use As mentioned in every 3rd PR, we are making the collection of snippets and we're not trying to make a library, so it is perfectly fine for me to include both |
@fejes713 and I also feel bad cause |
@Chalarangelo 5+ params is generally discouraged as it makes it unwieldy to call. Usually you just use an object in that case |
@fejes713 That would simplify things but what if a user sends |
@atomiks I completely agree with you, it's just that using an options object is something I forget how to do at times, so I just left it that way. I am all for making the only necessary arguments the |
|
@atomiks Don't get me wrong, I prefer promises when I have the chance to use them, even though I am not very confident in my abilities to utilize them all the time. However, teaching two difficult things in one snippet will definitely confuse people. |
After giving this some thought, I tried to simplify my above suggestion, based on the following:
const httpRequest = (verb, url, callback, data = null, err = console.error) => {
if(!['GET', 'PUT', 'POST', 'DELETE'].includes(verb.toUpperCase()))
throw new Error('Verb not supported!');
const request = new XMLHttpRequest();
request.open(verb.toUpperCase(), url, true);
request.setRequestHeader('Content-type','application/json; charset=utf-8');
request.send(data);
request.onload = () => callback(request);
request.onerror = () => err(request);
}; |
@Chalarangelo Well ... |
@fejes713 Apart from that, I think I have seen APIs that kinda want you to send data in a |
@Chalarangelo What should we do then? |
We need to vote. I believe there's value in exposing developers to both @fejes713 @atomiks @iamsoorena @skatcat31 @kingdavidmartins cast your votes please (everyone else is also welcome, I am just tagging some people to notify them). |
I vote to close due to complexity of implementation. We would almost be guaranteed to need to deference an object in the arguments to keep from involving an anti pattern( current implementation has the anti pattern in it's footprint ) If we do do this we should resolve the anti pattern by changing the footprint to However at that point this becomes a fairly long block of code requiring checking against the keys in the object and definition to make sure requests are correctly setup. If we do want to do a simple GET or POST example(s) I am more open to that and leaving the DELETE, PUT, OPTIONS, and HEADER verbs out of the repo in either XMLHTTPRequest OR Fetch |
@skatcat31 Actually |
To be fair I am electing for SIMPLE I do still think XMLHTTPRequest should be avoided but that's only my personal opinion. I will admit many people have a hard time using promises, but I think that's because they aren't used to async/event based programming and many of those same people don't understand the full ramifications of callbacks or |
Is there any danger of Simple |
If only get and post are included as requests, there can either be two snippets or one that defaults to get. My opinion is that http requests are useful learning resource and they could help new developers. The footprint is long however, so something like As far as the response is concerned, |
I think that just passing the My suggestion is that we update all snippets we currently have in this PR to do that, archive |
@mariastervic Thanks for suggestions and helping us solve this problem. @Chalarangelo I will update this PR in an approximately 2 hours, as soon as finish writing tests. |
@Chalarangelo time to close this finally. I've moved |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks. |
Description
I've added basic
CRUD
calls. They are implemented usingXMLHttpRequest
web API.In case someone missed the discussion on gitter earlier today this is the reason we are adding those snippets:
I think I've done everything correctly. I would like someone to review changes and descriptions before we merge this. I had few problems with Git but everything should be fine.
What does your PR belong to?
Types of changes
Checklist: