pub const NODEJS_SDK_TEMPLATE: &str = r#"/**
* Chasm Node.js SDK
* Auto-generated - Do not edit directly
* Version: {{version}}
*/
const https = require('https');
const http = require('http');
const { URL } = require('url');
const VERSION = '{{version}}';
const API_VERSION = '{{api_version}}';
/**
* Chasm client configuration
*/
class ChasmConfig {
constructor(options = {}) {
this.baseUrl = options.baseUrl || process.env.CHASM_BASE_URL || '{{base_url}}';
this.apiKey = options.apiKey || process.env.CHASM_API_KEY;
this.timeout = options.timeout || 30000;
this.retryCount = options.retryCount || 3;
}
}
/**
* Custom error classes
*/
class ChasmError extends Error {
constructor(message, statusCode, response) {
super(message);
this.name = 'ChasmError';
this.statusCode = statusCode;
this.response = response;
}
}
class AuthenticationError extends ChasmError {
constructor(message) {
super(message, 401);
this.name = 'AuthenticationError';
}
}
class NotFoundError extends ChasmError {
constructor(message) {
super(message, 404);
this.name = 'NotFoundError';
}
}
class RateLimitError extends ChasmError {
constructor(message) {
super(message, 429);
this.name = 'RateLimitError';
}
}
/**
* Low-level API client
*/
class ApiClient {
constructor(config) {
this.config = config;
}
async request(method, path, options = {}) {
const url = new URL(path, this.config.baseUrl);
const isHttps = url.protocol === 'https:';
const client = isHttps ? https : http;
if (options.params) {
Object.entries(options.params).forEach(([key, value]) => {
if (value !== undefined) {
url.searchParams.append(key, String(value));
}
});
}
const requestOptions = {
method,
hostname: url.hostname,
port: url.port || (isHttps ? 443 : 80),
path: url.pathname + url.search,
headers: {
'Content-Type': 'application/json',
'User-Agent': `chasm-nodejs/${VERSION}`,
...(this.config.apiKey && { Authorization: `Bearer ${this.config.apiKey}` }),
},
timeout: this.config.timeout,
};
return new Promise((resolve, reject) => {
const req = client.request(requestOptions, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
if (res.statusCode === 401) {
reject(new AuthenticationError('Invalid API key'));
} else if (res.statusCode === 404) {
reject(new NotFoundError('Resource not found'));
} else if (res.statusCode === 429) {
reject(new RateLimitError('Rate limit exceeded'));
} else if (res.statusCode >= 400) {
reject(new ChasmError(`API error: ${data}`, res.statusCode));
} else {
resolve(data ? JSON.parse(data) : {});
}
});
});
req.on('error', reject);
req.on('timeout', () => {
req.destroy();
reject(new ChasmError('Request timeout'));
});
if (options.body) {
req.write(JSON.stringify(options.body));
}
req.end();
});
}
get(path, params) {
return this.request('GET', path, { params });
}
post(path, body) {
return this.request('POST', path, { body });
}
put(path, body) {
return this.request('PUT', path, { body });
}
delete(path) {
return this.request('DELETE', path);
}
}
/**
* Sessions resource
*/
class SessionsResource {
constructor(client) {
this._client = client;
}
async list(options = {}) {
const params = {
limit: options.limit || 20,
offset: options.offset || 0,
workspace_id: options.workspaceId,
provider: options.provider,
archived: options.archived,
};
const response = await this._client.get('/api/sessions', params);
return response.sessions || [];
}
async get(sessionId) {
return this._client.get(`/api/sessions/${sessionId}`);
}
async create(data) {
return this._client.post('/api/sessions', data);
}
async update(sessionId, data) {
return this._client.put(`/api/sessions/${sessionId}`, data);
}
async delete(sessionId) {
await this._client.delete(`/api/sessions/${sessionId}`);
return true;
}
async search(query, limit = 20) {
const response = await this._client.get('/api/sessions/search', { q: query, limit });
return response.sessions || [];
}
}
/**
* Workspaces resource
*/
class WorkspacesResource {
constructor(client) {
this._client = client;
}
async list(options = {}) {
const params = {
limit: options.limit || 20,
offset: options.offset || 0,
};
const response = await this._client.get('/api/workspaces', params);
return response.workspaces || [];
}
async get(workspaceId) {
return this._client.get(`/api/workspaces/${workspaceId}`);
}
}
/**
* Harvest resource
*/
class HarvestResource {
constructor(client) {
this._client = client;
}
async run(providers) {
const data = providers ? { providers } : {};
return this._client.post('/api/harvest', data);
}
async status() {
return this._client.get('/api/harvest/status');
}
}
/**
* Main Chasm client
*/
class ChasmClient {
constructor(options = {}) {
const config = new ChasmConfig(options);
this._api = new ApiClient(config);
this.sessions = new SessionsResource(this._api);
this.workspaces = new WorkspacesResource(this._api);
this.harvest = new HarvestResource(this._api);
}
async health() {
return this._api.get('/health');
}
async stats() {
return this._api.get('/api/stats');
}
}
module.exports = {
ChasmClient,
ChasmConfig,
ChasmError,
AuthenticationError,
NotFoundError,
RateLimitError,
VERSION,
API_VERSION,
};
"#;Expand description
Node.js SDK template