Skip to content

Commit 8d3540a

Browse files
committed
version to tag
1 parent 0239061 commit 8d3540a

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
!dist
21
# Logs
32
logs
43
*.log
@@ -90,7 +89,6 @@ out
9089

9190
# Nuxt.js build / generate output
9291
.nuxt
93-
dist
9492

9593
# Gatsby files
9694
.cache/

dist/index.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
class MissingEnvironmentError extends Error {
2+
constructor(variable) {
3+
super(`Missing required environment variable: ${variable}`);
4+
this.name = 'MissingEnvironmentError';
5+
}
6+
}
7+
class CloudflareAPIError extends Error {
8+
constructor(message) {
9+
super(message);
10+
this.name = 'CloudflareAPIError';
11+
}
12+
}
13+
14+
// Utility Functions
15+
const validateEnvironment = () => {
16+
const requiredVars = [
17+
'INPUT_TOKEN',
18+
'INPUT_ZONE',
19+
'INPUT_TYPE',
20+
'INPUT_NAME',
21+
'INPUT_CONTENT',
22+
'INPUT_TTL',
23+
'INPUT_PROXIED'
24+
];
25+
for (const variable of requiredVars) {
26+
if (!process.env[variable]) {
27+
throw new MissingEnvironmentError(variable);
28+
}
29+
}
30+
};
31+
const logError = (message) => {
32+
console.error(`::error ::${message}`);
33+
};
34+
const logOutput = (name, value) => {
35+
console.log(`::set-output name=${name}::${value}`);
36+
};
37+
38+
class CloudflareClient {
39+
constructor(token, zone) {
40+
this.token = token;
41+
this.zone = zone;
42+
this.baseURL = 'https://api.cloudflare.com/client/v4/zones';
43+
this.headers = {
44+
Authorization: `Bearer ${this.token}`,
45+
'Content-Type': 'application/json'
46+
};
47+
}
48+
async getRecordId(name) {
49+
const url = `${this.baseURL}/${this.zone}/dns_records?name=${name}`;
50+
const response = await fetch(url, {
51+
method: 'GET',
52+
headers: this.headers
53+
});
54+
if (!response.ok) {
55+
throw new CloudflareAPIError(`HTTP Error! status ${response.status}`);
56+
}
57+
const data = (await response.json());
58+
if (data.success && data.result.length > 0) {
59+
return data.result[0].id;
60+
}
61+
return null;
62+
}
63+
async createRecord(record) {
64+
const url = `${this.baseURL}/${this.zone}/dns_records`;
65+
const response = await fetch(url, {
66+
method: 'POST',
67+
headers: this.headers,
68+
body: JSON.stringify(record)
69+
});
70+
if (!response.ok) {
71+
throw new CloudflareAPIError(`HTTP Error! status ${response.status}`);
72+
}
73+
const data = (await response.json());
74+
if (!data.success) {
75+
throw new CloudflareAPIError(data.errors[0].message);
76+
}
77+
logOutput('record_id', data.result[0].id);
78+
logOutput('name', data.result[0].name);
79+
}
80+
async updateRecord(id, record) {
81+
const url = `${this.baseURL}/${this.zone}/dns_records/${id}`;
82+
const response = await fetch(url, {
83+
method: 'PUT',
84+
headers: this.headers,
85+
body: JSON.stringify(record)
86+
});
87+
if (!response.ok) {
88+
throw new CloudflareAPIError(`HTTP Error! status ${response.status}`);
89+
}
90+
const data = (await response.json());
91+
if (!data.success) {
92+
throw new CloudflareAPIError(data.errors[0].message);
93+
}
94+
logOutput('record_id', data.result[0].id);
95+
logOutput('name', data.result[0].name);
96+
}
97+
}
98+
99+
const main = async () => {
100+
try {
101+
validateEnvironment();
102+
const { INPUT_TOKEN: token, INPUT_ZONE: zone, INPUT_TYPE: type, INPUT_NAME: name, INPUT_CONTENT: content, INPUT_TTL: ttl, INPUT_PROXIED: proxied } = process.env;
103+
const recordData = {
104+
type: type,
105+
name: name,
106+
content: content,
107+
ttl: Number(ttl),
108+
proxied: proxied === 'true'
109+
};
110+
const client = new CloudflareClient(token, zone);
111+
const recordId = await client.getRecordId(name);
112+
if (recordId) {
113+
await client.updateRecord(recordId, recordData);
114+
}
115+
else {
116+
await client.createRecord(recordData);
117+
}
118+
process.exit(0);
119+
}
120+
catch (error) {
121+
if (error instanceof Error) {
122+
logError(error.message);
123+
}
124+
else {
125+
logError('An unknown error occurred');
126+
}
127+
process.exit(1);
128+
}
129+
};
130+
main();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"scripts": {
2020
"test": "echo \"Error: no test specified\" && exit 1",
21-
"build": "rimraf dist && tsc && rollup -c rollup.config.mjs"
21+
"build": "rimraf dist && rollup -c rollup.config.mjs"
2222
},
2323
"keywords": [],
2424
"author": "",

0 commit comments

Comments
 (0)