MCPcopy Create free account
hub / github.com/BrasilAPI/BrasilAPI / fetchCep

Function fetchCep

services/cep/cep.js:50–83  ·  view source on GitHub ↗
(cep)

Source from the content-addressed store, hash-verified

48}
49
50export async function fetchCep(cep) {
51 const cleanCep = onlyDigits(cep);
52
53 if (!isValidCep(cep)) {
54 const errorMessage =
55 cleanCep.length < CEP_LENGTH
56 ? `CEP informado possui menos do que ${CEP_LENGTH} caracteres.`
57 : `CEP informado possui mais do que ${CEP_LENGTH} caracteres.`;
58
59 throw new CepPromiseError({
60 message: `CEP deve conter exatamente ${CEP_LENGTH} caracteres.`,
61 type: 'validation_error',
62 errors: [
63 {
64 message: errorMessage,
65 service: 'cep_validation',
66 },
67 ],
68 });
69 }
70
71 const fetchCepPromise = () =>
72 cepPromise(cleanCep, {
73 providers,
74 timeout: 5000,
75 });
76
77 return fetchOpenCep(cleanCep).catch(() =>
78 fetchCepPromise().then(async (data) => {
79 await updateOpenCep(cleanCep).catch(() => {});
80 return data;
81 })
82 );
83}

Callers 2

getCepWithLocationFunction · 0.90
CepFunction · 0.90

Calls 5

onlyDigitsFunction · 0.85
isValidCepFunction · 0.85
fetchOpenCepFunction · 0.85
fetchCepPromiseFunction · 0.85
updateOpenCepFunction · 0.85

Tested by

no test coverage detected