rxjs-fetch
v2.1.6
Published
RxJS-flavored version of HTTP fetch API for node.js (for RxJS 5.0+)
Downloads
51
Readme
rxjs-fetch
RxJS-flavored version of HTTP fetch API for node.js.
IMPORTANT: This library only supports RxJS 5.x.
Looking for RxJS 4.x support? Try rx-fetch. (Same name but replace 'rxjs' with 'rx'.)
Built on top of isomorphic-fetch.
Warnings
- This adds
fetch
as a global so that its API is consistent between client and server. - You must bring your own ES6 Promise compatible polyfill. I suggest es6-promise.
Installation
NPM
npm install --save rxjs-fetch
Usage
const rxFetch = require('rxjs-fetch');
rxFetch('http://tangledfruit.com/mumble.txt')
.subscribe(
response => {
/*
Occurs exactly once. "response" is an Object with the following properties:
- status (Number): HTTP status code
- ok (Boolean): true if status < 400
- statusText (String): HTTP status string
- headers (Object): Maps HTTP headers in the response to string values
- url (String): URL that was requested.
"response" has the following methods:
- text: Returns another Observable which resolves with the response body
as a String when available.
- json: Returns another Observable which resolves with the response body
parsed as JSON (i.e. an Object or Array) when available.
It is an error to call more than one of these methods or to call any
of these methods more than once.
*/
},
err => {
console.log(err);
// Should only happen if unable to reach server.
// Server error responses (status code >= 400)
// are not automatically mapped to errors.
});
There are some shortcut methods available on the Observable object that is returned from rxFetch
:
rxFetch('http://tangledfruit.com/mumble.txt').failOnHttpError()
// -> This Observable will yield an error notification using the HttpError
// object described below if the HTTP status code is >= 400.
rxFetch('http://tangledfruit.com/mumble.txt').failIfStatusNotIn([200, 404])
// -> This Observable will yield an error notification using the HttpError
// object described below if the HTTP status code is anything other than the
// codes listed (in this case, 200 and 404).
rxFetch('http://tangledfruit.com/mumble.txt').text()
// -> This Observable will yield a next notification containing only the
// body text of the HTTP response. The HTTP headers and status are discarded.
// This call implies .failOnHttpError().
rxFetch('http://tangledfruit.com/mumble.txt').json()
// -> This Observable will yield a next notification containing only the
// body of the HTTP response parsed as JSON. The HTTP headers and status are
// discarded. This call implies .failOnHttpError().
const recording = new Rx.ReplaySubject(); // can be any Subject
rxFetch('http://tangledfruit.com/mumble.txt').recordTo(recording).json()
// -> Same as above, but it will capture the request and response and
// send it to the Subject in a syntax that can then be used to write unit
// tests using Nock. (See rxjs-fetch's own unit tests for an example.)
// You can also invoke this by adding recordTo: subject in the options
// object on the .get method.
HTTP Error object
The .failOnHttpError
and .failIfStatusNotIn
methods will send an error
notification with an HttpError
object. This is the standard Error
Object,
but it has an extra member response
from which you can access other properties
as described earlier.
The message for the error will be "HTTP Error (status code): (server message)". For example, "HTTP Error 404: Not Found".
License
MIT