+server.js
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { API_HOST } from '$lib/api.js';
const API_BASE = API_HOST + '/api/entidad';
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' }
});
}
// For DAs (codigo contains dot like "1201.1"), extract the entidad number
const entidadNum = codigo.includes('.') ? codigo.split('.')[0] : codigo;
let apiUrl;
if (tipo === 'detalle') {
apiUrl = `${API_BASE}/${entidadNum}`;
} else if (tipo === 'objetos') {
apiUrl = `${API_BASE}/${entidadNum}/objetos`;
} else if (tipo === 'finfuns') {
apiUrl = `${API_BASE}/${entidadNum}/finfuns`;
} else if (tipo === 'actecos') {
apiUrl = `${API_BASE}/${entidadNum}/actecos`;
} else if (tipo === 'rubros') {
apiUrl = `${API_BASE}/${entidadNum}/rubros`;
} else if (tipo === 'organismos') {
apiUrl = `${API_BASE}/${entidadNum}/organismos`;
} 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' }
});
}
}