@qbraid-core/base
Version:
Core functionality for interacting with qBraid Cloud Services.
191 lines • 6.54 kB
JavaScript
"use strict";
// Copyright (c) 2025, qBraid Development Team
// All rights reserved.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.QbraidConfigManager = exports.ConfigManager = exports.DEFAULT_CONFIG = exports.USER_CONFIG_PATH = exports.DEFAULT_CONFIG_PATH = void 0;
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const ini = __importStar(require("ini"));
const session_1 = require("./session");
exports.DEFAULT_CONFIG_PATH = path.join(os.homedir(), '.qbraid', 'qbraidrc');
exports.USER_CONFIG_PATH = process.env.QBRAID_CONFIG_FILE || exports.DEFAULT_CONFIG_PATH;
exports.DEFAULT_CONFIG = {
default: {
'api-key': process.env.QBRAID_API_KEY ?? '',
'refresh-token': process.env.REFRESH ?? '',
email: process.env.JUPYTERHUB_USER ?? '',
url: session_1.DEFAULT_BASE_URL,
},
};
class ConfigManager {
configPath;
config;
constructor(filePath) {
this.configPath = filePath;
this.config = this.loadConfig();
}
ensureConfigFileExists() {
console.log('Checking config path:', this.configPath);
if (!fs.existsSync(this.configPath)) {
const dirPath = path.dirname(this.configPath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
fs.writeFileSync(this.configPath, '', 'utf-8');
}
}
loadConfig() {
try {
if (fs.existsSync(this.configPath)) {
const fileContent = fs.readFileSync(this.configPath, 'utf-8');
return this.parseConfig(fileContent);
}
else {
console.warn(`Config file not found at ${this.configPath}. Initializing with default config.`);
return this.getDefaultConfig();
}
}
catch (error) {
if (error instanceof Error) {
console.error(`Error loading config file: ${error.message}`);
}
else {
console.error(`An unknown error occurred while loading the config file.`);
}
return this.getDefaultConfig();
}
}
saveConfig() {
this.ensureConfigFileExists();
try {
const configString = this.stringifyConfig(this.config);
fs.writeFileSync(this.configPath, configString, 'utf-8');
}
catch (error) {
console.error(`Error saving config file: ${error}`);
}
}
getConfig() {
return this.config;
}
}
exports.ConfigManager = ConfigManager;
class QbraidConfigManager extends ConfigManager {
constructor(filePath = exports.USER_CONFIG_PATH) {
super(filePath);
this.initializeDefaultConfig();
}
parseConfig(content) {
return ini.parse(content);
}
stringifyConfig(config) {
return ini.stringify(config);
}
getDefaultConfig() {
return exports.DEFAULT_CONFIG;
}
initializeDefaultConfig() {
const currentConfig = this.getConfig();
const mergedConfig = this.deepMerge(exports.DEFAULT_CONFIG, currentConfig);
this.updateConfig(mergedConfig);
}
deepMerge(target, source) {
const output = { ...target };
Object.keys(source).forEach(key => {
if (typeof source[key] === 'object' && source[key] !== null) {
output[key] = output[key] ? { ...output[key], ...source[key] } : { ...source[key] };
}
});
return output;
}
getSection(section) {
return this.config[section];
}
getValue(section, key) {
return this.config[section]?.[key];
}
setValue(section, key, value) {
if (!this.config[section]) {
this.config[section] = {};
}
this.config[section][key] = value;
}
updateConfig(newConfig) {
this.config = { ...this.config, ...newConfig };
}
removeSection(section) {
delete this.config[section];
}
removeValue(section, key) {
if (this.config[section]) {
delete this.config[section][key];
}
}
getApiKey() {
return this.getValue('default', 'api-key') || '';
}
setApiKey(apiKey) {
this.setValue('default', 'api-key', apiKey);
}
getRefreshToken() {
return this.getValue('default', 'refresh-token') || '';
}
setRefreshToken(refreshToken) {
this.setValue('default', 'refresh-token', refreshToken);
}
getEmail() {
return this.getValue('default', 'email') || '';
}
setEmail(email) {
this.setValue('default', 'email', email);
}
getUrl() {
return this.getValue('default', 'url') || session_1.DEFAULT_BASE_URL;
}
setUrl(url) {
this.setValue('default', 'url', url);
}
getAuthData() {
return {
apiKey: this.getApiKey(),
refreshToken: this.getRefreshToken(),
email: this.getEmail(),
};
}
}
exports.QbraidConfigManager = QbraidConfigManager;
//# sourceMappingURL=config.js.map