Spaces:
Runtime error
Runtime error
File size: 5,535 Bytes
03cdf80 3d8743c 03cdf80 3d8743c 03cdf80 | 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 | const axios = require('axios');
const API_BASE = 'https://my.zone.id/api';
class ZoneIdAPI {
constructor() {
this.accessToken = null;
this.refreshTokenCookie = null;
this.client = axios.create({
baseURL: API_BASE,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'application/json',
'Content-Type': 'application/json',
'Origin': 'https://my.zone.id',
'Referer': 'https://my.zone.id/'
}
});
this.client.interceptors.request.use((config) => {
if (this.accessToken) {
config.headers.Authorization = `Bearer ${this.accessToken}`;
}
if (this.refreshTokenCookie) {
config.headers.Cookie = this.refreshTokenCookie;
}
return config;
});
this.client.interceptors.response.use(
(response) => response,
(error) => {
if (error.response) {
console.log('API Error:', error.response.status, error.response.data);
}
return Promise.reject(error);
}
);
}
setAccessToken(token) {
this.accessToken = token;
}
setRefreshTokenCookie(cookie) {
this.refreshTokenCookie = cookie;
}
async refreshAccessToken() {
if (!this.refreshTokenCookie) {
throw new Error('No refresh token cookie set');
}
const response = await this.client.post('/refresh-token');
if (response.data && response.data.token) {
this.accessToken = response.data.token;
}
return response.data;
}
async getAuthUrl() {
const response = await this.client.get('/authurl');
return response.data;
}
async authorize(code) {
const response = await this.client.post('/authorize', { auth_code: code });
return response.data;
}
async refreshToken() {
const response = await this.client.post('/refresh-token');
return response.data;
}
async logout() {
const response = await this.client.post('/logout');
return response.data;
}
async listSubdomains() {
const response = await this.client.get('/subdomains');
return response.data;
}
async createSubdomain(subdomain, suffix = 'zone.id', mode = 'dns_record', usageType, usageDescription) {
const fullSubdomain = `${subdomain}.${suffix}`;
console.log('Creating subdomain:', fullSubdomain);
const payload = {
subdomain: subdomain,
suffix: suffix,
mode
};
if (usageType) payload.usage_type = usageType;
if (usageDescription) payload.usage_description = usageDescription;
console.log('Payload:', JSON.stringify(payload));
const response = await this.client.post('/subdomains', payload);
console.log('Create response:', JSON.stringify(response.data));
if (!response.data || !response.data.id) {
console.log('No ID in response, fetching subdomain list...');
const list = await this.listSubdomains();
if (list && list.subdomains) {
const created = list.subdomains.find(d => d.subdomain === fullSubdomain);
if (created) {
console.log('Found created subdomain:', created.id);
return created;
}
}
}
return response.data;
}
async getSubdomain(id) {
const response = await this.client.get(`/subdomains/${id}`);
return response.data;
}
async updateSubdomain(id, data) {
const response = await this.client.patch(`/subdomains/${id}`, data);
return response.data;
}
async deleteSubdomain(id) {
const response = await this.client.delete(`/subdomains/${id}`);
return response.data;
}
async getDnsRecords(subdomainId) {
const response = await this.client.get(`/subdomains/${subdomainId}/dns`);
return response.data;
}
async createDnsRecord(subdomainId, record) {
const normalizedRecord = {
type: record.type,
hostname: record.hostname || record.name,
content: record.content || record.value
};
if (record.note) normalizedRecord.note = record.note;
const response = await this.client.post(`/subdomains/${subdomainId}/dns`, normalizedRecord);
return response.data;
}
async updateDnsRecord(subdomainId, recordId, record) {
const normalizedRecord = {};
if (record.type) normalizedRecord.type = record.type;
if (record.hostname || record.name) normalizedRecord.hostname = record.hostname || record.name;
if (record.content || record.value) normalizedRecord.content = record.content || record.value;
if (record.note) normalizedRecord.note = record.note;
const response = await this.client.patch(`/subdomains/${subdomainId}/dns/${recordId}`, normalizedRecord);
return response.data;
}
async deleteDnsRecord(subdomainId, recordId) {
const response = await this.client.delete(`/subdomains/${subdomainId}/dns/${recordId}`);
return response.data;
}
async getUrlForwarder(subdomainId) {
const response = await this.client.get(`/subdomains/${subdomainId}/url_forwarder`);
return response.data;
}
async createUrlForwarder(subdomainId, forwarder) {
const response = await this.client.post(`/subdomains/${subdomainId}/url_forwarder`, forwarder);
return response.data;
}
async getFeatured() {
const response = await this.client.get('/featured');
return response.data;
}
async getOfficial() {
const response = await this.client.get('/official');
return response.data;
}
}
module.exports = { ZoneIdAPI };
|