(cep)
| 48 | } |
| 49 | |
| 50 | export 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 | } |
no test coverage detected