From 8e9c5ce0dfb8968d2d906e85b630209f48d0787c Mon Sep 17 00:00:00 2001 From: Luan Altino Date: Mon, 11 Jun 2018 09:50:17 -0300 Subject: [PATCH 1/6] =?UTF-8?q?Acrescentando=20op=C3=A7=C3=A3o=20para=20se?= =?UTF-8?q?tar=20timeout=20no=20request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index a09a2f3..4aff397 100644 --- a/index.js +++ b/index.js @@ -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); } @@ -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, options) => { let ret; try { if (invalidCep(cep)) { @@ -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); }) @@ -62,11 +62,11 @@ const getDataAsync = cep => { }); } -module.exports = function getDetailsByZipCode (cep, sync) { +module.exports = function getDetailsByZipCode (cep, sync, options) { if (!_.isEmpty(cep) && isNaN(cep)) { cep = cep.replace(/[-\s]/g, ''); } return (sync === true || (arguments[1] && arguments[1].sync)) ? - getDataSync(cep) : - getDataAsync(cep); + getDataSync(cep, options) : + getDataAsync(cep, options); }; \ No newline at end of file From 746fa0253a2b353941000bf8a6e69ef34df367c0 Mon Sep 17 00:00:00 2001 From: Luan Altino Date: Mon, 11 Jun 2018 09:53:08 -0300 Subject: [PATCH 2/6] Atualizando readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a403593..63cd138 100644 --- a/README.md +++ b/README.md @@ -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', false, {timeout: 1000}) .then(endereco => { console.log(endereco); }) @@ -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', true, {timeout: 1000});//Também pode ser usado buscaCep('01001-000', {sync: true}); 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! From 909993c775b60afd384457fcfda029ed82f3cceb Mon Sep 17 00:00:00 2001 From: Luan Altino Date: Mon, 11 Jun 2018 15:52:28 -0300 Subject: [PATCH 3/6] =?UTF-8?q?Atualizando=20vers=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 0defc19..97f5a71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,10 @@ { "name": "busca-cep", +<<<<<<< 746fa0253a2b353941000bf8a6e69ef34df367c0 "version": "0.3.6", +======= + "version": "0.4.0", +>>>>>>> Atualizando versão "author": { "name": "Wagner Resende Santos", "email": "w.rsantos@outlook.com", From 26a2e2a37d2b226729049c5e61f562739941c94d Mon Sep 17 00:00:00 2001 From: Luan Altino Date: Fri, 15 Jun 2018 17:33:51 -0300 Subject: [PATCH 4/6] Adicionando objeto options como parametro --- README.md | 4 ++-- index.js | 8 ++++---- test/index.test.js | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 63cd138..ad7bddc 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Busca por ceps do Brasil utilizando o serviço ViaCEP ```javascript var buscaCep = require('busca-cep'); -buscaCep('01001-000', false, {timeout: 1000}) +buscaCep('01001-000', {sync: false, timeout: 1000}}) .then(endereco => { console.log(endereco); }) @@ -26,7 +26,7 @@ buscaCep('01001-000', false, {timeout: 1000}) Ou, caso você queira uma requisição síncrona, passe true no segundo parâmetro: ```javascript -var resposta = buscaCep('01001-000', true, {timeout: 1000});//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 { diff --git a/index.js b/index.js index 4aff397..983b8bd 100644 --- a/index.js +++ b/index.js @@ -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, options) => { +const getDataSync = (cep) => { let ret; try { if (invalidCep(cep)) { @@ -62,11 +62,11 @@ const getDataAsync = (cep, options) => { }); } -module.exports = function getDetailsByZipCode (cep, sync, options) { +module.exports = function getDetailsByZipCode (cep, options) { if (!_.isEmpty(cep) && isNaN(cep)) { cep = cep.replace(/[-\s]/g, ''); } - return (sync === true || (arguments[1] && arguments[1].sync)) ? - getDataSync(cep, options) : + return (options && options.sync === true) ? + getDataSync(cep) : getDataAsync(cep, options); }; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 85a8ee6..198c577 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -29,13 +29,13 @@ 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); it('Should work when passing the sync parameter', (done) => { - const zipCode = getDetailsByZipCode('31652130', true); + const zipCode = getDetailsByZipCode('31652130', {sync: true}); expect(zipCode).to.deep.equal(DEFAULT_RESPONSE); done(); }).timeout(5000); @@ -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.'); }); From cfb470f90e58ff102b32ccf8d89a87328a82fd0a Mon Sep 17 00:00:00 2001 From: Luan Altino Date: Fri, 15 Jun 2018 17:35:37 -0300 Subject: [PATCH 5/6] Adicionando objeto options como parametro --- index.js | 2 +- test/index.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 983b8bd..2b8405e 100644 --- a/index.js +++ b/index.js @@ -66,7 +66,7 @@ module.exports = function getDetailsByZipCode (cep, options) { if (!_.isEmpty(cep) && isNaN(cep)) { cep = cep.replace(/[-\s]/g, ''); } - return (options && options.sync === true) ? + return (options === true || (options && options.sync)) ? getDataSync(cep) : getDataAsync(cep, options); }; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 198c577..9382c37 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -35,7 +35,7 @@ describe('Zip search module', () => { }).timeout(5000); it('Should work when passing the sync parameter', (done) => { - const zipCode = getDetailsByZipCode('31652130', {sync: true}); + const zipCode = getDetailsByZipCode('31652130', true); expect(zipCode).to.deep.equal(DEFAULT_RESPONSE); done(); }).timeout(5000); From 994603068b5c6403af3d788983941fe4e42ebd6c Mon Sep 17 00:00:00 2001 From: Luan Altino Date: Fri, 15 Jun 2018 17:40:33 -0300 Subject: [PATCH 6/6] Resolvendo conflito --- package.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/package.json b/package.json index 97f5a71..22b5b57 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,6 @@ { "name": "busca-cep", -<<<<<<< 746fa0253a2b353941000bf8a6e69ef34df367c0 - "version": "0.3.6", -======= "version": "0.4.0", ->>>>>>> Atualizando versão "author": { "name": "Wagner Resende Santos", "email": "w.rsantos@outlook.com",