Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona parametro de timeout para serviços de cep #206

Merged
merged 7 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ dist/*
!dist/cep-promise*.js

#Yarn
yarn.lock
yarn.lock

# Intelij IDE stuff
.idea/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,16 @@ cep('123456789123456789')
// }]
// }
```
### Options
- `timeout`: Timeout em milisegundos das consultas em cada serviço. O tempo total poderá ser maior devido a limites no paralelismo.
- `providers`: Lista de providers a serem usados na consulta. Default é usar todos os providers disponíveis.

```js
import cep from 'cep-promise'
cep('5010000', { timeout: 5000, providers: ['brasilapi'] })
.then(console.log)

```

### Instalação

Expand Down
35 changes: 18 additions & 17 deletions dist/cep-promise-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,16 @@
return ServiceError;
}( /*#__PURE__*/_wrapNativeSuper(Error));

function fetchCorreiosService(cepWithLeftPad) {
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var url = "".concat(proxyURL, "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente");
function fetchCorreiosService(cepWithLeftPad, configurations) {
var url = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente';
var options = {
method: 'POST',
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>"),
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
'cache-control': 'no-cache'
}
},
timeout: configurations.timeout || 30000
};
return fetch(url, options).then(analyzeAndParseResponse)["catch"](throwApplicationError);
}
Expand Down Expand Up @@ -434,15 +434,15 @@
throw serviceError;
}

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

if (typeof window == 'undefined') {
Expand Down Expand Up @@ -492,15 +492,15 @@
throw serviceError;
}

function fetchWideNetService(cepWithLeftPad) {
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var url = "".concat(proxyURL, "https://cep.widenet.host/busca-cep/api/cep/").concat(cepWithLeftPad, ".json");
function fetchWideNetService(cepWithLeftPad, configurations) {
var url = "https://cep.widenet.host/busca-cep/api/cep/".concat(cepWithLeftPad, ".json");
var options = {
method: 'GET',
mode: 'cors',
headers: {
'content-type': 'application/json;charset=utf-8'
}
},
timeout: configurations.timeout || 30000
};
return fetch(url, options).then(analyzeAndParseResponse$2).then(checkForWideNetError).then(extractCepValuesFromResponse$1)["catch"](throwApplicationError$2);
}
Expand Down Expand Up @@ -545,14 +545,15 @@
throw serviceError;
}

function fetchBrasilAPIService(cepWithLeftPad) {
function fetchBrasilAPIService(cepWithLeftPad, configurations) {
var url = "https://brasilapi.com.br/api/cep/v1/".concat(cepWithLeftPad);
var options = {
method: 'GET',
mode: 'cors',
headers: {
'content-type': 'application/json;charset=utf-8'
}
},
timeout: configurations.timeout || 30000
};
return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3);
}
Expand Down Expand Up @@ -633,7 +634,7 @@
}

function validateProviders(providers) {
var availableProviders = ['brasilapi', 'correios', 'viacep', 'widenet'];
var availableProviders = Object.keys(getAvailableServices());

if (!Array.isArray(providers)) {
throw new CepPromiseError({
Expand Down Expand Up @@ -718,12 +719,12 @@

if (configurations.providers.length === 0) {
return Promise$1.any(Object.values(providersServices).map(function (provider) {
return provider(cepWithLeftPad);
return provider(cepWithLeftPad, configurations);
}));
}

return Promise$1.any(configurations.providers.map(function (provider) {
return providersServices[provider](cepWithLeftPad);
return providersServices[provider](cepWithLeftPad, configurations);
}));
}

Expand Down
2 changes: 1 addition & 1 deletion dist/cep-promise-browser.min.js

Large diffs are not rendered by default.

35 changes: 18 additions & 17 deletions dist/cep-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,16 @@
return ServiceError;
}( /*#__PURE__*/_wrapNativeSuper(Error));

function fetchCorreiosService(cepWithLeftPad) {
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var url = "".concat(proxyURL, "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente");
function fetchCorreiosService(cepWithLeftPad, configurations) {
var url = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente';
var options = {
method: 'POST',
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>"),
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
'cache-control': 'no-cache'
}
},
timeout: configurations.timeout || 30000
};
return fetch(url, options).then(analyzeAndParseResponse)["catch"](throwApplicationError);
}
Expand Down Expand Up @@ -385,15 +385,15 @@
throw serviceError;
}

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

if (typeof window == 'undefined') {
Expand Down Expand Up @@ -443,15 +443,15 @@
throw serviceError;
}

function fetchWideNetService(cepWithLeftPad) {
var proxyURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var url = "".concat(proxyURL, "https://cep.widenet.host/busca-cep/api/cep/").concat(cepWithLeftPad, ".json");
function fetchWideNetService(cepWithLeftPad, configurations) {
var url = "https://cep.widenet.host/busca-cep/api/cep/".concat(cepWithLeftPad, ".json");
var options = {
method: 'GET',
mode: 'cors',
headers: {
'content-type': 'application/json;charset=utf-8'
}
},
timeout: configurations.timeout || 30000
};
return fetch(url, options).then(analyzeAndParseResponse$2).then(checkForWideNetError).then(extractCepValuesFromResponse$1)["catch"](throwApplicationError$2);
}
Expand Down Expand Up @@ -496,14 +496,15 @@
throw serviceError;
}

function fetchBrasilAPIService(cepWithLeftPad) {
function fetchBrasilAPIService(cepWithLeftPad, configurations) {
var url = "https://brasilapi.com.br/api/cep/v1/".concat(cepWithLeftPad);
var options = {
method: 'GET',
mode: 'cors',
headers: {
'content-type': 'application/json;charset=utf-8'
}
},
timeout: configurations.timeout || 30000
};
return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3);
}
Expand Down Expand Up @@ -584,7 +585,7 @@
}

