0.2.13 • Published 3 years ago

axios-interceptor-hook v0.2.13

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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:

FieldsTypeDescription
dataObjectIt return the response of api (res.data)
isPendingBoolFor loading purpose return true while fetching and return false after completion or failure
apiHandlerFuncFunction 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

FieldsTypeDescription
methodstring'get', 'post', 'put', 'delete', 'patch'
urlstringit will be your endpoint
delaynumberdefault 5000
dataObjectrequired on post, put, patch requests
successMessagestring'Any Message'
errorMessagestring'Any Message'
positionstring'top-right', 'top-left', 'top-center', 'bottom-left', 'bottom-right', 'bottom-center'
hideProgressBarBooltrue or false
closeOnClickBooltrue or false
pauseOnHoverBooltrue or false
draggableBooltrue or false
themestring'light','dark','colored'

License

MIT

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago