@qbraid-core/ibm-cloud
Version:
Client for interacting with IBM's Qiskit Runtime Service via the IBM Cloud API.
160 lines • 5.4 kB
JavaScript
;
// Copyright (c) 2025, qBraid Development Team
// All rights reserved.
Object.defineProperty(exports, "__esModule", { value: true });
exports.IBMConfig = exports.IBM_DEFAULT_TOKEN_LIFETIME = exports.DEFAULT_IBM_CONFIG = void 0;
/**
* Default IBM Quantum Platform configuration.
* This can be used directly in frontend applications or as a base for filesystem config.
*/
exports.DEFAULT_IBM_CONFIG = {
'default-ibm-quantum-platform': {
// latest IBMQ channel, ibm_cloud will be deprecated soon
channel: 'ibm_quantum_platform',
// Loading account from environment variables (browser-safe)
// Ref : Qiskit IBM Runtime docs on environment variables
instance: (() => {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const p = globalThis.process;
return p?.env?.QISKIT_IBM_INSTANCE ?? '';
}
catch {
return '';
}
})(),
token: (() => {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const p = globalThis.process;
return p?.env?.QISKIT_IBM_TOKEN ?? '';
}
catch {
return '';
}
})(),
private_endpoint: false,
url: 'https://cloud.ibm.com',
},
};
exports.IBM_DEFAULT_TOKEN_LIFETIME = 3600_000; // 1 hour in milliseconds
/**
* Lightweight, browser-safe IBM Quantum Platform config wrapper.
* - In-memory only (no filesystem operations).
* - Provides helpers for API key (token) and service CRN (instance).
* - Maintains an in-memory bearer token with simple expiry logic.
*/
class IBMConfig {
config;
bearerToken = '';
tokenFetchTime = 0;
/**
* Create a new IBMConfig.
* @param config Optional initial configuration. If omitted, a deep copy of DEFAULT_IBM_CONFIG is used.
*/
constructor(config) {
this.config = config
? JSON.parse(JSON.stringify(config))
: JSON.parse(JSON.stringify(exports.DEFAULT_IBM_CONFIG));
}
/**
* Get a shallow copy of the current configuration object.
* Note: nested section objects are not deeply cloned.
* @returns A shallow copy of the current configuration.
*/
getConfig() {
return { ...this.config };
}
/**
* Shallow-merge top-level sections from the provided config into the current config.
*
* - Existing sections are preserved unless overwritten by the same section key in newConfig.
* - For a given section key, the entire section object is replaced (no deep merge of keys).
* Use setValue()/specific setters for per-key updates.
* @param newConfig The partial configuration to merge at the section level.
*/
updateConfig(newConfig) {
this.config = { ...this.config, ...newConfig };
}
/**
* Get the IBM API key (stored as 'token').
* @returns The API key string.
*/
getApiKey() {
return this.config['default-ibm-quantum-platform'].token;
}
/**
* Set the IBM API key (stored as 'token').
* @param apiKey The API key.
*/
setApiKey(apiKey) {
this.config['default-ibm-quantum-platform'].token = apiKey;
}
/**
* Get the IBM Quantum service CRN (stored as 'instance').
* @returns The service CRN string.
*/
getServiceCRN() {
return this.config['default-ibm-quantum-platform'].instance;
}
/**
* Set the IBM Quantum service CRN (stored as 'instance').
* @param crn The service CRN value.
*/
setServiceCRN(crn) {
this.config['default-ibm-quantum-platform'].instance = crn;
}
/**
* Get the in-memory bearer token (not persisted).
* @returns The bearer token string.
*/
getBearerToken() {
return this.bearerToken;
}
/**
* Set the in-memory bearer token (not persisted).
* @param token The bearer token value.
*/
setBearerToken(token) {
this.bearerToken = token;
this.tokenFetchTime = Date.now();
}
/**
* Bearer token expiry check.
* Returns true if token was never set or lifetime exceeded.
*/
isBearerTokenExpired() {
return (this.tokenFetchTime === 0 || Date.now() - this.tokenFetchTime > exports.IBM_DEFAULT_TOKEN_LIFETIME);
}
/**
* Get a configuration section by name.
* @param section The section key.
* @returns The section object if present, otherwise undefined.
*/
getSection(section) {
return this.config[section];
}
/**
* Get a value from a given section.
* @param section The section key.
* @param key The key within the section.
* @returns The value if present, otherwise undefined.
*/
getValue(section, key) {
return this.config[section]?.[key];
}
/**
* Set a value within a section, creating the section if needed.
* @param section The section key.
* @param key The key within the section.
* @param value The value to set.
*/
setValue(section, key, value) {
if (!this.config[section]) {
this.config[section] = {};
}
this.config[section][key] = value;
}
}
exports.IBMConfig = IBMConfig;
//# sourceMappingURL=config.js.map