All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
35 lines
1015 B
JavaScript
35 lines
1015 B
JavaScript
async function requestFoundry(path) {
|
|
const baseUrl = process.env.FOUNDRY_API_URL;
|
|
const token = process.env.FOUNDRY_API_TOKEN;
|
|
|
|
if (!baseUrl || !token) {
|
|
const error = new Error('Foundry API is not configured.');
|
|
error.code = 'FOUNDRY_NOT_CONFIGURED';
|
|
throw error;
|
|
}
|
|
|
|
const normalizedBase = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
|
|
const url = new URL(path.replace(/^\/+/, ''), normalizedBase);
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
},
|
|
signal: AbortSignal.timeout(10000)
|
|
});
|
|
|
|
const body = await response.text();
|
|
let data;
|
|
try {
|
|
data = JSON.parse(body);
|
|
} catch {
|
|
const error = new Error('Foundry API returned an invalid response.');
|
|
error.code = 'FOUNDRY_INVALID_RESPONSE';
|
|
throw error;
|
|
}
|
|
|
|
return { status: response.status, data };
|
|
}
|
|
|
|
module.exports = { requestFoundry };
|