wm-ddns
Version:
简单的 DNS 修改 Record 的包。 [https://DNSpod.cn](https://DNSpod.cn)。支持 callback & Promise & async/await。
218 lines (217 loc) • 8.15 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const DnsPodRecord_1 = __importDefault(require("./DnsPodRecord"));
const RecordType_1 = __importDefault(require("./enums/RecordType"));
class DnsPodDomain {
// private noCallback:
constructor(dnsPod, domainName, options) {
this.recordDataList = [];
this.domainOptions = {};
this.id = 0;
if (domainName === undefined) {
throw new Error('domainName can\'t be empty');
}
if (dnsPod === undefined) {
throw new Error('dnsPod can\'t be empty');
}
this.name = domainName;
this.dnsPod = dnsPod;
this.domainOptions = Object.assign({}, options);
}
getName() {
return this.name;
}
getId() {
return this.id;
}
setId(id) {
this.id = id;
}
getRecords() {
return this.recordDataList;
}
get recordsSize() {
return this.recordDataList.length;
}
getGetIpFunction() {
return this.dnsPod.getGetIpFunction();
}
setRawData(data) {
if (data === undefined) {
return;
}
if (data.id !== undefined) {
this.setId(data.id);
}
data.extStatus = data.ext_status;
data.dnspodNs = data.dnspod_nas;
this.raw = data;
}
recordList(options, raw) {
return __awaiter(this, void 0, void 0, function* () {
const path = '/Record.List';
const params = options === undefined ? {} : options;
const data = yield this.dnsPod.request(this, path, params);
const rawRecords = data.records;
if (raw === true) {
return rawRecords;
}
return rawRecords.map(record => new DnsPodRecord_1.default(this, record));
});
}
recordByName(name, raw = false) {
return __awaiter(this, void 0, void 0, function* () {
const recordList = yield this.recordList({ subDomain: name }, raw);
if (recordList !== undefined && (recordList.length > 0)) {
return recordList[0];
}
throw new Error('record by name not found');
});
}
recordById(recordId) {
return __awaiter(this, void 0, void 0, function* () {
const path = '/Record.Info';
const params = {
record_id: recordId
};
const data = yield this.dnsPod.request(this, path, params);
if (data === undefined || data.record === undefined) {
throw new Error('查询不到对应的 record');
}
return new DnsPodRecord_1.default(this, data.record);
});
}
recordByKeyword(keyword, offset, length) {
return __awaiter(this, void 0, void 0, function* () {
const recordList = yield this.recordList({ keyword, offset, length }, false);
if (recordList !== undefined && (recordList.length > 0)) {
return recordList[0];
}
});
}
createRecord(options) {
return __awaiter(this, void 0, void 0, function* () {
const path = '/Record.Create';
if (options.name === '') {
options.name = '@';
}
if (options.recordType === undefined) {
options.recordType = RecordType_1.default.A;
}
if (options.value === undefined) {
options.value = '127.0.0.1';
}
if (options.recordType === RecordType_1.default.MX) {
if (options.mx === undefined) {
throw new Error('options.mx must be get on MX record');
}
}
const json = {
sub_domain: options.name,
record_type: options.recordType,
value: options.value,
mx: options.mx,
record_line: options.recordLine === undefined ? '默认' : options.recordLine,
record_line_id: options.recordLineId,
ttl: options.ttl,
status: options.status,
weight: options.weight
};
const response = yield this.dnsPod.request(this, path, json);
const record = new DnsPodRecord_1.default(this, response);
yield record.sync();
return record;
});
}
updateRecord(options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const path = '/Record.Modify';
if (options.recordType === 'MX') {
if (options.mx === undefined) {
throw new Error('options.mx must be get on MX record');
}
}
const json = {
sub_domain: options.name,
record_id: options.id,
record_type: (_a = options.recordType) === null || _a === void 0 ? void 0 : _a.toString(),
value: options.value,
mx: options.mx,
record_line: options.recordLine === undefined ? '默认' : options.recordLine,
record_line_id: options.recordLineId,
ttl: options.ttl,
status: options.status,
weight: options.weight
};
return yield this.dnsPod.request(this, path, json);
});
}
// noinspection JSUnusedGlobalSymbols
updateRecordByName(name, options) {
return __awaiter(this, void 0, void 0, function* () {
const record = yield this.recordByName(name);
options.id = record.getId();
return yield this.updateRecord(options);
});
}
removeRecord(recordId) {
return __awaiter(this, void 0, void 0, function* () {
const path = '/Record.Remove';
const params = {
record_id: recordId
};
return yield this.dnsPod.request(this, path, params);
});
}
dns(options) {
return __awaiter(this, void 0, void 0, function* () {
const route = '/Record.Ddns';
const json = {
record_id: options.id,
sub_domain: options.name,
record_line: options.recordLine === undefined ? '默认' : options.recordLine,
record_line_id: options.recordLineId,
value: options.value
};
return yield this.dnsPod.request(this, route, json);
});
}
remark(recordId, remark) {
return __awaiter(this, void 0, void 0, function* () {
const path = '/Record.Remark';
return yield this.dnsPod.request(this, path, {
record_id: recordId,
remark
});
});
}
setStatus(recordId, status) {
return __awaiter(this, void 0, void 0, function* () {
const path = '/Record.Status';
return yield this.dnsPod.request(this, path, {
record_id: recordId,
status
});
});
}
toJSON() {
if (this.raw === undefined) {
throw new Error('api not init');
}
return this.raw;
}
}
exports.default = DnsPodDomain;