Openstatus www.openstatus.dev
at main 44 lines 1.4 kB view raw
1import { NextResponse } from "next/server"; 2import { z } from "zod"; 3 4import { monitorRegionSchema } from "@openstatus/db/src/schema/constants"; 5 6import { checkRegion } from "@/components/ping-response-analysis/utils"; 7import { httpPayloadSchema } from "@openstatus/utils"; 8import { isAnInvalidTestUrl } from "../../utils"; 9 10export const runtime = "edge"; 11export const preferredRegion = "auto"; 12export const dynamic = "force-dynamic"; 13export const revalidate = 0; 14 15export function GET() { 16 return NextResponse.json({ success: true }); 17} 18 19export async function POST(request: Request) { 20 try { 21 const json = await request.json(); 22 const _valid = httpPayloadSchema 23 .pick({ url: true, method: true, headers: true, body: true }) 24 .extend(z.object({ region: monitorRegionSchema.prefault("ams") }).shape) 25 .safeParse(json); 26 27 if (!_valid.success) { 28 return NextResponse.json({ success: false }, { status: 400 }); 29 } 30 31 const { url, region, method, headers, body } = _valid.data; 32 // 🧑‍💻 for the smart one who want to create a loop hole 33 if (isAnInvalidTestUrl(url)) { 34 return NextResponse.json({ success: true }, { status: 200 }); 35 } 36 37 const res = await checkRegion(url, region, { method, headers, body }); 38 39 return NextResponse.json(res); 40 } catch (e) { 41 console.error(e); 42 return NextResponse.json({ success: false }, { status: 400 }); 43 } 44}