get-project-list.js•2.41 kB
import dotenv from 'dotenv';
dotenv.config();
/**
* Function to get projects from the specified workspace.
*
* @param {Object} args - Arguments for the request.
* @param {string} [args.workspaceId] - The ID of the workspace to retrieve projects from. Defaults to env.
* @returns {Promise<Array>} - The list of projects in the specified workspace.
*/
const executeFunction = async ({ workspaceId } = {}) => {
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 resolvedWorkspaceId = workspaceId || process.env.BZM_WORKSPACE_ID;
try {
// Construct the URL with query parameters
const url = new URL(`${baseUrl}/api/v4/projects`);
url.searchParams.append('workspaceId', resolvedWorkspaceId);
// 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 projects.' };
}
}
};
/**
* Tool configuration for getting projects from a workspace.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'get_projects',
description: 'Get projects from a specified workspace.',
parameters: {
type: 'object',
properties: {
workspaceId: {
type: 'string',
description: 'The ID of the workspace to retrieve projects from.'
}
},
required: []
}
}
}
};
export { apiTool };