location/api/index.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-12-03 01:40:34 +01:00
interface NomadListLocation {
city: string
country: string
country_code: string
latitude: number
longitude: number
epoch_start: number
epoch_end: number
date_start: string
date_end: string
place_photo: string
}
interface NomadListLocationResponse {
location: {
now: NomadListLocation
previous: NomadListLocation
next: NomadListLocation
}
}
2021-12-02 20:13:08 +01:00
2022-11-17 15:36:16 +01:00
export const config = {
runtime: 'experimental-edge'
}
function removeUnwantedKeys(location: NomadListLocation) {
const { place_photo, latitude, longitude, epoch_start, epoch_end, ...rest } =
location
return rest
}
export default async () => {
2021-12-02 20:13:08 +01:00
try {
2022-11-17 00:19:53 +01:00
if (!process.env.NOMADLIST_PROFILE) {
throw new Error('Missing NOMADLIST_PROFILE env variable')
}
if (!process.env.NOMADLIST_KEY) {
throw new Error('Missing NOMADLIST_KEY env variable')
}
const response = await fetch(
2021-12-02 20:13:08 +01:00
`https://nomadlist.com/@${process.env.NOMADLIST_PROFILE}.json?key=${process.env.NOMADLIST_KEY}`
)
2022-11-17 00:19:53 +01:00
if (!response || !response.ok || response.status !== 200) {
throw new Error("Couldn't fetch data from NomadList")
}
const json = (await response.json()) as NomadListLocationResponse
2021-12-02 20:13:08 +01:00
2022-11-17 15:36:16 +01:00
// return only parts of the data
const final = {
now: removeUnwantedKeys(json.location.now),
next: removeUnwantedKeys(json.location.next)
}
return new Response(JSON.stringify(final), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 's-maxage=60, stale-while-revalidate'
}
})
2021-12-02 20:13:08 +01:00
} catch (error) {
2022-11-17 15:36:16 +01:00
return new Response(JSON.stringify(error), { status: 500 })
2021-12-02 20:13:08 +01:00
}
}