Skip to content

Commit ba34948

Browse files
authored
Merge pull request #199 from mshlz/master
add novo cep provider
2 parents c5a981a + 6b6fb22 commit ba34948

File tree

10 files changed

+373
-6
lines changed

10 files changed

+373
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,4 @@ Leia nosso guia de contribuição [aqui](CONTRIBUTING.md)
184184
## Autor
185185

186186
| [<img src="https://avatars0.githubusercontent.com/u/4248081?v=3&s=115"><br><sub>@filipedeschamps</sub>](https://github.com/filipedeschamps) |
187-
| :---: |
187+
| :---: |

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare module 'cep-promise' {
1111
export enum AvailableProviders {
1212
brasilapi = "brasilapi",
1313
correios = "correios",
14+
correiosAlt = "correios-alt",
1415
viacep = "viacep",
1516
widenet = "widenet"
1617
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/correios-alt.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict'
2+
3+
import fetch from 'node-fetch'
4+
import ServiceError from '../errors/service.js'
5+
6+
export default function fetchCorreiosAltAPIService(
7+
cepWithLeftPad,
8+
configurations
9+
) {
10+
const url = 'https://buscacepinter.correios.com.br/app/cep/carrega-cep.php'
11+
const options = {
12+
method: 'POST',
13+
mode: 'cors',
14+
headers: {
15+
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
16+
},
17+
body: `cep=${cepWithLeftPad}`,
18+
timeout: configurations.timeout || 30000
19+
}
20+
21+
return fetch(url, options)
22+
.then(parseResponse)
23+
.then(extractCepValuesFromResponse)
24+
.catch(throwApplicationError)
25+
}
26+
27+
function parseResponse(response) {
28+
return response.json().then(result => {
29+
if (result.total === 0 || result.erro || result.dados[0].cep === "") {
30+
throw new Error('CEP não encontrado na base dos Correios.')
31+
}
32+
return result
33+
})
34+
}
35+
36+
function extractCepValuesFromResponse(response) {
37+
const firstCep = response.dados[0]
38+
return {
39+
cep: firstCep.cep,
40+
state: firstCep.uf,
41+
city: firstCep.localidade,
42+
neighborhood: firstCep.bairro,
43+
street: firstCep.logradouroDNEC,
44+
service: 'correios-alt'
45+
}
46+
}
47+
48+
function throwApplicationError(error) {
49+
const serviceError = new ServiceError({
50+
message: error.message,
51+
service: 'correios-alt'
52+
})
53+
54+
if (error.name === 'FetchError') {
55+
serviceError.message = 'Erro ao se conectar com o serviço dos Correios Alt.'
56+
}
57+
58+
throw serviceError
59+
}

src/services/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Correios from './correios'
2+
import CorreiosAlt from './correios-alt'
23
import ViaCep from './viacep'
34
import WideNet from './widenet'
45
import BrasilAPI from './brasilapi.js'
@@ -16,6 +17,7 @@ export function getAvailableServices () {
1617

1718
return {
1819
correios: Correios,
20+
'correios-alt': CorreiosAlt,
1921
viacep: ViaCep,
2022
widenet: WideNet,
2123
brasilapi: BrasilAPI

test/e2e/cep-promise.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ describe('[e2e] cep-promise', () => {
6161
message: 'CEP INVÁLIDO',
6262
service: 'correios'
6363
},
64+
{
65+
message: 'CEP não encontrado na base dos Correios.',
66+
service: 'correios-alt'
67+
},
6468
{
6569
message: 'CEP não encontrado na base do ViaCEP.',
6670
service: 'viacep'
@@ -98,4 +102,4 @@ describe('[e2e] cep-promise', () => {
98102
})
99103
})
100104
})
101-
})
105+
})

