Skip to content

Commit c4af22f

Browse files
Merge branch '4.0.0' into feature/add-service-to-response
2 parents a21b293 + 7bd59c2 commit c4af22f

File tree

7 files changed

+1495
-15
lines changed

7 files changed

+1495
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Leia nosso guia de contribuição [aqui](CONTRIBUTING.md)
172172
| [<img src="https://avatars1.githubusercontent.com/u/8251208?v=3&s=115" width="115"><br><sub>@lucianopf</sub>](https://github.com/lucianopf) | [<img src="https://avatars1.githubusercontent.com/u/7863230?v=3&s=115" width="115"><br><sub>@MarcoWorms</sub>](https://github.com/MarcoWorms) | [<img src="https://avatars1.githubusercontent.com/u/551228?v=3&s=115" width="115"><br><sub>@caio-ribeiro-pereira</sub>](https://github.com/caio-ribeiro-pereira) | [<img src="https://avatars1.githubusercontent.com/u/1225447?v=3&s=115" width="115"><br><sub>@chrisbenseler</sub>](https://github.com/chrisbenseler) | [<img src="https://avatars0.githubusercontent.com/u/3428149?v=3&s=115" width="115"><br><sub>@luanmuniz</sub>](https://github.com/luanmuniz) | [<img src="https://avatars3.githubusercontent.com/u/3094496?v=3&s=115" width="115"><br><sub>@AlbertoTrindade</sub>](https://github.com/AlbertoTrindade) |
173173
|:-:|:-:|:-:|:-:|:-:|:-:|
174174
| [<img src="https://avatars1.githubusercontent.com/u/4137355?v=3&s=115" width="115"><br><sub>@pedrro</sub>](https://github.com/pedrro) | [<img src="https://avatars1.githubusercontent.com/u/8260778?v=3&s=115" width="115"><br><sub>@petronetto</sub>](https://github.com/Petronetto) | [<img src="https://avatars3.githubusercontent.com/u/434364?v=3&s=115" width="115"><br><sub>@olegon</sub>](https://github.com/olegon) | [<img src="https://avatars3.githubusercontent.com/u/2177742?v=3&s=115" width="115"><br><sub>@jhonnymoreira</sub>](https://github.com/jhonnymoreira) | [<img src="https://avatars3.githubusercontent.com/u/2955087?s=115&v=4" width="115"><br><sub>@claytonsilva</sub>](https://github.com/claytonsilva) | [<img src="https://avatars1.githubusercontent.com/u/13632762?s=115" width="115"><br><sub>@thiamsantos</sub>](https://github.com/thiamsantos) |
175-
| [<img src="https://avatars2.githubusercontent.com/u/6232791?v=3&s=115" width="115"><br><sub>@flyingluscas</sub>](https://github.com/flyingluscas) | [<img src="https://avatars1.githubusercontent.com/u/15306309?s=115&v=4" width="115"><br><sub>@otaviopace</sub>](https://github.com/otaviopace) | [<img src="https://avatars1.githubusercontent.com/u/16264950?s=400&v=4" width="115"><br><sub>@vagnercardosoweb</sub>](https://github.com/vagnercardosoweb) |
175+
| [<img src="https://avatars2.githubusercontent.com/u/6232791?v=3&s=115" width="115"><br><sub>@flyingluscas</sub>](https://github.com/flyingluscas) | [<img src="https://avatars1.githubusercontent.com/u/15306309?s=115&v=4" width="115"><br><sub>@otaviopace</sub>](https://github.com/otaviopace) | [<img src="https://avatars1.githubusercontent.com/u/16264950?s=400&v=4" width="115"><br><sub>@vagnercardosoweb</sub>](https://github.com/vagnercardosoweb) | [<img src="https://avatars2.githubusercontent.com/u/18074134?s=400&u=fd578b638c5c4dc69ded7ebd5fa834ff915a2250&v=4" width="115"><br><sub>@murilohns</sub>](https://github.com/murilohns) |
176176

177177
## Autor
178178

dist/cep-promise-browser.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,45 @@ Promise.any = function (iterable) {
356356
var CEP_SIZE = 8;
357357

358358
function cepPromise (cepRawValue) {
359-
return Promise.resolve(cepRawValue).then(validateInputType).then(removeSpecialCharacters).then(validateInputLength).then(leftPadWithZeros).then(fetchCepFromServices).catch(handleServicesError).catch(throwApplicationError$3);
359+
var providers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
360+
361+
return Promise.resolve(cepRawValue).then(validateInputType).then(function (zipcode) {
362+
validateProviders(providers);
363+
364+
return zipcode;
365+
}).then(removeSpecialCharacters).then(validateInputLength).then(leftPadWithZeros).then(function (cepWithLeftPad) {
366+
return fetchCepFromServices(cepWithLeftPad, providers);
367+
}).catch(handleServicesError).catch(throwApplicationError$3);
368+
}
369+
370+
function validateProviders(providers) {
371+
var availableProviders = ['correios', 'viacep', 'widenet'];
372+
373+
if (!Array.isArray(providers)) {
374+
throw new CepPromiseError({
375+
message: 'Erro ao inicializar a instância do CepPromise.',
376+
type: 'validation_error',
377+
errors: [{
378+
message: 'O par\xE2metro providers deve ser uma lista.',
379+
service: 'cep_validation'
380+
}]
381+
});
382+
}
383+
384+
return providers.map(function (provider) {
385+
if (!availableProviders.includes(provider)) {
386+
throw new CepPromiseError({
387+
message: 'Erro ao inicializar a instância do CepPromise.',
388+
type: 'validation_error',
389+
errors: [{
390+
message: 'O provider "' + provider + '" \xE9 inv\xE1lido. Os providers dispon\xEDveis s\xE3o: ' + availableProviders.join(", ") + '.',
391+
service: 'cep_validation'
392+
}]
393+
});
394+
}
395+
396+
return provider;
397+
});
360398
}
361399

362400
function validateInputType(cepRawValue) {
@@ -399,8 +437,22 @@ function validateInputLength(cepWithLeftPad) {
399437
});
400438
}
401439

402-
function fetchCepFromServices(cepWithLeftPad) {
403-
return Promise.any([WideNetService(cepWithLeftPad), CorreiosService(cepWithLeftPad), ViaCepService(cepWithLeftPad)]);
440+
function fetchCepFromServices(cepWithLeftPad, providers) {
441+
var providersServices = {
442+
correios: CorreiosService,
443+
widenet: WideNetService,
444+
viacep: ViaCepService
445+
};
446+
447+
if (providers.length === 0) {
448+
return Promise.any(Object.keys(providersServices).map(function (provider) {
449+
return providersServices[provider](cepWithLeftPad);
450+
}));
451+
}
452+
453+
return Promise.any(providers.map(function (provider) {
454+
return providersServices[provider](cepWithLeftPad);
455+
}));
404456
}
405457

406458
function handleServicesError(aggregatedErrors) {

dist/cep-promise.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,45 @@ Promise.any = function (iterable) {
317317
var CEP_SIZE = 8;
318318

319319
function cepPromise (cepRawValue) {
320-
return Promise.resolve(cepRawValue).then(validateInputType).then(removeSpecialCharacters).then(validateInputLength).then(leftPadWithZeros).then(fetchCepFromServices).catch(handleServicesError).catch(throwApplicationError$3);
320+
var providers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
321+
322+
return Promise.resolve(cepRawValue).then(validateInputType).then(function (zipcode) {
323+
validateProviders(providers);
324+
325+
return zipcode;
326+
}).then(removeSpecialCharacters).then(validateInputLength).then(leftPadWithZeros).then(function (cepWithLeftPad) {
327+
return fetchCepFromServices(cepWithLeftPad, providers);
328+
}).catch(handleServicesError).catch(throwApplicationError$3);
329+
}
330+
331+
function validateProviders(providers) {
332+
var availableProviders = ['correios', 'viacep', 'widenet'];
333+
334+
if (!Array.isArray(providers)) {
335+
throw new CepPromiseError({
336+
message: 'Erro ao inicializar a instância do CepPromise.',
337+
type: 'validation_error',
338+
errors: [{
339+
message: 'O par\xE2metro providers deve ser uma lista.',
340+
service: 'cep_validation'
341+
}]
342+
});
343+
}
344+
345+
return providers.map(function (provider) {
346+
if (!availableProviders.includes(provider)) {
347+
throw new CepPromiseError({
348+
message: 'Erro ao inicializar a instância do CepPromise.',
349+
type: 'validation_error',
350+
errors: [{
351+
message: 'O provider "' + provider + '" \xE9 inv\xE1lido. Os providers dispon\xEDveis s\xE3o: ' + availableProviders.join(", ") + '.',
352+
service: 'cep_validation'
353+
}]
354+
});
355+
}
356+
357+
return provider;
358+
});
321359
}
322360

323361
function validateInputType(cepRawValue) {
@@ -360,8 +398,22 @@ function validateInputLength(cepWithLeftPad) {
360398
});
361399
}
362400

363-
function fetchCepFromServices(cepWithLeftPad) {
364-
return Promise.any([WideNetService(cepWithLeftPad), CorreiosService(cepWithLeftPad), ViaCepService(cepWithLeftPad)]);
401+
function fetchCepFromServices(cepWithLeftPad, providers) {
402+
var providersServices = {
403+
correios: CorreiosService,
404+
widenet: WideNetService,
405+
viacep: ViaCepService
406+
};
407+
408+
if (providers.length === 0) {
409+
return Promise.any(Object.keys(providersServices).map(function (provider) {
410+
return providersServices[provider](cepWithLeftPad);
411+
}));
412+
}
413+
414+
return Promise.any(providers.map(function (provider) {
415+
return providersServices[provider](cepWithLeftPad);
416+
}));
365417
}
366418

367419
function handleServicesError(aggregatedErrors) {

0 commit comments

Comments
 (0)