|
| 1 | +import { withWorkspaceUser } from '@saasfy/api/server'; |
| 2 | +import { createAdminClient } from '@saasfy/supabase/server'; |
| 3 | + |
| 4 | +type VercelError = { |
| 5 | + error: { |
| 6 | + code: string; |
| 7 | + message: string; |
| 8 | + }; |
| 9 | +}; |
| 10 | + |
| 11 | +export const POST = withWorkspaceUser<{ domainSlug: string }>( |
| 12 | + ['owner', 'member'] as const, |
| 13 | + async ({ workspace, params }) => { |
| 14 | + const supabase = createAdminClient(); |
| 15 | + |
| 16 | + const { data: domain } = await supabase |
| 17 | + .from('domains') |
| 18 | + .select('*') |
| 19 | + .eq('slug', params.domainSlug) |
| 20 | + .eq('workspace_id', workspace.id) |
| 21 | + .single(); |
| 22 | + |
| 23 | + if (!domain) { |
| 24 | + return Response.json( |
| 25 | + { |
| 26 | + errors: ['Domain not found'], |
| 27 | + domain: null, |
| 28 | + }, |
| 29 | + { |
| 30 | + status: 404, |
| 31 | + }, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + if (domain.verified && domain.configured) { |
| 36 | + return Response.json( |
| 37 | + { |
| 38 | + ...domain, |
| 39 | + errors: null, |
| 40 | + }, |
| 41 | + { |
| 42 | + status: 200, |
| 43 | + }, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + const [vercelDomainConfigResponse, vercelDomainResponse] = await Promise.all([ |
| 48 | + fetch( |
| 49 | + `https://api.vercel.com/v6/domains/${domain.slug}/config?teamId=${process.env.TEAM_ID_VERCEL}`, |
| 50 | + { |
| 51 | + method: 'GET', |
| 52 | + headers: { |
| 53 | + Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`, |
| 54 | + 'Content-Type': 'application/json', |
| 55 | + }, |
| 56 | + }, |
| 57 | + ), |
| 58 | + fetch( |
| 59 | + `https://api.vercel.com/v9/projects/${process.env.PROJECT_ID_VERCEL}/domains/${domain.slug}?teamId=${process.env.TEAM_ID_VERCEL}`, |
| 60 | + { |
| 61 | + method: 'GET', |
| 62 | + headers: { |
| 63 | + Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`, |
| 64 | + 'Content-Type': 'application/json', |
| 65 | + }, |
| 66 | + }, |
| 67 | + ), |
| 68 | + ]); |
| 69 | + |
| 70 | + const vercelDomainConfig = (await vercelDomainConfigResponse.json()) as { |
| 71 | + misconfigured: boolean; |
| 72 | + error: string; |
| 73 | + conflicts: { |
| 74 | + name: string; |
| 75 | + type: string; |
| 76 | + value: string; |
| 77 | + }[]; |
| 78 | + }; |
| 79 | + |
| 80 | + const vercelDomain = (await vercelDomainResponse.json()) as { |
| 81 | + apexName: string; |
| 82 | + name: string; |
| 83 | + verified: boolean; |
| 84 | + verification: { |
| 85 | + domain: string; |
| 86 | + reason: string; |
| 87 | + type: string; |
| 88 | + value: string; |
| 89 | + }[]; |
| 90 | + } & VercelError; |
| 91 | + |
| 92 | + if (vercelDomainResponse.status !== 200) { |
| 93 | + return Response.json( |
| 94 | + { |
| 95 | + ...domain, |
| 96 | + errors: vercelDomain.error ? [vercelDomain.error.message] : null, |
| 97 | + }, |
| 98 | + { |
| 99 | + status: vercelDomainResponse.status, |
| 100 | + }, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * If domain is not verified, we try to verify now |
| 106 | + */ |
| 107 | + let domainVerification = null; |
| 108 | + if (!vercelDomain.verified) { |
| 109 | + const domainVerificationResponse = await fetch( |
| 110 | + `https://api.vercel.com/v9/projects/${process.env.PROJECT_ID_VERCEL}/domains/${params.domainSlug}/verify?teamId=${process.env.TEAM_ID_VERCEL}`, |
| 111 | + { |
| 112 | + method: 'POST', |
| 113 | + headers: { |
| 114 | + Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`, |
| 115 | + 'Content-Type': 'application/json', |
| 116 | + }, |
| 117 | + }, |
| 118 | + ); |
| 119 | + |
| 120 | + domainVerification = (await domainVerificationResponse.json()) as { |
| 121 | + apexName: string; |
| 122 | + name: string; |
| 123 | + verified: boolean; |
| 124 | + verification: { |
| 125 | + domain: string; |
| 126 | + reason: string; |
| 127 | + type: string; |
| 128 | + value: string; |
| 129 | + }[]; |
| 130 | + } & VercelError; |
| 131 | + |
| 132 | + if (domainVerificationResponse.status !== 200) { |
| 133 | + return Response.json( |
| 134 | + { |
| 135 | + ...domain, |
| 136 | + ...vercelDomain, |
| 137 | + errors: domainVerification.error ? [domainVerification.error.message] : null, |
| 138 | + }, |
| 139 | + { |
| 140 | + status: domainVerificationResponse.status, |
| 141 | + }, |
| 142 | + ); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if ( |
| 147 | + ((domainVerification && domainVerification.verified) || vercelDomain.verified) && |
| 148 | + !domain.verified |
| 149 | + ) { |
| 150 | + await supabase.from('domains').update({ verified: true }).eq('id', domain.id); |
| 151 | + } |
| 152 | + |
| 153 | + if (!vercelDomainConfig.misconfigured && !domain.configured) { |
| 154 | + await supabase.from('domains').update({ configured: true }).eq('id', domain.id); |
| 155 | + } |
| 156 | + |
| 157 | + return Response.json({ |
| 158 | + ...domain, |
| 159 | + ...vercelDomain, |
| 160 | + ...(domainVerification ? domainVerification : {}), |
| 161 | + conflicts: vercelDomainConfig.conflicts, |
| 162 | + configured: !vercelDomainConfig.misconfigured, |
| 163 | + errors: null, |
| 164 | + }); |
| 165 | + }, |
| 166 | +); |
0 commit comments