Axios Goose is a lightweight and simple library that wraps around Axios for making HTTP requests to a RESTful API. This library is designed to streamline the process of managing authorization tokens (access tokens and refresh tokens) and offers a convenient interface for making various types of HTTP requests.
To install Axios Goose, you can use npm:
npm install axios-goose
Below is an example of how to use axios-goose in your project:
Import Axios Goose in your project.
import { Axios } from 'axios-goose';Initialize Axios Goose before making any requests.
const isAuthReq = true; // Set this to true if you are making authorized requests
const baseURL = 'https://api.example.com'; // Optionally set the baseURL
const axiosInstance = new Axios(isAuthReq, baseURL);isAuthReq is a boolean that determines whether the requests should contain an Authorization header. You should provide a baseURL which will be used as the base URL for all the HTTP requests made using this axios instance.
To make a simple GET request:
axiosInstance.get('/api/endpoint');To make a GET request with query parameters:
axiosInstance.getByQuery('/api/endpoint', {
param1: 'value1',
param2: 'value2',
});To make a GET request with path parameters:
axiosInstance.getByParams('/api/endpoint', 'param-value');To make a POST request:
axiosInstance.post('/api/endpoint', { dataKey: 'dataValue' });To make a POST request with multipart/form-data:
const formData = new FormData();
formData.append('key', 'value');
axiosInstance.postMultipartFormData('/api/endpoint', formData);To make a PUT request:
axiosInstance.put('/api/endpoint', { dataKey: 'dataValue' }, optionalID);To make a PATCH request:
axiosInstance.patch('/api/endpoint', { dataKey: 'dataValue' });To make a DELETE request:
axiosInstance.delete('/api/endpoint', itemID);This library automatically handles access tokens and refresh tokens. Tokens are stored in cookies and are automatically attached to authorized requests. When an access token expires, it attempts to refresh the token using the refresh token.
The library expects that the backend responds with a
1002 status codewhen the access token is expired.
In order to enable automatic token refreshing functionality, the backend should provide an endpoint for token refreshing.
Note that this library uses a fixed path
/api/auth/refresh-tokenfor token refreshing.
Please ensure that your backend implements an API endpoint at the following path for handling refresh token requests:
/api/auth/refresh-tokenThis endpoint should accept a POST request with a refresh token, and it should return a new access token if the provided refresh token is valid.
- HTTP Method:
POST - Endpoint URL:
/api/auth/refresh-token
{
"refreshToken": "<your-refresh-token>"
}{
"accessToken": "<your-new-access-token>"
}Please configure your backend service to provide this endpoint with the above specifications to ensure that token refresh works seamlessly with this library.