|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract base class for making HTTP requests using axios |
| 6 | + * @abstract |
| 7 | + */ |
| 8 | +export abstract class APIService { |
| 9 | + protected baseURL: string; |
| 10 | + private axiosInstance: AxiosInstance; |
| 11 | + |
| 12 | + /** |
| 13 | + * Creates an instance of APIService |
| 14 | + * @param {string} baseURL - The base URL for all HTTP requests |
| 15 | + */ |
| 16 | + constructor(baseURL: string) { |
| 17 | + this.baseURL = baseURL; |
| 18 | + this.axiosInstance = axios.create({ |
| 19 | + baseURL, |
| 20 | + withCredentials: true, |
| 21 | + }); |
| 22 | + |
| 23 | + this.setupInterceptors(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Sets up axios interceptors for handling responses |
| 28 | + * Currently handles 401 unauthorized responses by redirecting to login |
| 29 | + * @private |
| 30 | + */ |
| 31 | + private setupInterceptors() { |
| 32 | + this.axiosInstance.interceptors.response.use( |
| 33 | + (response) => response, |
| 34 | + (error) => { |
| 35 | + if (error.response && error.response.status === 401) { |
| 36 | + const currentPath = window.location.pathname; |
| 37 | + window.location.replace(`/${currentPath ? `?next_path=${currentPath}` : ``}`); |
| 38 | + } |
| 39 | + return Promise.reject(error); |
| 40 | + } |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Makes a GET request to the specified URL |
| 46 | + * @param {string} url - The endpoint URL |
| 47 | + * @param {object} [params={}] - URL parameters |
| 48 | + * @param {AxiosRequestConfig} [config={}] - Additional axios configuration |
| 49 | + * @returns {Promise} Axios response promise |
| 50 | + */ |
| 51 | + get(url: string, params = {}, config: AxiosRequestConfig = {}) { |
| 52 | + return this.axiosInstance.get(url, { |
| 53 | + ...params, |
| 54 | + ...config, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Makes a POST request to the specified URL |
| 60 | + * @param {string} url - The endpoint URL |
| 61 | + * @param {object} [data={}] - Request body data |
| 62 | + * @param {AxiosRequestConfig} [config={}] - Additional axios configuration |
| 63 | + * @returns {Promise} Axios response promise |
| 64 | + */ |
| 65 | + post(url: string, data = {}, config: AxiosRequestConfig = {}) { |
| 66 | + return this.axiosInstance.post(url, data, config); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Makes a PUT request to the specified URL |
| 71 | + * @param {string} url - The endpoint URL |
| 72 | + * @param {object} [data={}] - Request body data |
| 73 | + * @param {AxiosRequestConfig} [config={}] - Additional axios configuration |
| 74 | + * @returns {Promise} Axios response promise |
| 75 | + */ |
| 76 | + put(url: string, data = {}, config: AxiosRequestConfig = {}) { |
| 77 | + return this.axiosInstance.put(url, data, config); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Makes a PATCH request to the specified URL |
| 82 | + * @param {string} url - The endpoint URL |
| 83 | + * @param {object} [data={}] - Request body data |
| 84 | + * @param {AxiosRequestConfig} [config={}] - Additional axios configuration |
| 85 | + * @returns {Promise} Axios response promise |
| 86 | + */ |
| 87 | + patch(url: string, data = {}, config: AxiosRequestConfig = {}) { |
| 88 | + return this.axiosInstance.patch(url, data, config); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Makes a DELETE request to the specified URL |
| 93 | + * @param {string} url - The endpoint URL |
| 94 | + * @param {any} [data] - Request body data |
| 95 | + * @param {AxiosRequestConfig} [config={}] - Additional axios configuration |
| 96 | + * @returns {Promise} Axios response promise |
| 97 | + */ |
| 98 | + delete(url: string, data?: any, config: AxiosRequestConfig = {}) { |
| 99 | + return this.axiosInstance.delete(url, { data, ...config }); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Makes a custom request with the provided configuration |
| 104 | + * @param {object} [config={}] - Axios request configuration |
| 105 | + * @returns {Promise} Axios response promise |
| 106 | + */ |
| 107 | + request(config = {}) { |
| 108 | + return this.axiosInstance(config); |
| 109 | + } |
| 110 | +} |
0 commit comments