From de3508993cb5074fb89543c07f707c022458bef3 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Tue, 9 Sep 2025 16:07:36 +0200 Subject: [PATCH] Factor out geodbFetch --- backend/api/src/search-location.ts | 37 ++++--------------------- backend/api/src/search-near-city.ts | 43 ++++++----------------------- common/src/constants.ts | 2 -- common/src/geodb.ts | 32 +++++++++++++++++++++ 4 files changed, 45 insertions(+), 69 deletions(-) delete mode 100644 common/src/constants.ts create mode 100644 common/src/geodb.ts diff --git a/backend/api/src/search-location.ts b/backend/api/src/search-location.ts index 89c8f94a..aa2d5a61 100644 --- a/backend/api/src/search-location.ts +++ b/backend/api/src/search-location.ts @@ -1,35 +1,8 @@ -import { APIHandler } from './helpers/endpoint' -import {geodbHost} from "common/constants"; +import {APIHandler} from './helpers/endpoint' +import {geodbFetch} from "common/geodb"; export const searchLocation: APIHandler<'search-location'> = async (body) => { - const { term, limit } = body - const apiKey = process.env.GEODB_API_KEY - - if (!apiKey) { - return { status: 'failure', data: 'Missing GEODB API key' } - } - const baseUrl = `https://${geodbHost}/v1/geo` - const url = `${baseUrl}/cities?namePrefix=${term}&limit=${ - limit ?? 10 - }&offset=0&sort=-population` - - try { - const res = await fetch(url, { - method: 'GET', - headers: { - 'X-RapidAPI-Key': apiKey, - 'X-RapidAPI-Host': geodbHost, - }, - }) - if (!res.ok) { - throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`) - } - - const data = await res.json() - - return { status: 'success', data: data } - } catch (error: any) { - console.log('failure', error) - return { status: 'failure', data: error.message } - } + const {term, limit} = body + const endpoint = `/cities?namePrefix=${term}&limit=${limit ?? 10}&offset=0&sort=-population` + return await geodbFetch(endpoint) } diff --git a/backend/api/src/search-near-city.ts b/backend/api/src/search-near-city.ts index 3b6a6d8a..a528cbb4 100644 --- a/backend/api/src/search-near-city.ts +++ b/backend/api/src/search-near-city.ts @@ -1,44 +1,17 @@ -import { APIHandler } from './helpers/endpoint' -import { geodbHost } from 'common/constants' +import {APIHandler} from './helpers/endpoint' +import {geodbFetch} from "common/geodb"; + +const searchNearCityMain = async (cityId: string, radius: number) => { + // Limit to 10 cities for now for free plan, was 100 before (may need to buy plan) + const endpoint = `/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=10` + return await geodbFetch(endpoint) +} export const searchNearCity: APIHandler<'search-near-city'> = async (body) => { const { cityId, radius } = body return await searchNearCityMain(cityId, radius) } -const searchNearCityMain = async (cityId: string, radius: number) => { - const apiKey = process.env.GEODB_API_KEY - - if (!apiKey) { - return { status: 'failure', data: 'Missing GEODB API key' } - } - const baseUrl = `https://${geodbHost}/v1/geo` - // Limit to 10 cities for now for free plan, was 100 before (may need to buy plan) - const url = `${baseUrl}/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=10` - - try { - const res = await fetch(url, { - method: 'GET', - headers: { - 'X-RapidAPI-Key': apiKey, - 'X-RapidAPI-Host': geodbHost, - }, - }) - - if (!res.ok) { - throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`) - } - - const data = await res.json() - console.log('searchNearCityMain', data) - - return { status: 'success', data: data } - } catch (error) { - console.log('failure', error) - return { status: 'failure', data: error } - } -} - export const getNearbyCities = async (cityId: string, radius: number) => { const result = await searchNearCityMain(cityId, radius) const cityIds = (result.data.data as any[]).map( diff --git a/common/src/constants.ts b/common/src/constants.ts deleted file mode 100644 index 6b228adb..00000000 --- a/common/src/constants.ts +++ /dev/null @@ -1,2 +0,0 @@ - -export const geodbHost = 'wft-geo-db.p.rapidapi.com' \ No newline at end of file diff --git a/common/src/geodb.ts b/common/src/geodb.ts new file mode 100644 index 00000000..0b3057ca --- /dev/null +++ b/common/src/geodb.ts @@ -0,0 +1,32 @@ + +export const geodbHost = 'wft-geo-db.p.rapidapi.com' + +export const geodbFetch = async (endpoint: string) => { + const apiKey = process.env.GEODB_API_KEY + if (!apiKey) { + return {status: 'failure', data: 'Missing GEODB API key'} + } + const baseUrl = `https://${geodbHost}/v1/geo` + const url = `${baseUrl}${endpoint}` + + try { + const res = await fetch(url, { + method: 'GET', + headers: { + 'X-RapidAPI-Key': apiKey, + 'X-RapidAPI-Host': geodbHost, + }, + }) + + if (!res.ok) { + throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`) + } + + const data = await res.json() + console.log('geodbFetch', endpoint, data) + return {status: 'success', data} + } catch (error) { + console.log('geodbFetch', endpoint, error) + return {status: 'failure', data: error} + } +} \ No newline at end of file