Skip to content

Commit 8d0a223

Browse files
authored
Merge pull request #19 from Theryston/feature/CNH
Feature/cnh
2 parents c7a94f7 + f9f79a5 commit 8d0a223

File tree

7 files changed

+126
-1
lines changed

7 files changed

+126
-1
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,48 @@ return:
134134
}
135135
```
136136

137+
## CNH:
138+
139+
CNH is one of the classes that 4devs has, this class is able to generate and validate a CNH based on the services on the pages [https://www.4devs.com.br/validador_cnh](https://www.4devs.com.br/validador_cnh) and [https://www.4devs.com.br/gerador_de_cnh](https://www.4devs.com.br/gerador_de_cnh)
140+
141+
### validate a CNH
142+
143+
```ts
144+
import { CNH } from '4devs';
145+
146+
const { isValid } = await CNH.validate({
147+
cnh: '79973493843',
148+
});
149+
```
150+
151+
parameters:
152+
153+
- `cnh` (required) the cnh you want to validate
154+
155+
return:
156+
157+
```ts
158+
{
159+
isValid: boolean;
160+
}
161+
```
162+
163+
### generate a new CNH
164+
165+
```ts
166+
import { CNH } from '4devs';
167+
168+
const { cnh } = await CNH.generate();
169+
```
170+
171+
return:
172+
173+
```ts
174+
{
175+
cnh: string;
176+
}
177+
```
178+
137179
# Footer:
138180

139181
This package is created using the website [https://www.4devs.com.br](https://www.4devs.com.br/). but the site has no responsibility for this content it was made in an open source way and without contacting the site.

__test__/CNH.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { CNH } from '../lib/index';
2+
3+
const mockCNH = {
4+
cnh: '27872004149',
5+
isValid: true,
6+
};
7+
8+
describe('CNH', () => {
9+
it('should create an instance', async () => {
10+
expect(new CNH()).toBeTruthy();
11+
});
12+
13+
it('should to generate a new CNH', async () => {
14+
const { cnh } = await CNH.generate();
15+
expect(cnh).toBeTruthy();
16+
expect(cnh.length).toBeGreaterThan(10);
17+
});
18+
19+
it('should validate the CNH', async () => {
20+
const { isValid } = await CNH.validate({ cnh: mockCNH.cnh });
21+
expect(isValid).toBeTruthy();
22+
});
23+
});

lib/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Certificates } from './modules/Certificates';
22
import { CPF } from './modules/CPF';
3+
import { CNH } from './modules/CNH';
34

4-
export { Certificates, CPF };
5+
export { Certificates, CPF, CNH };

lib/interfaces/ICNH.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface ICNH {
2+
cnh: string;
3+
}
4+
5+
export interface ICNHValid {
6+
isValid: boolean;
7+
}

lib/modules/CNH/GenerateCNH.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ICNH } from '../../interfaces/ICNH';
2+
import { RequestUtil } from '../../utils/RequestUtil';
3+
4+
export class GenerateCNH {
5+
requestUtil = new RequestUtil();
6+
async execute(): Promise<ICNH> {
7+
const { data: cnh }: { data: string } = await this.requestUtil.post({
8+
path: '/ferramentas_online.php',
9+
json: {
10+
acao: 'gerar_cnh',
11+
},
12+
});
13+
14+
return {
15+
cnh: cnh,
16+
};
17+
}
18+
}

lib/modules/CNH/ValidateCNH.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { RequestUtil } from '../../utils/RequestUtil';
2+
import { ICNHValid } from '../../interfaces/ICNH';
3+
4+
export class ValidateCNH {
5+
requestUtil = new RequestUtil();
6+
async execute({ cnh }: { cnh: string }): Promise<ICNHValid> {
7+
const { data }: { data: string } = await this.requestUtil.post({
8+
path: '/ferramentas_online.php',
9+
json: {
10+
acao: 'validar_cnh',
11+
txt_cnh: cnh,
12+
},
13+
});
14+
const isValid = data.toLowerCase().includes('verdadeiro');
15+
16+
return {
17+
isValid,
18+
};
19+
}
20+
}

lib/modules/CNH/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ICNH, ICNHValid } from 'lib/interfaces/ICNH';
2+
import { GenerateCNH } from './GenerateCNH';
3+
import { ValidateCNH } from './ValidateCNH';
4+
5+
export class CNH {
6+
public static async validate({ cnh }: { cnh: string }): Promise<ICNHValid> {
7+
const validateCNH = new ValidateCNH();
8+
return await validateCNH.execute({ cnh });
9+
}
10+
public static async generate(): Promise<ICNH> {
11+
const generate = new GenerateCNH();
12+
return generate.execute();
13+
}
14+
}

0 commit comments

Comments
 (0)