Skip to content

Commit 8628535

Browse files
committed
Merge pull request #206 from luisguilhermemsalmeida/master
Adiciona parametro de timeout para serviços de cep
2 parents 173af6e + 46bbc89 commit 8628535

17 files changed

+133
-62
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ dist/*
3434
!dist/cep-promise*.js
3535

3636
#Yarn
37-
yarn.lock
37+
yarn.lock
38+
39+
# Intelij IDE stuff
40+
.idea/

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,16 @@ cep('123456789123456789')
129129
// }]
130130
// }
131131
```
132+
### Options
133+
- `timeout`: Timeout em milisegundos das consultas em cada serviço. O tempo total poderá ser maior devido a limites no paralelismo.
134+
- `providers`: Lista de providers a serem usados na consulta. Default é usar todos os providers disponíveis.
132135

136+
```js
137+
import cep from 'cep-promise'
138+
cep('5010000', { timeout: 5000, providers: ['brasilapi'] })
139+
.then(console.log)
140+
141+
```
133142

134143
### Instalação
135144

dist/cep-promise-browser.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,16 @@
351351
return ServiceError;
352352
}( /*#__PURE__*/_wrapNativeSuper(Error));
353353

354-
function fetchCorreiosService(cepWithLeftPad) {
355-
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
356-
var url = "".concat(proxyURL, "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente");
354+
function fetchCorreiosService(cepWithLeftPad, configurations) {
355+
var url = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente';
357356
var options = {
358357
method: 'POST',
359358
body: "<?xml version=\"1.0\"?>\n<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cli=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">\n <soapenv:Header />\n <soapenv:Body>\n <cli:consultaCEP>\n <cep>".concat(cepWithLeftPad, "</cep>\n </cli:consultaCEP>\n </soapenv:Body>\n</soapenv:Envelope>"),
360359
headers: {
361360
'Content-Type': 'text/xml;charset=UTF-8',
362361
'cache-control': 'no-cache'
363-
}
362+
},
363+
timeout: configurations.timeout || 30000
364364
};
365365
return fetch(url, options).then(analyzeAndParseResponse)["catch"](throwApplicationError);
366366
}
@@ -434,15 +434,15 @@
434434
throw serviceError;
435435
}
436436

437-
function fetchViaCepService(cepWithLeftPad) {
438-
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
439-
var url = "".concat(proxyURL, "https://viacep.com.br/ws/").concat(cepWithLeftPad, "/json/");
437+
function fetchViaCepService(cepWithLeftPad, configurations) {
438+
var url = "https://viacep.com.br/ws/".concat(cepWithLeftPad, "/json/");
440439
var options = {
441440
method: 'GET',
442441
mode: 'cors',
443442
headers: {
444443
'content-type': 'application/json;charset=utf-8'
445-
}
444+
},
445+
timeout: configurations.timeout || 30000
446446
};
447447

448448
if (typeof window == 'undefined') {
@@ -492,15 +492,15 @@
492492
throw serviceError;
493493
}
494494

495-
function fetchWideNetService(cepWithLeftPad) {
496-
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
497-
var url = "".concat(proxyURL, "https://cep.widenet.host/busca-cep/api/cep/").concat(cepWithLeftPad, ".json");
495+
function fetchWideNetService(cepWithLeftPad, configurations) {
496+
var url = "https://cep.widenet.host/busca-cep/api/cep/".concat(cepWithLeftPad, ".json");
498497
var options = {
499498
method: 'GET',
500499
mode: 'cors',
501500
headers: {
502501
'content-type': 'application/json;charset=utf-8'
503-
}
502+
},
503+
timeout: configurations.timeout || 30000
504504
};
505505
return fetch(url, options).then(analyzeAndParseResponse$2).then(checkForWideNetError).then(extractCepValuesFromResponse$1)["catch"](throwApplicationError$2);
506506
}
@@ -545,14 +545,15 @@
545545
throw serviceError;
546546
}
547547

