From 32aa790cc8afe9405d9dd43aebe7895beb15fdf2 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 28 Mar 2025 13:28:03 +0000 Subject: [PATCH] fix: migrate to provider with free tier --- app/components/TheHome.server.vue | 2 +- app/components/layout/TheSiteHeader.vue | 4 +- nuxt.config.ts | 3 +- server/api/current-location.get.ts | 15 ++++--- server/api/update-location.post.ts | 54 ++++++++++--------------- 5 files changed, 32 insertions(+), 46 deletions(-) diff --git a/app/components/TheHome.server.vue b/app/components/TheHome.server.vue index e6530416..b8ae9dbe 100644 --- a/app/components/TheHome.server.vue +++ b/app/components/TheHome.server.vue @@ -433,7 +433,7 @@ interface Talk { > Drop me a line - if you'd like to meet up in person! I'm planning to be in {{ currentLocation.city }}, {{ currentLocation.country === 'United Kingdom' ? 'the UK' : currentLocation.country === 'United States' ? 'the US' : currentLocation.country }} today. + if you'd like to meet up in person! I'm planning to be in {{ currentLocation.city }}, {{ currentLocation.area }} today.

diff --git a/app/components/layout/TheSiteHeader.vue b/app/components/layout/TheSiteHeader.vue index e8ed297e..9fd63892 100644 --- a/app/components/layout/TheSiteHeader.vue +++ b/app/components/layout/TheSiteHeader.vue @@ -9,7 +9,7 @@ const showMenu = ref(false) const { data: currentLocation } = await useFetch('/api/current-location', { transform: location => ({ - country: location.country, + area: location.area, flagEmoji: location.flagEmoji, }), }) @@ -78,7 +78,7 @@ function toggleMenu (input?: Event | boolean) { class="ml-1" > - is currently in {{currentLocation.country === 'United Kingdom' ? 'the UK' : currentLocation.country === 'United States' ? 'the US' : currentLocation.country}} + is currently in {{ currentLocation.area }} {{ currentLocation.flagEmoji }} diff --git a/nuxt.config.ts b/nuxt.config.ts index b097af3c..4de55edc 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -120,8 +120,7 @@ export default defineNuxtConfig({ resendApiKey: '', // Location API key for secure updates locationApiKey: '', - // OpenCage API key for geocoding coordinates - openCageApiKey: '', + bigdataApiKey: '', twitch: { clientId: '', clientSecret: '', diff --git a/server/api/current-location.get.ts b/server/api/current-location.get.ts index cc1a2161..7f2ef390 100644 --- a/server/api/current-location.get.ts +++ b/server/api/current-location.get.ts @@ -20,12 +20,6 @@ interface Location { _updatedAt: string } -const locationMaps: Record = { - 'City of Edinburgh': 'Edinburgh', - 'United Kingdom': 'the UK', - 'United States': 'the US', -} - export default defineEventHandler(async event => { const sanity = useSanity(event) @@ -43,10 +37,15 @@ export default defineEventHandler(async event => { char.charCodeAt(0) + 127397)) : '🌍' + const locationMaps: Record = { + 'United Kingdom of Great Britain and Northern Ireland (the)': location.region, + 'United States of America (the)': location.region, + } + return { - ...location, + meetupAvailable: location.meetupAvailable, city: locationMaps[location.city] || location.city, - country: location.region === 'Scotland' ? 'Scotland' : locationMaps[location.country] || location.country, + area: location.region === 'Scotland' ? 'Scotland' : locationMaps[location.country] || location.country, flagEmoji, } }) diff --git a/server/api/update-location.post.ts b/server/api/update-location.post.ts index 602cd9c2..1e9c0963 100644 --- a/server/api/update-location.post.ts +++ b/server/api/update-location.post.ts @@ -18,31 +18,27 @@ export default defineEventHandler(async event => { }) } - const response = await $fetch(`https://api.opencagedata.com/geocode/v1/json`, { + const response = await $fetch(`https://api.bigdatacloud.net/data/reverse-geocode-client`, { params: { - q: `${latitude},${longitude}`, - key: config.openCageApiKey, - no_annotations: 1, - language: 'en', + latitude, + longitude, + localityLanguage: 'en', }, }) - if (!response.results || response.results.length === 0) { + if (!response || !response.city) { return createError({ statusCode: 400, statusMessage: 'Could not geocode the provided coordinates', }) } - const location = response.results[0] - const components = location.components - // Extract relevant information const locationData = { - city: components.city || components.town || components.village || components.hamlet || 'Unknown', - region: components.state || components.province || components.county || '', - country: components.country || 'Unknown', - countryCode: components.country_code ? components.country_code.toUpperCase() : 'XX', + city: response.city || response.locality || 'Unknown', + region: response.principalSubdivision || response.localityInfo?.administrative?.[1]?.name || '', + country: response.countryName || 'Unknown', + countryCode: response.countryCode || 'XX', latitude, longitude, meetupAvailable: meetupAvailable !== undefined ? meetupAvailable : true, @@ -69,24 +65,16 @@ export default defineEventHandler(async event => { return null }) -type OpenCageResponse = { - results: Array<{ - components: { - city?: string - town?: string - village?: string - hamlet?: string - state?: string - province?: string - county?: string - country?: string - country_code?: string - } - geometry: { - lat: number - lng: number - } - }> - status: { code: number, message: string } - total_results: number +type BigDataResponse = { + city: string + locality?: string + principalSubdivision?: string + countryName: string + countryCode: string + localityInfo?: { + administrative?: Array<{ + name: string + level: number + }> + } } -- 2.51.2