cloudflare/vinext
Publicmirrored from https://github.com/cloudflare/vinextAvailable
tests/fixtures/app-basic/app/api/methods/route.ts
73lines · modecode
| 1 | /** |
| 2 | * Route handler supporting all HTTP methods. |
| 3 | * |
| 4 | * Ported from: https://github.com/opennextjs/opennextjs-cloudflare/blob/main/examples/e2e/app-router/open-next/app/methods/route.ts |
| 5 | * Tests: ON-3 in TRACKING.md |
| 6 | */ |
| 7 | |
| 8 | import { NextResponse } from "next/server"; |
| 9 | |
| 10 | export async function GET() { |
| 11 | return NextResponse.json( |
| 12 | { message: "vinext route handler" }, |
| 13 | { |
| 14 | headers: { |
| 15 | "content-type": "text/html; charset=utf-8", |
| 16 | "special-header": "vinext is great", |
| 17 | }, |
| 18 | }, |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | export async function POST(request: Request) { |
| 23 | const contentType = request.headers.get("content-type") || ""; |
| 24 | |
| 25 | if (contentType.includes("application/x-www-form-urlencoded") || contentType.includes("multipart/form-data")) { |
| 26 | const formData = await request.formData(); |
| 27 | const name = formData.get("name"); |
| 28 | const email = formData.get("email"); |
| 29 | return NextResponse.json({ message: "ok", name, email }, { status: 202 }); |
| 30 | } |
| 31 | |
| 32 | const body = await request.text(); |
| 33 | if (body.includes("not awesome")) { |
| 34 | return NextResponse.json({ message: "forbidden" }, { status: 403 }); |
| 35 | } |
| 36 | return NextResponse.json({ message: "ok" }, { status: 202 }); |
| 37 | } |
| 38 | |
| 39 | export async function PUT(request: Request) { |
| 40 | const body = await request.json(); |
| 41 | return NextResponse.json({ message: "ok", ...body }, { status: 201 }); |
| 42 | } |
| 43 | |
| 44 | export async function PATCH(_request: Request) { |
| 45 | return NextResponse.json( |
| 46 | { message: "ok", modified: true, timestamp: new Date().toISOString() }, |
| 47 | { status: 202 }, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | export async function DELETE(_request: Request) { |
| 52 | return new Response(null, { status: 204 }); |
| 53 | } |
| 54 | |
| 55 | export async function HEAD() { |
| 56 | return new Response(null, { |
| 57 | status: 200, |
| 58 | headers: { |
| 59 | "content-type": "text/html; charset=utf-8", |
| 60 | "special-header": "vinext is great", |
| 61 | }, |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | export async function OPTIONS() { |
| 66 | return new Response(null, { |
| 67 | status: 204, |
| 68 | headers: { |
| 69 | allow: "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS", |
| 70 | special: "vinext is great", |
| 71 | }, |
| 72 | }); |
| 73 | } |
| 74 | |