Skip to content

Commit

Permalink
feat: ✨ fix update logic and refactor to use axios
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Gad committed Mar 16, 2023
1 parent 0545401 commit 073f648
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 58 deletions.
152 changes: 152 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"typescript": "^4.9.5"
},
"dependencies": {
"axios": "^1.3.4",
"dotenv": "^16.0.3",
"zod": "^3.21.4"
}
Expand Down
75 changes: 28 additions & 47 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Record } from './schemas';
import { updatedDNSRecordSchema, createdDNSRecord } from './schemas';
import { recordSchema } from './schemas';
import { log, retry } from './utils';
import { log, vercelAxios } from './utils';
import env from './env';
import { z } from 'zod';

Expand All @@ -14,16 +15,8 @@ async function getPublicIp() {
}

async function getDNSRecords() {
const res = await fetch(`https://api.vercel.com/v4/domains/${env.domain}/records`, {
headers: {
Authorization: `Bearer ${env.vercelApiKey}`,
},
method: 'get',
});
if (!res.ok) throw new Error(res.statusText);

const data = await res.json();
const records = z.array(recordSchema).parse(data.records);
const res = await vercelAxios.get(`v4/domains/${env.domain}/records`);
const records = z.array(recordSchema).parse(res.data.records);

log({
status: 'SUCCESS',
Expand All @@ -33,59 +26,47 @@ async function getDNSRecords() {
.map((r) => r.name)
.join(', '),
});
log({ status: 'SUCCESS', function: 'getDNSRecords', record: data.name, data });
return records;
}

async function updateDNSRecord(record: Record, publicIp: string) {
const res = await fetch(`https://api.vercel.com/v1/domains/records/${record.id}`, {
body: JSON.stringify({
name: record.name,
type: 'A',
value: publicIp,
ttl: 60,
}),
headers: {
Authorization: `Bearer ${env.vercelApiKey}`,
'Content-Type': 'application/json',
},
method: 'patch',
const res = await vercelAxios.patch(`v1/domains/records/${record.id}`, {
name: record.name,
type: 'A',
value: publicIp,
ttl: 60,
});
if (!res.ok) throw new Error(res.statusText);

const data = recordSchema.parse(await res.json());
log({ status: 'SUCCESS', function: 'updateDNSRecord', record: data.name, data });
const newRecord = updatedDNSRecordSchema.parse(res.data);
log({
status: 'SUCCESS',
function: 'updateDNSRecord',
record: newRecord.name,
data: newRecord,
});
}

async function createDNSRecord(name: string, value: string) {
const res = await fetch(`https://api.vercel.com/v2/domains/${env.domain}/records`, {
body: JSON.stringify({
name,
type: 'A',
value,
ttl: 60,
}),
headers: {
Authorization: `Bearer ${env.vercelApiKey}`,
'Content-Type': 'application/json',
},
method: 'post',
const res = await vercelAxios.post(`v2/domains/${env.domain}/records`, {
name: name,
type: 'A',
value: value,
ttl: 60,
});
if (!res.ok) throw new Error(res.statusText);

const data = await res.json();
log({ status: 'SUCCESS', function: 'createDNSRecord', data });
return data;
const record = createdDNSRecord.parse(res.data);
log({ status: 'SUCCESS', function: 'createDNSRecord', data: record });
return record;
}

async function main() {
const publicIp = await retry(getPublicIp);
const records = await retry(getDNSRecords);
const publicIp = await getPublicIp();
const records = await getDNSRecords();

env.subdomains.forEach(async (subdomain) => {
const record = records?.find((record) => record.name === subdomain);
if (!record) await retry(() => createDNSRecord(subdomain, publicIp));
if (record && record.value !== publicIp) await retry(() => updateDNSRecord(record, publicIp));
if (!record) await createDNSRecord(subdomain, publicIp);
if (record && record.value !== publicIp) await updateDNSRecord(record, publicIp);
});
}

Expand Down
Loading

0 comments on commit 073f648

Please sign in to comment.