FIX: Mudança de API para busca por CEP.
This commit is contained in:
parent
4260b6bf09
commit
e3c2352276
32
app.js
32
app.js
@ -26,19 +26,31 @@ function createApp() {
|
|||||||
const numero = rawNumero ? String(rawNumero).trim() : "";
|
const numero = rawNumero ? String(rawNumero).trim() : "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const viaCepUrl = `https://viacep.com.br/ws/${cep}/json/`;
|
const viaCepUrl = 'https://api.cep.rest/';
|
||||||
let viaCepData = await fetchJson(viaCepUrl);
|
let cepRestData = await fetch(viaCepUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ cep }) })
|
||||||
|
.then(response => response.json());
|
||||||
// ViaCEP com retry e backoff (corrige shadowing e trata exceções)
|
// ViaCEP com retry e backoff (corrige shadowing e trata exceções)
|
||||||
|
|
||||||
const maxAttempts = 5;
|
const maxAttempts = 5;
|
||||||
let attempt = 0;
|
let attempt = 0;
|
||||||
let retryDelayMs = 1000; // delay inicial
|
let retryDelayMs = 1000; // delay inicial
|
||||||
|
|
||||||
if (!viaCepData || viaCepData.erro) {
|
if (!cepRestData || cepRestData.erro) {
|
||||||
while (attempt < maxAttempts) {
|
while (attempt < maxAttempts) {
|
||||||
try {
|
try {
|
||||||
viaCepData = await fetchJson(viaCepUrl);
|
cepRestData = await fetch(viaCepUrl, {
|
||||||
if (viaCepData && !viaCepData.erro) break;
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ cep })
|
||||||
|
}).then(response => response.json());
|
||||||
|
if (cepRestData && !cepRestData.erro) break;
|
||||||
console.info(`[INFO] ViaCEP retornou erro ou vazio na tentativa ${attempt + 1} para CEP ${cep}`);
|
console.info(`[INFO] ViaCEP retornou erro ou vazio na tentativa ${attempt + 1} para CEP ${cep}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn(`[WARN] Erro na chamada ViaCEP (tentativa ${attempt + 1}) para ${cep}: ${e && e.message ? e.message : e}`);
|
console.warn(`[WARN] Erro na chamada ViaCEP (tentativa ${attempt + 1}) para ${cep}: ${e && e.message ? e.message : e}`);
|
||||||
@ -50,15 +62,15 @@ function createApp() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!viaCepData || viaCepData.erro) {
|
if (!cepRestData || cepRestData.erro) {
|
||||||
|
|
||||||
console.error(`[ERROR] ViaCEP falhou após ${maxAttempts} tentativas para CEP ${cep}`);
|
console.error(`[ERROR] ViaCEP falhou após ${maxAttempts} tentativas para CEP ${cep}`);
|
||||||
return res.status(404).json({ error: "CEP não encontrado" });
|
return res.status(404).json({ error: "CEP não encontrado" });
|
||||||
}
|
}
|
||||||
const logradouro = viaCepData.logradouro || "";
|
const logradouro = cepRestData.data.logradouro || "";
|
||||||
const bairro = viaCepData.bairro || "";
|
const bairro = cepRestData.data.bairro || "";
|
||||||
const cidade = viaCepData.localidade || "";
|
const cidade = cepRestData.data.localidade || "";
|
||||||
const uf = viaCepData.uf || "";
|
const uf = cepRestData.data.uf || "";
|
||||||
const endereco = `${logradouro}, ${numero}, ${bairro}, ${cidade} - ${uf}`
|
const endereco = `${logradouro}, ${numero}, ${bairro}, ${cidade} - ${uf}`
|
||||||
.replace(/, ,/g, ",")
|
.replace(/, ,/g, ",")
|
||||||
.replace(/^,\s*/, "");
|
.replace(/^,\s*/, "");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user