Rxios
A RxJS wrapper for axios
Rxios makes the awesome axios library reactive, so that it's responses are returned as RxJS observables.
Observables? Why?
Regular promises are cool, especially for HTTP requests in async/await functions.
However, Observables provide operators like map, forEach, reduce... There are also powerful operators like retry()
or replay()
, that are often quite handy.
Observables also excel when we need to perform some kind of manipulation on the received data, or when we need to chain several requests.
Lastly, Reactive stuff is what all the cool kids are doing.
Installation
npm install axios rxjs rxios
Usage
You can use Rxios by either
instantiating the class yourself
;const rxios = /* options here */ const request = rxios...
importing a "ready-to-use" generic instance
;const request = rxios...
In any case, please keep in mind that, when importing, Rxios
refers to the class and rxios
to the instance.
Syntax details
const Rxios = ;// or import { Rxios } from 'rxios'; const http = // all regular axios request configuration options are valid here // check https://github.com/axios/axios#request-config baseURL: 'https://jsonplaceholder.typicode.com'; // plain GET requesthttp; // GET request with query paramshttp // you can pass an object as second param to the get() method ; // POST requesthttp ;
TypeScript usage
Rxios is written in TypeScript, and its typings are provided in this same package.
Also, just like with axios or with Angular's Http module, response types are accepted by the method, like:
;;;http.get'/posts/1' .subscriberesp: MyResponse ;
Advanced usage
All Rxios methods always return an Observable, to which we can apply advanced RxJS operations.
For example, we could make two simultaneous requests and merge their responses as they come, without needing to wait for both to be completed.
;;const http = ; const firstReq = http;const secondReq = http;firstReq;