This pull request introduces a custom useAxios hook to streamline API requests in our application. The hook integrates Auth0 for authentication and provides a unified interface for making authenticated HTTP requests.
Key features:
Automatic token handling with Auth0
Support for all the common HTTP methods (GET, POST, PUT, DELETE)
Built-in loading and error states
This aims to simplify API calls, reduce the amount of boilerplate code and ensure consistent authentication and error handling.
Changes Made
Created a new custom hook useAxios:
Implemented useCallback for memoized function calls (sendRequest)
Uses Auth0's getAccessTokenSilently for JWT verification
Uses axios for HTTP requests
Supports custom headers, request body and URL parameters
Implemented environment variable check for base url:
Created an env variable for the base_url including /api
Throws an error if the base url is not defined
Future Improvements:
Error Handling could be improved depending on type of error
Caching?
Retries for certain errors
Cancellation requests
Interceptors
Screenshots
No additional screenshots
Testing Instructions
No additional testing instructions
Special Notes for Your Reviewer(s)
The VITE_BASE_URL environment variable must be set for the hook to function correctly
Consider if we want to add additional features like request cancellation or automatic retries in future iterations
Related Issues
Closes #67
Linked Issue: #127
Summary
This pull request introduces a custom
useAxios
hook to streamline API requests in our application. The hook integrates Auth0 for authentication and provides a unified interface for making authenticated HTTP requests.Key features:
This aims to simplify API calls, reduce the amount of boilerplate code and ensure consistent authentication and error handling.
Changes Made
useAxios
:useCallback
for memoized function calls (sendRequest
)getAccessTokenSilently
for JWT verificationFuture Improvements:
Screenshots
No additional screenshots
Testing Instructions
No additional testing instructions
Special Notes for Your Reviewer(s)
VITE_BASE_URL
environment variable must be set for the hook to function correctly