eregistrations-js
Version:
A JavaScript library that simplifies usage of eRegistrations APIs.
44 lines (43 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServiceBots = getServiceBots;
/**
* Fetches bots for a specific service
* @param config Configuration object with API and auth details including serviceId
* @returns Service bots data
*/
async function getServiceBots(config) {
try {
if (!config || !config.baseApiUrl) {
throw new Error('Invalid configuration: baseApiUrl is required');
}
if (!config.serviceId) {
throw new Error('serviceId is required in config');
}
const url = `${config.baseApiUrl}/bparest/bpa/v2016/06/service/${config.serviceId}/bot`;
console.log(`Fetching service bots for service ${config.serviceId}...`);
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${config.token}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
const errorBody = await response.text();
console.error(`Failed to fetch service bots: ${response.status} ${response.statusText}`);
console.error(`Error details: ${errorBody}`);
if (response.status === 404) {
throw new Error(`Service not found: ${config.serviceId}`);
}
throw new Error(`Failed to fetch service bots: ${response.status} ${response.statusText}`);
}
const bots = await response.json();
console.log(`Successfully fetched ${Array.isArray(bots) ? bots.length : 1} service bots`);
return bots;
}
catch (error) {
console.error('Error fetching service bots:', error);
throw error;
}
}