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-interceptor-hook

v0.2.13

Published

Axios Interceptor Hook

Downloads

89

Readme

axios-interceptor-hooks

npm

React hooks for axios with built in react-tostify integration. Simple to use with minimum configuration.

axios-interceptor-hook

Features

  • All the axios awesomeness you are familiar with
  • Zero configuration, but configurable if needed
  • Integrated react-toastify for better toast messages
  • Minimize file managment

Installation

npm install axios react-toastify axios-interceptor-hook

axios and react-toastify are peer dependencies and needs to be installed explicitly

Example

import { ToastContainer } from 'react-toastify';
import { useAxiosInterceptor } from 'axios-interceptor-hook';
import { useEffect } from 'react';
import 'react-toastify/dist/ReactToastify.css';

function App() {
  const { data, isPending, apiHandler } = useAxiosInterceptor();
  // with toast message
  const config = {
    method: 'get',
    url: '/todos',
    delay: 4000,
    displayToast: true,
    successMessage: 'Todos data retrieve',
    errorMessage: 'Todos data fetch error',
    toastPosition: 'top-right',
    hideProgressBar: false,
    closeOnClick: true,
    pauseOnHover: true,
    draggable: true,
    theme: 'colored',
  };

  // without toast message
  // const config = {
  //   method: 'get',
  //   url: '/todos',
  //   delay: 4000,
  //   displayToast: false
  // }

  const getTodosData = async () => {
    await apiHandler(config);
  };

  useEffect(() => {
    getTodosData();
  }, []);

  return (
    <div>
      <ToastContainer />
      {isPending && <p>Loading.......</p>}
      {data &&
        Object.keys(data).length > 0 &&
        data?.map((todo, index) => (
          <h6 key={index + 'testing'}>{todo.title}</h6>
        ))}
    </div>
  );
}

export default App;

Documentation

Return

It will return following fields:

| Fields | Type | Description | | ---------- | :----: | --------------------------------------------------------------------------------------------: | | data | Object | It return the response of api (res.data) | | isPending | Bool | For loading purpose return true while fetching and return false after completion or failure | | apiHandler | Func | Function to give you control over calling when you need just by passing config |

For Bearer Token

Need to save your auth token as, interceptor will automatically get it.

localStorage.setItem('authToken', <YOUR TOKEN>);

Environment Variable for Base URL

Please add env variable into your .env or .env.local file.

REACT_APP_BASE_URL="https://jsonplaceholder.typicode.com"

Config Props

| Fields | Type | Description | | --------------- | :----: | ------------------------------------------------------------------------------------: | | method | string | 'get', 'post', 'put', 'delete', 'patch' | | url | string | it will be your endpoint | | delay | number | default 5000 | | data | Object | required on post, put, patch requests | | successMessage | string | 'Any Message' | | errorMessage | string | 'Any Message' | | position | string | 'top-right', 'top-left', 'top-center', 'bottom-left', 'bottom-right', 'bottom-center' | | hideProgressBar | Bool | true or false | | closeOnClick | Bool | true or false | | pauseOnHover | Bool | true or false | | draggable | Bool | true or false | | theme | string | 'light','dark','colored' |

License

MIT