cloudflare/dist/cflare.classes.cflareaccount.js

164 lines
13 KiB
JavaScript
Raw Normal View History

2016-05-15 17:51:48 +00:00
"use strict";
2017-01-22 18:37:00 +00:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
2017-06-04 15:29:19 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2016-05-24 21:36:06 +00:00
require("typings-global");
2016-07-31 21:54:13 +00:00
const plugins = require("./cflare.plugins");
class CflareAccount {
2017-01-22 18:37:00 +00:00
constructor() {
}
2016-07-31 21:54:13 +00:00
auth(optionsArg) {
2016-05-15 17:51:48 +00:00
this.authEmail = optionsArg.email;
this.authKey = optionsArg.key;
2016-07-31 21:54:13 +00:00
}
getZoneId(domainName) {
2017-06-04 15:29:19 +00:00
return __awaiter(this, void 0, void 0, function* () {
let zoneArray = yield this.listZones(domainName);
let filteredResponse = zoneArray.filter((zoneArg) => {
2016-05-16 01:29:29 +00:00
return zoneArg.name === domainName;
});
if (filteredResponse.length >= 1) {
2017-06-04 15:29:19 +00:00
return filteredResponse[0].id;
2016-05-16 01:29:29 +00:00
}
else {
2017-01-22 18:37:00 +00:00
plugins.beautylog.error(`the domain ${domainName} does not appear to be in this account!`);
2017-06-04 15:29:19 +00:00
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
2016-05-16 01:29:29 +00:00
}
});
2016-07-31 21:54:13 +00:00
}
getRecord(domainNameArg, typeArg) {
let done = plugins.q.defer();
2017-01-22 18:37:00 +00:00
let result;
2016-07-31 21:54:13 +00:00
let domain = new plugins.smartstring.Domain(domainNameArg);
2016-06-18 13:36:15 +00:00
this.listRecords(domain.zoneName)
2017-01-22 18:37:00 +00:00
.then((recordArrayArg) => {
let filteredResponse = recordArrayArg.filter((recordArg) => {
2017-01-29 16:27:48 +00:00
return (recordArg.type === typeArg && recordArg.name === domainNameArg);
2016-05-16 01:44:02 +00:00
});
2016-06-21 17:04:43 +00:00
done.resolve(filteredResponse[0]);
2016-05-16 01:44:02 +00:00
});
return done.promise;
2016-07-31 21:54:13 +00:00
}
createRecord(domainNameArg, typeArg, contentArg) {
2017-06-04 15:29:19 +00:00
return __awaiter(this, void 0, void 0, function* () {
let done = plugins.q.defer();
let domain = new plugins.smartstring.Domain(domainNameArg);
let domainIdArg = yield this.getZoneId(domain.zoneName);
2016-07-31 21:54:13 +00:00
let dataObject = {
2016-05-25 04:26:48 +00:00
name: domain.fullName,
2016-05-24 21:36:06 +00:00
type: typeArg,
content: contentArg
};
2017-01-29 16:27:48 +00:00
this.request('POST', '/zones/' + domainIdArg + '/dns_records', dataObject)
2016-05-24 21:36:06 +00:00
.then(function (responseArg) {
done.resolve(responseArg);
});
2017-06-04 15:29:19 +00:00
return done.promise;
2016-05-24 21:36:06 +00:00
});
2016-07-31 21:54:13 +00:00
}
removeRecord(domainNameArg, typeArg) {
let done = plugins.q.defer();
let domain = new plugins.smartstring.Domain(domainNameArg);
2016-06-21 17:04:43 +00:00
this.getRecord(domain.fullName, typeArg)
2016-07-31 21:54:13 +00:00
.then((responseArg) => {
2016-06-21 17:04:43 +00:00
if (responseArg) {
2017-01-29 16:27:48 +00:00
let requestRoute = '/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id;
this.request('DELETE', requestRoute)
2016-07-31 21:54:13 +00:00
.then((responseArg) => {
2016-06-21 17:04:43 +00:00
done.resolve(responseArg);
});
}
else {
done.reject();
}
2016-05-25 04:26:48 +00:00
});
2016-05-15 17:51:48 +00:00
return done.promise;
2016-07-31 21:54:13 +00:00
}
updateRecord(domainNameArg, typeArg, valueArg) {
let done = plugins.q.defer();
let domain = new plugins.smartstring.Domain(domainNameArg);
2016-05-16 01:44:02 +00:00
return done.promise;
2016-07-31 21:54:13 +00:00
}
2017-06-04 15:29:19 +00:00
/**
* list all records of a specified domain name
* @param domainNameArg - the domain name that you want to get the records from
*/
2016-07-31 21:54:13 +00:00
listRecords(domainNameArg) {
2017-06-04 15:29:19 +00:00
return __awaiter(this, void 0, void 0, function* () {
let domain = new plugins.smartstring.Domain(domainNameArg);
let domainId = yield this.getZoneId(domain.zoneName);
let responseArg = yield this.request('GET', '/zones/' + domainId + '/dns_records?per_page=100');
let result = responseArg.result;
return result;
2016-05-16 01:29:29 +00:00
});
2016-07-31 21:54:13 +00:00
}
2017-06-04 15:29:19 +00:00
/**
* list all zones in the associated authenticated account
* @param domainName
*/
2016-07-31 21:54:13 +00:00
listZones(domainName) {
2017-06-04 15:29:19 +00:00
return __awaiter(this, void 0, void 0, function* () {
let requestRoute = '/zones?per_page=50';
// may be optionally filtered by domain name
if (domainName) {
requestRoute = requestRoute + '&name=' + domainName;
}
let response = yield this.request('GET', requestRoute);
let result = response.result;
return result;
2016-05-15 17:51:48 +00:00
});
2016-07-31 21:54:13 +00:00
}
request(methodArg, routeArg, dataArg = {}) {
let done = plugins.q.defer();
let jsonArg = JSON.stringify(dataArg);
let options = {
2016-05-15 17:51:48 +00:00
method: methodArg,
headers: {
2017-01-29 16:27:48 +00:00
'Content-Type': 'application/json',
'X-Auth-Email': this.authEmail,
'X-Auth-Key': this.authKey
2016-05-16 01:29:29 +00:00
},
2017-01-29 16:27:48 +00:00
requestBody: jsonArg
2016-05-15 17:51:48 +00:00
};
2017-01-29 16:27:48 +00:00
// console.log(options);
2017-01-22 18:37:00 +00:00
let retryCount = 0;
2017-01-29 16:27:48 +00:00
let makeRequest = () => __awaiter(this, void 0, void 0, function* () {
let response = yield plugins.smartrequest.request(`https://api.cloudflare.com/client/v4${routeArg}`, options);
if (response.statusCode === 200) {
done.resolve(response.body);
}
2017-01-29 17:05:39 +00:00
else if (response.statusCode === 429) {
2017-01-29 16:27:48 +00:00
console.log('rate limited! Waiting for retry!');
retryRequest();
}
2017-01-29 17:05:39 +00:00
else if (response.statusCode === 400) {
console.log('bad request! Going to retry!');
}
2017-01-29 16:27:48 +00:00
else {
2017-01-29 16:42:15 +00:00
console.log(response.statusCode);
2017-01-29 16:27:48 +00:00
done.reject(new Error('request failed'));
}
});
2017-01-29 16:42:15 +00:00
let retryRequest = (delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)) => __awaiter(this, void 0, void 0, function* () {
2017-01-22 18:37:00 +00:00
console.log(`retry started and waiting for ${delayTimeArg} ms`);
yield plugins.smartdelay.delayFor(delayTimeArg);
2017-01-29 16:27:48 +00:00
if (retryCount < 10) {
2017-01-22 18:37:00 +00:00
retryCount++;
2017-01-29 16:27:48 +00:00
return yield makeRequest();
2016-05-15 17:51:48 +00:00
}
});
2017-01-22 18:37:00 +00:00
makeRequest();
2016-05-15 17:51:48 +00:00
return done.promise;
2016-07-31 21:54:13 +00:00
}
2017-01-29 16:27:48 +00:00
authCheck() {
return (this.authEmail && this.authKey); // check if auth is available
}
2016-07-31 21:54:13 +00:00
}
2016-05-15 17:51:48 +00:00
exports.CflareAccount = CflareAccount;
2017-06-04 16:09:46 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2ZsYXJlLmNsYXNzZXMuY2ZsYXJlYWNjb3VudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL2NmbGFyZS5jbGFzc2VzLmNmbGFyZWFjY291bnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLDBCQUF1QjtBQUN2Qiw0Q0FBNEM7QUFHNUM7SUFHRTtJQUVBLENBQUM7SUFFRCxJQUFJLENBQUUsVUFBMEM7UUFDOUMsSUFBSSxDQUFDLFNBQVMsR0FBRyxVQUFVLENBQUMsS0FBSyxDQUFBO1FBQ2pDLElBQUksQ0FBQyxPQUFPLEdBQ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