From 43174b009e197a84f231b3836a507425bffa60d7 Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Sat, 5 Apr 2025 17:24:36 +0100 Subject: [PATCH] refactor: use NextResponse instead of Response --- src/app/api/auth/username/route.ts | 15 ++++++++------- src/app/api/like/route.ts | 8 +++++--- src/app/api/submit/route.ts | 28 +++++++++++++++------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/app/api/auth/username/route.ts b/src/app/api/auth/username/route.ts index a6e9e3f..f691bab 100644 --- a/src/app/api/auth/username/route.ts +++ b/src/app/api/auth/username/route.ts @@ -1,3 +1,4 @@ +import { NextResponse } from "next/server"; import { z } from "zod"; import { auth } from "@/lib/auth"; @@ -11,28 +12,28 @@ const usernameSchema = z export async function GET() { const session = await auth(); - if (!session) return Response.json({ error: "Unauthorized" }, { status: 401 }); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); - return Response.json({ username: session.user.username }); + return NextResponse.json({ username: session.user.username }); } export async function PATCH(request: Request) { const session = await auth(); - if (!session) return Response.json({ error: "Unauthorized" }, { status: 401 }); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); const { username } = await request.json(); - if (!username) return Response.json({ error: "Username is required" }, { status: 400 }); + if (!username) return NextResponse.json({ error: "Username is required" }, { status: 400 }); const validation = usernameSchema.safeParse(username); - if (!validation.success) return Response.json({ error: validation.error.errors[0].message }, { status: 400 }); + if (!validation.success) return NextResponse.json({ error: validation.error.errors[0].message }, { status: 400 }); const existingUser = await prisma.user.findUnique({ where: { username } }); - if (existingUser) return Response.json({ error: "Username is already taken" }, { status: 400 }); + if (existingUser) return NextResponse.json({ error: "Username is already taken" }, { status: 400 }); await prisma.user.update({ where: { email: session.user?.email ?? undefined }, data: { username }, }); - return Response.json({ success: true }); + return NextResponse.json({ success: true }); } diff --git a/src/app/api/like/route.ts b/src/app/api/like/route.ts index 88d2510..7b9974a 100644 --- a/src/app/api/like/route.ts +++ b/src/app/api/like/route.ts @@ -1,3 +1,5 @@ +import { NextResponse } from "next/server"; + import { auth } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; @@ -5,10 +7,10 @@ export async function PATCH(request: Request) { // todo: rate limit const session = await auth(); - if (!session) return Response.json({ error: "Unauthorized" }, { status: 401 }); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); const { miiId } = await request.json(); - if (!miiId) return Response.json({ error: "Mii ID is required" }, { status: 400 }); + if (!miiId) return NextResponse.json({ error: "Mii ID is required" }, { status: 400 }); const result = await prisma.$transaction(async (tx) => { const existingLike = await tx.like.findUnique({ @@ -47,5 +49,5 @@ export async function PATCH(request: Request) { return { liked: !existingLike, count: likeCount }; }); - return Response.json({ success: true, liked: result.liked, count: result.count }); + return NextResponse.json({ success: true, liked: result.liked, count: result.count }); } diff --git a/src/app/api/submit/route.ts b/src/app/api/submit/route.ts index 03f42c4..ac13a74 100644 --- a/src/app/api/submit/route.ts +++ b/src/app/api/submit/route.ts @@ -1,3 +1,5 @@ +import { NextResponse } from "next/server"; + import fs from "fs/promises"; import path from "path"; import sharp from "sharp"; @@ -17,20 +19,20 @@ const uploadsDirectory = path.join(process.cwd(), "public", "mii"); export async function POST(request: Request) { const session = await auth(); - if (!session) return Response.json({ error: "Unauthorized" }, { status: 401 }); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); const { name, tags, qrBytesRaw } = await request.json(); - if (!name) return Response.json({ error: "Name is required" }, { status: 400 }); - if (!tags || tags.length == 0) return Response.json({ error: "At least one tag is required" }, { status: 400 }); - if (!qrBytesRaw || qrBytesRaw.length == 0) return Response.json({ error: "A QR code is required" }, { status: 400 }); + if (!name) return NextResponse.json({ error: "Name is required" }, { status: 400 }); + if (!tags || tags.length == 0) return NextResponse.json({ error: "At least one tag is required" }, { status: 400 }); + if (!qrBytesRaw || qrBytesRaw.length == 0) return NextResponse.json({ error: "A QR code is required" }, { status: 400 }); const nameValidation = nameSchema.safeParse(name); - if (!nameValidation.success) return Response.json({ error: nameValidation.error.errors[0].message }, { status: 400 }); + if (!nameValidation.success) return NextResponse.json({ error: nameValidation.error.errors[0].message }, { status: 400 }); const tagsValidation = tagsSchema.safeParse(tags); - if (!tagsValidation.success) return Response.json({ error: tagsValidation.error.errors[0].message }, { status: 400 }); + if (!tagsValidation.success) return NextResponse.json({ error: tagsValidation.error.errors[0].message }, { status: 400 }); // Validate QR code size - if (qrBytesRaw.length !== 372) return Response.json({ error: "QR code size is not a valid Tomodachi Life QR code" }, { status: 400 }); + if (qrBytesRaw.length !== 372) return NextResponse.json({ error: "QR code size is not a valid Tomodachi Life QR code" }, { status: 400 }); const qrBytes = new Uint8Array(qrBytesRaw); @@ -47,7 +49,7 @@ export async function POST(request: Request) { decrypted = AES_CCM.decrypt(content, MII_DECRYPTION_KEY, nonceWithZeros, undefined, 16); } catch (error) { console.warn("Failed to decrypt QR code:", error); - return Response.json({ error: "Failed to decrypt QR code. It may be invalid or corrupted." }, { status: 400 }); + return NextResponse.json({ error: "Failed to decrypt QR code. It may be invalid or corrupted." }, { status: 400 }); } const result = new Uint8Array(96); @@ -57,7 +59,7 @@ export async function POST(request: Request) { // Check if QR code is valid (after decryption) if (result.length !== 0x60 || (result[0x16] !== 0 && result[0x17] !== 0)) - return Response.json({ error: "QR code is not a valid Mii QR code" }, { status: 400 }); + return NextResponse.json({ error: "QR code is not a valid Mii QR code" }, { status: 400 }); // Convert to Mii class let mii: Mii; @@ -75,7 +77,7 @@ export async function POST(request: Request) { } } catch (error) { console.warn("Mii data is not valid:", error); - return Response.json({ error: "Mii data is not valid" }, { status: 400 }); + return NextResponse.json({ error: "Mii data is not valid" }, { status: 400 }); } // Create Mii in database @@ -112,7 +114,7 @@ export async function POST(request: Request) { // Clean up if something went wrong await prisma.mii.delete({ where: { id: miiRecord.id } }); console.error("Failed to download Mii image:", error); - return Response.json({ error: "Failed to download Mii image" }, { status: 500 }); + return NextResponse.json({ error: "Failed to download Mii image" }, { status: 500 }); } try { @@ -140,10 +142,10 @@ export async function POST(request: Request) { // todo: upload user images - return Response.json({ success: true, id: miiRecord.id }); + return NextResponse.json({ success: true, id: miiRecord.id }); } catch (error) { await prisma.mii.delete({ where: { id: miiRecord.id } }); console.error("Error processing Mii files:", error); - return Response.json({ error: "Failed to process and store Mii files" }, { status: 500 }); + return NextResponse.json({ error: "Failed to process and store Mii files" }, { status: 500 }); } }