UNPKG

@qbraid-core/base

Version:

Core functionality for interacting with qBraid Cloud Services.

76 lines 2.87 kB
"use strict"; // Copyright (c) 2025, qBraid Development Team // All rights reserved. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QbraidSession = exports.DEFAULT_BASE_URL = exports.DEFAULT_USER_POOL = void 0; /** * Module for making requests to the qBraid API. */ const axios_1 = __importDefault(require("axios")); const _version_1 = require("./_version"); exports.DEFAULT_USER_POOL = 'qbraid'; exports.DEFAULT_BASE_URL = 'https://api.qbraid.com/api'; class QbraidSession { client; constructor(authData, userPool = exports.DEFAULT_USER_POOL, baseURL = exports.DEFAULT_BASE_URL) { this.client = this.createAxiosInstance(authData, userPool, baseURL); } authDataToHeaders(authData) { let headers = {}; if (authData.refreshToken && authData.email) { headers['refresh-token'] = authData.refreshToken; headers['email'] = authData.email; } else if (authData.apiKey) { headers['api-key'] = authData.apiKey; } else { throw new Error('Invalid authentication data. Please provide either an API key or refresh token and email.'); } return headers; } normalizeBaseURL(baseURL) { return baseURL.replace(/\/+$/, ''); } createAxiosInstance(authData, userPool, baseURL) { const authHeaders = this.authDataToHeaders(authData); const headers = { domain: userPool, 'Content-Type': 'application/json; charset=utf-8', }; // Some environments (browsers) disallow setting User-Agent; add only when allowed try { const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; if (!isBrowser) { headers['User-Agent'] = this.getUserAgent(); } } catch { // ignore } const axiosInstance = axios_1.default.create({ baseURL: this.normalizeBaseURL(baseURL), headers, }); axiosInstance.interceptors.request.use(async (config) => { const updatedConfig = { ...config }; updatedConfig.headers.set(authHeaders); return updatedConfig; }); return axiosInstance; } getUserAgent() { return `QbraidCoreJs/${_version_1.version}`; } addUserAgent(userAgent) { const current = this.client.defaults.headers['User-Agent']; if (typeof current === 'string' && !current.includes(userAgent)) { this.client.defaults.headers['User-Agent'] = `${current} ${userAgent}`.trim(); } } } exports.QbraidSession = QbraidSession; //# sourceMappingURL=session.js.map