feat(api): add react-query client generation and redesign api responses

This commit is contained in:
Dominik 2025-06-12 19:47:05 +02:00
parent 8a8dc04956
commit 08931da294
Signed by: dominik
GPG key ID: 06A4003FC5049644
12 changed files with 2026 additions and 119 deletions

View file

@ -20,7 +20,13 @@ import { prisma } from '@/prisma';
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/PublicUser'
* type: 'object'
* properties:
* success:
* type: 'boolean'
* default: true
* user:
* $ref: '#/components/schemas/PublicUser'
* 401:
* description: User is not authenticated.
* content:
@ -29,7 +35,8 @@ import { prisma } from '@/prisma';
* $ref: '#/components/schemas/ErrorResponse'
* example:
* {
* message: 'Not authenticated'
* success: false,
* message: 'Not authenticated',
* }
* 404:
* description: User not found.
@ -39,34 +46,44 @@ import { prisma } from '@/prisma';
* $ref: '#/components/schemas/ErrorResponse'
* example:
* {
* success: false,
* message: 'User not found'
* }
*/
*/
export const GET = auth(async function GET(req, { params }) {
if (!req.auth)
return NextResponse.json({ message: 'Not authenticated' }, { status: 401 });
return NextResponse.json(
{ success: false, message: 'Not authenticated' },
{ status: 401 },
);
if (!req.auth.user || !req.auth.user.id)
return NextResponse.json({ message: 'User not found' }, { status: 404 });
return NextResponse.json(
{ success: false, message: 'User not found' },
{ status: 404 },
);
const requestedUser = (await params).user;
const dbUser = await prisma.user.findFirst({
where: {
OR: [
{ id: requestedUser },
{ name: requestedUser },
],
OR: [{ id: requestedUser }, { name: requestedUser }],
},
});
if (!dbUser)
return NextResponse.json({ message: 'User not found' }, { status: 404 });
return NextResponse.json(
{ success: false, message: 'User not found' },
{ status: 404 },
);
return NextResponse.json({
id: dbUser.id,
name: dbUser.name,
first_name: dbUser.first_name,
last_name: dbUser.last_name,
timezone: dbUser.timezone,
image: dbUser.image,
success: true,
user: {
id: dbUser.id,
name: dbUser.name,
first_name: dbUser.first_name,
last_name: dbUser.last_name,
timezone: dbUser.timezone,
image: dbUser.image,
},
});
});

View file

