Path: blob/master/cloud/ai-service-apps/nextjs-carbon-react-ui/src/app/api/theme/route.js
6410 views
import { cookies } from "next/headers";1import { COOKIE_KEYS, THEME } from "../../../utils/constants";2import { NextResponse } from "next/server";34export async function GET() {5try {6const cookiesStorage = await cookies();7const theme = cookiesStorage.get(COOKIE_KEYS.THEME)?.value || THEME.SYSTEM;89return NextResponse.json({ theme }, { status: 200 });10} catch (err) {11return NextResponse.json(12{ error: `Error fetching theme cookie: ${err.message}` },13{ status: 500 }14);15}16}1718export async function POST(req) {19try {20const { theme } = await req.json();2122const cookiesStorage = await cookies();23cookiesStorage.set(COOKIE_KEYS.THEME, theme, {24path: "/",25maxAge: 2592000,26});2728return NextResponse.json({ success: true }, { status: 200 });29} catch (err) {30return NextResponse.json(31{ error: `Error updating theme cookie: ${err.message}` },32{ status: 500 }33);34}35}363738