Skip to content

Commit

Permalink
Merge pull request #6 from luanaltino/master
Browse files Browse the repository at this point in the history
Acrescentando opção para setar timeout no request
  • Loading branch information
wagoid authored Jul 8, 2018
2 parents ec8ff0f + 9946030 commit 5809520
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Busca por ceps do Brasil utilizando o serviço ViaCEP
```javascript
var buscaCep = require('busca-cep');

buscaCep('01001-000')
buscaCep('01001-000', {sync: false, timeout: 1000}})
.then(endereco => {
console.log(endereco);
})
Expand All @@ -26,12 +26,14 @@ buscaCep('01001-000')
Ou, caso você queira uma requisição síncrona, passe true no segundo parâmetro:

```javascript
var resposta = buscaCep('01001-000', true);//Também pode ser usado buscaCep('01001-000', {sync: true});
var resposta = buscaCep('01001-000', {sync: true, timeout: 1000});
if (!resposta.hasError) {
console.log(resposta);
} else {
console.log(`Erro: statusCode ${resposta.statusCode} e mensagem ${resposta.message}`);
}

O terceiro parâmetro da função é um options, nele você você pode setar o valor do timeout do request (em milisegundos)
```

Enjoy it!
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const requestPromise = require('request-promise'),
const CEP_SIZE = 8,
VIACEP_URI = config.apiViaCep.url;

const callViaCep = cep => {
const callViaCep = (cep, options) => {
let requestOptions = {
json: true,
uri: `${VIACEP_URI}/ws/${cep}/json`
uri: `${VIACEP_URI}/ws/${cep}/json`,
timeout: options ? options.timeout : 5000
};

return requestPromise(requestOptions);
}

Expand All @@ -27,7 +27,7 @@ const getValidationMessage = () => {
return `The CEP should be a number or string of size ${CEP_SIZE}. Please check your parameter.`;
}

const getDataSync = cep => {
const getDataSync = (cep) => {
let ret;
try {
if (invalidCep(cep)) {
Expand All @@ -46,12 +46,12 @@ const getDataSync = cep => {
return ret;
}

const getDataAsync = cep => {
const getDataAsync = (cep, options) => {
return new Promise((resolve, reject) => {
if (invalidCep(cep)) {
reject({ message: getValidationMessage() });
} else {
callViaCep(cep)
callViaCep(cep, options)
.then(placeInfo => {
resolve(placeInfo);
})
Expand All @@ -62,11 +62,11 @@ const getDataAsync = cep => {
});
}

module.exports = function getDetailsByZipCode (cep, sync) {
module.exports = function getDetailsByZipCode (cep, options) {
if (!_.isEmpty(cep) && isNaN(cep)) {
cep = cep.replace(/[-\s]/g, '');
}
return (sync === true || (arguments[1] && arguments[1].sync)) ?
return (options === true || (options && options.sync)) ?
getDataSync(cep) :
getDataAsync(cep);
getDataAsync(cep, options);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "busca-cep",
"version": "0.3.6",
"version": "0.4.0",
"author": {
"name": "Wagner Resende Santos",
"email": "[email protected]",
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function makeDefaultNock(cep) {

describe('Zip search module', () => {
it('Should get a valid response when passing a valid zip code', (done) => {
const zipCode = getDetailsByZipCode('31652130', { sync: true });
const zipCode = getDetailsByZipCode('31652130', true);
expect(zipCode).to.deep.equal(DEFAULT_RESPONSE);
done();
}).timeout(5000);
Expand All @@ -56,7 +56,7 @@ describe('Zip search module', () => {
});

it('Should return an error message when passing wrong parameter', () => {
const result = getDetailsByZipCode('wrong', true);
const result = getDetailsByZipCode('wrong', {sync: true});
expect(result.hasError).to.be.true;
expect(result.message).to.equal('The CEP should be a number or string of size 8. Please check your parameter.');
});
Expand Down

0 comments on commit 5809520

Please sign in to comment.