mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* refactor: dedupe edge api json response assembly * refactor: expand jsonResponse helper to all edge functions Roll out jsonResponse() from _json-response.js to 16 files (14 handlers + 2 shared helpers), eliminating 55 instances of the new Response(JSON.stringify(...)) boilerplate. Only exception: health.js uses JSON.stringify(body, null, indent) for pretty-print mode, which is incompatible with the helper signature. Replaced local jsonResponse/json() definitions in contact.js, register-interest.js, and cache-purge.js with the shared import.
25 lines
763 B
JavaScript
25 lines
763 B
JavaScript
import { createRelayHandler } from './_relay.js';
|
|
import { jsonResponse } from './_json-response.js';
|
|
|
|
export const config = { runtime: 'edge' };
|
|
|
|
export default createRelayHandler({
|
|
buildRelayPath: (_req, url) => {
|
|
const endpoint = url.searchParams.get('endpoint');
|
|
return endpoint === 'history' ? '/oref/history' : '/oref/alerts';
|
|
},
|
|
forwardSearch: false,
|
|
timeout: 12000,
|
|
onlyOk: true,
|
|
cacheHeaders: () => ({
|
|
'Cache-Control': 'public, max-age=60, s-maxage=300, stale-while-revalidate=120, stale-if-error=900',
|
|
}),
|
|
fallback: (_req, corsHeaders) => jsonResponse({
|
|
configured: false,
|
|
alerts: [],
|
|
historyCount24h: 0,
|
|
timestamp: new Date().toISOString(),
|
|
error: 'No data source available',
|
|
}, 503, corsHeaders),
|
|
});
|