From a9f32e9f16f019dfaef2a322cbe1d3efb7dce8f8 Mon Sep 17 00:00:00 2001 From: Chris Hendrix Date: Tue, 28 Nov 2023 22:09:55 -0500 Subject: [PATCH] move some functions --- src/app/api/users/[id]/route.ts | 3 +-- src/app/api/users/route.ts | 26 +------------------------- src/utils/api.ts | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/app/api/users/[id]/route.ts b/src/app/api/users/[id]/route.ts index 0374ec5..1205169 100644 --- a/src/app/api/users/[id]/route.ts +++ b/src/app/api/users/[id]/route.ts @@ -1,8 +1,7 @@ import { NextRequest, NextResponse } from 'next/server' import prisma from '@/lib/prisma' -import { ApiError, routeWrapper, checkUserMatchesSession } from '@/utils/api' +import { ApiError, routeWrapper, checkUserMatchesSession, checkUserBody, sanitizeUserSelect } from '@/utils/api' import { generateHash, validatePassword } from '@/utils/hash' -import { checkUserBody, sanitizeUserSelect } from '../route' export const GET = routeWrapper( async (req: NextRequest, { params }: { params: { id: string } }) => { diff --git a/src/app/api/users/route.ts b/src/app/api/users/route.ts index 2447de6..3ac8194 100644 --- a/src/app/api/users/route.ts +++ b/src/app/api/users/route.ts @@ -1,31 +1,7 @@ import { NextRequest, NextResponse } from 'next/server' -import { Prisma } from '@prisma/client' import prisma from '@/lib/prisma' import { generateHash } from '@/utils/hash' -import { ApiError, routeWrapper, getQueryParams } from '@/utils/api' - -export const sanitizeUserSelect = () => { - const fields = Object.keys(Prisma.UserScalarFieldEnum) - return Object.fromEntries(fields.map((k) => [k, k !== 'password'])) -} - -export const checkUserBody = async (body: any, id: string | null = null) => { - const usernameExists = async (username: string) => { - const user = await prisma.user.findUnique({ where: { username } }) - if (!id) return Boolean(user) - return Boolean(user && user.id !== id) - } - const emailExists = async (email: string) => { - const user = await prisma.user.findUnique({ where: { email } }) - if (!id) return Boolean(user) - return Boolean(user && user.id !== id) - } - - if (!body) throw new ApiError('Request must have body', 400) - const { username, email } = body - if (username && await usernameExists(username)) throw new ApiError('Username exists', 400) - if (email && await emailExists(email)) throw new ApiError('Email exists', 400) -} +import { routeWrapper, getQueryParams, sanitizeUserSelect, checkUserBody } from '@/utils/api' export const GET = routeWrapper( async (req: NextRequest) => { diff --git a/src/utils/api.ts b/src/utils/api.ts index 450c2da..2424875 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -1,5 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable no-console */ +import prisma from '@/lib/prisma' +import { Prisma } from '@prisma/client' import { NextRequest, NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import authOptions from '@/lib/auth' @@ -121,3 +123,26 @@ export const getQueryParams = (nextUrl: NextURL) => { return queryParams } + +export const sanitizeUserSelect = () => { + const fields = Object.keys(Prisma.UserScalarFieldEnum) + return Object.fromEntries(fields.map((k) => [k, k !== 'password'])) +} + +export const checkUserBody = async (body: any, id: string | null = null) => { + const usernameExists = async (username: string) => { + const user = await prisma.user.findUnique({ where: { username } }) + if (!id) return Boolean(user) + return Boolean(user && user.id !== id) + } + const emailExists = async (email: string) => { + const user = await prisma.user.findUnique({ where: { email } }) + if (!id) return Boolean(user) + return Boolean(user && user.id !== id) + } + + if (!body) throw new ApiError('Request must have body', 400) + const { username, email } = body + if (username && await usernameExists(username)) throw new ApiError('Username exists', 400) + if (email && await emailExists(email)) throw new ApiError('Email exists', 400) +}