@fetch-suit/react-axios-interceptor
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

React Axios Interceptor

A very simple axios interception solution for react

Installation

npm install @fetch-suit/react-axios-interceptor

How To Use

Create a interceptor component

import { createInterceptorComponent } from '@fetch-suit/react-axios-interceptor';

export const TimestampInterceptor = createInterceptorComponent({
  request: {
    onFulfilled: (config) => {
      config.params = {
        _t: Date.now().toString(),
        ...config.params,
      };
      return config;
    },
  },
});

export const BearerTokenInterceptor = createInterceptorComponent({
  request: {
    onFulfilled(config) {
      config.headers = {
        'Authorization': `Bearer fake.bearer.token`,
        ...config.headers,
      };
      return config;
    },
  },
});
import { useAxios } from '@fetch-suit/react-axios-interceptor';
import { PropsWithChildren, useEffect } from 'react';
import { BearerTokenInterceptor, TimestampInterceptor } from './interceptors';

function AxiosTest(props: PropsWithChildren<{ url: string }>) {
  const axios = useAxios();

  useEffect(
    () => {
      const controller = new AbortController();
      axios.get(props.url, { signal: controller.signal })
        .then(response => {})
        .catch(err => console.log(err));
      return () => controller.abort();
    },
    [axios],
  );

  return null;
}

export default function App() {
  return (
    <TimestampInterceptor>
      <AxiosTest url='/test1' />
      <AxiosTest url='/test3' />
      <BearerTokenInterceptor>
        <AxiosTest url='/test2' />
      </BearerTokenInterceptor>
    </TimestampInterceptor>
  );
}

Full example: click here

/@fetch-suit/react-axios-interceptor/

    Package Sidebar

    Install

    npm i @fetch-suit/react-axios-interceptor

    Weekly Downloads

    1

    Version

    0.0.6

    License

    UNLICENSED

    Unpacked Size

    6.02 kB

    Total Files

    10

    Last publish

    Collaborators

    • hungtcs