@@ -109,84 +109,50 @@ const _ipgeolocation = function() {
109109 urlParameters = addUrlParameter ( urlParameters , "location" , location ) ;
110110 }
111111
112- if ( window . fetch ) {
113- try {
114- if ( asyncCall ) {
115- fetch ( "https://api.ipgeolocation.io/" . concat ( subUrl , urlParameters , "" ) , {
116- method : "GET" ,
117- headers : {
118- "Accept" : "application/json"
119- }
120- } )
121- . then ( ( response ) => {
122- if ( ! response . ok ) {
123- if ( callback ) {
124- callback ( JSON . parse ( "{'status': " . concat ( response . status , ", 'message': '" , json . message , "'}" ) ) ) ;
125- } else {
126- console . error ( "status:" , response . status , json ) ;
127- }
128- }
129-
130- return response . json ( ) ;
131- } )
132- . then ( ( json ) => {
133- if ( useSessionStorage ) {
134- key = geolocationResponseName ;
135-
136- if ( subUrl === timezoneEndpoint ) {
137- key = timezoneResponseName ;
138- } else if ( subUrl === useragentEndpoint ) {
139- key = useragentResponseName ;
140- }
141-
142- sessionStorage . setItem ( key , JSON . stringify ( json ) ) ;
143- }
144-
145- if ( callback ) {
146- callback ( json ) ;
147- }
148- } ) ;
149- } else {
150- const response = await fetch ( "https://api.ipgeolocation.io/" . concat ( subUrl , urlParameters , "" ) , {
151- method : "GET" ,
152- headers : {
153- "Accept" : "application/json"
154- }
155- } ) ;
156- const json = await response . json ( ) ;
112+ const url = "https://api.ipgeolocation.io/" . concat ( subUrl , urlParameters , "" ) ;
113+ const requestOptions = {
114+ method : "GET" ,
115+ headers : {
116+ "Accept" : "application/json"
117+ }
118+ } ;
157119
120+ try {
121+ if ( asyncCall ) {
122+ fetch ( url , requestOptions )
123+ . then ( ( response ) => {
158124 if ( ! response . ok ) {
159- if ( callback ) {
160- callback ( JSON . parse ( "{'status': " . concat ( response . status , ", 'message': '" , json . message , "'}" ) ) ) ;
161- } else {
162- console . error ( "status:" , response . status , json ) ;
163- }
125+ callback ( JSON . parse ( "{'status': " . concat ( response . status , ", 'message': '" , json . message , "'}" ) ) ) ;
164126 }
165-
127+
128+ return response . json ( ) ;
129+ } )
130+ . then ( ( json ) => {
166131 if ( useSessionStorage ) {
167- key = geolocationResponseName ;
168-
169- if ( subUrl === timezoneEndpoint ) {
170- key = timezoneResponseName ;
171- } else if ( subUrl === useragentEndpoint ) {
172- key = useragentResponseName ;
173- }
174-
175- sessionStorage . setItem ( key , JSON . stringify ( json ) ) ;
132+ cacheInSessionStorage ( subUrl , json ) ;
176133 }
177134
178- if ( callback ) {
179- callback ( json ) ;
180- }
181- }
182- } catch ( error ) {
183- console . error ( error ) ;
184-
185- if ( callback ) {
186- callback ( JSON . parse ( "{'status': 400, 'message': 'Something went wrong while query ipgeolocation.io API. If the error persists, contact us at support@ipgeolocation.io'}" ) ) ;
135+ callback ( json ) ;
136+ } ) ;
137+ } else {
138+ const response = await fetch ( url , requestOptions ) ;
139+ const json = await response . json ( ) ;
140+
141+ if ( ! response . ok ) {
142+ callback ( JSON . parse ( "{'status': " . concat ( response . status , ", 'message': '" , json . message , "'}" ) ) ) ;
143+ }
144+
145+ if ( useSessionStorage ) {
146+ cacheInSessionStorage ( subUrl , json ) ;
187147 }
148+
149+ callback ( json ) ;
188150 }
189- }
151+ } catch ( error ) {
152+ console . error ( error ) ;
153+
154+ callback ( JSON . parse ( "{'status': 400, 'message': 'Something went wrong while query ipgeolocation.io API. If the error persists, contact us at support@ipgeolocation.io'}" ) ) ;
155+ }
190156 }
191157
192158 function addUrlParameter ( parameters , parameterName , parameterValue ) {
@@ -199,6 +165,18 @@ const _ipgeolocation = function() {
199165 return parameters ;
200166 }
201167
168+ function cacheInSessionStorage ( endpoint , json ) {
169+ let key = geolocationResponseName ;
170+
171+ if ( endpoint === timezoneEndpoint ) {
172+ key = timezoneResponseName ;
173+ } else if ( endpoint === useragentEndpoint ) {
174+ key = useragentResponseName ;
175+ }
176+
177+ sessionStorage . setItem ( key , JSON . stringify ( json ) ) ;
178+ }
179+
202180 return {
203181 enableSessionStorage : function ( e = false ) {
204182 useSessionStorage = e ;
0 commit comments