react-http-provider
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

react-http-provider

Library for Managing http requests with authentication API

Requires ...

  • react >= 18
  • react-redux >= 18
  • @reduxjs/toolkit > 1.9

Installation

write this command after download requires packages

npm install --save react-http-provider

Usage

for using the library after install the package you should follow theses steps

1 - create store file for storing authentication states and use { authStoreBuilder } to build the store

import {authStoreBuilder} from 'react-http-provider';

const {AuthProvider,loginAction,logoutAction,useAuthDispatch,useAuthStore} = authStoreBuilder<{id:string,userName:string,token:string}>({
  getToke() {
    return localStorage.getItem('token');
  },
  removeToken(){
    localStorage.removeItem('token');
  },
  setToken(token) {
    localStorage.setItem('token',token);
  },
});


export {
  AuthProvider,
  loginAction,
  logoutAction,
  useAuthDispatch,
  useAuthStore
}

2 - create useHttp file to build custome http hook

import { logoutAction, useAuthDispatch } from "auth/store";
import { httpProviderBuilder } from "react-http-provider";


const useHttp = httpProviderBuilder({
  baseUrl:'https://localhost:7160',
  defaultApplyError(error){
    console.error(error);
  },
  getToken(){
    return localStorage.getItem('token');
  },
  refreshToken(respones) {
    
  },
  dispatchHook:useAuthDispatch,
  logoutAction:logoutAction
});

export default useHttp;

Package Sidebar

Install

npm i react-http-provider

Weekly Downloads

0

Version

2.0.0

License

ISC

Unpacked Size

23.6 kB

Total Files

8

Last publish

Collaborators

  • basel_askar