-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathindex.js
39 lines (34 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import prisma from '@/api/models'
// TODO: Authentication for this?
export default async function handler (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
// Only allow GET requests
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' })
}
try {
// fetch all custom domains from the database
const domains = await prisma.customDomain.findMany({
select: {
domain: true,
subName: true
},
where: {
verificationState: 'VERIFIED'
}
})
// map domains to a key-value pair
const domainMappings = domains.reduce((acc, domain) => {
acc[domain.domain.toLowerCase()] = {
subName: domain.subName
}
return acc
}, {})
return res.status(200).json(domainMappings)
} catch (error) {
console.error('cannot fetch domains:', error)
return res.status(500).json({ error: 'Failed to fetch domains' })
}
}