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

axios-watch-request

v1.0.0

Published

[Axios](https://github.com/axios/axios) adapter enhancer for observing request responses. **Requires an `Observable` implementation suitable for [any-observable](https://github.com/sindresorhus/any-observable)**.

Downloads

9

Readme

axios-watch-request :eyes:

Axios adapter enhancer for observing request responses. Requires an Observable implementation suitable for any-observable.

Build Status npm version

Install

yarn add axios-watch-request
npm install axios-watch-request

How does it work?

First, define your own axios adapter, or use the default adapter, axios.defaults.adapter. Then pass your adapter to the createEnhancedAdapter function:

import 'any-observable/register/zen'; // You can also use any Observable implementation suitable for any-observable
import createEnhancedAdapter from 'axios-watch-request';
import axios from 'axios';

const adapter = axios.defaults.adapter;

const { adapter: enhancedAdapter, watchRequest } = createEnhancedAdapter({
  adapter,
});

const client = axios.create({
  adapter: enhancedAdapter,
});

Now, you are ready watch any request by calling watchRequest, with your request's axios config object:

const observable = watchRequest({
  method: 'get',
  url: '/users/me',
});

observable.subscribe(({ loading, data, error }) => {
  // ...
});

client.get('/users/me')
  .then(() => {
    // The first result of this request will come directly from the cache, and the actual result when the response arrives
    client.get('/users/me');
  }};

The library utilizes simple cache and it operates by "cache and network" policy. This means, that when request's response is in the cache, it is passed to the observable. After that, the actual request is made, and eventually its response is cached and passed to the observable. You can disable the cache by calling createEnhancedAdapter with cache option as null.

Why do I need it?

In client applications, it is quite natural to subscribe to a certain data source and observe its changes. On top of that, it is convenient to able refetch data anywhere in the application, and pass the fresh data to everyone interested in it.

API

ConfigSerializer: (AxiosRequestConfig) => string

Function for serializing request config. Used by the default cache implementation and the adapter enhancer.

Cache: { set: (AxiosRequestConfig, any) => void, get: (AxiosRequestConfig) => any }

Cache implementation for createEnhancedAdapter.

createEnhancedAdapter({ adapter: AxiosAdapter, cache?: Cache, serializeConfig?: ConfigSerializer }): { adapter: AxiosAdapter, watchRequest: (AxiosRequestConfig) => Observable }

createEnhancedAdapter return an enhanced axios adapter, adapter and watchRequest function, which allows to subscribe to a request config. Setting cache option as null disables caching.

Tests

Run yarn test