112 lines
No EOL
3.3 KiB
JavaScript
112 lines
No EOL
3.3 KiB
JavaScript
/**
|
|
* CVS Proxy API Client
|
|
* Handles all API calls to the backend
|
|
*/
|
|
|
|
class CVSProxyAPI {
|
|
constructor(baseURL = '') {
|
|
this.baseURL = baseURL || '';
|
|
}
|
|
|
|
/**
|
|
* Make a fetch request to the API
|
|
* @param {string} endpoint - API endpoint
|
|
* @param {object} options - Fetch options
|
|
* @returns {Promise} Response data
|
|
*/
|
|
async request(endpoint, options = {}) {
|
|
const url = `${this.baseURL}/v1${endpoint}`;
|
|
|
|
try {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...options.headers,
|
|
},
|
|
...options,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({}));
|
|
throw new Error(error.error || `HTTP ${response.status}`);
|
|
}
|
|
|
|
// Handle different content types
|
|
const contentType = response.headers.get('content-type');
|
|
if (contentType && contentType.includes('application/json')) {
|
|
return await response.json();
|
|
} else {
|
|
return await response.text();
|
|
}
|
|
} catch (error) {
|
|
console.error('API Error:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get repository tree structure
|
|
* @param {string} module - Optional module to list
|
|
* @returns {Promise<Array>} List of files and directories
|
|
*/
|
|
async getTree(module = null) {
|
|
const params = new URLSearchParams();
|
|
if (module) {
|
|
params.append('module', module);
|
|
}
|
|
const query = params.toString() ? `?${params.toString()}` : '';
|
|
return this.request(`/tree${query}`);
|
|
}
|
|
|
|
/**
|
|
* Get file content
|
|
* @param {string} filePath - Path to the file
|
|
* @param {string} revision - Optional specific revision
|
|
* @returns {Promise<string>} File content
|
|
*/
|
|
async getFileContent(filePath, revision = null) {
|
|
const params = new URLSearchParams();
|
|
params.append('file', filePath);
|
|
if (revision) {
|
|
params.append('revision', revision);
|
|
}
|
|
return this.request(`/file?${params.toString()}`);
|
|
}
|
|
|
|
/**
|
|
* Get file revision history
|
|
* @param {string} filePath - Path to the file
|
|
* @returns {Promise<Array>} Array of revision objects
|
|
*/
|
|
async getFileHistory(filePath) {
|
|
const params = new URLSearchParams();
|
|
params.append('file', filePath);
|
|
return this.request(`/history?${params.toString()}`);
|
|
}
|
|
|
|
/**
|
|
* Get diff between two revisions
|
|
* @param {string} filePath - Path to the file
|
|
* @param {string} rev1 - First revision
|
|
* @param {string} rev2 - Second revision
|
|
* @returns {Promise<object>} Diff object with diff property
|
|
*/
|
|
async getDiff(filePath, rev1, rev2) {
|
|
const params = new URLSearchParams();
|
|
params.append('file', filePath);
|
|
params.append('rev1', rev1);
|
|
params.append('rev2', rev2);
|
|
return this.request(`/diff?${params.toString()}`);
|
|
}
|
|
|
|
/**
|
|
* Check API health
|
|
* @returns {Promise<object>} Health status
|
|
*/
|
|
async getHealth() {
|
|
return this.request('/health');
|
|
}
|
|
}
|
|
|
|
// Create global API instance
|
|
const api = new CVSProxyAPI(); |