+server.js 1.47 KB
const API_BASE = 'https://abierto.economiayfinanzas.gob.bo/presupuesto/_api/diario';

export async function GET({ url }) {
  const tipo = url.searchParams.get('tipo') || 'status';

  const endpoints = {
    status: `${API_BASE}/status`,
    timeseries: `${API_BASE}/timeseries`,
    entidades: `${API_BASE}/entidades`,
    clasificadores: `${API_BASE}/clasificadores`,
    timeline: `${API_BASE}/timeline`
  };

  let apiUrl = endpoints[tipo];
  if (!apiUrl) {
    return new Response(JSON.stringify({ error: 'Invalid tipo' }), {
      status: 400, headers: { 'Content-Type': 'application/json' }
    });
  }

  // Pasar parámetros adicionales
  const params = new URLSearchParams();
  const entidad = url.searchParams.get('entidad');
  const dias = url.searchParams.get('dias');
  if (entidad) params.set('entidad', entidad);
  if (dias) params.set('dias', dias);
  if (params.toString()) apiUrl += `?${params.toString()}`;

  try {
    const response = await fetch(apiUrl);
    if (!response.ok) {
      return new Response(JSON.stringify({ error: `API error: ${response.status}` }), {
        status: response.status, headers: { 'Content-Type': 'application/json' }
      });
    }
    const data = await response.json();
    return new Response(JSON.stringify(data), {
      headers: { 'Content-Type': 'application/json' }
    });
  } catch {
    return new Response(JSON.stringify({ error: 'Failed to fetch' }), {
      status: 500, headers: { 'Content-Type': 'application/json' }
    });
  }
}