Foundry and discord bot
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
This commit is contained in:
34
backend/src/services/foundry.js
Normal file
34
backend/src/services/foundry.js
Normal file
@@ -0,0 +1,34 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user