js-kompo-utils 1.0.35 → 1.0.36

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)
@@ -293,10 +301,10 @@ export default {
293
301
  // Set initial value
294
302
  // this.component.value = this.phoneValue
295
303
 
296
- // Auto-detect country if no explicit country is set and no phone value exists
304
+ // A stored number always decides its own country; an empty field may ask the IP.
297
305
  if (this.phoneValue) {
298
306
  this.autoDetectCountryFromPhone(this.phoneValue)
299
- } else if (!this.selectedCountryCode) {
307
+ } else if (this.shouldDetectByIp) {
300
308
  this.detectCountryByLocation()
301
309
  }
302
310
  },
@@ -672,40 +680,25 @@ export default {
672
680
  }
673
681
  },
674
682
 
683
+ // Free, keyless, https + CORS. http:// providers are mixed content on an https page and
684
+ // never resolve, and a provider answering without a code (ipapi.co rate-limits often) has
685
+ // to fall through to the next one instead of ending the chain.
675
686
  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)
687
+ const providers = [
688
+ ['https://get.geojs.io/v1/ip/country.json', data => data.country],
689
+ ['https://ipwho.is/', data => data.success && data.country_code],
690
+ ['https://ipapi.co/json/', data => data.country_code],
691
+ ]
692
+
693
+ for (const [url, readCountry] of providers) {
694
+ try {
695
+ const code = readCountry(await (await fetch(url)).json())
696
+ if (code) return code
697
+ } catch (error) {
698
+ console.warn(`${url} failed:`, error)
699
+ }
707
700
  }
708
-
701
+
709
702
  return null
710
703
  },
711
704
 
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.36",
4
4
  "description": "",
5
5
  "main": "app.js",
6
6
  "scripts": {