@@ -73,80 +73,44 @@ var CepPromiseError = function (_Error) {
7373 return CepPromiseError ;
7474} ( Error ) ;
7575
76- var index = typeof fetch == 'function' ? fetch . bind ( ) : function ( url , options ) {
77- options = options || { } ;
78- return new Promise ( function ( resolve , reject ) {
79- var request = new XMLHttpRequest ( ) ;
80-
81- request . open ( options . method || 'get' , url ) ;
82-
83- for ( var i in options . headers ) {
84- request . setRequestHeader ( i , options . headers [ i ] ) ;
85- }
86-
87- request . withCredentials = options . credentials == 'include' ;
88-
89- request . onload = function ( ) {
90- resolve ( response ( ) ) ;
91- } ;
92-
93- request . onerror = reject ;
94-
95- request . send ( options . body ) ;
96-
97- function response ( ) {
98- var _keys = [ ] ,
99- all = [ ] ,
100- headers = { } ,
101- header ;
102-
103- request . getAllResponseHeaders ( ) . replace ( / ^ ( .* ?) : \s * ( [ \s \S ] * ?) $ / gm, function ( m , key , value ) {
104- _keys . push ( key = key . toLowerCase ( ) ) ;
105- all . push ( [ key , value ] ) ;
106- header = headers [ key ] ;
107- headers [ key ] = header ? header + "," + value : value ;
108- } ) ;
109-
110- return {
111- ok : ( request . status / 200 | 0 ) == 1 , // 200-299
112- status : request . status ,
113- statusText : request . statusText ,
114- url : request . responseURL ,
115- clone : response ,
116- text : function text ( ) {
117- return Promise . resolve ( request . responseText ) ;
118- } ,
119- json : function json ( ) {
120- return Promise . resolve ( request . responseText ) . then ( JSON . parse ) ;
121- } ,
122- blob : function blob ( ) {
123- return Promise . resolve ( new Blob ( [ request . response ] ) ) ;
124- } ,
125- headers : {
126- keys : function keys ( ) {
127- return _keys ;
128- } ,
129- entries : function entries ( ) {
130- return all ;
131- } ,
132- get : function get ( n ) {
133- return headers [ n . toLowerCase ( ) ] ;
134- } ,
135- has : function has ( n ) {
136- return n . toLowerCase ( ) in headers ;
137- }
138- }
139- } ;
140- }
141- } ) ;
142- } ;
76+ function unfetch ( e , n ) {
77+ return n = n || { } , new Promise ( function ( t , r ) {
78+ var s = new XMLHttpRequest ( ) ,
79+ o = [ ] ,
80+ u = [ ] ,
81+ i = { } ,
82+ a = function a ( ) {
83+ return { ok : 2 == ( s . status / 100 | 0 ) , statusText : s . statusText , status : s . status , url : s . responseURL , text : function text ( ) {
84+ return Promise . resolve ( s . responseText ) ;
85+ } , json : function json ( ) {
86+ return Promise . resolve ( JSON . parse ( s . responseText ) ) ;
87+ } , blob : function blob ( ) {
88+ return Promise . resolve ( new Blob ( [ s . response ] ) ) ;
89+ } , clone : a , headers : { keys : function keys ( ) {
90+ return o ;
91+ } , entries : function entries ( ) {
92+ return u ;
93+ } , get : function get ( e ) {
94+ return i [ e . toLowerCase ( ) ] ;
95+ } , has : function has ( e ) {
96+ return e . toLowerCase ( ) in i ;
97+ } } } ;
98+ } ; for ( var l in s . open ( n . method || "get" , e , ! 0 ) , s . onload = function ( ) {
99+ s . getAllResponseHeaders ( ) . replace ( / ^ ( .* ?) : [ ^ \S \n ] * ( [ \s \S ] * ?) $ / gm, function ( e , n , t ) {
100+ o . push ( n = n . toLowerCase ( ) ) , u . push ( [ n , t ] ) , i [ n ] = i [ n ] ? i [ n ] + "," + t : t ;
101+ } ) , t ( a ( ) ) ;
102+ } , s . onerror = r , s . withCredentials = "include" == n . credentials , n . headers ) {
103+ s . setRequestHeader ( l , n . headers [ l ] ) ;
104+ } s . send ( n . body || null ) ;
105+ } ) ;
106+ }
143107
144108
145- var unfetch_es = Object . freeze ( {
146- default : index
109+ var unfetch$1 = Object . freeze ( {
110+ default : unfetch
147111} ) ;
148112
149- var require$$0 = ( unfetch_es && index ) || unfetch_es ;
113+ var require$$0 = ( unfetch$1 && unfetch ) || unfetch$1 ;
150114
151115var browser = window . fetch || ( window . fetch = require$$0 . default || require$$0 ) ;
152116
@@ -304,6 +268,60 @@ function throwApplicationError$1(error) {
304268 throw serviceError ;
305269}
306270
271+ function fetchWideNetService ( cepWithLeftPad ) {
272+ var proxyURL = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : '' ;
273+
274+ var url = proxyURL + 'https://cep.widenet.host/busca-cep/api/cep/' + cepWithLeftPad + '.json' ;
275+ var options = {
276+ method : 'GET' ,
277+ mode : 'cors' ,
278+ headers : {
279+ 'content-type' : 'application/json;charset=utf-8'
280+ }
281+ } ;
282+
283+ return browser ( url , options ) . then ( analyzeAndParseResponse$2 ) . then ( checkForWideNetError ) . then ( extractCepValuesFromResponse$1 ) . catch ( throwApplicationError$2 ) ;
284+ }
285+
286+ function analyzeAndParseResponse$2 ( response ) {
287+ if ( response . ok ) {
288+ return response . json ( ) ;
289+ }
290+
291+ throw Error ( 'Erro ao se conectar com o serviço WideNet.' ) ;
292+ }
293+
294+ function checkForWideNetError ( object ) {
295+ if ( object . ok === false || object . status !== 200 ) {
296+ throw new Error ( 'CEP não encontrado na base do WideNet.' ) ;
297+ }
298+
299+ return object ;
300+ }
301+
302+ function extractCepValuesFromResponse$1 ( object ) {
303+ return {
304+ cep : object . code . replace ( '-' , '' ) ,
305+ state : object . state ,
306+ city : object . city ,
307+ neighborhood : object . district ,
308+ street : object . address
309+ } ;
310+ }
311+
312+ function throwApplicationError$2 ( error ) {
313+ var serviceError = new ServiceError ( {
314+ message : error . message ,
315+ service : 'widenet'
316+ } ) ;
317+
318+ if ( error . name === 'FetchError' ) {
319+ serviceError . message = 'Erro ao se conectar com o serviço WideNet.' ;
320+ }
321+
322+ throw serviceError ;
323+ }
324+
307325var PROXY_URL = 'https://proxier.now.sh/api?url=' ;
308326
309327/* istanbul ignore next */
@@ -320,6 +338,7 @@ function injectProxy(Service) {
320338
321339var CorreiosService = isBrowser ( ) ? injectProxy ( fetchCorreiosService ) : fetchCorreiosService ;
322340var ViaCepService = fetchViaCepService ;
341+ var WideNetService = fetchWideNetService ;
323342
324343var reverse = function reverse ( promise ) {
325344 return new Promise ( function ( resolve , reject ) {
@@ -334,7 +353,7 @@ Promise.any = function (iterable) {
334353var CEP_SIZE = 8 ;
335354
336355function cepPromise ( cepRawValue ) {
337- return Promise . resolve ( cepRawValue ) . then ( validateInputType ) . then ( removeSpecialCharacters ) . then ( validateInputLength ) . then ( leftPadWithZeros ) . then ( fetchCepFromServices ) . catch ( handleServicesError ) . catch ( throwApplicationError$2 ) ;
356+ return Promise . resolve ( cepRawValue ) . then ( validateInputType ) . then ( removeSpecialCharacters ) . then ( validateInputLength ) . then ( leftPadWithZeros ) . then ( fetchCepFromServices ) . catch ( handleServicesError ) . catch ( throwApplicationError$3 ) ;
338357}
339358
340359function validateInputType ( cepRawValue ) {
@@ -378,7 +397,7 @@ function validateInputLength(cepWithLeftPad) {
378397}
379398
380399function fetchCepFromServices ( cepWithLeftPad ) {
381- return Promise . any ( [ CorreiosService ( cepWithLeftPad ) , ViaCepService ( cepWithLeftPad ) ] ) ;
400+ return Promise . any ( [ WideNetService ( cepWithLeftPad ) , CorreiosService ( cepWithLeftPad ) , ViaCepService ( cepWithLeftPad ) ] ) ;
382401}
383402
384403function handleServicesError ( aggregatedErrors ) {
@@ -392,7 +411,7 @@ function handleServicesError(aggregatedErrors) {
392411 throw aggregatedErrors ;
393412}
394413
395- function throwApplicationError$2 ( _ref ) {
414+ function throwApplicationError$3 ( _ref ) {
396415 var message = _ref . message ,
397416 type = _ref . type ,
398417 errors = _ref . errors ;
0 commit comments