npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rxios

v2.0.0

Published

A RxJS wrapper for axios

Downloads

94

Readme

Rxios

A RxJS wrapper for axios

npm CI bundlephobia

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

import { Rxios } from 'rxios';
const rxios = new Rxios({ /* options here */ })
const request = rxios.get(url)...

importing a "ready-to-use" generic instance

import { rxios } from 'rxios';
const request = rxios.get(url)...

In any case, please keep in mind that, when importing, Rxios refers to the class and rxios to the instance.

Syntax details

const { Rxios } = require('rxios');
// or import { Rxios } from 'rxios';

const http = new Rxios({
  // all regular axios request configuration options are valid here
  // check https://github.com/axios/axios#request-config
  baseURL: 'https://jsonplaceholder.typicode.com',
});

// plain GET request
http.get('/posts').subscribe(
  response => {
    console.log(response); // no need to 'response.data'
  },
  err => {
    console.error(err);
  }
);

// GET request with query params
http
  .get('/posts', { userId: 1 }) // you can pass an object as second param to the get() method
  .subscribe(
    response => {
      console.log(response); // no need to 'response.data'
    },
    err => {
      console.error(err);
    }
  );

// POST request
http
  .post('/posts', {
    // this object will be the payload of the request
    userId: 1,
    id: 1,
    title:
      'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
  })
  .subscribe(
    response => {
      console.log(response); // again, no need to 'response.data'
    },
    err => {
      console.error(err);
    }
  );

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:

import { Rxios } from 'rxios';
const http = new Rxios();
interface MyResponse = {userId: number; id: number; title: string};
http.get<MyResponse[]>('/posts/1')
  .subscribe(resp: 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.

import { Observable } from 'rxjs/Rx';
import { Rxios } from 'rxios';
const http = new Rxios();

const firstReq = http.get('/posts/1');
const secondReq = http.get('/posts/2');
firstReq.merge(secondReq).subscribe(...);