Skip to content

Commit de35089

Browse files
committed
Factor out geodbFetch
1 parent fd3e7a6 commit de35089

File tree

4 files changed

+45
-69
lines changed

4 files changed

+45
-69
lines changed

backend/api/src/search-location.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
1-
import { APIHandler } from './helpers/endpoint'
2-
import {geodbHost} from "common/constants";
1+
import {APIHandler} from './helpers/endpoint'
2+
import {geodbFetch} from "common/geodb";
33

44
export const searchLocation: APIHandler<'search-location'> = async (body) => {
5-
const { term, limit } = body
6-
const apiKey = process.env.GEODB_API_KEY
7-
8-
if (!apiKey) {
9-
return { status: 'failure', data: 'Missing GEODB API key' }
10-
}
11-
const baseUrl = `https://${geodbHost}/v1/geo`
12-
const url = `${baseUrl}/cities?namePrefix=${term}&limit=${
13-
limit ?? 10
14-
}&offset=0&sort=-population`
15-
16-
try {
17-
const res = await fetch(url, {
18-
method: 'GET',
19-
headers: {
20-
'X-RapidAPI-Key': apiKey,
21-
'X-RapidAPI-Host': geodbHost,
22-
},
23-
})
24-
if (!res.ok) {
25-
throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`)
26-
}
27-
28-
const data = await res.json()
29-
30-
return { status: 'success', data: data }
31-
} catch (error: any) {
32-
console.log('failure', error)
33-
return { status: 'failure', data: error.message }
34-
}
5+
const {term, limit} = body
6+
const endpoint = `/cities?namePrefix=${term}&limit=${limit ?? 10}&offset=0&sort=-population`
7+
return await geodbFetch(endpoint)
358
}

backend/api/src/search-near-city.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
1-
import { APIHandler } from './helpers/endpoint'
2-
import { geodbHost } from 'common/constants'
3-
4-
export const searchNearCity: APIHandler<'search-near-city'> = async (body) => {
5-
const { cityId, radius } = body
6-
return await searchNearCityMain(cityId, radius)
7-
}
1+
import {APIHandler} from './helpers/endpoint'
2+
import {geodbFetch} from "common/geodb";
83

94
const searchNearCityMain = async (cityId: string, radius: number) => {
10-
const apiKey = process.env.GEODB_API_KEY
11-
12-
if (!apiKey) {
13-
return { status: 'failure', data: 'Missing GEODB API key' }
14-
}
15-
const baseUrl = `https://${geodbHost}/v1/geo`
165
// Limit to 10 cities for now for free plan, was 100 before (may need to buy plan)
17-
const url = `${baseUrl}/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=10`
18-
19-
try {
20-
const res = await fetch(url, {
21-
method: 'GET',
22-
headers: {
23-
'X-RapidAPI-Key': apiKey,
24-
'X-RapidAPI-Host': geodbHost,
25-
},
26-
})
27-
28-
if (!res.ok) {
29-
throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`)
30-
}
31-
32-
const data = await res.json()
33-
console.log('searchNearCityMain', data)
6+
const endpoint = `/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=10`
7+
return await geodbFetch(endpoint)
8+
}
349

35-
return { status: 'success', data: data }
36-
} catch (error) {
37-
console.log('failure', error)
38-
return { status: 'failure', data: error }
39-
}
10+
export const searchNearCity: APIHandler<'search-near-city'> = async (body) => {
11+
const { cityId, radius } = body
12+
return await searchNearCityMain(cityId, radius)
4013
}
4114

4215
export const getNearbyCities = async (cityId: string, radius: number) => {

common/src/constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

common/src/geodb.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
export const geodbHost = 'wft-geo-db.p.rapidapi.com'
3+
4+
export const geodbFetch = async (endpoint: string) => {
5+
const apiKey = process.env.GEODB_API_KEY
6+
if (!apiKey) {
7+
return {status: 'failure', data: 'Missing GEODB API key'}
8+
}
9+
const baseUrl = `https://${geodbHost}/v1/geo`
10+
const url = `${baseUrl}${endpoint}`
11+
12+
try {
13+
const res = await fetch(url, {
14+
method: 'GET',
15+
headers: {
16+
'X-RapidAPI-Key': apiKey,
17+
'X-RapidAPI-Host': geodbHost,
18+
},
19+
})
20+
21+
if (!res.ok) {
22+
throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`)
23+
}
24+
25+
const data = await res.json()
26+
console.log('geodbFetch', endpoint, data)
27+
return {status: 'success', data}
28+
} catch (error) {
29+
console.log('geodbFetch', endpoint, error)
30+
return {status: 'failure', data: error}
31+
}
32+
}

0 commit comments

Comments
 (0)