Files
Checkmate/Client/src/Utils/NetworkService.js
T

684 lines
23 KiB
JavaScript
Raw Normal View History

import axios from "axios";
2024-09-27 12:13:39 +08:00
const BASE_URL =
import.meta.env.VITE_APP_API_BASE_URL || "http://localhost:5000/api/v1";
2024-08-20 10:34:29 -07:00
import { logger } from "./Logger";
2024-08-18 17:42:49 -07:00
class NetworkService {
constructor(store) {
this.store = store;
2024-09-27 12:13:39 +08:00
let baseURL = BASE_URL;
this.axiosInstance = axios.create();
this.setBaseUrl(baseURL);
this.unsubscribe = store.subscribe(() => {
const state = store.getState();
baseURL = state.settings.apiBaseUrl || BASE_URL;
this.setBaseUrl(baseURL);
});
2024-08-18 17:42:49 -07:00
this.axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
2024-08-20 10:34:29 -07:00
logger.error(error);
2024-08-18 17:42:49 -07:00
if (error.response && error.response.status === 401) {
2024-08-20 10:34:29 -07:00
logger.error("Invalid token received");
2024-08-18 17:42:49 -07:00
}
return Promise.reject(error);
}
);
}
2024-09-27 12:13:39 +08:00
setBaseUrl = (url) => {
this.axiosInstance.defaults.baseURL = url;
};
cleanup() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
/**
*
* ************************************
* Create a new monitor
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.monitorId - The monitor ID to be sent in the param.
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*/
2024-09-22 12:17:34 +08:00
async getMonitorById(config) {
return this.axiosInstance.get(`/monitors/${config.monitorId}`, {
headers: {
Authorization: `Bearer ${config.authToken}`,
"Content-Type": "application/json",
},
});
}
2024-08-18 17:56:18 -07:00
/**
*
* ************************************
* Create a new monitor
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {Object} config.monitor - The monitor object to be sent in the request body.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
async createMonitor(config) {
return this.axiosInstance.post(`/monitors`, config.monitor, {
2024-08-18 17:42:49 -07:00
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-08-18 17:42:49 -07:00
"Content-Type": "application/json",
},
});
}
/**
*
* ************************************
* Gets monitors and summary of stats by TeamID
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.teamId - Team ID
* @param {Array<string>} config.types - Array of monitor types
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
async getMonitorsAndSummaryByTeamId(config) {
2024-09-05 12:16:29 -07:00
const params = new URLSearchParams();
if (config.types) {
config.types.forEach((type) => {
2024-09-05 12:16:29 -07:00
params.append("type", type);
});
}
return this.axiosInstance.get(
`/monitors/team/summary/${config.teamId}?${params.toString()}`,
2024-09-05 12:16:29 -07:00
{
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-09-05 12:16:29 -07:00
"Content-Type": "application/json",
},
}
);
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
2024-09-05 12:16:29 -07:00
* Get all uptime monitors for a Team
2024-08-18 17:56:18 -07:00
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.teamId - The ID of the team whose monitors are to be retrieved.
* @param {number} [config.limit] - The maximum number of monitors to retrieve.
* @param {Array<string>} [config.types] - The types of monitors to retrieve.
* @param {string} [config.status] - The status of the monitors to retrieve.
* @param {string} [config.checkOrder] - The order in which to sort the retrieved monitors.
* @param {boolean} [config.normalize] - Whether to normalize the retrieved monitors.
* @param {number} [config.page] - The page number for pagination.
* @param {number} [config.rowsPerPage] - The number of rows per page for pagination.
* @param {string} [config.filter] - The filter to apply to the monitors.
* @param {string} [config.field] - The field to sort by.
* @param {string} [config.order] - The order in which to sort the field.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*/
async getMonitorsByTeamId(config) {
const {
authToken,
teamId,
limit,
types,
status,
checkOrder,
normalize,
page,
rowsPerPage,
filter,
field,
order,
} = config;
2024-08-18 17:42:49 -07:00
const params = new URLSearchParams();
2024-08-18 17:42:49 -07:00
if (limit) params.append("limit", limit);
if (types) {
types.forEach((type) => {
params.append("type", type);
});
}
2024-08-18 17:42:49 -07:00
if (status) params.append("status", status);
2024-09-18 10:32:41 +08:00
if (checkOrder) params.append("checkOrder", checkOrder);
2024-08-18 17:42:49 -07:00
if (normalize) params.append("normalize", normalize);
2024-09-04 17:37:30 -07:00
if (page) params.append("page", page);
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
2024-09-18 10:39:47 +08:00
if (filter) params.append("filter", filter);
if (field) params.append("field", field);
if (order) params.append("order", order);
2024-08-18 17:42:49 -07:00
return this.axiosInstance.get(
`/monitors/team/${teamId}?${params.toString()}`,
2024-08-18 17:42:49 -07:00
{
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
}
);
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Get stats for a monitor
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.monitorId - The ID of the monitor whose statistics are to be retrieved.
* @param {string} config.sortOrder - The order in which to sort the retrieved statistics.
* @param {number} config.limit - The maximum number of statistics to retrieve.
* @param {string} config.dateRange - The date range for which to retrieve statistics.
* @param {number} config.numToDisplay - The number of checks to display.
* @param {boolean} config.normalize - Whether to normalize the retrieved statistics.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*/
async getStatsByMonitorId(config) {
2024-08-18 17:42:49 -07:00
const params = new URLSearchParams();
if (config.sortOrder) params.append("sortOrder", config.sortOrder);
if (config.limit) params.append("limit", config.limit);
if (config.dateRange) params.append("dateRange", config.dateRange);
if (config.numToDisplay) params.append("numToDisplay", config.numToDisplay);
if (config.normalize) params.append("normalize", config.normalize);
2024-08-18 17:42:49 -07:00
return this.axiosInstance.get(
`/monitors/stats/${config.monitorId}?${params.toString()}`,
2024-08-18 17:42:49 -07:00
{
2024-09-03 20:00:48 -04:00
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-09-03 20:00:48 -04:00
},
}
);
}
/**
* ************************************
* Updates a single monitor
2024-09-03 20:00:48 -04:00
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.monitorId - The ID of the monitor to be updated.
* @param {Object} config.updatedFields - The fields to be updated for the monitor.
* @returns {Promise<AxiosResponse>} The response from the axios PUT request.
2024-09-03 20:00:48 -04:00
*/
async updateMonitor(config) {
return this.axiosInstance.put(
`/monitors/${config.monitorId}`,
config.updatedFields,
2024-09-03 20:00:48 -04:00
{
2024-08-18 17:42:49 -07:00
headers: {
Authorization: `Bearer ${config.authToken}`,
"Content-Type": "application/json",
2024-08-18 17:42:49 -07:00
},
}
);
}
2024-08-17 09:50:52 -07:00
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Deletes a single monitor by its ID
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.monitorId - The ID of the monitor to be deleted.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios DELETE request.
*/
async deleteMonitorById(config) {
return this.axiosInstance.delete(`/monitors/${config.monitorId}`, {
2024-08-18 17:42:49 -07:00
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-08-30 09:47:11 -07:00
"Content-Type": "application/json",
},
});
}
/**
* ************************************
* Deletes all checks for all monitor by teamID
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
2024-09-22 12:19:08 +08:00
* @param {string} config.teamId - The team ID of the monitors to be deleted.
2024-08-30 09:47:11 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios DELETE request.
*/
async deleteChecksByTeamId(config) {
return this.axiosInstance.delete(`/checks/team/${config.teamId}`, {
2024-08-30 09:47:11 -07:00
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-08-18 17:42:49 -07:00
"Content-Type": "application/json",
},
});
}
/**
* ************************************
* Pauses a single monitor by its ID
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.monitorId - The ID of the monitor to be paused.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
async pauseMonitorById(config) {
2024-08-29 12:17:37 -07:00
return this.axiosInstance.post(
`/monitors/pause/${config.monitorId}`,
2024-08-29 12:17:37 -07:00
{},
{
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-08-29 12:17:37 -07:00
"Content-Type": "application/json",
},
}
);
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Adds demo monitors
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
async addDemoMonitors(config) {
return this.axiosInstance.post(
`/monitors/demo`,
{},
{
headers: {
Authorization: `Bearer ${config.authToken}`,
"Content-Type": "application/json",
},
}
);
}
/**
* ************************************
* Deletes all monitors for a team by team ID
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @returns {Promise<AxiosResponse>} The response from the axios DELETE request.
*/
async deleteAllMonitors(config) {
return this.axiosInstance.delete(`/monitors/`, {
headers: {
Authorization: `Bearer ${config.authToken}`,
"Content-Type": "application/json",
},
});
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Gets the certificate expiry for a monitor
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.monitorId - The ID of the monitor whose certificate expiry is to be retrieved.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*
*/
async getCertificateExpiry(config) {
return this.axiosInstance.get(`/monitors/certificate/${config.monitorId}`, {
2024-08-18 17:42:49 -07:00
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-08-18 17:42:49 -07:00
},
2024-08-17 09:50:52 -07:00
});
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Registers a new user
* ************************************
*
* @async
* @param {Object} form - The form data for the new user to be registered.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
2024-08-18 17:42:49 -07:00
async registerUser(form) {
return this.axiosInstance.post(`/auth/register`, form);
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Logs in a user
* ************************************
*
* @async
* @param {Object} form - The form data for the user to be logged in.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*
*/
2024-08-18 17:42:49 -07:00
async loginUser(form) {
return this.axiosInstance.post(`/auth/login`, form);
}
2024-08-18 17:18:16 -07:00
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Updates a user
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.userId - The ID of the user to be updated.
* @param {Object} config.form - The form data for the user to be updated.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios PUT request.
*
*/
async updateUser(config) {
return this.axiosInstance.put(`/auth/user/${config.userId}`, config.form, {
2024-08-18 17:18:16 -07:00
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-08-18 17:18:16 -07:00
},
2024-08-18 17:42:49 -07:00
});
}
/**
* ************************************
* Deletes a user
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.userId - The ID of the user to be deleted.
*
**/
async deleteUser(config) {
return this.axiosInstance.delete(`/auth/user/${config.userId}`, {
headers: { Authorization: `Bearer ${config.authToken}` },
});
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Forgot password request
* ************************************
*
* @async
* @param {Object} form - The form data for the password recovery request.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*
*/
2024-08-18 17:42:49 -07:00
async forgotPassword(form) {
return this.axiosInstance.post(`/auth/recovery/request`, form);
}
2024-08-18 17:18:16 -07:00
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Validates a recovery token
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.recoveryToken - The recovery token to be validated.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*
*/
async validateRecoveryToken(config) {
2024-08-18 17:42:49 -07:00
return this.axiosInstance.post("/auth/recovery/validate", {
recoveryToken: config.recoveryToken,
2024-08-18 17:42:49 -07:00
});
}
2024-08-17 13:03:24 -07:00
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Requests password recovery
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.recoveryToken - The token for recovery request.
* @param {Object} config.form - The form data for the password recovery request.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*
*/
async setNewPassword(config) {
2024-08-18 17:42:49 -07:00
return this.axiosInstance.post("/auth/recovery/reset", {
...config.form,
recoveryToken: config.recoveryToken,
2024-08-18 17:42:49 -07:00
});
}
2024-08-17 13:03:24 -07:00
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Checks if an admin user exists
* ************************************
*
* @async
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*
*/
2024-08-26 10:00:34 -07:00
async doesSuperAdminExist() {
return this.axiosInstance.get("/auth/users/superadmin");
2024-08-18 17:42:49 -07:00
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Get all users
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*
*/
async getAllUsers(config) {
2024-08-18 17:42:49 -07:00
return this.axiosInstance.get("/auth/users", {
headers: { Authorization: `Bearer ${config.authToken}` },
2024-08-18 17:42:49 -07:00
});
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Requests an invitation token
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.email - The email of the user to be invited.
* @param {string} config.role - The role of the user to be invited.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*
*/
async requestInvitationToken(config) {
2024-08-18 17:42:49 -07:00
return this.axiosInstance.post(
`/invite`,
{ email: config.email, role: config.role },
2024-08-18 17:42:49 -07:00
{
headers: { Authorization: `Bearer ${config.authToken}` },
2024-08-18 17:42:49 -07:00
}
);
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Verifies an invitation token
* ************************************
*
* @async
* @param {string} token - The invitation token to be verified.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*
*/
2024-08-18 17:42:49 -07:00
async verifyInvitationToken(token) {
return this.axiosInstance.post(`/invite/verify`, {
2024-08-18 17:42:49 -07:00
token,
});
}
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Get all checks for a given monitor
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.monitorId - The ID of the monitor.
* @param {string} config.sortOrder - The order in which to sort the checks.
* @param {number} config.limit - The maximum number of checks to retrieve.
* @param {string} config.dateRange - The range of dates for which to retrieve checks.
* @param {string} config.filter - The filter to apply to the checks.
* @param {number} config.page - The page number to retrieve in a paginated list.
* @param {number} config.rowsPerPage - The number of rows per page in a paginated list.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*
*/
2024-08-18 12:20:28 -07:00
async getChecksByMonitor(config) {
2024-08-18 17:42:49 -07:00
const params = new URLSearchParams();
if (config.sortOrder) params.append("sortOrder", config.sortOrder);
if (config.limit) params.append("limit", config.limit);
if (config.dateRange) params.append("dateRange", config.dateRange);
if (config.filter) params.append("filter", config.filter);
if (config.page) params.append("page", config.page);
if (config.rowsPerPage) params.append("rowsPerPage", config.rowsPerPage);
2024-08-18 12:20:28 -07:00
return this.axiosInstance.get(
`/checks/${config.monitorId}?${params.toString()}`,
{
headers: { Authorization: `Bearer ${config.authToken}` },
}
);
2024-08-18 17:42:49 -07:00
}
2024-08-18 17:18:16 -07:00
2024-08-18 17:56:18 -07:00
/**
* ************************************
* Get all checks for a given user
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} config.userId - The ID of the user.
* @param {string} config.sortOrder - The order in which to sort the checks.
* @param {number} config.limit - The maximum number of checks to retrieve.
* @param {string} config.dateRange - The range of dates for which to retrieve checks.
* @param {string} config.filter - The filter to apply to the checks.
* @param {number} config.page - The page number to retrieve in a paginated list.
* @param {number} config.rowsPerPage - The number of rows per page in a paginated list.
2024-08-18 17:56:18 -07:00
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*
*/
async getChecksByTeam(config) {
2024-08-18 17:42:49 -07:00
const params = new URLSearchParams();
if (config.sortOrder) params.append("sortOrder", config.sortOrder);
if (config.limit) params.append("limit", config.limit);
if (config.dateRange) params.append("dateRange", config.dateRange);
if (config.filter) params.append("filter", config.filter);
if (config.page) params.append("page", config.page);
if (config.rowsPerPage) params.append("rowsPerPage", config.rowsPerPage);
2024-08-18 17:42:49 -07:00
return this.axiosInstance.get(
`/checks/team/${config.teamId}?${params.toString()}`,
2024-08-18 17:42:49 -07:00
{
headers: { Authorization: `Bearer ${config.authToken}` },
2024-08-18 17:42:49 -07:00
}
);
}
2024-09-16 12:13:29 +08:00
/**
* ************************************
* Get all checks for a given user
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {number} config.ttl - TTL for checks
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*
*/
async updateChecksTTL(config) {
2024-09-16 12:13:29 +08:00
return this.axiosInstance.put(
`/checks/team/ttl`,
{ ttl: config.ttl },
2024-09-16 12:13:29 +08:00
{
headers: {
Authorization: `Bearer ${config.authToken}`,
2024-09-16 12:13:29 +08:00
"Content-Type": "application/json",
},
}
);
}
2024-09-26 16:18:25 +08:00
/**
* ************************************
* Get app settings
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @returns {Promise<AxiosResponse>} The response from the axios GET request.
*
*/
async getAppSettings(config) {
return this.axiosInstance.get("/settings", {
headers: {
Authorization: `Bearer ${config.authToken}`,
"Content-Type": "application/json",
},
});
}
2024-09-27 12:13:39 +08:00
/**
*
* ************************************
* Create a new monitor
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {Object} config.settings - The monitor object to be sent in the request body.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
async updateAppSettings(config) {
return this.axiosInstance.put(`/settings`, config.settings, {
headers: {
Authorization: `Bearer ${config.authToken}`,
"Content-Type": "application/json",
},
});
}
2024-08-18 17:42:49 -07:00
}
2024-08-18 17:18:16 -07:00
2024-08-18 17:42:49 -07:00
export default NetworkService;