test/unit/cep-promise-node.spec.js

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ describe('[unit] cep-promise for node', () => {
3434
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
3535
)
3636

37+
nock('https://buscacepinter.correios.com.br')
38+
.post('/app/cep/carrega-cep.php')
39+
.replyWithFile(
40+
200,
41+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
42+
)
43+
3744
nock('https://viacep.com.br')
3845
.get('/ws/05010000/json/')
3946
.replyWithFile(
@@ -156,6 +163,13 @@ describe('[unit] cep-promise for node', () => {
156163
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
157164
)
158165

166+
nock('https://buscacepinter.correios.com.br')
167+
.post('/app/cep/carrega-cep.php')
168+
.replyWithFile(
169+
200,
170+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
171+
)
172+
159173
nock('https://viacep.com.br')
160174
.get('/ws/05010000/json/')
161175
.replyWithFile(
@@ -199,6 +213,13 @@ describe('[unit] cep-promise for node', () => {
199213
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
200214
)
201215

216+
nock('https://buscacepinter.correios.com.br')
217+
.post('/app/cep/carrega-cep.php')
218+
.replyWithFile(
219+
200,
220+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
221+
)
222+
202223
nock('https://viacep.com.br')
203224
.get('/ws/05010000/json/')
204225
.replyWithFile(
@@ -242,6 +263,64 @@ describe('[unit] cep-promise for node', () => {
242263
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
243264
)
244265

266+
nock('https://buscacepinter.correios.com.br')
267+
.post('/app/cep/carrega-cep.php')
268+
.replyWithFile(
269+
200,
270+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
271+
)
272+
273+
nock('https://viacep.com.br')
274+
.get('/ws/05010000/json/')
275+
.replyWithFile(
276+
200,
277+
path.join(__dirname, '/fixtures/viacep-cep-99999999-error.json')
278+
)
279+
280+
nock('https://cep.widenet.host')
281+
.get('/busca-cep/api/cep/05010000.json')
282+
.replyWithFile(
283+
200,
284+
path.join(__dirname, '/fixtures/widenet-cep-99999999-error.json')
285+
)
286+
287+
nock('https://brasilapi.com.br/')
288+
.get('/api/cep/v1/99999999')
289+
.replyWithFile(
290+
404,
291+
path.join(__dirname, '/fixtures/brasilapi-cep-99999999-error.json')
292+
)
293+
294+
295+
return cep('05010000')
296+
.then(address => expect(address).to.deep.equal({
297+
cep: '05010000',
298+
state: 'SP',
299+
city: 'São Paulo',
300+
neighborhood: 'Perdizes',
301+
street: 'Rua Caiubi',
302+
service: 'correios'
303+
})
304+
)
305+
})
306+
})
307+
308+
describe('Should succeed only with correios-alt service', () => {
309+
it('should fulfill with correct address', () => {
310+
nock('https://apps.correios.com.br')
311+
.post('/SigepMasterJPA/AtendeClienteService/AtendeCliente')
312+
.replyWithFile(
313+
500,
314+
path.join(__dirname, '/fixtures/response-unknown-format.xml')
315+
)
316+
317+
nock('https://buscacepinter.correios.com.br')
318+
.post('/app/cep/carrega-cep.php')
319+
.replyWithFile(
320+
200,
321+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
322+
)
323+
245324
nock('https://viacep.com.br')
246325
.get('/ws/05010000/json/')
247326
.replyWithFile(
@@ -271,7 +350,7 @@ describe('[unit] cep-promise for node', () => {
271350
city: 'São Paulo',
272351
neighborhood: 'Perdizes',
273352
street: 'Rua Caiubi',
274-
service: 'correios'
353+
service: 'correios-alt'
275354
})
276355
)
277356
})
@@ -286,6 +365,13 @@ describe('[unit] cep-promise for node', () => {
286365
path.join(__dirname, '/fixtures/response-unknown-format.xml')
287366
)
288367

368+
nock('https://buscacepinter.correios.com.br')
369+
.post('/app/cep/carrega-cep.php')
370+
.replyWithFile(
371+
200,
372+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
373+
)
374+
289375
nock('https://viacep.com.br')
290376
.get('/ws/05010000/json/')
291377
.replyWithFile(
@@ -329,6 +415,13 @@ describe('[unit] cep-promise for node', () => {
329415
path.join(__dirname, '/fixtures/response-unknown-format.xml')
330416
)
331417

418+
nock('https://buscacepinter.correios.com.br')
419+
.post('/app/cep/carrega-cep.php')
420+
.replyWithFile(
421+
200,
422+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
423+
)
424+
332425
nock('https://viacep.com.br')
333426
.get('/ws/05010000/json/')
334427
.replyWithFile(
@@ -371,6 +464,13 @@ describe('[unit] cep-promise for node', () => {
371464
path.join(__dirname, '/fixtures/response-unknown-format.xml')
372465
)
373466

467+
nock('https://buscacepinter.correios.com.br')
468+
.post('/app/cep/carrega-cep.php')
469+
.replyWithFile(
470+
200,
471+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
472+
)
473+
374474
nock('https://viacep.com.br')
375475
.get('/ws/05010000/json/')
376476
.replyWithFile(
@@ -413,6 +513,13 @@ describe('[unit] cep-promise for node', () => {
413513
path.join(__dirname, '/fixtures/response-bad-xml.xml')
414514
)
415515

516+
nock('https://buscacepinter.correios.com.br')
517+
.post('/app/cep/carrega-cep.php')
518+
.replyWithFile(
519+
200,
520+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
521+
)
522+
416523
nock('https://viacep.com.br')
417524
.get('/ws/05010000/json/')
418525
.replyWithFile(
@@ -454,6 +561,13 @@ describe('[unit] cep-promise for node', () => {
454561
'getaddrinfo ENOTFOUND apps.correios.com.br apps.correios.com.br:443'
455562
)
456563

564+
nock('https://buscacepinter.correios.com.br')
565+
.post('/app/cep/carrega-cep.php')
566+
.replyWithFile(
567+
200,
568+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
569+
)
570+
457571
nock('https://viacep.com.br')
458572
.get('/ws/05010000/json/')
459573
.replyWithFile(
@@ -496,6 +610,13 @@ describe('[unit] cep-promise for node', () => {
496610
path.join(__dirname, '/fixtures/response-cep-not-found.xml')
497611
)
498612

613+
nock('https://buscacepinter.correios.com.br')
614+
.post('/app/cep/carrega-cep.php')
615+
.replyWithFile(
616+
200,
617+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
618+
)
619+
499620
nock('https://viacep.com.br')
500621
.get('/ws/99999999/json/')
501622
.replyWithFile(
@@ -529,6 +650,10 @@ describe('[unit] cep-promise for node', () => {
529650
message: 'CEP NAO ENCONTRADO',
530651
service: 'correios'
531652
},
653+
{
654+
message: 'CEP não encontrado na base dos Correios.',
655+
service: 'correios-alt'
656+
},
532657
{
533658
message: 'CEP não encontrado na base do ViaCEP.',
534659
service: 'viacep'
@@ -571,6 +696,10 @@ describe('[unit] cep-promise for node', () => {
571696
'getaddrinfo ENOTFOUND apps.correios.com.br apps.correios.com.br:443'
572697
)
573698

699+
nock('https://buscacepinter.correios.com.br')
700+
.post('/app/cep/carrega-cep.php')
701+
.reply(400, '<h2>Bad Request (400)</h2>')
702+
574703
nock('https://viacep.com.br')
575704
.get('/ws/05010000/json/')
576705
.reply(400, '<h2>Bad Request (400)</h2>')
@@ -595,6 +724,10 @@ describe('[unit] cep-promise for node', () => {
595724
message: 'Erro ao se conectar com o serviço dos Correios.',
596725
service: 'correios'
597726
},
727+
{
728+
message: 'Erro ao se conectar com o serviço dos Correios Alt.',
729+
service: 'correios-alt'
730+
},
598731
{
599732
message: 'Erro ao se conectar com o serviço ViaCEP.',
600733
service: 'viacep'
@@ -623,6 +756,10 @@ describe('[unit] cep-promise for node', () => {
623756
path.join(__dirname, '/fixtures/response-bad-xml.xml')
624757
)
625758

759+
nock('https://buscacepinter.correios.com.br')
760+
.post('/app/cep/carrega-cep.php')
761+
.reply(200, {erro:true})
762+
626763
nock('https://viacep.com.br')
627764
.get('/ws/05010000/json/')
628765
.reply(400, '<h2>Bad Request (400)</h2>')
@@ -647,6 +784,10 @@ describe('[unit] cep-promise for node', () => {
647784
message: 'Não foi possível interpretar o XML de resposta.',
648785
service: 'correios'
649786
},
787+
{
788+
message: 'CEP não encontrado na base dos Correios.',
789+
service: 'correios-alt'
790+
},
650791
{
651792
message: 'Erro ao se conectar com o serviço ViaCEP.',
652793
service: 'viacep'
@@ -674,6 +815,10 @@ describe('[unit] cep-promise for node', () => {
674815
'getaddrinfo ENOTFOUND apps.correios.com.br apps.correios.com.br:443'
675816
)
676817

818+
nock('https://buscacepinter.correios.com.br')
819+
.post('/app/cep/carrega-cep.php')
820+
.reply(200, {erro:true})
821+
677822
nock('https://viacep.com.br')
678823
.get('/ws/05010000/json/')
679824
.replyWithError(
@@ -704,6 +849,10 @@ describe('[unit] cep-promise for node', () => {
704849
message: 'Erro ao se conectar com o serviço dos Correios.',
705850
service: 'correios'
706851
},
852+
{
853+
message: 'CEP não encontrado na base dos Correios.',
854+
service: 'correios-alt'
855+
},
707856
{
708857
message: 'Erro ao se conectar com o serviço ViaCEP.',
709858
service: 'viacep'

0 commit comments

Comments
 (0)