70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
/**
|
|
* Service for interacting with the Connection Management API.
|
|
*/
|
|
|
|
const API_BASE = '/api/settings/connections';
|
|
|
|
// [DEF:getConnections:Function]
|
|
/* @PURPOSE: Fetch a list of saved connections.
|
|
@PRE: None.
|
|
@POST: Returns a promise resolving to an array of connections.
|
|
*/
|
|
/**
|
|
* Fetch a list of saved connections.
|
|
* @returns {Promise<Array>} List of connections.
|
|
*/
|
|
export async function getConnections() {
|
|
const response = await fetch(API_BASE);
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch connections: ${response.statusText}`);
|
|
}
|
|
return await response.json();
|
|
}
|
|
// [/DEF:getConnections:Function]
|
|
|
|
// [DEF:createConnection:Function]
|
|
/* @PURPOSE: Create a new connection configuration.
|
|
@PRE: connectionData must be a valid object.
|
|
@POST: Returns a promise resolving to the created connection.
|
|
*/
|
|
/**
|
|
* Create a new connection configuration.
|
|
* @param {Object} connectionData - The connection data.
|
|
* @returns {Promise<Object>} The created connection instance.
|
|
*/
|
|
export async function createConnection(connectionData) {
|
|
const response = await fetch(API_BASE, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(connectionData)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.detail || `Failed to create connection: ${response.statusText}`);
|
|
}
|
|
return await response.json();
|
|
}
|
|
// [/DEF:createConnection:Function]
|
|
|
|
// [DEF:deleteConnection:Function]
|
|
/* @PURPOSE: Delete a connection configuration.
|
|
@PRE: connectionId must be a valid string.
|
|
@POST: Returns a promise that resolves when deletion is complete.
|
|
*/
|
|
/**
|
|
* Delete a connection configuration.
|
|
* @param {string} connectionId - The ID of the connection to delete.
|
|
*/
|
|
export async function deleteConnection(connectionId) {
|
|
const response = await fetch(`${API_BASE}/${connectionId}`, {
|
|
method: 'DELETE'
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to delete connection: ${response.statusText}`);
|
|
}
|
|
}
|
|
// [/DEF:deleteConnection:Function]
|