UNPKG

wm-ddns

Version:

简单的 DNS 修改 Record 的包。 [https://DNSpod.cn](https://DNSpod.cn)。支持 callback & Promise & async/await。

216 lines (215 loc) 8.78 kB
"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 }); /** * Created by maple on 2018/1/10. */ const domain_api_1 = __importDefault(require("./apis/domain.api")); const record_api_1 = __importDefault(require("./apis/record.api")); const DNSRecord_1 = __importDefault(require("./DNSRecord")); class DNSDomain { // private noCallback: constructor(dnsPod, domainName, options) { this.id = 0; this.isSync = false; this.domainRaw = null; this.recordsList = []; this.recordClient = null; this.name = domainName; this.dnsPod = dnsPod; this.token = dnsPod.getToken(); if (options) { this.domainOptions = Object.assign({}, options); } this.domainClient = new domain_api_1.default(this.token, options); } getName() { return this.name; } getRecords() { if (!this.isSync) { throw new Error("domain not synced"); } return this.recordsList; } getRaw() { if (!this.isSync || this.domainRaw == null) { throw new Error("domain not synced"); } return this.domainRaw; } sync() { return __awaiter(this, void 0, void 0, function* () { // 只允许同步一次 if (this.isSync) { return; } this.isSync = true; const domainRaws = yield this.domainClient.queryDomainList(); // 查询列表 const domain = domainRaws.find(d => d.name === this.name); if (!domain) { throw new Error("domain not found"); } // 赋值 this.id = domain.id; this.domainRaw = domain; const recordClient = new record_api_1.default(this.id, this.token, this.domainOptions); this.recordClient = recordClient; const recordRaws = yield recordClient.recordList(); this.recordsList = recordRaws.map(raw => new DNSRecord_1.default(this, raw, recordClient)); }); } syncRecords() { return __awaiter(this, void 0, void 0, function* () { if (!this.isSync || this.recordClient == null) { yield this.sync(); return this.recordsList; } const recordClient = this.recordClient; const recordRaws = yield this.recordClient.recordList(); this.recordsList = recordRaws.map(raw => new DNSRecord_1.default(this, raw, recordClient)); return this.recordsList; }); } createRecord(options) { return __awaiter(this, void 0, void 0, function* () { if (!this.isSync) { yield this.sync(); } // !this.recordClient // if (this.recordClient == null) { // throw new Error("record client is uninitialized") // } const record = yield this.recordClient.createRecord(options); const recordInfo = yield this.recordClient.recordById(record.id); const recordRaw = Object.assign(Object.assign({}, recordInfo), { name: recordInfo.sub_domain, type: (yield recordInfo).record_type }); const recordClient = this.recordClient; const dnsRecord = new DNSRecord_1.default(this, recordRaw, recordClient); // 加入到 recordsList this.recordsList.push(dnsRecord); return dnsRecord; }); } queryRecordById(recordId, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.isSync) { yield this.sync(); } let recordsList = this.recordsList; if (options === null || options === void 0 ? void 0 : options.sync) { recordsList = yield this.syncRecords(); } const dnsRecord = recordsList.find(record => record.getId() === recordId); if (dnsRecord) { return dnsRecord; } return null; }); } queryRecordByName(name, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.isSync) { yield this.sync(); } let recordsList = this.recordsList; if (options === null || options === void 0 ? void 0 : options.sync) { recordsList = yield this.syncRecords(); } const dnsRecord = recordsList.find(record => record.getName() === name); if (dnsRecord) { return dnsRecord; } return null; }); } updateRecord(updateParams) { return __awaiter(this, void 0, void 0, function* () { if (!this.isSync) { yield this.sync(); } const updatedRecord = yield this.recordClient.updateRecord(updateParams); const record = yield this.queryRecordById(updatedRecord.id); if (!record) { throw new Error("updated record not found"); } for (let i = 0; i < this.recordsList.length; i++) { if (record.getId() === this.recordsList[i].getId()) { this.recordsList[i].updateByRaw(record.getRaw()); return this.recordsList[i]; } } // 没有在列表找到这个 record this.recordsList.push(record); // 返回默认的 record return record; }); } get(type, target) { return __awaiter(this, void 0, void 0, function* () { if (!this.isSync) { yield this.sync(); } let record = null; if (typeof target === "number") { record = yield this.queryRecordById(target); } else if (typeof target === "string") { record = yield this.queryRecordByName(target); } else if (target instanceof DNSRecord_1.default) { record = target; } if (!record) { throw new Error("record not found"); } return record; }); } ddns(type, target, value) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.get(type, target); const options = { id: record.getId(), name: record.getName(), value: value }; const result = yield this.recordClient.dns(options); record.updateByRaw(Object.assign(Object.assign({}, record.getRaw()), { value: result.value })); return record; }); } setRemark(type, target, remark) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.get(type, target); yield this.recordClient.remark(record.getId(), remark); record.updateByRaw(Object.assign(Object.assign({}, record.getRaw()), { remark: remark })); return record; }); } setStatus(type, target, status) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.get(type, target); const result = yield this.recordClient.setStatus(record.getId(), status); record.updateByRaw(Object.assign(Object.assign({}, record.getRaw()), { status: result.status })); return record; }); } deleteRecord(type, target) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.get(type, target); const result = yield this.recordClient.removeRecord(record.getId()); // 标记为删除 record.setDeleteStatus(); this.recordsList = this.recordsList.filter(re => re.getId() !== record.getId()); }); } } exports.default = DNSDomain;