eregistrations-js
Version:
A JavaScript library that simplifies usage of eRegistrations APIs.
46 lines (45 loc) • 1.66 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServiceForms = getServiceForms;
/**
* Fetches forms for a specific service
* @param config Configuration object with API and auth details including serviceId
* @returns Service forms data
*/
async function getServiceForms(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}/forms`;
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 forms: ${response.statusText}`);
}
const data = await response.json();
console.log('Received forms response:', {
status: response.status,
dataLength: Array.isArray(data) ? data.length : 'not an array'
});
return data;
}
catch (error) {
console.error('Error fetching service forms:', error);
throw error;
}
}
;