mirror of
https://github.com/kremalicious/location.git
synced 2024-11-21 17:36:59 +01:00
switch to Edge Functions
This commit is contained in:
parent
dfa347b324
commit
1d927dd109
32
api/index.ts
32
api/index.ts
@ -1,6 +1,3 @@
|
||||
import type { VercelRequest, VercelResponse } from '@vercel/node'
|
||||
import fetch from 'cross-fetch'
|
||||
|
||||
interface NomadListLocation {
|
||||
city: string
|
||||
country: string
|
||||
@ -22,7 +19,17 @@ interface NomadListLocationResponse {
|
||||
}
|
||||
}
|
||||
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
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 () => {
|
||||
try {
|
||||
if (!process.env.NOMADLIST_PROFILE) {
|
||||
throw new Error('Missing NOMADLIST_PROFILE env variable')
|
||||
@ -39,10 +46,19 @@ export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
}
|
||||
const json = (await response.json()) as NomadListLocationResponse
|
||||
|
||||
res.setHeader('Cache-Control', 's-maxage=1, stale-while-revalidate')
|
||||
// return only the location part of the data
|
||||
res.status(200).json(json.location)
|
||||
// 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'
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
res.status(500).send(error)
|
||||
return new Response(JSON.stringify(error), { status: 500 })
|
||||
}
|
||||
}
|
||||
|
1798
package-lock.json
generated
1798
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -10,12 +10,8 @@
|
||||
"format": "prettier --ignore-path .gitignore './**/*.{css,yml,js,ts,tsx,json}' --write",
|
||||
"type-check": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"cross-fetch": "^3.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.11.9",
|
||||
"@vercel/node": "^2.6.3",
|
||||
"prettier": "^2.7.1",
|
||||
"typescript": "^4.9.3"
|
||||
},
|
||||
|
@ -5,7 +5,7 @@
|
||||
"dest": "/api",
|
||||
"headers": {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET",
|
||||
"Access-Control-Allow-Methods": "GET,OPTIONS",
|
||||
"Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user