mirror of
https://github.com/dsoaress/link-free.git
synced 2026-08-18 07:26:03 +01:00
feat: add user and session
This commit is contained in:
@@ -1,14 +1,31 @@
|
||||
import { Prisma } from '@prisma/client'
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import nc from 'next-connect'
|
||||
|
||||
import { prisma } from '../../services/prisma'
|
||||
import { ExceptionError } from '../../utils/error'
|
||||
|
||||
const handler = nc<NextApiRequest, NextApiResponse>({
|
||||
onNoMatch: (_req, res) => {
|
||||
res.status(404).json({ error: 'Not found' })
|
||||
},
|
||||
onError: (err, _req, res) => {
|
||||
res.status(500).json({ error: err.message })
|
||||
if (err instanceof ExceptionError) {
|
||||
res.status(err.status).json({
|
||||
status: err.status,
|
||||
error: err.message
|
||||
})
|
||||
} else if (err instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
res.status(400).json({
|
||||
status: 400,
|
||||
error: err.message
|
||||
})
|
||||
} else {
|
||||
res.status(500).json({
|
||||
status: 500,
|
||||
error: err.message
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
.get(async (_req, res) => {
|
||||
@@ -17,7 +34,7 @@ const handler = nc<NextApiRequest, NextApiResponse>({
|
||||
})
|
||||
|
||||
if (!response) {
|
||||
throw new Error('No data found')
|
||||
throw new ExceptionError('No data found')
|
||||
}
|
||||
|
||||
res.status(200).json(response?.data)
|
||||
@@ -27,7 +44,7 @@ const handler = nc<NextApiRequest, NextApiResponse>({
|
||||
const { data } = req.body
|
||||
|
||||
if (!data) {
|
||||
throw new Error('No data provided')
|
||||
throw new ExceptionError('No data provided')
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -38,8 +55,7 @@ const handler = nc<NextApiRequest, NextApiResponse>({
|
||||
|
||||
res.status(200).json(response)
|
||||
} catch (err: any) {
|
||||
console.error(err)
|
||||
res.status(500).json({ error: err.message })
|
||||
throw new ExceptionError(err)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user