get-workspace-list.js•2.4 kB
import dotenv from 'dotenv';
dotenv.config();
/**
* Function to get workspaces from the specified account.
*
* @param {Object} args - Arguments for the request.
* @param {string} [args.accountId] - The ID of the account to retrieve workspaces from. Defaults to env.
* @returns {Promise<Array>} - The list of workspaces in the specified account.
*/
const executeFunction = async ({ accountId } = {}) => {
const baseUrl = process.env.BASE_URL; // loaded from .env
const username = process.env.BZM_USERNAME; // loaded from .env
const password = process.env.BZM_PASSWORD; // loaded from .env
const resolvedAccountId = accountId || process.env.BZM_ACCOUNT_ID;
try {
// Construct the URL with query parameters
const url = new URL(`${baseUrl}/api/v4/workspaces`);
url.searchParams.append('accountId', resolvedAccountId);
// Set up headers for the request
const headers = {
'Authorization': 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'),
'Accept': 'application/json'
};
// Perform the fetch request
const response = await fetch(url.toString(), {
method: 'GET',
headers
});
// Check if the response was successful
if (!response.ok) {
let errorData;
try {
errorData = await response.json();
} catch (jsonErr) {
errorData = await response.text();
}
throw new Error(`HTTP ${response.status} ${response.statusText}: ${typeof errorData === 'string' ? errorData : JSON.stringify(errorData)}`);
}
// Parse and return the response data
const data = await response.json();
return data;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
} else {
return { error: 'Unknown error occurred while getting workspaces.' };
}
}
};
/**
* Tool configuration for getting workspaces from an account.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'get_workspaces',
description: 'Get workspaces from a specified account.',
parameters: {
type: 'object',
properties: {
accountId: {
type: 'string',
description: 'The ID of the account to retrieve workspaces from.'
}
},
required: []
}
}
}
};
export { apiTool };