@@ -356,7 +356,45 @@ Promise.any = function (iterable) {
356356var CEP_SIZE = 8 ;
357357
358358function 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
362400function 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
406458function handleServicesError ( aggregatedErrors ) {
0 commit comments