548-
function fetchBrasilAPIService(cepWithLeftPad) {
548+
function fetchBrasilAPIService(cepWithLeftPad, configurations) {
549549
var url = "https://brasilapi.com.br/api/cep/v1/".concat(cepWithLeftPad);
550550
var options = {
551551
method: 'GET',
552552
mode: 'cors',
553553
headers: {
554554
'content-type': 'application/json;charset=utf-8'
555-
}
555+
},
556+
timeout: configurations.timeout || 30000
556557
};
557558
return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3);
558559
}
@@ -633,7 +634,7 @@
633634
}
634635

635636
function validateProviders(providers) {
636-
var availableProviders = ['brasilapi', 'correios', 'viacep', 'widenet'];
637+
var availableProviders = Object.keys(getAvailableServices());
637638

638639
if (!Array.isArray(providers)) {
639640
throw new CepPromiseError({
@@ -718,12 +719,12 @@
718719

719720
if (configurations.providers.length === 0) {
720721
return Promise$1.any(Object.values(providersServices).map(function (provider) {
721-
return provider(cepWithLeftPad);
722+
return provider(cepWithLeftPad, configurations);
722723
}));
723724
}
724725

725726
return Promise$1.any(configurations.providers.map(function (provider) {
726-
return providersServices[provider](cepWithLeftPad);
727+
return providersServices[provider](cepWithLeftPad, configurations);
727728
}));
728729
}
729730

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: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,16 @@
302302
return ServiceError;
303303
}( /*#__PURE__*/_wrapNativeSuper(Error));
304304

305-
function fetchCorreiosService(cepWithLeftPad) {
306-
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
307-
var url = "".concat(proxyURL, "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente");
305+
function fetchCorreiosService(cepWithLeftPad, configurations) {
306+
var url = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente';
308307
var options = {
309308
method: 'POST',
310309
body: "<?xml version=\"1.0\"?>\n<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cli=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">\n <soapenv:Header />\n <soapenv:Body>\n <cli:consultaCEP>\n <cep>".concat(cepWithLeftPad, "</cep>\n </cli:consultaCEP>\n </soapenv:Body>\n</soapenv:Envelope>"),
311310
headers: {
312311
'Content-Type': 'text/xml;charset=UTF-8',
313312
'cache-control': 'no-cache'
314-
}
313+
},
314+
timeout: configurations.timeout || 30000
315315
};
316316
return fetch(url, options).then(analyzeAndParseResponse)["catch"](throwApplicationError);
317317
}
@@ -385,15 +385,15 @@
385385
throw serviceError;
386386
}
387387

388-
function fetchViaCepService(cepWithLeftPad) {
389-
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
390-
var url = "".concat(proxyURL, "https://viacep.com.br/ws/").concat(cepWithLeftPad, "/json/");
388+
function fetchViaCepService(cepWithLeftPad, configurations) {
389+
var url = "https://viacep.com.br/ws/".concat(cepWithLeftPad, "/json/");
391390
var options = {
392391
method: 'GET',
393392
mode: 'cors',
394393
headers: {
395394
'content-type': 'application/json;charset=utf-8'
396-
}
395+
},
396+
timeout: configurations.timeout || 30000
397397
};
398398

399399
if (typeof window == 'undefined') {
@@ -443,15 +443,15 @@
443443
throw serviceError;
444444
}
445445

446-
function fetchWideNetService(cepWithLeftPad) {
447-
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
448-
var url = "".concat(proxyURL, "https://cep.widenet.host/busca-cep/api/cep/").concat(cepWithLeftPad, ".json");
446+
function fetchWideNetService(cepWithLeftPad, configurations) {
447+
var url = "https://cep.widenet.host/busca-cep/api/cep/".concat(cepWithLeftPad, ".json");
449448
var options = {
450449
method: 'GET',
451450
mode: 'cors',
452451
headers: {
453452
'content-type': 'application/json;charset=utf-8'
454-
}
453+
},
454+
timeout: configurations.timeout || 30000
455455
};
456456
return fetch(url, options).then(analyzeAndParseResponse$2).then(checkForWideNetError).then(extractCepValuesFromResponse$1)["catch"](throwApplicationError$2);
457457
}
@@ -496,14 +496,15 @@
496496
throw serviceError;
497497
}
498498

499-
function fetchBrasilAPIService(cepWithLeftPad) {
499+
function fetchBrasilAPIService(cepWithLeftPad, configurations) {
500500
var url = "https://brasilapi.com.br/api/cep/v1/".concat(cepWithLeftPad);
501501
var options = {
502502
method: 'GET',
503503
mode: 'cors',
504504
headers: {
505505
'content-type': 'application/json;charset=utf-8'
506-
}
506+
},
507+
timeout: configurations.timeout || 30000
507508
};
508509
return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3);
509510
}
@@ -584,7 +585,7 @@
584585
}
585586

