11import { IS_PLATFORM } from 'common'
2- import { NextApiRequest , NextApiResponse } from 'next'
2+ import { NextResponse } from 'next/server '
33
44import { InternalServerError } from '@/lib/api/apiHelpers'
55import { getActiveIncidents , type IncidentCache } from '@/lib/api/incident-status'
@@ -42,22 +42,26 @@ async function fetchIncidentCache(incidentIds: Array<string>): Promise<Map<strin
4242 return cacheMap
4343}
4444
45- async function handler ( req : NextApiRequest , res : NextApiResponse ) {
46- if ( ! IS_PLATFORM ) {
47- return res . status ( 404 ) . end ( )
48- }
45+ export async function OPTIONS ( ) {
46+ if ( ! IS_PLATFORM ) return new Response ( null , { status : 404 } )
47+ return new Response ( null , {
48+ status : 204 ,
49+ headers : {
50+ Allow : 'GET, HEAD, OPTIONS' ,
51+ } ,
52+ } )
53+ }
4954
50- const { method } = req
55+ export async function HEAD ( ) {
56+ if ( ! IS_PLATFORM ) return new Response ( null , { status : 404 } )
57+ return new Response ( null , {
58+ status : 200 ,
59+ headers : { 'Cache-Control' : CACHE_CONTROL_SETTINGS } ,
60+ } )
61+ }
5162
52- if ( method === 'HEAD' ) {
53- res . setHeader ( 'Cache-Control' , CACHE_CONTROL_SETTINGS )
54- return res . status ( 200 ) . end ( )
55- }
56-
57- if ( method !== 'GET' ) {
58- res . setHeader ( 'Allow' , [ 'GET' , 'HEAD' ] )
59- return res . status ( 405 ) . json ( { error : `Method ${ method } Not Allowed` } )
60- }
63+ export async function GET ( ) {
64+ if ( ! IS_PLATFORM ) return new Response ( null , { status : 404 } )
6165
6266 try {
6367 const allIncidents = await getActiveIncidents ( )
@@ -75,17 +79,18 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
7579 cache : cacheMap . get ( incident . id ) ?? null ,
7680 } ) )
7781
78- res . setHeader ( 'Cache-Control' , CACHE_CONTROL_SETTINGS )
79-
80- return res . status ( 200 ) . json ( enrichedIncidents )
82+ return NextResponse . json ( enrichedIncidents , {
83+ headers : { 'Cache-Control' : CACHE_CONTROL_SETTINGS } ,
84+ } )
8185 } catch ( error ) {
8286 let errorCode = 500
87+ const headers = new Headers ( )
8388
8489 if ( error instanceof InternalServerError ) {
8590 if ( typeof error . details ?. status === 'number' ) errorCode = error . details . status
8691 if ( errorCode === 420 ) errorCode = 429
8792 if ( errorCode === 429 && typeof error . details ?. retryAfter === 'string' ) {
88- res . setHeader ( 'Retry-After' , error . details . retryAfter )
93+ headers . set ( 'Retry-After' , error . details . retryAfter )
8994 }
9095 console . error ( 'Failed to fetch active StatusPage incidents: %O' , {
9196 message : error . message ,
@@ -95,8 +100,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
95100 console . error ( 'Unexpected error fetching active StatusPage incidents: %O' , error )
96101 }
97102
98- return res . status ( errorCode ) . json ( { error : 'Unable to fetch incidents at this time' } )
103+ return NextResponse . json (
104+ { error : 'Unable to fetch incidents at this time' } ,
105+ { status : errorCode , headers }
106+ )
99107 }
100108}
101-
102- export default handler
0 commit comments