mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-05-05 16:09:48 -05:00
Refactored axios util into a service
This commit is contained in:
+245
-245
@@ -2,273 +2,273 @@ 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);
|
||||
class NetworkService {
|
||||
constructor(store) {
|
||||
this.store = store;
|
||||
this.axiosInstance = axios.create({ baseURL: BASE_URL });
|
||||
this.axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
console.error(error);
|
||||
if (error.response && error.response.status === 401) {
|
||||
console.log("Invalid token revoked");
|
||||
networkService;
|
||||
}
|
||||
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,
|
||||
status,
|
||||
sortOrder,
|
||||
normalize
|
||||
) => {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (limit) params.append("limit", limit);
|
||||
if (types) {
|
||||
types.forEach((type) => {
|
||||
params.append("type", type);
|
||||
// **********************************
|
||||
// Create a new monitor
|
||||
// **********************************
|
||||
async createMonitor(authToken, monitor) {
|
||||
return this.axiosInstance.post(`/monitors`, monitor, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (status) params.append("status", status);
|
||||
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",
|
||||
},
|
||||
});
|
||||
};
|
||||
// **********************************
|
||||
// Get all uptime monitors for a user
|
||||
// **********************************
|
||||
async getMonitorsByUserId(
|
||||
authToken,
|
||||
userId,
|
||||
limit,
|
||||
types,
|
||||
status,
|
||||
sortOrder,
|
||||
normalize
|
||||
) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
// **********************************
|
||||
// Get stats for a monitor
|
||||
// **********************************
|
||||
axiosInstance.getStatsByMonitorId = async (
|
||||
authToken,
|
||||
monitorId,
|
||||
sortOrder,
|
||||
limit,
|
||||
dateRange,
|
||||
numToDisplay,
|
||||
normalize
|
||||
) => {
|
||||
const params = new URLSearchParams();
|
||||
if (sortOrder) params.append("sortOrder", sortOrder);
|
||||
if (limit) params.append("limit", limit);
|
||||
if (dateRange) params.append("dateRange", dateRange);
|
||||
if (numToDisplay) params.append("numToDisplay", numToDisplay);
|
||||
if (normalize) params.append("normalize", normalize);
|
||||
if (limit) params.append("limit", limit);
|
||||
if (types) {
|
||||
types.forEach((type) => {
|
||||
params.append("type", type);
|
||||
});
|
||||
}
|
||||
if (status) params.append("status", status);
|
||||
if (sortOrder) params.append("sortOrder", sortOrder);
|
||||
if (normalize) params.append("normalize", normalize);
|
||||
|
||||
return axiosInstance.get(
|
||||
`/monitors/stats/${monitorId}?${params.toString()}`,
|
||||
{
|
||||
return this.axiosInstance.get(
|
||||
`/monitors/user/${userId}?${params.toString()}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Get stats for a monitor
|
||||
// **********************************
|
||||
async getStatsByMonitorId(
|
||||
authToken,
|
||||
monitorId,
|
||||
sortOrder,
|
||||
limit,
|
||||
dateRange,
|
||||
numToDisplay,
|
||||
normalize
|
||||
) {
|
||||
const params = new URLSearchParams();
|
||||
if (sortOrder) params.append("sortOrder", sortOrder);
|
||||
if (limit) params.append("limit", limit);
|
||||
if (dateRange) params.append("dateRange", dateRange);
|
||||
if (numToDisplay) params.append("numToDisplay", numToDisplay);
|
||||
if (normalize) params.append("normalize", normalize);
|
||||
|
||||
return this.axiosInstance.get(
|
||||
`/monitors/stats/${monitorId}?${params.toString()}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Updates a single monitor
|
||||
// **********************************
|
||||
async updateMonitor(authToken, monitorId, updatedFields) {
|
||||
return this.axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
// **********************************
|
||||
// Deletes a single monitor
|
||||
// **********************************
|
||||
async deleteMonitorById(authToken, monitorId) {
|
||||
return this.axiosInstance.delete(`/monitors/${monitorId}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
// **********************************
|
||||
// Get certificate
|
||||
// **********************************
|
||||
async getCertificateExpiry(authToken, monitorId) {
|
||||
return this.axiosInstance.get(`/monitors/certificate/${monitorId}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Updates a single monitor
|
||||
// **********************************
|
||||
axiosInstance.updateMonitor = async (authToken, monitorId, updatedFields) => {
|
||||
return axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
};
|
||||
// **********************************
|
||||
// Register a new user
|
||||
// **********************************
|
||||
async registerUser(form) {
|
||||
return this.axiosInstance.post(`/auth/register`, form);
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Deletes a single monitor
|
||||
// **********************************
|
||||
axiosInstance.deleteMonitorById = async (authToken, monitorId) => {
|
||||
return axiosInstance.delete(`/monitors/${monitorId}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
};
|
||||
// **********************************
|
||||
// Log in an exisiting user
|
||||
// **********************************
|
||||
async loginUser(form) {
|
||||
return this.axiosInstance.post(`/auth/login`, form);
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Get certificate
|
||||
// **********************************
|
||||
axiosInstance.getCertificateExpiry = async (authToken, monitorId) => {
|
||||
return axiosInstance.get(`/monitors/certificate/${monitorId}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
});
|
||||
};
|
||||
// **********************************
|
||||
// Update in an exisiting user
|
||||
// **********************************
|
||||
async updateUser(authToken, userId, form) {
|
||||
return this.axiosInstance.put(`/auth/user/${userId}`, form, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Register a new user
|
||||
// **********************************
|
||||
axiosInstance.registerUser = async (form) => {
|
||||
return axiosInstance.post(`/auth/register`, form);
|
||||
};
|
||||
// **********************************
|
||||
// Forgot password request
|
||||
// **********************************
|
||||
async forgotPassword(form) {
|
||||
return this.axiosInstance.post(`/auth/recovery/request`, form);
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Log in an exisiting user
|
||||
// **********************************
|
||||
axiosInstance.loginUser = async (form) => {
|
||||
return axiosInstance.post(`/auth/login`, form);
|
||||
};
|
||||
async validateRecoveryToken(recoveryToken) {
|
||||
return this.axiosInstance.post("/auth/recovery/validate", {
|
||||
recoveryToken,
|
||||
});
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// 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",
|
||||
},
|
||||
});
|
||||
};
|
||||
// **********************************
|
||||
// Set new password request
|
||||
// **********************************
|
||||
async setNewPassword(recoveryToken, form) {
|
||||
return this.axiosInstance.post("/auth/recovery/reset", {
|
||||
...form,
|
||||
recoveryToken,
|
||||
});
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Forgot password request
|
||||
// **********************************
|
||||
axiosInstance.forgotPassword = async (form) => {
|
||||
return axiosInstance.post(`/auth/recovery/request`, form);
|
||||
};
|
||||
// **********************************
|
||||
// Check for admin user
|
||||
// **********************************
|
||||
async doesAdminExist() {
|
||||
return this.axiosInstance.get("/auth/users/admin");
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
};
|
||||
|
||||
// **********************************
|
||||
// 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}` },
|
||||
});
|
||||
};
|
||||
|
||||
// **********************************
|
||||
// Request Invitation Token
|
||||
// **********************************
|
||||
axiosInstance.requestInvitationToken = async (authToken, email, role) => {
|
||||
return axiosInstance.post(
|
||||
`/auth/invite`,
|
||||
{ email, role },
|
||||
{
|
||||
// **********************************
|
||||
// Get all users
|
||||
// **********************************
|
||||
async getAllUsers(authToken) {
|
||||
return this.axiosInstance.get("/auth/users", {
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
}
|
||||
);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Verify Invitation Token
|
||||
// **********************************
|
||||
axiosInstance.verifyInvitationToken = async (token) => {
|
||||
return axiosInstance.post(`/auth/invite/verify`, {
|
||||
token,
|
||||
});
|
||||
};
|
||||
// **********************************
|
||||
// Request Invitation Token
|
||||
// **********************************
|
||||
async requestInvitationToken(authToken, email, role) {
|
||||
return this.axiosInstance.post(
|
||||
`/auth/invite`,
|
||||
{ email, role },
|
||||
{
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// **********************************
|
||||
// Get all checks for a given monitor
|
||||
// **********************************
|
||||
// **********************************
|
||||
// Verify Invitation Token
|
||||
// **********************************
|
||||
async verifyInvitationToken(token) {
|
||||
return this.axiosInstance.post(`/auth/invite/verify`, {
|
||||
token,
|
||||
});
|
||||
}
|
||||
|
||||
axiosInstance.getChecksByMonitor = async (
|
||||
authToken,
|
||||
monitorId,
|
||||
sortOrder,
|
||||
limit,
|
||||
dateRange,
|
||||
filter,
|
||||
page,
|
||||
rowsPerPage
|
||||
) => {
|
||||
const params = new URLSearchParams();
|
||||
if (sortOrder) params.append("sortOrder", sortOrder);
|
||||
if (limit) params.append("limit", limit);
|
||||
if (dateRange) params.append("dateRange", dateRange);
|
||||
if (filter) params.append("filter", filter);
|
||||
if (page) params.append("page", page);
|
||||
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
|
||||
// **********************************
|
||||
// Get all checks for a given monitor
|
||||
// **********************************
|
||||
|
||||
return axiosInstance.get(`/checks/${monitorId}?${params.toString()}`, {
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
});
|
||||
};
|
||||
async getChecksByMonitor(
|
||||
authToken,
|
||||
monitorId,
|
||||
sortOrder,
|
||||
limit,
|
||||
dateRange,
|
||||
filter,
|
||||
page,
|
||||
rowsPerPage
|
||||
) {
|
||||
const params = new URLSearchParams();
|
||||
if (sortOrder) params.append("sortOrder", sortOrder);
|
||||
if (limit) params.append("limit", limit);
|
||||
if (dateRange) params.append("dateRange", dateRange);
|
||||
if (filter) params.append("filter", filter);
|
||||
if (page) params.append("page", page);
|
||||
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
|
||||
|
||||
// **********************************
|
||||
// Get all checks for a given user
|
||||
// **********************************
|
||||
axiosInstance.getChecksByUser = async (
|
||||
authToken,
|
||||
userId,
|
||||
sortOrder,
|
||||
limit,
|
||||
dateRange,
|
||||
filter,
|
||||
page,
|
||||
rowsPerPage
|
||||
) => {
|
||||
const params = new URLSearchParams();
|
||||
if (sortOrder) params.append("sortOrder", sortOrder);
|
||||
if (limit) params.append("limit", limit);
|
||||
if (dateRange) params.append("dateRange", dateRange);
|
||||
if (filter) params.append("filter", filter);
|
||||
if (page) params.append("page", page);
|
||||
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
|
||||
return axiosInstance.get(`/checks/user/${userId}?${params.toString()}`, {
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
});
|
||||
};
|
||||
return this.axiosInstance.get(`/checks/${monitorId}?${params.toString()}`, {
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
});
|
||||
}
|
||||
|
||||
export default axiosInstance;
|
||||
// **********************************
|
||||
// Get all checks for a given user
|
||||
// **********************************
|
||||
async getChecksByUser(
|
||||
authToken,
|
||||
userId,
|
||||
sortOrder,
|
||||
limit,
|
||||
dateRange,
|
||||
filter,
|
||||
page,
|
||||
rowsPerPage
|
||||
) {
|
||||
const params = new URLSearchParams();
|
||||
if (sortOrder) params.append("sortOrder", sortOrder);
|
||||
if (limit) params.append("limit", limit);
|
||||
if (dateRange) params.append("dateRange", dateRange);
|
||||
if (filter) params.append("filter", filter);
|
||||
if (page) params.append("page", page);
|
||||
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
|
||||
return this.axiosInstance.get(
|
||||
`/checks/user/${userId}?${params.toString()}`,
|
||||
{
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default NetworkService;
|
||||
|
||||
Reference in New Issue
Block a user