function validateProviders(providers) {
var availableProviders = ['brasilapi', 'correios', 'viacep', 'widenet'];
var availableProviders = Object.keys(getAvailableServices());

if (!Array.isArray(providers)) {
throw new CepPromiseError({
Expand Down Expand Up @@ -669,12 +670,12 @@

if (configurations.providers.length === 0) {
return Promise$1.any(Object.values(providersServices).map(function (provider) {
return provider(cepWithLeftPad);
return provider(cepWithLeftPad, configurations);
}));
}

return Promise$1.any(configurations.providers.map(function (provider) {
return providersServices[provider](cepWithLeftPad);
return providersServices[provider](cepWithLeftPad, configurations);
}));
}

Expand Down
2 changes: 1 addition & 1 deletion dist/cep-promise.min.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ declare module 'cep-promise' {

// this workarround is because this : https://github.com/Microsoft/TypeScript/issues/5073
namespace cep {}

type AvaliableProviders =
"brasilapi" |
"correios" |
"viacep" |
"widenet"

export interface Configurations {
providers: AvaliableProviders[]
providers?: AvaliableProviders[],
timeout?: number
}

export function cep(cep: string | number, configurations: Configurations): Promise<CEP>

export default cep
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cep-promise",
"version": "4.0.4",
"version": "4.1.0",
"description": "Busca por CEP integrado diretamente aos serviços dos Correios e ViaCEP",
"main": "dist/cep-promise.min.js",
"module": "dist/cep-promise.min.js",
Expand Down
8 changes: 4 additions & 4 deletions src/cep-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function (cepRawValue, configurations = {}) {
.then(cepRawValue => {
configurations.providers = configurations.providers ? configurations.providers : []
validateProviders(configurations.providers)

return cepRawValue
})
.then(removeSpecialCharacters)
Expand All @@ -26,7 +26,7 @@ export default function (cepRawValue, configurations = {}) {
}

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

if (!Array.isArray(providers)) {
throw new CepPromiseError({
Expand Down Expand Up @@ -111,13 +111,13 @@ function fetchCepFromServices (cepWithLeftPad, configurations) {

if (configurations.providers.length === 0) {
return Promise.any(
Object.values(providersServices).map(provider => provider(cepWithLeftPad))
Object.values(providersServices).map(provider => provider(cepWithLeftPad, configurations))
)
}

return Promise.any(
configurations.providers.map(provider => {
return providersServices[provider](cepWithLeftPad)
return providersServices[provider](cepWithLeftPad, configurations)
})
)
}
Expand Down
7 changes: 4 additions & 3 deletions src/services/brasilapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import fetch from 'node-fetch'
import ServiceError from '../errors/service.js'

export default function fetchBrasilAPIService (cepWithLeftPad) {
export default function fetchBrasilAPIService (cepWithLeftPad, configurations) {
const url = `https://brasilapi.com.br/api/cep/v1/${cepWithLeftPad}`
const options = {
method: 'GET',
mode: 'cors',
headers: {
'content-type': 'application/json;charset=utf-8'
}
},
timeout: configurations.timeout || 30000
}

return fetch(url, options)
Expand All @@ -23,7 +24,7 @@ function parseResponse (response) {
if (response.ok === false || response.status !== 200) {
throw new Error('CEP não encontrado na base do BrasilAPI.')
}

return response.json()
}

Expand Down
7 changes: 4 additions & 3 deletions src/services/correios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import fetch from 'node-fetch'
import ServiceError from '../errors/service.js'

export default function fetchCorreiosService (cepWithLeftPad, proxyURL = '') {
const url = `${proxyURL}https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente`
export default function fetchCorreiosService (cepWithLeftPad, configurations) {
const url = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente'
const options = {
method: 'POST',
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>${cepWithLeftPad}</cep>\n </cli:consultaCEP>\n </soapenv:Body>\n</soapenv:Envelope>`,
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
'cache-control': 'no-cache'
}
},
timeout: configurations.timeout || 30000
}

return fetch(url, options)
Expand Down
7 changes: 4 additions & 3 deletions src/services/viacep.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import fetch from 'node-fetch'
import ServiceError from '../errors/service.js'

export default function fetchViaCepService (cepWithLeftPad, proxyURL = '') {
const url = `${proxyURL}https://viacep.com.br/ws/${cepWithLeftPad}/json/`
export default function fetchViaCepService (cepWithLeftPad, configurations) {
const url = `https://viacep.com.br/ws/${cepWithLeftPad}/json/`
const options = {
method: 'GET',
mode: 'cors',
headers: {
'content-type': 'application/json;charset=utf-8'
}
},
timeout: configurations.timeout || 30000
}

if (typeof window == 'undefined') {
Expand Down
7 changes: 4 additions & 3 deletions src/services/widenet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import fetch from 'node-fetch'
import ServiceError from '../errors/service.js'

export default function fetchWideNetService (cepWithLeftPad, proxyURL = '') {
const url = `${proxyURL}https://cep.widenet.host/busca-cep/api/cep/${cepWithLeftPad}.json`
export default function fetchWideNetService (cepWithLeftPad, configurations) {
const url = `https://cep.widenet.host/busca-cep/api/cep/${cepWithLeftPad}.json`
const options = {
method: 'GET',
mode: 'cors',
headers: {
'content-type': 'application/json;charset=utf-8'
}
},
timeout: configurations.timeout || 30000
}

return fetch(url, options)
Expand Down
Loading