+server.js 1.48 KB
const API_BASE = 'http://136.112.29.74/api/ubigeo';

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

  if (!codigo) {
    return new Response(JSON.stringify({ error: 'Missing codigo' }), {
      status: 400, headers: { 'Content-Type': 'application/json' }
    });
  }

  let apiUrl;
  if (tipo === 'detalle') {
    apiUrl = `${API_BASE}/${codigo}`;
  } else if (tipo === 'objetos') {
    apiUrl = `${API_BASE}/${codigo}/objetos`;
  } else if (tipo === 'finfuns') {
    apiUrl = `${API_BASE}/${codigo}/finfuns`;
  } else if (tipo === 'actecos') {
    apiUrl = `${API_BASE}/${codigo}/actecos`;
  } else if (tipo === 'clasificador') {
    apiUrl = `${API_BASE}/clasificador`;
  } else {
    return new Response(JSON.stringify({ error: 'Invalid tipo' }), {
      status: 400, headers: { 'Content-Type': 'application/json' }
    });
  }

  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 (err) {
    return new Response(JSON.stringify({ error: 'Failed to fetch' }), {
      status: 500, headers: { 'Content-Type': 'application/json' }
    });
  }
}