(request, response)
| 7 | 'max-age=0, s-maxage=86400, stale-while-revalidate, public'; |
| 8 | |
| 9 | async function getCepWithLocation(request, response) { |
| 10 | const requestedCep = request.query.cep; |
| 11 | |
| 12 | response.setHeader('Cache-Control', CACHE_CONTROL_HEADER_VALUE); |
| 13 | |
| 14 | try { |
| 15 | const cepFromCepPromise = await fetchCep(requestedCep); |
| 16 | const location = await fetchGeocoordinateFromBrazilLocation( |
| 17 | cepFromCepPromise |
| 18 | ); |
| 19 | |
| 20 | const timezoneName = await fetchTimezoneNameFromBrazilLocation({ |
| 21 | ...location, |
| 22 | ...cepFromCepPromise, |
| 23 | }); |
| 24 | |
| 25 | if (!cepFromCepPromise.street) { |
| 26 | cepFromCepPromise.street = null; |
| 27 | } |
| 28 | |
| 29 | if (!cepFromCepPromise.neighborhood) { |
| 30 | cepFromCepPromise.neighborhood = null; |
| 31 | } |
| 32 | |
| 33 | response.status(200); |
| 34 | response.json({ ...cepFromCepPromise, timezoneName, location }); |
| 35 | } catch (error) { |
| 36 | if (error.name === 'CepPromiseError') { |
| 37 | switch (error.type) { |
| 38 | case 'validation_error': |
| 39 | response.status(400); |
| 40 | break; |
| 41 | case 'service_error': |
| 42 | response.status(404); |
| 43 | break; |
| 44 | default: |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | response.json(error); |
| 49 | return; |
| 50 | } else if (error.type === 'bad_request') { |
| 51 | throw error; |
| 52 | } |
| 53 | |
| 54 | response.status(500); |
| 55 | response.json(error); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | export default app({ cache: 172800 }).get(getCepWithLocation); |
nothing calls this directly
no test coverage detected