/**
* Module: Axios HTTP Request Utility
* This module provides functions for sending HTTP requests to the backend API
* using Axios, a promise-based HTTP client for the browser and Node.js.
*
* @module axios
*/
import axios from 'axios';
/**
* Base URL for the backend API.
* @constant {string}
*/
const BASE_URL = 'http://localhost:3001/api';
/**
* Sends a request to the backend API.
* @async
* @function sendRequest
* @param {string} method - The HTTP method for the request (e.g., GET, POST, PUT, DELETE).
* @param {string} path - The endpoint path for the request.
* @param {Object} [data] - The data to be sent with the request (optional).
* @returns {Promise<Object>} A promise that resolves to an object containing the request status and data.
*/
const sendRequest = async(method, path, data) => {
try {
const response = await axios({
method,
url: `${BASE_URL}${path}`,
data,
validateStatus: status => status < 500
});
return { success: response.status >= 200 && response.status < 300, data: response.data };
} catch (error) {
console.error(method + ' request to ' + path + ' failed:', error.response ? error.response.data : error.message);
return { success: false, error: error.response ? error.response.data : 'An error occurred while trying to send the request' };
}
};
/**
* Signs the user out by sending a request to the signout endpoint.
* @async
* @function signOut
* @returns {Promise<Object>} A promise that resolves to an object indicating the success or failure of the sign-out operation.
*/
const signOut = async() => {
try {
const token = localStorage.getItem('token'); // Retrieve the token from local storage
console.log(token)
const response = await axios.post(BASE_URL + '/signout', null, {
headers: {
Authorization: `Bearer ${token}` // Include the token in the authorization header
}
});
if (response.data.success) {
localStorage.removeItem('token');
localStorage.removeItem('user');
return { success: true, message: response.data.message };
} else {
return { success: false, error: response.data.message };
}
} catch (error) {
console.error('Error signing out:', error);
return { success: false, error: 'An error occurred while signing out' };
}
};
/**
* Sends authenticated requests to the backend API using the user's token.
* @async
* @function todoApi
* @param {string} method - The HTTP method for the request.
* @param {string} path - The endpoint path for the request.
* @param {Object} [data] - The data to be sent with the request (optional).
* @returns {Promise<Object>} A promise that resolves to an object containing the request status and data.
*/
const todoApi = async(method, path, data) => {
try {
const token = localStorage.getItem('token'); // Retrieve the token from local storage
console.log(token)
const response = await axios({
method,
url: `${BASE_URL}/${path}`,
data,
headers: {
Authorization: `Bearer ${token}` // Include the token in the authorization header
},
validateStatus: status => status < 500
});
return { success: response.status >= 200 && response.status < 300, data: response.data };
} catch (error) {
console.error(method + ' request to ' + path + ' failed:', error.response ? error.response.data : error.message);
return { success: false, error: error.response ? error.response.data : 'An error occurred while trying to send the request' };
}
};
export { sendRequest, signOut, todoApi };