refactor: use NextResponse instead of Response
This commit is contained in:
parent
58ac69e33d
commit
43174b009e
3 changed files with 28 additions and 23 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
|
|
@ -11,28 +12,28 @@ const usernameSchema = z
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
const session = await auth();
|
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) {
|
export async function PATCH(request: Request) {
|
||||||
const session = await auth();
|
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();
|
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);
|
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 } });
|
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({
|
await prisma.user.update({
|
||||||
where: { email: session.user?.email ?? undefined },
|
where: { email: session.user?.email ?? undefined },
|
||||||
data: { username },
|
data: { username },
|
||||||
});
|
});
|
||||||
|
|
||||||
return Response.json({ success: true });
|
return NextResponse.json({ success: true });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
|
|
||||||
|
|
@ -5,10 +7,10 @@ export async function PATCH(request: Request) {
|
||||||
// todo: rate limit
|
// todo: rate limit
|
||||||
|
|
||||||
const session = await auth();
|
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();
|
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 result = await prisma.$transaction(async (tx) => {
|
||||||
const existingLike = await tx.like.findUnique({
|
const existingLike = await tx.like.findUnique({
|
||||||
|
|
@ -47,5 +49,5 @@ export async function PATCH(request: Request) {
|
||||||
return { liked: !existingLike, count: likeCount };
|
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 });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
|
|
@ -17,20 +19,20 @@ const uploadsDirectory = path.join(process.cwd(), "public", "mii");
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const session = await auth();
|
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();
|
const { name, tags, qrBytesRaw } = await request.json();
|
||||||
if (!name) return Response.json({ error: "Name is required" }, { status: 400 });
|
if (!name) return NextResponse.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 (!tags || tags.length == 0) return NextResponse.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 (!qrBytesRaw || qrBytesRaw.length == 0) return NextResponse.json({ error: "A QR code is required" }, { status: 400 });
|
||||||
|
|
||||||
const nameValidation = nameSchema.safeParse(name);
|
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);
|
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
|
// 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);
|
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);
|
decrypted = AES_CCM.decrypt(content, MII_DECRYPTION_KEY, nonceWithZeros, undefined, 16);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("Failed to decrypt QR code:", 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);
|
const result = new Uint8Array(96);
|
||||||
|
|
@ -57,7 +59,7 @@ export async function POST(request: Request) {
|
||||||
|
|
||||||
// Check if QR code is valid (after decryption)
|
// Check if QR code is valid (after decryption)
|
||||||
if (result.length !== 0x60 || (result[0x16] !== 0 && result[0x17] !== 0))
|
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
|
// Convert to Mii class
|
||||||
let mii: Mii;
|
let mii: Mii;
|
||||||
|
|
@ -75,7 +77,7 @@ export async function POST(request: Request) {
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("Mii data is not valid:", 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
|
// Create Mii in database
|
||||||
|
|
@ -112,7 +114,7 @@ export async function POST(request: Request) {
|
||||||
// Clean up if something went wrong
|
// Clean up if something went wrong
|
||||||
await prisma.mii.delete({ where: { id: miiRecord.id } });
|
await prisma.mii.delete({ where: { id: miiRecord.id } });
|
||||||
console.error("Failed to download Mii image:", error);
|
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 {
|
try {
|
||||||
|
|
@ -140,10 +142,10 @@ export async function POST(request: Request) {
|
||||||
|
|
||||||
// todo: upload user images
|
// todo: upload user images
|
||||||
|
|
||||||
return Response.json({ success: true, id: miiRecord.id });
|
return NextResponse.json({ success: true, id: miiRecord.id });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await prisma.mii.delete({ where: { id: miiRecord.id } });
|
await prisma.mii.delete({ where: { id: miiRecord.id } });
|
||||||
console.error("Error processing Mii files:", error);
|
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 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue