export interface PrefeituraInfo { codigoMunicipio: number nomePrefeitura: string dominio: string template: string pathLogo?: string pathBackground?: string } interface ApiEnvelope { data: T } const CACHE_TTL_SECONDS = 300 // 5 min export async function fetchPrefeituraInfo(dominio: string): Promise { if (!dominio) return null const cacheKey = `prefeitura:${dominio}` let redisAvailable = true try { const cached = await useRedis().get(cacheKey) if (cached) { try { return JSON.parse(cached) as PrefeituraInfo } catch { // cache corrompido — segue para refetch } } } catch { redisAvailable = false } const cfg = useRuntimeConfig() try { const res = await $fetch>( `${cfg.coreApiUrl}/api/v1/publico/prefeitura/${encodeURIComponent(dominio)}`, { timeout: 8000 }, ) const info = res?.data if (!info?.codigoMunicipio) return null if (redisAvailable) { await useRedis().set(cacheKey, JSON.stringify(info), 'EX', CACHE_TTL_SECONDS).catch(() => {}) } return info } catch (err) { console.error(`[prefeitura] lookup falhou para '${dominio}':`, (err as Error).message) return null } }