@ -20,7 +20,13 @@ import {
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* type: object
* properties:
* success:
* type: boolean
* default: true
* user:
* $ref: '#/components/schemas/User'
* 401:
* description: User is not authenticated.
* content:
@ -29,6 +35,7 @@ import {
* $ref: '#/components/schemas/ErrorResponse'
* example:
* {
* success: false,
* message: 'Not authenticated'
* }
* 404:
@ -39,14 +46,21 @@ import {
* $ref: '#/components/schemas/ErrorResponse'
* example:
* {
* success: false,
* message: 'User not found'
* }
*/
export const GET = auth(async function GET(req) {
if (!req.auth)
return NextResponse.json({ message: 'Not authenticated' }, { status: 401 });
return NextResponse.json(
{ success: false, message: 'Not authenticated' },
{ status: 401 },
);
if (!req.auth.user || !req.auth.user.id)
return NextResponse.json({ message: 'User not found' }, { status: 404 });
return NextResponse.json(
{ success: false, message: 'User not found' },
{ status: 404 },
);
const dbUser = await prisma.user.findUnique({
where: {
@ -54,13 +68,19 @@ export const GET = auth(async function GET(req) {
},
});
if (!dbUser)
return NextResponse.json({ message: 'User not found' }, { status: 404 });
return NextResponse.json(
{ success: false, message: 'User not found' },
{ status: 404 },
);
return NextResponse.json(
{
...dbUser,
password_hash: undefined, // Exclude sensitive information
email_verified: undefined, // Exclude sensitive information
success: true,
user: {
...dbUser,
password_hash: undefined, // Exclude sensitive information
email_verified: undefined, // Exclude sensitive information
},
},
{ status: 200 },
);
@ -102,7 +122,13 @@ export const GET = auth(async function GET(req) {
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* type: object
* properties:
* success:
* type: boolean
* default: true
* user:
* $ref: '#/components/schemas/User'
* 401:
* description: User is not authenticated.
* content:
@ -111,6 +137,7 @@ export const GET = auth(async function GET(req) {
* $ref: '#/components/schemas/ErrorResponse'
* example:
* {
* success: false,
* message: 'Not authenticated'
* }
* 404:
@ -121,6 +148,7 @@ export const GET = auth(async function GET(req) {
* $ref: '#/components/schemas/ErrorResponse'
* example:
* {
* success: false,
* message: 'User not found'
* }
* 400:
@ -131,20 +159,27 @@ export const GET = auth(async function GET(req) {
* $ref: '#/components/schemas/ErrorResponse'
* example:
* {
* success: false,
* message: 'No fields to update'
* }
*/
export const PUT = auth(async function PUT(req) {
if (!req.auth)
return NextResponse.json({ message: 'Not authenticated' }, { status: 401 });
return NextResponse.json(
{ success: false, message: 'Not authenticated' },
{ status: 401 },
);
if (!req.auth.user)
return NextResponse.json({ message: 'User not found' }, { status: 404 });
return NextResponse.json(
{ success: false, message: 'User not found' },
{ status: 404 },
);
const body = await req.json();
const { name, first_name, last_name, email, image, timezone } = body;
if (!name && !first_name && !last_name && !email && !image && !timezone)
return NextResponse.json(
{ message: 'No fields to update' },
{ success: false, message: 'No fields to update' },
{ status: 400 },
);
const updateData: Record<string, string> = {};
@ -152,7 +187,7 @@ export const PUT = auth(async function PUT(req) {
const nameValidation = userNameSchema.safeParse(name);
if (!nameValidation.success) {
return NextResponse.json(
{ message: nameValidation.error.errors[0].message },
{ success: false, message: nameValidation.error.errors[0].message },
{ status: 400 },
);
}
@ -160,9 +195,12 @@ export const PUT = auth(async function PUT(req) {
const existingUser = await prisma.user.findUnique({
where: { name },
});
if ((existingUser && existingUser.id !== req.auth.user.id) || disallowedUsernames.includes(name.toLowerCase())) {
if (
(existingUser && existingUser.id !== req.auth.user.id) ||
disallowedUsernames.includes(name.toLowerCase())
) {
return NextResponse.json(
{ message: 'Username in use by another account' },
{ success: false, message: 'Username in use by another account' },
{ status: 400 },
);
}
@ -172,7 +210,10 @@ export const PUT = auth(async function PUT(req) {
const firstNameValidation = userFirstNameSchema.safeParse(first_name);
if (!firstNameValidation.success) {
return NextResponse.json(
{ message: firstNameValidation.error.errors[0].message },
{
success: false,
message: firstNameValidation.error.errors[0].message,
},
{ status: 400 },
);
}
@ -182,7 +223,7 @@ export const PUT = auth(async function PUT(req) {
const lastNameValidation = userLastNameSchema.safeParse(last_name);
if (!lastNameValidation.success) {
return NextResponse.json(
{ message: lastNameValidation.error.errors[0].message },
{ success: false, message: lastNameValidation.error.errors[0].message },
{ status: 400 },
);
}
@ -192,7 +233,7 @@ export const PUT = auth(async function PUT(req) {
const emailValidation = userEmailSchema.safeParse(email);
if (!emailValidation.success) {
return NextResponse.json(
{ message: emailValidation.error.errors[0].message },
{ success: false, message: emailValidation.error.errors[0].message },
{ status: 400 },
);
}
@ -202,7 +243,7 @@ export const PUT = auth(async function PUT(req) {
});
if (existingUser && existingUser.id !== req.auth.user.id) {
return NextResponse.json(
{ message: 'Email in use by another account' },
{ success: false, message: 'Email in use by another account' },
{ status: 400 },
);
}
@ -221,12 +262,18 @@ export const PUT = auth(async function PUT(req) {
data: updateData,
});
if (!updatedUser)
return NextResponse.json({ message: 'User not found' }, { status: 404 });
return NextResponse.json(
{ success: false, message: 'User not found' },
{ status: 404 },
);
return NextResponse.json(
{
...updatedUser,
password_hash: undefined, // Exclude sensitive information
email_verified: undefined, // Exclude sensitive information
success: true,
user: {
...updatedUser,
password_hash: undefined, // Exclude sensitive information
email_verified: undefined, // Exclude sensitive information
},
},
{ status: 200 },
);