2024-07-09 11:45:14 -07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
// Create a new monitor
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
axiosInstance.createMonitor = async (authToken, monitor) => {
|
|
|
|
|
return axiosInstance.post(`/monitors`, monitor, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
// Get all uptime monitors for a user
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
axiosInstance.getMonitorsByUserId = async (
|
|
|
|
|
authToken,
|
|
|
|
|
userId,
|
|
|
|
|
limit,
|
|
|
|
|
types,
|
|
|
|
|
sortOrder,
|
|
|
|
|
normalize
|
|
|
|
|
) => {
|
2024-08-17 09:50:52 -07:00
|
|
|
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",
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-08-17 09:35:50 -07:00
|
|
|
};
|
|
|
|
|
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
// Updates a single monitor
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
axiosInstance.updateMonitor = async (authToken, monitorId, updatedFields) => {
|
|
|
|
|
return axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
// Deletes a single monitor
|
2024-08-17 12:55:57 -07:00
|
|
|
// **********************************
|
2024-08-17 09:35:50 -07:00
|
|
|
axiosInstance.deleteMonitorById = async (authToken, monitorId) => {
|
|
|
|
|
return axiosInstance.delete(`/monitors/${monitorId}`, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-17 13:03:24 -07:00
|
|
|
// **********************************
|
|
|
|
|
// 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);
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-17 20:23:34 -07:00
|
|
|
// **********************************
|
|
|
|
|
// 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,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-18 12:20:28 -07:00
|
|
|
// **********************************
|
|
|
|
|
// Check for admin user
|
|
|
|
|
// **********************************
|
|
|
|
|
axiosInstance.doesAdminExist = async () => {
|
|
|
|
|
return axiosInstance.get("/auth/users/admin");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// **********************************
|
|
|
|
|
// Get all users
|
|
|
|
|
// **********************************
|
|
|
|
|
axiosInstance.getAllUsers = async (authToken) => {
|
|
|
|
|
return axiosInstance.get("/auth/users", {
|
|
|
|
|
headers: { Authorization: `Bearer ${authToken}` },
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// **********************************
|
|
|
|
|
// Verify Invitation Token
|
|
|
|
|
// **********************************
|
|
|
|
|
axiosInstance.verifyInvitationToken = async (token) => {
|
|
|
|
|
return axiosInstance.post(`/auth/invite/verify`, {
|
|
|
|
|
token,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-09 11:45:14 -07:00
|
|
|
export default axiosInstance;
|