- Item 10: Convert to ES modules with import/export, single module entry point - Item 11: Replace inline styles with CSS classes (background overlay, card animations, highlight effect, config modal form elements) - Item 12: Move ConfigManager modal HTML from JS template literal to <template> element in index.html - Item 13: Replace deprecated url.parse() with new URL() in server.js and update route handlers to use searchParams - Item 14: Replace JSON.parse/stringify deep clone with structuredClone() - Item 15: Remove dead JSON-fixing regex code from departures.js route Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
105 lines
3.0 KiB
JavaScript
105 lines
3.0 KiB
JavaScript
/**
|
|
* Departures route handler
|
|
* Handles fetching and returning transit departure data
|
|
*/
|
|
|
|
const https = require('https');
|
|
|
|
/**
|
|
* Fetch departures for a specific site from SL Transport API
|
|
* @param {string} siteId - The site ID to fetch departures for
|
|
* @returns {Promise<Object>} - Departure data
|
|
*/
|
|
function fetchDeparturesForSite(siteId) {
|
|
return new Promise((resolve, reject) => {
|
|
const apiUrl = `https://transport.integration.sl.se/v1/sites/${siteId}/departures`;
|
|
console.log(`Fetching data from: ${apiUrl}`);
|
|
|
|
https.get(apiUrl, (res) => {
|
|
let data = '';
|
|
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
try {
|
|
const parsedData = JSON.parse(data);
|
|
resolve(parsedData);
|
|
} catch (error) {
|
|
console.error('Error parsing departures API response:', error);
|
|
resolve({
|
|
departures: [],
|
|
error: 'Failed to parse API response: ' + error.message
|
|
});
|
|
}
|
|
});
|
|
}).on('error', (error) => {
|
|
console.error('Error fetching departures:', error);
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Fetch departures for all enabled sites
|
|
* @param {Array} enabledSites - Array of enabled site configurations
|
|
* @returns {Promise<Object>} - Object with sites array containing departure data
|
|
*/
|
|
async function fetchAllDepartures(enabledSites) {
|
|
if (enabledSites.length === 0) {
|
|
return { sites: [], error: 'No enabled sites configured' };
|
|
}
|
|
|
|
try {
|
|
const sitesPromises = enabledSites.map(async (site) => {
|
|
try {
|
|
const departureData = await fetchDeparturesForSite(site.id);
|
|
return {
|
|
siteId: site.id,
|
|
siteName: site.name,
|
|
data: departureData
|
|
};
|
|
} catch (error) {
|
|
console.error(`Error fetching departures for site ${site.id}:`, error);
|
|
return {
|
|
siteId: site.id,
|
|
siteName: site.name,
|
|
error: error.message
|
|
};
|
|
}
|
|
});
|
|
|
|
const results = await Promise.all(sitesPromises);
|
|
return { sites: results };
|
|
} catch (error) {
|
|
console.error('Error fetching all departures:', error);
|
|
return { sites: [], error: error.message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle departures API endpoint
|
|
* @param {http.IncomingMessage} req - HTTP request object
|
|
* @param {http.ServerResponse} res - HTTP response object
|
|
* @param {Object} config - Application configuration
|
|
*/
|
|
async function handleDepartures(req, res, config) {
|
|
try {
|
|
const enabledSites = config.sites.filter(site => site.enabled);
|
|
const data = await fetchAllDepartures(enabledSites);
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify(data));
|
|
} catch (error) {
|
|
console.error('Error handling departures request:', error);
|
|
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ error: error.message }));
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
handleDepartures,
|
|
fetchDeparturesForSite,
|
|
fetchAllDepartures
|
|
};
|