File
Blob: src/workerd/api/tests/opennextjs/src/app/api/cookies/route.js
| 1 | // Copyright (c) 2026 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | import { NextResponse } from 'next/server'; |
| 6 | import { cookies } from 'next/headers'; |
| 7 | |
| 8 | export async function GET() { |
| 9 | const cookieStore = await cookies(); |
| 10 | const allCookies = {}; |
| 11 | |
| 12 | cookieStore.getAll().forEach((cookie) => { |
| 13 | allCookies[cookie.name] = cookie.value; |
| 14 | }); |
| 15 | |
| 16 | return NextResponse.json({ |
| 17 | message: 'Cookies API', |
| 18 | timestamp: Date.now(), |
| 19 | cookies: allCookies, |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | export async function POST(request) { |
| 24 | const cookieStore = await cookies(); |
| 25 | const body = await request.json().catch(() => ({})); |
| 26 | const { name, value, options } = body; |
| 27 | |
| 28 | if (name && value) { |
| 29 | cookieStore.set(name, value, options || {}); |
| 30 | } |
| 31 | |
| 32 | return NextResponse.json({ |
| 33 | message: 'Cookie set', |
| 34 | timestamp: Date.now(), |
| 35 | cookie: { name, value }, |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | export async function DELETE(request) { |
| 40 | const cookieStore = await cookies(); |
| 41 | const name = request.nextUrl.searchParams.get('name'); |
| 42 | |
| 43 | if (name) { |
| 44 | cookieStore.delete(name); |
| 45 | } |
| 46 | |
| 47 | return NextResponse.json({ |
| 48 | message: 'Cookie deleted', |
| 49 | timestamp: Date.now(), |
| 50 | deleted: name, |
| 51 | }); |
| 52 | } |