eregistrations-js
Version:
A JavaScript library that simplifies usage of eRegistrations APIs.
49 lines (48 loc) • 1.86 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServiceRegistrations = getServiceRegistrations;
/**
* Fetches registrations for a specific service
* @param config Configuration object with API and auth details including serviceId
* @returns Service registrations data
*/
async function getServiceRegistrations(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}/registration`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${config.token}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
if (response.status === 401) {
throw new Error('Authentication failed: Invalid or expired token');
}
else if (response.status === 404) {
throw new Error(`Service ${config.serviceId} not found`);
}
throw new Error(`Failed to fetch service registrations: ${response.statusText}`);
}
const data = await response.json();
console.log('Received registrations response:', {
status: response.status,
dataLength: Array.isArray(data) ? data.length : 'not an array'
});
return data;
}
catch (error) {
console.error('Error fetching registrations:', error);
if (error instanceof Error) {
throw error;
}
throw new Error('Unknown error occurred while fetching service registrations');
}
}
;