UNPKG

kohin-js

Version:

The Kohin JS is a comprehensive developer toolkit designed to integrate Kohin's decentralized insurance system seamlessly into your applications. It enables efficient interaction with Kohin smart contracts and backend APIs, facilitating management and ana

49 lines (48 loc) 1.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseApi = void 0; const axios_1 = __importDefault(require("axios")); const serverConfig_1 = require("../config/serverConfig"); const serverConstants_1 = require("../config/serverConstants"); // Base URL for API requests /** * Base class for API interactions, handling axios instance setup and authentication. */ class BaseApi { /** * 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, secretKey) { 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_1.default.create({ baseURL: (0, serverConstants_1.getEnvConstant)("BASE_URL"), headers: serverConfig_1.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) { Object.assign(this.axiosInstance.defaults.headers, headers); } } exports.BaseApi = BaseApi;