586587
function validateProviders(providers) {
587-
var availableProviders = ['brasilapi', 'correios', 'viacep', 'widenet'];
588+
var availableProviders = Object.keys(getAvailableServices());
588589

589590
if (!Array.isArray(providers)) {
590591
throw new CepPromiseError({
@@ -669,12 +670,12 @@
669670

670671
if (configurations.providers.length === 0) {
671672
return Promise$1.any(Object.values(providersServices).map(function (provider) {
672-
return provider(cepWithLeftPad);
673+
return provider(cepWithLeftPad, configurations);
673674
}));
674675
}
675676

676677
return Promise$1.any(configurations.providers.map(function (provider) {
677-
return providersServices[provider](cepWithLeftPad);
678+
return providersServices[provider](cepWithLeftPad, configurations);
678679
}));
679680
}
680681

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.

index.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ declare module 'cep-promise' {
1010

1111
// this workarround is because this : https://github.com/Microsoft/TypeScript/issues/5073
1212
namespace cep {}
13-
13+
1414
type AvaliableProviders =
1515
"brasilapi" |
1616
"correios" |
1717
"viacep" |
1818
"widenet"
1919

2020
export interface Configurations {
21-
providers: AvaliableProviders[]
21+
providers?: AvaliableProviders[],
22+
timeout?: number
2223
}
23-
24+
2425
export function cep(cep: string | number, configurations: Configurations): Promise<CEP>
2526

2627
export default cep

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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cep-promise",
3-
"version": "4.0.4",
3+
"version": "4.1.0",
44
"description": "Busca por CEP integrado diretamente aos serviços dos Correios e ViaCEP",
55
"main": "dist/cep-promise.min.js",
66
"module": "dist/cep-promise.min.js",

src/cep-promise.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function (cepRawValue, configurations = {}) {
1212
.then(cepRawValue => {
1313
configurations.providers = configurations.providers ? configurations.providers : []
1414
validateProviders(configurations.providers)
15-
15+
1616
return cepRawValue
1717
})
1818
.then(removeSpecialCharacters)
@@ -26,7 +26,7 @@ export default function (cepRawValue, configurations = {}) {
2626
}
2727

2828
function validateProviders (providers) {
29-
let availableProviders = ['brasilapi', 'correios', 'viacep', 'widenet']
29+
let availableProviders = Object.keys(getAvailableServices())
3030

3131
if (!Array.isArray(providers)) {
3232
throw new CepPromiseError({
@@ -111,13 +111,13 @@ function fetchCepFromServices (cepWithLeftPad, configurations) {
111111

112112
if (configurations.providers.length === 0) {
113113
return Promise.any(
114-
Object.values(providersServices).map(provider => provider(cepWithLeftPad))
114+
Object.values(providersServices).map(provider => provider(cepWithLeftPad, configurations))
115115
)
116116
}
117117

118118
return Promise.any(
119119
configurations.providers.map(provider => {
120-
return providersServices[provider](cepWithLeftPad)
120+
return providersServices[provider](cepWithLeftPad, configurations)
121121
})
122122
)
123123
}

0 commit comments

Comments
 (0)