41 lines
1.5 KiB
JavaScript
41 lines
1.5 KiB
JavaScript
async function requestFoundry(path) {
|
|
// The bridge a campaign's icon and world reads go to is the very one that
|
|
// world already pushes its snapshot to. It is often configured under
|
|
// FOUNDRY_API_CLIENT_URL alone — the address the Foundry client reaches —
|
|
// while FOUNDRY_API_URL is the optional private one the backend prefers.
|
|
// Falling back to it keeps a connected, syncing world's reads working
|
|
// instead of reporting its bridge as unset (Syndria98/dragonroll#74).
|
|
const baseUrl = process.env.FOUNDRY_API_URL || process.env.FOUNDRY_API_CLIENT_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 };
|