@neurolint/cli
Version:
NeuroLint CLI for React/Next.js modernization with advanced 6-layer orchestration and intelligent AST transformations
170 lines (147 loc) • 3.93 kB
JavaScript
/**
* NeuroLint CLI API Client
* Handles communication with NeuroLint backend APIs
*/
const https = require('https');
const http = require('http');
class NeuroLintAPIClient {
constructor(options = {}) {
this.baseURL = options.baseURL || process.env.NEUROLINT_API_URL || 'https://app.neurolint.dev';
this.apiKey = options.apiKey || process.env.NEUROLINT_API_KEY;
this.timeout = options.timeout || 30000;
}
/**
* Make HTTP request to NeuroLint API
*/
async request(path, options = {}) {
const url = new URL(path, this.baseURL);
const isHttps = url.protocol === 'https:';
const client = isHttps ? https : http;
const requestOptions = {
hostname: url.hostname,
port: url.port || (isHttps ? 443 : 80),
path: url.pathname + url.search,
method: options.method || 'GET',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'NeuroLint-CLI/1.2.0',
...options.headers
},
timeout: this.timeout
};
// Add authentication
if (this.apiKey) {
requestOptions.headers['X-API-Key'] = this.apiKey;
}
return new Promise((resolve, reject) => {
const req = client.request(requestOptions, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const response = {
status: res.statusCode,
headers: res.headers,
data: data ? JSON.parse(data) : null
};
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(response);
} else {
reject(new Error(`API Error ${res.statusCode}: ${response.data?.message || 'Unknown error'}`));
}
} catch (parseError) {
reject(new Error(`Failed to parse response: ${parseError.message}`));
}
});
});
req.on('error', (error) => {
reject(new Error(`Request failed: ${error.message}`));
});
req.on('timeout', () => {
req.destroy();
reject(new Error('Request timeout'));
});
// Send request body if provided
if (options.body) {
req.write(JSON.stringify(options.body));
}
req.end();
});
}
/**
* Analyze code without applying fixes
*/
async analyze(code, filename = 'file.js', layers = [1, 2, 3, 4, 5, 6]) {
try {
const response = await this.request('/api/cli/analyze', {
method: 'POST',
body: {
code,
filename,
layers,
options: {
dryRun: true,
verbose: false
}
}
});
return response.data;
} catch (error) {
throw new Error(`Analysis failed: ${error.message}`);
}
}
/**
* Analyze and fix code
*/
async fix(code, filename = 'file.js', layers = [1, 2, 3, 4, 5, 6]) {
try {
const response = await this.request('/api/cli/fix', {
method: 'POST',
body: {
code,
filename,
layers,
options: {
dryRun: false,
verbose: false
}
}
});
return response.data;
} catch (error) {
throw new Error(`Fix failed: ${error.message}`);
}
}
/**
* Check API health
*/
async health() {
try {
const response = await this.request('/api/health', {
method: 'GET'
});
return response.data;
} catch (error) {
throw new Error(`Health check failed: ${error.message}`);
}
}
/**
* Set API key for authentication
*/
setApiKey(apiKey) {
this.apiKey = apiKey;
}
/**
* Get current configuration
*/
getConfig() {
return {
baseURL: this.baseURL,
hasApiKey: !!this.apiKey,
timeout: this.timeout
};
}
}
module.exports = { NeuroLintAPIClient };