Renamed axiosConfig to NetworkService

This commit is contained in:
Alex Holliday committed 2024-08-18 12:11:24 -07:00
1 parent 6d99002fd1
commit 5beb0684a7
14 files changed
+13 -13

No files matched your search

+154
View File
@@ -0,0 +1,154 @@
import axios from "axios";
import { clearAuthState } from "../Features/Auth/authSlice";
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
let store;
export const injectStore = (s) => {
store = s;
};
const axiosInstance = axios.create({
baseURL: BASE_URL,
});
axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
console.error(error);
if (error.response && error.response.status === 401) {
console.log("Invalid token revoked");
store.dispatch(clearAuthState());
}
return Promise.reject(error);
}
);
// **********************************
// Create a new monitor
// **********************************
axiosInstance.createMonitor = async (authToken, monitor) => {
return axiosInstance.post(`/monitors`, monitor, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Get all uptime monitors for a user
// **********************************
axiosInstance.getMonitorsByUserId = async (
authToken,
userId,
limit,
types,
sortOrder,
normalize
) => {
const params = new URLSearchParams();
if (limit) params.append("limit", limit);
if (types) {
types.forEach((type) => {
params.append("type", type);
});
}
if (sortOrder) params.append("sortOrder", sortOrder);
if (normalize) params.append("normalize", normalize);
return axiosInstance.get(`/monitors/user/${userId}?${params.toString()}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Updates a single monitor
// **********************************
axiosInstance.updateMonitor = async (authToken, monitorId, updatedFields) => {
return axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Deletes a single monitor
// **********************************
axiosInstance.deleteMonitorById = async (authToken, monitorId) => {
return axiosInstance.delete(`/monitors/${monitorId}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Register a new user
// **********************************
axiosInstance.registerUser = async (form) => {
return axiosInstance.post(`/auth/register`, form);
};
// **********************************
// Log in an exisiting user
// **********************************
axiosInstance.loginUser = async (form) => {
return axiosInstance.post(`/auth/login`, form);
};
// **********************************
// Update in an exisiting user
// **********************************
axiosInstance.updateUser = async (authToken, userId, form) => {
return axiosInstance.put(`/auth/user/${userId}`, form, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Delete an exisiting user
// **********************************
axiosInstance.deleteUser = async (authToken, userId) => {
return axiosInstance.delete(`/auth/user/${userId}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Forgot password request
// **********************************
axiosInstance.forgotPassword = async (form) => {
return axiosInstance.post(`/auth/recovery/request`, form);
};
axiosInstance.validateRecoveryToken = async (recoveryToken) => {
return axiosInstance.post("/auth/recovery/validate", {
recoveryToken,
});
};
// **********************************
// Set new password request
// **********************************
axiosInstance.setNewPassword = async (recoveryToken, form) => {
return axiosInstance.post("/auth/recovery/reset", {
...form,
recoveryToken,
});
};
export default axiosInstance;