feat: create cpf endpoint (rebase #544) (#860) · BrasilAPI/BrasilAPI@bd42ba0 · GitHub
Skip to content

Commit bd42ba0

Browse files
lucianopfrodcoffaniHermes Agent
authored
feat: create cpf endpoint (rebase #544) (#860)
* feat: create cpf endpoint Co-authored-by: Hermes Agent <noreply@nousresearch.com> Ai-tool: hermes Ai-model: deepseek-v4-flash Ai-os: wsl * test(cpf): converte para ESM (vitest sem globals) Co-authored-by: Hermes Agent <noreply@nousresearch.com> Ai-tool: hermes Ai-model: deepseek-v4-flash Ai-os: wsl --------- Co-authored-by: Rodrigo Coffani <rodrigo.coffani@gmail.com> Co-authored-by: Hermes Agent <noreply@nousresearch.com>
1 parent 0500242 commit bd42ba0

5 files changed

Lines changed: 285 additions & 0 deletions

File tree

‎pages/api/cpf/v1/[cpf].js‎

Lines changed: 60 additions & 0 deletions

‎pages/docs/doc/cpf.json‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"tags": [
3+
{
4+
"name": "CPF",
5+
"description": "Informações referentes a CPFs"
6+
}
7+
],
8+
9+
"paths": {
10+
"/cpf/v1/{cpf}": {
11+
"get": {
12+
"tags": ["CPF"],
13+
"summary": "Verifica o CPF e caso válido retorna sua região",
14+
"description": "",
15+
"parameters": [
16+
{
17+
"name": "CPF",
18+
"description": "O CPF (Cadastro de Pessoas Físicas) é um sistema de códigos de registro de contribuintes mantido pela Receita Federal que representa unicamente uma pessoa física no Brasil.\n",
19+
"in": "path",
20+
"required": false,
21+
"schema": {
22+
"type": "string",
23+
"format": "string"
24+
}
25+
}
26+
],
27+
"responses": {
28+
"200": {
29+
"description": "Success",
30+
"content": {
31+
"application/json": {
32+
"schema": {
33+
"$ref": "#/components/schemas/CPFInfo"
34+
}
35+
}
36+
}
37+
},
38+
"400": {
39+
"description": "CPF em um padrão inválido",
40+
"content": {
41+
"application/json": {
42+
"schema": {
43+
"$ref": "#/components/schemas/ErrorMessage"
44+
},
45+
"example": {
46+
"name": "BadRequestError",
47+
"message": "CPF 10723555072 inválido.",
48+
"type": "bad_request"
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
},
57+
"components": {
58+
"schemas": {
59+
"CPFInfo": {
60+
"title": "CPF",
61+
"required": ["cpf", "isValid", "rf", "ufs"],
62+
"type": "object",
63+
"properties": {
64+
"cpf": {
65+
"type": "string"
66+
},
67+
"isValid": {
68+
"type": "boolean"
69+
},
70+
"rf": {
71+
"type": "integer"
72+
},
73+
"ufs": {
74+
"type": "object"
75+
}
76+
},
77+
"example": {
78+
"cpf": "10723555079",
79+
"isValid": true,
80+
"rf": 0,
81+
"ufs": ["RS"]
82+
}
83+
}
84+
}
85+
}
86+
}

‎pages/sitemap.xml/index.jsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const getDocs = () => {
2626
'PIX',
2727
'CAMBIO',
2828
'TICKERS',
29+
'CPF',
2930
];
3031
};
3132

‎services/cpf/cpf.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export const formatCpf = (cpf) => {
2+
return cpf.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4');
3+
};
4+
5+
export const cpfIsValid = (cpf) => {
6+
const arr = cpf.split('');
7+
if (arr.every((digit) => digit === arr[0])) return false;
8+
let add = 0;
9+
let rev = 0;
10+
for (let i = 0; i < 9; i += 1) add += parseInt(cpf.charAt(i), 10) * (10 - i);
11+
rev = 11 - (add % 11);
12+
if (rev === 10 || rev === 11) rev = 0;
13+
if (rev !== parseInt(cpf.charAt(9), 10)) return false;
14+
15+
add = 0;
16+
for (let i = 0; i < 10; i += 1) add += parseInt(cpf.charAt(i), 10) * (11 - i);
17+
rev = 11 - (add % 11);
18+
if (rev === 10 || rev === 11) rev = 0;
19+
if (rev !== parseInt(cpf.charAt(10), 10)) return false;
20+
return true;
21+
};
22+
23+
export const getCpfRegionAndUfs = (requestedCpf) => {
24+
const cpfDigit = Number(requestedCpf[8]);
25+
26+
const regionsAndufs = [
27+
{
28+
regionNumber: 0,
29+
ufs: ['RS'],
30+
},
31+
{
32+
regionNumber: 1,
33+
ufs: ['DF', 'GO', 'MT', 'MS', 'TO'],
34+
},
35+
{
36+
regionNumber: 2,
37+
ufs: ['AC', 'AP', 'AM', 'PA', 'RO', 'RR'],
38+
},
39+
{
40+
regionNumber: 3,
41+
ufs: ['CE', 'MA', 'PI'],
42+
},
43+
{
44+
regionNumber: 4,
45+
ufs: ['AL', 'PB', 'PE', 'RN'],
46+
},
47+
{
48+
regionNumber: 5,
49+
ufs: ['BA', 'SE'],
50+
},
51+
{
52+
regionNumber: 6,
53+
ufs: ['MG'],
54+
},
55+
{
56+
regionNumber: 7,
57+
ufs: ['ES', 'RJ'],
58+
},
59+
{
60+
regionNumber: 8,
61+
ufs: ['SP'],
62+
},
63+
{
64+
regionNumber: 9,
65+
ufs: ['PR', 'SC'],
66+
},
67+
];
68+
return regionsAndufs[cpfDigit];
69+
};

‎tests/cpf-v1.test.js‎

Lines changed: 69 additions & 0 deletions

0 commit comments

Comments
 (0)