@qbraid-core/base
Version:
Core functionality for interacting with qBraid Cloud Services.
120 lines • 4.63 kB
JavaScript
;
// 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.QbraidSessionV1 = exports.DEFAULT_BASE_URL = exports.DEFAULT_USER_DOMAIN = void 0;
/**
* Module for making requests to the qBraid API v1.
*/
const axios_1 = __importDefault(require("axios"));
const _version_1 = require("./_version");
exports.DEFAULT_USER_DOMAIN = 'qbraid.com';
// Current default base URL for qBraid API v1
exports.DEFAULT_BASE_URL = 'https://api-staging.qbraid.com/api/v1';
class QbraidSessionV1 {
apiKey;
accessToken;
userDomain;
baseURL;
client;
/**
* Construct a session.
*
* Accepts either:
* - new QbraidSessionV1(apiKey, userDomain?, baseURL?) // legacy positional
* - new QbraidSessionV1({ apiKey?, accessToken?, userDomain?, baseURL? })
*
* If neither apiKey nor accessToken is provided, falls back to the
* QBRAID_ACCESS_TOKEN environment variable.
*/
constructor(apiKeyOrOptions, userDomain = exports.DEFAULT_USER_DOMAIN, baseURL = exports.DEFAULT_BASE_URL) {
if (typeof apiKeyOrOptions === 'object' && apiKeyOrOptions !== null) {
this.apiKey = apiKeyOrOptions.apiKey;
this.accessToken = apiKeyOrOptions.accessToken;
this.userDomain = apiKeyOrOptions.userDomain ?? exports.DEFAULT_USER_DOMAIN;
this.baseURL = apiKeyOrOptions.baseURL ?? exports.DEFAULT_BASE_URL;
}
else {
this.apiKey = apiKeyOrOptions || undefined;
this.userDomain = userDomain;
this.baseURL = baseURL;
}
// Fall back to env var (Node only) if no auth provided
if (!this.apiKey && !this.accessToken) {
try {
const envToken = typeof process !== 'undefined' && process.env
? process.env.QBRAID_ACCESS_TOKEN
: undefined;
if (envToken) {
this.accessToken = envToken;
}
}
catch {
// browser environments may not have process — ignore
}
}
this.client = this.initializeClient();
}
authDataToHeaders(authData) {
const headers = {};
if (authData.accessToken) {
// qBraid API expects access tokens in the X-Access-Token header
// (not Authorization: Bearer — which is reserved for JWTs)
headers['X-Access-Token'] = authData.accessToken;
}
else if (authData.apiKey) {
headers['X-Api-Key'] = `${authData.apiKey}`;
}
else {
throw new Error('Invalid authentication data. Provide an API key, accessToken, or set QBRAID_ACCESS_TOKEN.');
}
return headers;
}
normalizeBaseURL(baseURL) {
return baseURL.replace(/\/+$/, '');
}
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();
}
}
initializeClient() {
// Build auth headers
const authData = { apiKey: this.apiKey, accessToken: this.accessToken };
const authHeaders = this.authDataToHeaders(authData);
const headers = {
'X-Domain': this.userDomain,
'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
}
// Create axios instance
const axiosInstance = axios_1.default.create({
baseURL: this.normalizeBaseURL(this.baseURL),
headers,
});
axiosInstance.interceptors.request.use(async (config) => {
const updatedConfig = { ...config };
updatedConfig.headers.set(authHeaders);
return updatedConfig;
});
return axiosInstance;
}
}
exports.QbraidSessionV1 = QbraidSessionV1;
//# sourceMappingURL=session-v1.js.map