switch to Edge Functions

This commit is contained in:
Matthias Kretschmann 2022-11-17 14:36:16 +00:00
parent dfa347b324
commit 1d927dd109
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 25 additions and 1811 deletions

View File

@ -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

File diff suppressed because it is too large Load Diff

View File

@ -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"
},

View File

@ -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"
}
}