@datalayer/core
Version:
[](https://datalayer.io)
89 lines (88 loc) • 3.02 kB
JavaScript
/*
* Copyright (c) 2023-2025 Datalayer, Inc.
* Distributed under the terms of the Modified BSD License.
*/
import { validateJSON } from '../api/utils/validation';
/**
* Environment domain model that wraps API responses with convenient methods.
* Provides information about available computational environments.
*
* @example
* ```typescript
* const environments = await sdk.listEnvironments();
* const aiEnv = environments.find(env => env.name === 'ai-env');
* console.log(aiEnv.title); // "AI Environment"
* ```
*/
export class EnvironmentDTO {
/** @internal */
_data;
/**
* Create an Environment instance.
*
* @param data - Environment data from API
* @param _sdk - SDK instance (not currently used but kept for consistency)
*/
constructor(data, _sdk) {
this._data = data;
// SDK instance not currently used but kept for future extensibility
}
// ========================================================================
// Properties
// ========================================================================
/** Human-readable title for the environment (e.g., 'AI Environment', 'Python CPU Environment'). */
get title() {
return this._data.title;
}
/** Unique name identifier for the environment (e.g., 'ai-env', 'python-cpu-env'). */
get name() {
return this._data.name;
}
/** Credits consumed per hour for this environment. */
get burningRate() {
return this._data.burning_rate;
}
/** Rich description of the environment (contains HTML markup). */
get richDescription() {
return this._data.description;
}
/** Clean description without HTML tags. */
get description() {
// Simple HTML tag removal
return this._data.description.replace(/<[^>]*>/g, '').trim();
}
// ========================================================================
// Utility Methods
// ========================================================================
/**
* Get environment data in camelCase format.
* Returns only the core fields that consumers need.
* This provides a stable interface regardless of API changes.
*
* @returns Core environment data with camelCase properties
*/
toJSON() {
const obj = {
title: this.title,
name: this.name,
burningRate: this.burningRate,
description: this.description,
richDescription: this.richDescription,
};
validateJSON(obj, 'Environment');
return obj;
}
/**
* Get the raw environment data exactly as received from the API.
* This preserves the original snake_case naming from the API response.
*
* @returns Raw environment data from API
*/
rawData() {
return this._data;
}
/** String representation of the environment. */
toString() {
return `Environment(${this.name}, ${this.title})`;
}
}