

Show HN: Xr – A modern ES6 Promise-based XMLHttpRequest wrapper - antihero
https://github.com/radiosilence/xr

======
vjeux
You should look at `fetch` which is the new specification for a promise based
XHR wrapper.

Polyfill: [https://github.com/github/fetch](https://github.com/github/fetch)

To use Fetch in Firefox (38), you'll currently need to enable it — go to
about:config, set dom.fetch.enabled to true, and restart the browser. To use
Fetch in Chrome 41/Opera 28, you need to enable the "Experimental Web Platform
Features" pref in chrome://flags.

~~~
VeejayRampay
Looks awesome, though the name fetch does work better for GET requests than
for POST/PUT/PATCH/DELETE.

~~~
gobengo
Every HTTP request, regardless of method, is implicitly 'fetch'ing an HTTP
response.

------
antihero
Hello, author here. I thought I'd make this because I couldn't find a library
that was tiny, and opinionated about using promises and sane defaults (JSON
headers, for instance), and exposed the bits of XMLHttpRequest that I wanted.
It actually does very little, but provides some nice little helpers to do AJAX
requests. Everything is override-able. I'm toying with even making the basic
Object.assign and Promise calls injectable too (see this branch:
[https://github.com/radiosilence/xr/blob/compat/src/xr.js](https://github.com/radiosilence/xr/blob/compat/src/xr.js))
but it makes the code a bit uglier.

The other handy feature is that it will use whichever Promise library that's
available in the global namespace, so as long as that conforms to the spec, it
will happily work with it.

Also, title should be lower case, as with most node modules.

~~~
pseudobry
Have you seen
[https://github.com/mzabriskie/axios](https://github.com/mzabriskie/axios)?

~~~
antihero
Haven't seen it, no. Looks neat, but a bit more complex than what I was going
for. This is basically a nicer interface to XHR, intended for people who are
happy to read up on the XHR API and use that for anything more complicated.

------
pimlottc
> * Rejects if not 200.

There are other valid 20x return codes that indicate success, such as 202
Accepted and 204 No Content.

~~~
glenjamin
The error logic I tend to use for HTTP status codes is >= 400 as those are
explicitly marked as errors in the spec.

------
bsimpson
XHR (XML HTTP Request) always seemed like a sadly particular API name for
something that's rarely used to consume XML. From that point-of-view, HR seems
like a better name than XR. ;-)

~~~
antihero
True, but I figured having a two letter function was kinda useful, and HR
means other things. Plus, it's a light wrapper for XHR, so hey.

Another nice side effect is that if you use 2-spaces then your promise .then
lines up with the initial call...

    
    
        xr.get()
          .then(...)

------
breuleux
Pretty cool, but I have a nitpick: reject() should be called when the loader
throws an exception (e.g. if the response is malformed JSON).

~~~
antihero
If the loader gets malformed JSON it should throw an exception, which should
be seen as a rejection, I think.

------
rayshan
What happens after .then? Does xhr get GC'd? Would the inner xhr be considered
out of scope?

~~~
antihero
Ask your JS VM and promise implementation :)

