-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathdomain-utils.ts
More file actions
240 lines (216 loc) · 6.34 KB
/
Copy pathdomain-utils.ts
File metadata and controls
240 lines (216 loc) · 6.34 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { parse } from "tldts";
const VERCEL_CNAME_TARGET = "cname.vercel-dns.com";
const VERCEL_A_RECORD = "76.76.21.21";
export const isSubdomain = (raw: string): boolean => {
const input =
raw
.trim()
.replace(/^https?:\/\//i, "")
.split("/")[0] ?? "";
if (!input) return false;
const host = (input.replace(/\.$/, "").split(":")[0] || "").toLowerCase();
const { subdomain } = parse(host);
return Boolean(subdomain);
};
export const getConfigResponse = async (domain: string) => {
const response = await fetch(
`https://api.vercel.com/v6/domains/${domain.toLowerCase()}/config?teamId=${
process.env.VERCEL_TEAM_ID
}&strict=true`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
cache: "no-store",
},
).then((res) => res.json());
return response;
};
export const getDomainResponse = async (domain: string) => {
const response = await fetch(
`https://api.vercel.com/v9/projects/${
process.env.VERCEL_PROJECT_ID
}/domains/${domain.toLowerCase()}?teamId=${process.env.VERCEL_TEAM_ID}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
cache: "no-store",
},
).then((res) => res.json());
return response;
};
export const verifyDomain = async (domain: string) => {
const response = await fetch(
`https://api.vercel.com/v9/projects/${
process.env.VERCEL_PROJECT_ID
}/domains/${domain.toLowerCase()}/verify?teamId=${
process.env.VERCEL_TEAM_ID
}&strict=true`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
},
).then((res) => res.json());
return response;
};
export const addDomain = async (domain: string) => {
const response = await fetch(
`https://api.vercel.com/v9/projects/${process.env.VERCEL_PROJECT_ID}/domains?teamId=${process.env.VERCEL_TEAM_ID}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ name: domain }),
cache: "no-store",
},
).then((res) => res.json());
return response;
};
export const removeDomain = async (domain: string) => {
const response = await fetch(
`https://api.vercel.com/v9/projects/${
process.env.VERCEL_PROJECT_ID
}/domains/${domain.toLowerCase()}?teamId=${process.env.VERCEL_TEAM_ID}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
},
},
).then((res) => res.json());
return response;
};
export const getRequiredConfig = async (domain: string) => {
// First try to get the records directly
try {
const recordsResponse = await fetch(
`https://api.vercel.com/v4/domains/${domain.toLowerCase()}/records?limit=10&teamId=${
process.env.VERCEL_TEAM_ID
}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
cache: "no-store",
},
).then((res) => res.json());
if (recordsResponse.records) {
const aRecord = recordsResponse.records.find(
(record: any) => record.type === "A" && record.name === "",
);
if (aRecord) {
return {
configuredBy: "vercel",
aValues: [aRecord.value],
serviceType: "vercel",
};
}
}
} catch (_error) {
// Continue to fallback
}
// Fallback to the old config endpoint
const response = await fetch(
`https://api.vercel.com/v6/domains/${domain.toLowerCase()}/config?teamId=${
process.env.VERCEL_TEAM_ID
}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
cache: "no-store",
},
).then((res) => res.json());
// If we still don't have the A record, try the project domains endpoint
if (!response.aValues || response.aValues.length === 0) {
const projectResponse = await fetch(
`https://api.vercel.com/v9/projects/${process.env.VERCEL_PROJECT_ID}/domains?teamId=${process.env.VERCEL_TEAM_ID}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.VERCEL_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
cache: "no-store",
},
).then((res) => res.json());
if (projectResponse.domains) {
const projectDomain = projectResponse.domains.find(
(d: any) => d.name === domain,
);
if (projectDomain?.apexValue) {
response.aValues = [projectDomain.apexValue];
}
}
}
return response;
};
export const checkDomainStatus = async (domain: string) => {
try {
const [domainJson, configJson, requiredConfigJson] = await Promise.all([
getDomainResponse(domain),
getConfigResponse(domain),
getRequiredConfig(domain),
]);
let verified = false;
if (configJson.misconfigured || domainJson?.error?.code === "not_found") {
verified = false;
} else if (domainJson.verified) {
verified = true;
} else {
const verificationJson = await verifyDomain(domain);
verified = verificationJson?.verified;
}
const currentAValues = configJson.aValues || [];
const subdomain = isSubdomain(domain);
// Vercel's recommendations are the source of truth, but the config
// endpoint can omit them (e.g. for domains not using Vercel DNS). Fall
// back to the well-known Vercel targets so the setup UI always has a
// record to display instead of rendering empty.
let recommendedCNAME: Array<{ rank: number; value: string }> =
configJson.recommendedCNAME || [];
let recommendedIPv4: Array<{ rank: number; value: string[] | string }> =
configJson.recommendedIPv4 || [];
if (subdomain && recommendedCNAME.length === 0) {
recommendedCNAME = [{ rank: 0, value: VERCEL_CNAME_TARGET }];
}
if (!subdomain && recommendedIPv4.length === 0) {
recommendedIPv4 = [{ rank: 0, value: [VERCEL_A_RECORD] }];
}
const requiredAValue =
requiredConfigJson.aValues?.[0] ??
(!subdomain ? VERCEL_A_RECORD : undefined);
return {
verified,
config: {
...configJson,
verification: domainJson?.verification || [],
currentAValues,
requiredAValue,
recommendedCNAME,
recommendedIPv4,
},
status: domainJson,
};
} catch (error) {
console.error("checkDomainStatus failed", { domain, error });
return {
verified: false,
error: "Failed to check domain status",
};
}
};