@@ -78,7 +78,7 @@ var index = typeof fetch == 'function' ? fetch.bind() : function (url, options)
7878 return new Promise ( function ( resolve , reject ) {
7979 var request = new XMLHttpRequest ( ) ;
8080
81- request . open ( options . method || 'get' , url , true ) ;
81+ request . open ( options . method || 'get' , url ) ;
8282
8383 for ( var i in options . headers ) {
8484 request . setRequestHeader ( i , options . headers [ i ] ) ;
@@ -92,23 +92,23 @@ var index = typeof fetch == 'function' ? fetch.bind() : function (url, options)
9292
9393 request . onerror = reject ;
9494
95- request . send ( options . body || null ) ;
95+ request . send ( options . body ) ;
9696
9797 function response ( ) {
9898 var _keys = [ ] ,
9999 all = [ ] ,
100100 headers = { } ,
101101 header ;
102102
103- request . getAllResponseHeaders ( ) . replace ( / ^ ( .* ?) : [ ^ \S \n ] * ( [ \s \S ] * ?) $ / gm, function ( m , key , value ) {
103+ request . getAllResponseHeaders ( ) . replace ( / ^ ( .* ?) : \s * ( [ \s \S ] * ?) $ / gm, function ( m , key , value ) {
104104 _keys . push ( key = key . toLowerCase ( ) ) ;
105105 all . push ( [ key , value ] ) ;
106106 header = headers [ key ] ;
107107 headers [ key ] = header ? header + "," + value : value ;
108108 } ) ;
109109
110110 return {
111- ok : ( request . status / 100 | 0 ) == 2 , // 200-299
111+ ok : ( request . status / 200 | 0 ) == 1 , // 200-299
112112 status : request . status ,
113113 statusText : request . statusText ,
114114 url : request . responseURL ,
@@ -171,62 +171,6 @@ var ServiceError = function (_Error) {
171171 return ServiceError ;
172172} ( Error ) ;
173173
174- var PROXY_URL = 'https://proxier.now.sh/' ;
175- var CEP_ABERTO_TOKEN = '37d718d2984e6452584a76d3d59d3a26' ;
176-
177- function fetchCepAbertoService ( cepWithLeftPad ) {
178- var proxyURL = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : '' ;
179-
180- var url = proxyURL + 'http://www.cepaberto.com/api/v2/ceps.json?cep=' + cepWithLeftPad ;
181- var options = {
182- method : 'GET' ,
183- mode : 'cors' ,
184- headers : {
185- 'content-type' : 'application/json;charset=utf-8' ,
186- 'Authorization' : 'Token token=' + CEP_ABERTO_TOKEN
187- }
188- } ;
189-
190- return browser ( url , options ) . then ( analyzeAndParseResponse ) . then ( checkForViaCepError ) . then ( extractCepValuesFromResponse ) . catch ( throwApplicationError ) ;
191- }
192-
193- function analyzeAndParseResponse ( response ) {
194- if ( response . ok ) {
195- return response . json ( ) ;
196- }
197- throw Error ( 'Erro ao se conectar com o serviço Cep Aberto.' ) ;
198- }
199-
200- function checkForViaCepError ( responseObject ) {
201- if ( ! Object . keys ( responseObject ) . length ) {
202- throw new Error ( 'CEP não encontrado na base do Cep Aberto.' ) ;
203- }
204- return responseObject ;
205- }
206-
207- function extractCepValuesFromResponse ( responseObject ) {
208- return {
209- cep : responseObject . cep ,
210- state : responseObject . estado ,
211- city : responseObject . cidade ,
212- neighborhood : responseObject . bairro ,
213- street : responseObject . logradouro
214- } ;
215- }
216-
217- function throwApplicationError ( error ) {
218- var serviceError = new ServiceError ( {
219- message : error . message ,
220- service : 'cepaberto'
221- } ) ;
222-
223- if ( error . name === 'FetchError' ) {
224- serviceError . message = 'Erro ao se conectar com o serviço Cep Aberto.' ;
225- }
226-
227- throw serviceError ;
228- }
229-
230174function fetchCorreiosService ( cepWithLeftPad ) {
231175 var proxyURL = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : '' ;
232176
@@ -240,10 +184,10 @@ function fetchCorreiosService(cepWithLeftPad) {
240184 }
241185 } ;
242186
243- return browser ( url , options ) . then ( analyzeAndParseResponse$1 ) . catch ( throwApplicationError$1 ) ;
187+ return browser ( url , options ) . then ( analyzeAndParseResponse ) . catch ( throwApplicationError ) ;
244188}
245189
246- function analyzeAndParseResponse$1 ( response ) {
190+ function analyzeAndParseResponse ( response ) {
247191 if ( response . ok ) {
248192 return response . text ( ) . then ( parseSuccessXML ) . then ( extractValuesFromSuccessResponse ) ;
249193 }
@@ -293,7 +237,7 @@ function extractValuesFromSuccessResponse(xmlObject) {
293237 } ;
294238}
295239
296- function throwApplicationError$1 ( error ) {
240+ function throwApplicationError ( error ) {
297241 var serviceError = new ServiceError ( {
298242 message : error . message ,
299243 service : 'correios'
@@ -307,7 +251,9 @@ function throwApplicationError$1(error) {
307251}
308252
309253function fetchViaCepService ( cepWithLeftPad ) {
310- var url = 'https://viacep.com.br/ws/' + cepWithLeftPad + '/json/' ;
254+ var proxyURL = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : '' ;
255+
256+ var url = proxyURL + 'https://viacep.com.br/ws/' + cepWithLeftPad + '/json/' ;
311257 var options = {
312258 method : 'GET' ,
313259 mode : 'cors' ,
@@ -316,26 +262,26 @@ function fetchViaCepService(cepWithLeftPad) {
316262 }
317263 } ;
318264
319- return browser ( url , options ) . then ( analyzeAndParseResponse$2 ) . then ( checkForViaCepError$1 ) . then ( extractCepValuesFromResponse$1 ) . catch ( throwApplicationError$2 ) ;
265+ return browser ( url , options ) . then ( analyzeAndParseResponse$1 ) . then ( checkForViaCepError ) . then ( extractCepValuesFromResponse ) . catch ( throwApplicationError$1 ) ;
320266}
321267
322- function analyzeAndParseResponse$2 ( response ) {
268+ function analyzeAndParseResponse$1 ( response ) {
323269 if ( response . ok ) {
324270 return response . json ( ) ;
325271 }
326272
327273 throw Error ( 'Erro ao se conectar com o serviço ViaCEP.' ) ;
328274}
329275
330- function checkForViaCepError$1 ( responseObject ) {
276+ function checkForViaCepError ( responseObject ) {
331277 if ( responseObject . erro === true ) {
332278 throw new Error ( 'CEP não encontrado na base do ViaCEP.' ) ;
333279 }
334280
335281 return responseObject ;
336282}
337283
338- function extractCepValuesFromResponse$1 ( responseObject ) {
284+ function extractCepValuesFromResponse ( responseObject ) {
339285 return {
340286 cep : responseObject . cep . replace ( '-' , '' ) ,
341287 state : responseObject . uf ,
@@ -345,7 +291,7 @@ function extractCepValuesFromResponse$1(responseObject) {
345291 } ;
346292}
347293
348- function throwApplicationError$2 ( error ) {
294+ function throwApplicationError$1 ( error ) {
349295 var serviceError = new ServiceError ( {
350296 message : error . message ,
351297 service : 'viacep'
@@ -358,6 +304,8 @@ function throwApplicationError$2(error) {
358304 throw serviceError ;
359305}
360306
307+ var PROXY_URL = 'https://proxier.now.sh/' ;
308+
361309/* istanbul ignore next */
362310function isBrowser ( ) {
363311 return typeof window !== 'undefined' ;
@@ -370,9 +318,8 @@ function injectProxy(Service) {
370318 } ;
371319}
372320
373- var CepAbertoService = isBrowser ( ) ? injectProxy ( fetchCepAbertoService ) : fetchCepAbertoService ;
374321var CorreiosService = isBrowser ( ) ? injectProxy ( fetchCorreiosService ) : fetchCorreiosService ;
375- var ViaCepService = fetchViaCepService ;
322+ var ViaCepService = isBrowser ( ) ? injectProxy ( fetchViaCepService ) : fetchViaCepService ;
376323
377324var reverse = function reverse ( promise ) {
378325 return new Promise ( function ( resolve , reject ) {
@@ -387,7 +334,7 @@ Promise.any = function (iterable) {
387334var CEP_SIZE = 8 ;
388335
389336function cepPromise ( cepRawValue ) {
390- return Promise . resolve ( cepRawValue ) . then ( validateInputType ) . then ( removeSpecialCharacters ) . then ( validateInputLength ) . then ( leftPadWithZeros ) . then ( fetchCepFromServices ) . catch ( handleServicesError ) . catch ( throwApplicationError$3 ) ;
337+ return Promise . resolve ( cepRawValue ) . then ( validateInputType ) . then ( removeSpecialCharacters ) . then ( validateInputLength ) . then ( leftPadWithZeros ) . then ( fetchCepFromServices ) . catch ( handleServicesError ) . catch ( throwApplicationError$2 ) ;
391338}
392339
393340function validateInputType ( cepRawValue ) {
@@ -431,7 +378,7 @@ function validateInputLength(cepWithLeftPad) {
431378}
432379
433380function fetchCepFromServices ( cepWithLeftPad ) {
434- return Promise . any ( [ CepAbertoService ( cepWithLeftPad ) , CorreiosService ( cepWithLeftPad ) , ViaCepService ( cepWithLeftPad ) ] ) ;
381+ return Promise . any ( [ CorreiosService ( cepWithLeftPad ) , ViaCepService ( cepWithLeftPad ) ] ) ;
435382}
436383
437384function handleServicesError ( aggregatedErrors ) {
@@ -445,7 +392,7 @@ function handleServicesError(aggregatedErrors) {
445392 throw aggregatedErrors ;
446393}
447394
448- function throwApplicationError$3 ( _ref ) {
395+ function throwApplicationError$2 ( _ref ) {
449396 var message = _ref . message ,
450397 type = _ref . type ,
451398 errors = _ref . errors ;
0 commit comments