-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlive.js
86 lines (70 loc) · 2.06 KB
/
live.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
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
import fetch from 'node-fetch'
import createHttpError from 'http-errors'
// TODO: extract a separate package from this
const BASE_URL = 'https://porkbun.com/api/json/v3/dns/'
const DOMAIN = 'coding.blog'
const SUBDOMAIN_PATTERN = /^([a-z0-9-]+)\.coding\.blog$/
export async function getLiveBlogs() {
const response = await fetch(`${BASE_URL}retrieve/${DOMAIN}`, {
method: 'POST',
body: JSON.stringify({
apikey: process.env.API_KEY,
secretapikey: process.env.API_SECRET,
})
})
if (response.ok) {
const data = await response.json()
return data.records
.filter(record =>
record.type === 'CNAME' &&
SUBDOMAIN_PATTERN.test(record.name) &&
record.name.substring(0, 4) !== 'www.'
)
.map(record => ({
name: record.name.split('.')[0],
host: record.content,
}))
} else {
throw createHttpError(response.status, response.statusText)
}
}
export async function addLiveBlog(name, host) {
const response = await fetch(`${BASE_URL}create/${DOMAIN}`, {
method: 'POST',
body: JSON.stringify({
apikey: process.env.API_KEY,
secretapikey: process.env.API_SECRET,
type: 'CNAME',
name,
content: host,
})
})
if (!response.ok) {
throw createHttpError(response.status, response.statusText)
}
}
export async function updateLiveBlog(name, host) {
const response = await fetch(`${BASE_URL}editByNameType/${DOMAIN}/CNAME/${name}`, {
method: 'POST',
body: JSON.stringify({
apikey: process.env.API_KEY,
secretapikey: process.env.API_SECRET,
content: host,
})
})
if (!response.ok) {
throw createHttpError(response.status, response.statusText)
}
}
export async function deleteLiveBlog(name) {
const response = await fetch(`${BASE_URL}deleteByNameType/${DOMAIN}/CNAME/${name}`, {
method: 'POST',
body: JSON.stringify({
apikey: process.env.API_KEY,
secretapikey: process.env.API_SECRET,
})
})
if (!response.ok) {
throw createHttpError(response.status, response.statusText)
}
}