bb-plane-fork/packages/services/src/api.service.ts
2025-01-14 16:37:42 +05:30

123 lines
3.9 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import { IndexedDBService } from "./indexedDB.service";
/**
* Abstract base class for making HTTP requests using axios
* @abstract
*/
export abstract class APIService {
protected baseURL: string;
private axiosInstance: AxiosInstance;
/**
* Creates an instance of APIService
* @param {string} baseURL - The base URL for all HTTP requests
*/
constructor(baseURL: string) {
this.baseURL = baseURL;
this.axiosInstance = axios.create({
baseURL,
withCredentials: true,
});
this.setupInterceptors();
}
/**
* Sets up axios interceptors for handling responses
* Currently handles 401 unauthorized responses by redirecting to login
* @private
*/
private setupInterceptors() {
this.axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && error.response.status === 401) {
const currentPath = window.location.pathname;
let prefix = "/";
let updatedPath = currentPath;
// Check for special path prefixes
if (currentPath.startsWith("/god-mode")) {
prefix = "/god-mode";
updatedPath = currentPath.replace("/god-mode", "");
} else if (currentPath.startsWith("/spaces")) {
prefix = "/spaces";
updatedPath = currentPath.replace("/spaces", "");
}
window.location.replace(`${prefix}${updatedPath ? `?next_path=${updatedPath}` : ""}`);
}
return Promise.reject(error);
}
);
}
/**
* Makes a GET request to the specified URL
* @param {string} url - The endpoint URL
* @param {object} [params={}] - URL parameters
* @param {AxiosRequestConfig} [config={}] - Additional axios configuration
* @returns {Promise} Axios response promise
*/
get(url: string, params = {}, config: AxiosRequestConfig = {}) {
return this.axiosInstance.get(url, {
...params,
...config,
});
}
/**
* Makes a POST request to the specified URL
* @param {string} url - The endpoint URL
* @param {object} [data={}] - Request body data
* @param {AxiosRequestConfig} [config={}] - Additional axios configuration
* @returns {Promise} Axios response promise
*/
post(url: string, data = {}, config: AxiosRequestConfig = {}) {
return this.axiosInstance.post(url, data, config);
}
/**
* Makes a PUT request to the specified URL
* @param {string} url - The endpoint URL
* @param {object} [data={}] - Request body data
* @param {AxiosRequestConfig} [config={}] - Additional axios configuration
* @returns {Promise} Axios response promise
*/
put(url: string, data = {}, config: AxiosRequestConfig = {}) {
return this.axiosInstance.put(url, data, config);
}
/**
* Makes a PATCH request to the specified URL
* @param {string} url - The endpoint URL
* @param {object} [data={}] - Request body data
* @param {AxiosRequestConfig} [config={}] - Additional axios configuration
* @returns {Promise} Axios response promise
*/
patch(url: string, data = {}, config: AxiosRequestConfig = {}) {
return this.axiosInstance.patch(url, data, config);
}
/**
* Makes a DELETE request to the specified URL
* @param {string} url - The endpoint URL
* @param {any} [data] - Request body data
* @param {AxiosRequestConfig} [config={}] - Additional axios configuration
* @returns {Promise} Axios response promise
*/
delete(url: string, data?: any, config: AxiosRequestConfig = {}) {
return this.axiosInstance.delete(url, { data, ...config });
}
/**
* Makes a custom request with the provided configuration
* @param {object} [config={}] - Axios request configuration
* @returns {Promise} Axios response promise
*/
request(config = {}) {
return this.axiosInstance(config);
}
}