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

Function getCepWithLocation

pages/api/cep/v2/[cep].js:9–57  ·  view source on GitHub ↗
(request, response)

Source from the content-addressed store, hash-verified

7 'max-age=0, s-maxage=86400, stale-while-revalidate, public';
8
9async 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
59export default app({ cache: 172800 }).get(getCepWithLocation);

Callers

nothing calls this directly

Calls 3

fetchCepFunction · 0.90

Tested by

no test coverage detected