Skip to content

Commit fe05d46

Browse files
Add postmon as new provider
1 parent 43fbb00 commit fe05d46

File tree

9 files changed

+460
-435
lines changed

9 files changed

+460
-435
lines changed

dist/cep-promise-browser.js

Lines changed: 172 additions & 234 deletions
Large diffs are not rendered by default.

dist/cep-promise-browser.min.js

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

dist/cep-promise.js

Lines changed: 141 additions & 199 deletions
Large diffs are not rendered by default.

dist/cep-promise.min.js

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/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Correios from './correios'
22
import CorreiosAlt from './correios-alt'
33
import ViaCep from './viacep'
4+
import Postmon from './postmon'
45
import WideNet from './widenet'
56
import BrasilAPI from './brasilapi.js'
67

@@ -11,6 +12,7 @@ export function getAvailableServices () {
1112
return {
1213
viacep: ViaCep,
1314
widenet: WideNet,
15+
postmon: Postmon,
1416
brasilapi: BrasilAPI
1517
}
1618
}
@@ -20,6 +22,7 @@ export function getAvailableServices () {
2022
'correios-alt': CorreiosAlt,
2123
viacep: ViaCep,
2224
widenet: WideNet,
25+
postmon: Postmon,
2326
brasilapi: BrasilAPI
2427
}
2528
}

src/services/postmon.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict'
2+
3+
import fetch from 'node-fetch'
4+
import ServiceError from '../errors/service.js'
5+
6+
export default function fetchViaCepService (cepWithLeftPad, configurations) {
7+
const url = `https://api.postmon.com.br/v1/cep/${cepWithLeftPad}`
8+
const options = {
9+
method: 'GET',
10+
mode: 'cors',
11+
headers: {
12+
'content-type': 'application/json;charset=utf-8'
13+
},
14+
timeout: configurations.timeout || 30000
15+
}
16+
17+
if (typeof window == 'undefined') {
18+
options.headers['user-agent'] = 'cep-promise'
19+
}
20+
21+
return fetch(url, options)
22+
.then(analyzeAndParseResponse)
23+
.then(checkForPostmanError)
24+
.then(extractCepValuesFromResponse)
25+
.catch(throwApplicationError)
26+
}
27+
28+
function analyzeAndParseResponse (response) {
29+
if (response.ok) {
30+
return response.json()
31+
}
32+
33+
throw Error('Erro ao se conectar com o serviço Postmon.')
34+
}
35+
36+
function checkForPostmanError (responseObject) {
37+
if (!responseObject) {
38+
throw new Error('CEP não encontrado na base do Postmon.')
39+
}
40+
41+
return responseObject
42+
}
43+
44+
function extractCepValuesFromResponse (responseObject) {
45+
return {
46+
cep: responseObject.cep.replace('-', ''),
47+
state: responseObject.estado,
48+
city: responseObject.cidade,
49+
neighborhood: responseObject.bairro,
50+
street: responseObject.logradouro,
51+
service: 'postmon'
52+
}
53+
}
54+
55+
function throwApplicationError (error) {
56+
const serviceError = new ServiceError({
57+
message: error.message,
58+
service: 'viacep'
59+
})
60+
61+
if (error.name === 'FetchError') {
62+
serviceError.message = 'Erro ao se conectar com o serviço Postmon.'
63+
}
64+
65+
throw serviceError
66+
}

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ describe('when invoked with providers parameter', () => {
142142
200,
143143
path.join(__dirname, '/fixtures/viacep-cep-05010000-found.json')
144144
)
145+
const postmonMock = nock('https://postmon.com.br')
146+
.get('/v1/cep/05010000')
147+
.replyWithFile(
148+
200,
149+
path.join(__dirname, '/fixtures/viacep-cep-05010000-found.json')
150+
)
145151

146152
const wideNetMock = nock('https://cdn.apicep.com')
147153
.get('/file/apicep/05010-000.json')
@@ -165,6 +171,63 @@ describe('when invoked with providers parameter', () => {
165171
expect(correiosMock.isDone()).to.be.equal(false)
166172
expect(correiosAltMock.isDone()).to.be.equal(false)
167173
expect(wideNetMock.isDone()).to.be.equal(false)
174+
expect(postmonMock.isDone()).to.be.equal(false)
175+
})
176+
})
177+
})
178+
179+
describe('and the providers param is [\'postmon\']', () => {
180+
it('should call only postmon service', () => {
181+
const correiosMock = nock('https://apps.correios.com.br')
182+
.post('/SigepMasterJPA/AtendeClienteService/AtendeCliente')
183+
.replyWithFile(
184+
200,
185+
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
186+
)
187+
188+
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
189+
.post('/app/cep/carrega-cep.php')
190+
.replyWithFile(
191+
200,
192+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
193+
)
194+
195+
const viaCepMock = nock('https://viacep.com.br')
196+
.get('/ws/05010000/json/')
197+
.replyWithFile(
198+
200,
199+
path.join(__dirname, '/fixtures/viacep-cep-05010000-found.json')
200+
)
201+
const postmonMock = nock('https://postmon.com.br')
202+
.get('/v1/cep/05010000')
203+
.replyWithFile(
204+
200,
205+
path.join(__dirname, '/fixtures/postmon-cep-05010000-found.json')
206+
)
207+
208+
const wideNetMock = nock('https://cdn.apicep.com')
209+
.get('/file/apicep/05010-000.json')
210+
.replyWithFile(
211+
200,
212+
path.join(__dirname, '/fixtures/widenet-cep-05010000-found.json')
213+
)
214+
215+
return cep('05010000', { providers: ['postmon'] })
216+
.then(address => {
217+
expect(address).to.deep.equal({
218+
cep: '05010000',
219+
state: 'SP',
220+
city: 'São Paulo',
221+
neighborhood: 'Perdizes',
222+
street: 'Rua Caiubi',
223+
service: 'postmon'
224+
})
225+
226+
expect(viaCepMock.isDone()).to.be.equal(false)
227+
expect(correiosMock.isDone()).to.be.equal(false)
228+
expect(correiosAltMock.isDone()).to.be.equal(false)
229+
expect(wideNetMock.isDone()).to.be.equal(false)
230+
expect(postmonMock.isDone()).to.be.equal(true)
168231
})
169232
})
170233
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"bairro": "Perdizes",
3+
"cidade": "S\u00e3o Paulo",
4+
"logradouro": "Rua Caiubi",
5+
"estado_info": {
6+
"area_km2": "248.221,996",
7+
"codigo_ibge": "35",
8+
"nome": "S\u00e3o Paulo"
9+
},
10+
"cep": "05010000",
11+
"cidade_info": { "area_km2": "1521,11", "codigo_ibge": "3550308" },
12+
"estado": "SP"
13+
}

test/unit/fixtures/postmon-cep-99999999-error.json

Whitespace-only changes.

0 commit comments

Comments
 (0)