Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 4x 4x 4x 4x 25x 2x 23x 23x 23x 23x 14x 14x 14x 14x 1x 1x | import axios, { AxiosInstance } from "axios";
import { serverConfig } from "../config/serverConfig";
import { getEnvConstant } from "../config/serverConstants";
// Base URL for API requests
/**
* Base class for API interactions, handling axios instance setup and authentication.
*/
export class BaseApi {
protected axiosInstance: AxiosInstance; // Axios instance for API requests
protected accessKey: string; // API access key for authentication
protected secretKey: string; // API secret key for authentication
/**
* Initializes the BaseApi with access and secret keys, and sets up axios instance.
* @param accessKey - API access key.
* @param secretKey - API secret key.
*/
constructor(accessKey: string, secretKey: string) {
if (!accessKey || !secretKey) {
throw new Error("Access key and secret key are required.");
}
this.accessKey = accessKey;
this.secretKey = secretKey;
// Create an axios instance with base URL and default headers
this.axiosInstance = axios.create({
baseURL: getEnvConstant("BASE_URL") as string,
headers: serverConfig.headers,
});
// Add request interceptor for including authorization headers
this.axiosInstance.interceptors.request.use(
(config) => {
if (config.headers) {
config.headers["access-key"] = `${this.accessKey}`;
config.headers["secret-key"] = `${this.secretKey}`;
}
return config;
},
(error) => Promise.reject(error)
);
}
/**
* Sets custom headers for axios instance.
* @param headers - An object containing header key-value pairs.
*/
setHeaders(headers: Record<string, string>) {
Object.assign(this.axiosInstance.defaults.headers, headers);
}
}
|