-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare.js
61 lines (53 loc) · 1.77 KB
/
cloudflare.js
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
class Cloudflare {
/** @param {string} apiToken Your Cloudflare API token */
constructor(apiToken) {
this.apiToken = apiToken
}
request(
url,
method = 'GET',
body,
headers = {
Authorization: `Bearer ${this.apiToken}`,
'Content-Type': 'application/json',
}
) {
return new Promise((resolve, reject) => {
fetch(url, {
method: method,
body: body,
headers: headers,
})
.then(response => {
return response.json()
})
.then(json => {
if (!json.success) {
reject(`Encountered error during request (${method}) to ${url}:\n${formatErrors(json.errors)}`)
} else {
resolve(json.result)
}
})
})
}
verifyToken() {
return this.request('https://api.cloudflare.com/client/v4/user/tokens/verify')
}
updateRecord(zoneId, recordId, data) {
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${recordId}`
return this.request(url, 'PUT', JSON.stringify(data))
}
getZones() {
return this.request('https://api.cloudflare.com/client/v4/zones')
}
getRecords(zoneId) {
return this.request(`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`)
}
getRecord(zoneId, recordId) {
return this.request(`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${recordId}`)
}
}
function formatErrors(errors) {
return errors.map(e => ` Code ${e.code}: ${e.message}`).join('\n')
}
module.exports = { Cloudflare, formatErrors }