1
1
import { cachedFetcher } from '@/lib/fetch'
2
- import prisma from '@/api/models'
3
2
4
3
export const loggerInstance = process . env . CUSTOM_DOMAIN_LOGGER === 'true'
5
4
? {
@@ -17,32 +16,21 @@ export const loggerInstance = process.env.CUSTOM_DOMAIN_LOGGER === 'true'
17
16
18
17
export const domainLogger = ( ) => loggerInstance
19
18
20
- // fetch custom domain mappings from database , caching it for 5 minutes
19
+ // fetch custom domain mappings from our API , caching it for 5 minutes
21
20
export const getDomainMappingsCache = cachedFetcher ( async function fetchDomainMappings ( ) {
22
- domainLogger ( ) . log ( 'fetching domain mappings from database' ) // TEST
21
+ const url = `${ process . env . NEXT_PUBLIC_URL } /api/domains`
22
+ domainLogger ( ) . log ( 'fetching domain mappings from' , url ) // TEST
23
23
try {
24
- // fetch all VERIFIED custom domains from the database
25
- const domains = await prisma . customDomain . findMany ( {
26
- select : {
27
- domain : true ,
28
- subName : true
29
- } ,
30
- where : {
31
- status : 'ACTIVE'
32
- }
33
- } )
34
-
35
- // map domains to a key-value pair
36
- const domainMappings = domains . reduce ( ( acc , domain ) => {
37
- acc [ domain . domain . toLowerCase ( ) ] = {
38
- subName : domain . subName
39
- }
40
- return acc
41
- } , { } )
24
+ const response = await fetch ( url )
25
+ if ( ! response . ok ) {
26
+ domainLogger ( ) . error ( `Cannot fetch domain mappings: ${ response . status } ${ response . statusText } ` )
27
+ return null
28
+ }
42
29
43
- return domainMappings
30
+ const data = await response . json ( )
31
+ return Object . keys ( data ) . length > 0 ? data : null
44
32
} catch ( error ) {
45
- domainLogger ( ) . error ( 'cannot fetch domain mappings from db :' , error )
33
+ domainLogger ( ) . error ( 'Cannot fetch domain mappings:' , error )
46
34
return null
47
35
}
48
36
} , {
0 commit comments