2
0
Files
bot/apps/builder/src/pages/api/customDomains/[domain].ts

35 lines
1.2 KiB
TypeScript
Raw Normal View History

import prisma from '@/lib/prisma'
2022-02-18 14:57:10 +01:00
import { NextApiRequest, NextApiResponse } from 'next'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils/api'
2022-02-18 14:57:10 +01:00
import { got } from 'got'
import { getAuthenticatedUser } from '@/features/auth/api'
2022-02-18 14:57:10 +01:00
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
2022-05-13 15:22:44 -07:00
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res)
2022-02-18 14:57:10 +01:00
if (req.method === 'DELETE') {
2022-08-08 08:21:36 +02:00
const domain = req.query.domain as string
try {
await deleteDomainOnVercel(domain)
} catch {
/* empty */
}
2022-02-18 14:57:10 +01:00
const customDomains = await prisma.customDomain.delete({
where: { name: domain },
})
2022-02-18 14:57:10 +01:00
return res.send({ customDomains })
}
return methodNotAllowed(res)
}
const deleteDomainOnVercel = (name: string) =>
got.delete({
url: `https://api.vercel.com/v8/projects/${process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME}/domains/${name}?teamId=${process.env.VERCEL_TEAM_ID}`,
2022-02-18 14:57:10 +01:00
headers: { Authorization: `Bearer ${process.env.VERCEL_TOKEN}` },
})
export default handler