js-kompo-utils 1.0.35 → 1.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -181,6 +181,14 @@ export default {
181
181
  return `${baseClass} ${errorClass}`
182
182
  },
183
183
 
184
+ // An explicit country() is a hard setting. Otherwise the IP lookup runs when it is turned
185
+ // on — its result wins over defaultCountry — or when there is no default to start from.
186
+ shouldDetectByIp() {
187
+ if (this.$_config('country')) return false
188
+
189
+ return !!this.$_config('detectCountryByIp') || !this.selectedCountryCode
190
+ },
191
+
184
192
  // Current country info
185
193
  currentCountry() {
186
194
  const country = this.countries.find(c => c.code === this.selectedCountryCode)
@@ -289,14 +297,15 @@ export default {
289
297
  mounted() {
290
298
  // Close dropdown when clicking outside
291
299
  document.addEventListener('click', this.handleClickOutside)
292
-
293
- // Set initial value
294
- // this.component.value = this.phoneValue
295
-
296
- // Auto-detect country if no explicit country is set and no phone value exists
300
+
301
+ // `component` is not guaranteed to be bound when data() runs — see phoneValue's `?.` — and
302
+ // a country read as undefined there would send us asking an IP service we don't need.
303
+ this.selectedCountryCode = this.selectedCountryCode || this.$_config('country') || this.$_config('defaultCountry')
304
+
305
+ // A stored number always decides its own country; an empty field may ask the IP.
297
306
  if (this.phoneValue) {
298
307
  this.autoDetectCountryFromPhone(this.phoneValue)
299
- } else if (!this.selectedCountryCode) {
308
+ } else if (this.shouldDetectByIp) {
300
309
  this.detectCountryByLocation()
301
310
  }
302
311
  },
@@ -672,40 +681,25 @@ export default {
672
681
  }
673
682
  },
674
683
 
684
+ // Free, keyless, https + CORS. http:// providers are mixed content on an https page and
685
+ // never resolve, and a provider answering without a code (ipapi.co rate-limits often) has
686
+ // to fall through to the next one instead of ending the chain.
675
687
  async getCountryFromIP() {
676
- // Try ipapi.co first (free, reliable)
677
- try {
678
- const response = await fetch('https://ipapi.co/json/', {
679
- timeout: 5000
680
- })
681
- const data = await response.json()
682
- return data.country_code
683
- } catch (error) {
684
- console.warn('ipapi.co failed:', error)
685
- }
686
-
687
- // Fallback to ip-api.com
688
- try {
689
- const response = await fetch('http://ip-api.com/json/', {
690
- timeout: 5000
691
- })
692
- const data = await response.json()
693
- return data.countryCode
694
- } catch (error) {
695
- console.warn('ip-api.com failed:', error)
696
- }
697
-
698
- // Fallback to ipinfo.io
699
- try {
700
- const response = await fetch('https://ipinfo.io/json', {
701
- timeout: 5000
702
- })
703
- const data = await response.json()
704
- return data.country
705
- } catch (error) {
706
- console.warn('ipinfo.io failed:', error)
688
+ const providers = [
689
+ ['https://get.geojs.io/v1/ip/country.json', data => data.country],
690
+ ['https://ipwho.is/', data => data.success && data.country_code],
691
+ ['https://ipapi.co/json/', data => data.country_code],
692
+ ]
693
+
694
+ for (const [url, readCountry] of providers) {
695
+ try {
696
+ const code = readCountry(await (await fetch(url)).json())
697
+ if (code) return code
698
+ } catch (error) {
699
+ console.warn(`${url} failed:`, error)
700
+ }
707
701
  }
708
-
702
+
709
703
  return null
710
704
  },
711
705
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-kompo-utils",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
4
4
  "description": "",
5
5
  "main": "app.js",
6
6
  "scripts": {