mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-04-28 18:28:43 -04:00
Factor out geodbFetch
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
export const geodbHost = 'wft-geo-db.p.rapidapi.com'
|
||||
32
common/src/geodb.ts
Normal file
32
common/src/geodb.ts
Normal file
@@ -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}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user