Axios Wrapper
a wrapper that allows working with Axios to be less messy, more concise, clear, and functional.
Install
yarn add @rainxh11/axios-wrapper
# npm
npm install --save @rainxh11/axios-wrapper
Usage Example:
import { createAxios } from '@rainxh11/axios-wrapper'
const { client: httpClient, createRequestFn } = createAxios(instance => {
instance.interceptors.request.use(req => {
console.log(`${req.method}`, req.url)
req.headers.Authorization = `Bearer ${getSomeToken()}`
})
return instance
},
{
baseURL: 'https://random-data-api.com/api/v2',
headers: {
'Content-Type': 'application/json',
},
},
)
const getUsers = createRequestFn((client, { size }, signal) =>
client.get('/users', { params: { size }, signal }),
)
const users = await getUsers({ size: 2 })
import { useQuery } from 'react-query'
function UserListComponent() {
const [size, setSize] = useState(2)
const {
data,
isLoading,
} = size =>
useQuery(['users', size], ({ signal }) => getUsers({ size }, signal))
}