All checks were successful
Dev Build & Deploy Portal / build-deploy (push) Successful in 2m32s
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
export interface PrefeituraInfo {
|
|
codigoMunicipio: number
|
|
nomePrefeitura: string
|
|
dominio: string
|
|
template: string
|
|
pathLogo?: string
|
|
pathBackground?: string
|
|
}
|
|
|
|
interface ApiEnvelope<T> {
|
|
data: T
|
|
}
|
|
|
|
const CACHE_TTL_SECONDS = 300 // 5 min
|
|
|
|
export async function fetchPrefeituraInfo(dominio: string): Promise<PrefeituraInfo | null> {
|
|
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<ApiEnvelope<PrefeituraInfo>>(
|
|
`${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
|
|
}
|
|
}
|