Created
July 19, 2022 15:29
-
-
Save NuroDev/953c49714456e2fed2afa65a08b513c6 to your computer and use it in GitHub Desktop.
π¨ Planetscale Incidents β TypeScript Planetscale status API proxy endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api/database/incidents.ts | |
interface Incident { | |
id: string; | |
url: string; | |
name: string; | |
} | |
interface UnresolvedIncidentsResponse { | |
page: { | |
id: string; | |
name: string; | |
time_zone: string; | |
updated_at: Date; | |
url: string; | |
}; | |
incidents: Array<Incident>; | |
} | |
export default async function handler() { | |
try { | |
const res = await fetch( | |
'https://www.planetscalestatus.com/api/v2/incidents/unresolved.json' | |
); | |
const json: UnresolvedIncidentsResponse = await res.json(); | |
const incident = json?.incidents?.[0] | |
? { | |
...json?.incidents?.[0], | |
url: `https://www.planetscalestatus.com/incidents/${json?.incidents?.[0].id}`, | |
} | |
: null; | |
return new Response(JSON.stringify(incident), { | |
status: 200, | |
headers: { | |
'cache-control': 's-maxage=1, stale-while-revalidate', | |
}, | |
}); | |
} catch ({ status = 500, message = 'Internal Server Error' }) { | |
return new Response(JSON.stringify({ message }), { | |
status: status as number, | |
}); | |
} | |
} | |
export const config = { | |
runtime: 'experimental-edge', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment