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

Function getCitiesByDdd

pages/api/ddd/v1/[ddd].js:8–54  ·  view source on GitHub ↗
(request, response)

Source from the content-addressed store, hash-verified

6import { getDddsData } from '@/services/ddd';
7
8async function getCitiesByDdd(request, response) {
9 try {
10 const requestedDdd = Number(request.query.ddd.toString()).toString();
11
12 const lengthDdd = requestedDdd.length;
13
14 if (lengthDdd !== 2) {
15 throw new BadRequestError({
16 message: 'DDD deve conter apenas 2 dígitos',
17 type: 'ddd_error',
18 name: 'DDD_INVALID',
19 });
20 }
21
22 const allDddData = await getDddsData();
23
24 const dddData = allDddData.filter(({ ddd }) => ddd === requestedDdd);
25
26 if (dddData.length === 0) {
27 throw new NotFoundError({
28 message: 'DDD não encontrado',
29 type: 'ddd_error',
30 name: 'DDD_NOT_FOUND',
31 });
32 }
33
34 const { state } = dddData[0];
35
36 const cities = dddData.map((ddd) => ddd.city);
37
38 const dddResult = {
39 state,
40 cities,
41 };
42
43 return response.status(200).json(dddResult);
44 } catch (error) {
45 if (error instanceof BaseError) {
46 throw error;
47 }
48
49 throw new InternalError({
50 message: 'Todos os serviços de DDD retornaram erro.',
51 type: 'service_error',
52 });
53 }
54}
55
56export default app().get(getCitiesByDdd);

Callers

nothing calls this directly

Calls 1

getDddsDataFunction · 0.90

Tested by

no test coverage detected