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

Function Cep

pages/api/cep/v1/[cep].js:12–61  ·  view source on GitHub ↗
(request, response)

Source from the content-addressed store, hash-verified

10const tempBlockedUserAgents = ['Go-http-client/2.0'];
11
12async function Cep(request, response) {
13 const clientIp =
14 request.headers['x-forwarded-for'] || request.connection.remoteAddress;
15
16 if (
17 clientIp &&
18 tempBlockedUserAgents.includes(request.headers['user-agent']) &&
19 tempBlockedIps.includes(clientIp)
20 ) {
21 response.status(429);
22 return response.send(
23 'please stop abusing our public API, join our slack to chat a bit https://join.slack.com/t/brasilapi/shared_invite/zt-1k9w5h27p-4yLWoOQqIMgwqunnHCyWCQ'
24 );
25 }
26
27 if (
28 tempBlockedUserAgents.includes(request.headers['user-agent']) &&
29 request.url.includes(pathToBlock)
30 ) {
31 response.status(429);
32 return response.send(
33 'please stop abusing our public API, join our slack to chat a bit https://join.slack.com/t/brasilapi/shared_invite/zt-1k9w5h27p-4yLWoOQqIMgwqunnHCyWCQ'
34 );
35 }
36
37 try {
38 const requestedCep = request.query.cep;
39
40 const cepResult = await fetchCep(requestedCep);
41
42 response.status(200);
43 return response.json(cepResult);
44 } catch (error) {
45 if (error.name !== 'CepPromiseError') {
46 throw error;
47 }
48
49 if (error.type === 'validation_error') {
50 throw new BadRequestError(error);
51 }
52
53 if (error.type === 'service_error') {
54 throw new NotFoundError(error);
55 }
56
57 if (error.type === 'bad_request') {
58 throw error;
59 }
60 }
61}
62
63export default app({ cache: 172800 }).get(Cep);

Callers

nothing calls this directly

Calls 1

fetchCepFunction · 0.90

Tested by

no test coverage detected