cloudflare/ts/cflare.classes.cflareaccount.ts

167 lines
6.4 KiB
TypeScript
Raw Normal View History

2016-05-24 21:36:06 +00:00
import "typings-global";
2016-04-27 00:20:27 +00:00
import plugins = require("./cflare.plugins");
import helpers = require("./cflare.classes.helpers");
2017-01-22 18:37:00 +00:00
import * as interfaces from './cflare.interfaces'
2016-04-27 00:20:27 +00:00
2016-05-15 17:51:48 +00:00
export class CflareAccount {
2017-01-22 18:37:00 +00:00
private authEmail: string;
private authKey: string;
private authCheck() {
2016-04-27 01:08:14 +00:00
return (this.authEmail && this.authKey); //check if auth is available
}
2017-01-22 18:37:00 +00:00
constructor() {
2016-04-27 00:20:27 +00:00
};
2017-01-22 18:37:00 +00:00
auth(optionsArg: { email: string, key: string }) {
2016-04-27 01:08:14 +00:00
this.authEmail = optionsArg.email;
2017-01-22 18:37:00 +00:00
this.authKey = optionsArg.key;
2016-04-27 01:08:14 +00:00
}
2017-01-22 18:37:00 +00:00
getZoneId(domainName: string) {
2016-05-16 01:29:29 +00:00
let done = plugins.q.defer();
this.listZones(domainName)
2017-01-22 18:37:00 +00:00
.then(zoneArrayArg => {
let filteredResponse = zoneArrayArg.filter((zoneArg) => {
2016-05-16 01:29:29 +00:00
return zoneArg.name === domainName;
});
2017-01-22 18:37:00 +00:00
if (filteredResponse.length >= 1) {
2016-05-16 01:29:29 +00:00
done.resolve(filteredResponse[0].id);
} else {
2017-01-22 18:37:00 +00:00
plugins.beautylog.error(`the domain ${domainName} does not appear to be in this account!`);
2016-05-16 01:29:29 +00:00
done.reject(undefined);
}
});
return done.promise;
}
2017-01-22 18:37:00 +00:00
getRecord(domainNameArg: string, typeArg: string): Promise<interfaces.ICflareRecord> {
2016-05-16 01:29:29 +00:00
let done = plugins.q.defer();
2017-01-22 18:37:00 +00:00
let result: interfaces.ICflareRecord
2016-05-25 04:32:56 +00:00
let domain = new plugins.smartstring.Domain(domainNameArg);
2016-05-25 05:06:06 +00:00
this.listRecords(domain.zoneName)
2017-01-22 18:37:00 +00:00
.then((recordArrayArg) => {
let filteredResponse = recordArrayArg.filter((recordArg) => {
return (recordArg.type == typeArg && recordArg.name == domainNameArg);
2016-05-16 01:29:29 +00:00
})
2016-06-21 17:04:43 +00:00
done.resolve(filteredResponse[0]);
2016-05-16 01:29:29 +00:00
})
return done.promise;
};
2017-01-22 18:37:00 +00:00
createRecord(domainNameArg: string, typeArg: string, contentArg: string) {
2016-04-27 01:08:14 +00:00
let done = plugins.q.defer();
2016-05-25 04:26:48 +00:00
let domain = new plugins.smartstring.Domain(domainNameArg);
2016-05-25 05:06:06 +00:00
this.getZoneId(domain.zoneName)
2017-01-22 18:37:00 +00:00
.then((domainIdArg) => {
2016-05-24 21:36:06 +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-22 18:37:00 +00:00
this.request("POST", "/zones/" + domainIdArg + "/dns_records", dataObject)
.then(function (responseArg) {
2016-05-24 21:36:06 +00:00
done.resolve(responseArg);
});
});
2016-04-27 01:08:14 +00:00
return done.promise;
};
2017-01-22 18:37:00 +00:00
removeRecord(domainNameArg: string, typeArg: string) {
2016-04-27 01:08:14 +00:00
let done = plugins.q.defer();
2016-05-25 04:26:48 +00:00
let domain = new plugins.smartstring.Domain(domainNameArg);
2017-01-22 18:37:00 +00:00
this.getRecord(domain.fullName, typeArg)
2016-05-25 04:26:48 +00:00
.then((responseArg) => {
2017-01-22 18:37:00 +00:00
if (responseArg) {
let requestRoute: string = "/zones/" + responseArg.zone_id + "/dns_records/" + responseArg.id;
this.request("DELETE", requestRoute)
2016-06-21 17:04:43 +00:00
.then((responseArg) => {
done.resolve(responseArg);
});
} else {
done.reject();
}
2016-05-25 04:26:48 +00:00
});
2016-04-27 01:08:14 +00:00
return done.promise;
};
2017-01-22 18:37:00 +00:00
updateRecord(domainNameArg: string, typeArg: string, valueArg) {
2016-05-16 01:29:29 +00:00
let done = plugins.q.defer();
2016-05-25 05:06:06 +00:00
let domain = new plugins.smartstring.Domain(domainNameArg);
2016-05-16 01:29:29 +00:00
return done.promise;
};
2017-01-22 18:37:00 +00:00
listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
let done = plugins.q.defer<interfaces.ICflareRecord[]>();
let result: interfaces.ICflareRecord[] = []
2016-05-25 05:06:06 +00:00
let domain = new plugins.smartstring.Domain(domainNameArg);
this.getZoneId(domain.zoneName)
2017-01-22 18:37:00 +00:00
.then((domainIdArg) => {
this.request("GET", "/zones/" + domainIdArg + "/dns_records?per_page=100")
.then(function (responseArg: any) {
result = responseArg.result
done.resolve(result);
2016-05-16 01:29:29 +00:00
});
});
2016-04-27 01:08:14 +00:00
return done.promise;
}
2017-01-22 18:37:00 +00:00
listZones(domainName?: string): Promise<interfaces.ICflareZone[]> { // TODO: handle pagination
let done = plugins.q.defer<interfaces.ICflareZone[]>();
2016-05-16 01:29:29 +00:00
let requestRoute = "/zones?per_page=50"
2017-01-22 18:37:00 +00:00
if (domainName) requestRoute = requestRoute + "&name=" + domainName;
let result = [];
this.request("GET", requestRoute)
.then((responseArg: any) => {
result = responseArg.result;
2016-05-16 01:29:29 +00:00
done.resolve(result);
});
2016-04-27 01:08:14 +00:00
return done.promise;
};
2017-01-22 18:37:00 +00:00
request(methodArg: string, routeArg: string, dataArg = {}) {
2016-04-27 01:08:14 +00:00
let done = plugins.q.defer();
2017-01-22 18:37:00 +00:00
let jsonArg: string = JSON.stringify(dataArg);
let options = {
2017-01-22 18:37:00 +00:00
method: methodArg,
url: "https://api.cloudflare.com/client/v4" + routeArg,
headers: {
"Content-Type": "application/json",
"X-Auth-Email": this.authEmail,
"X-Auth-Key": this.authKey
2016-05-16 01:29:29 +00:00
},
2017-01-22 18:37:00 +00:00
body: jsonArg
2016-05-16 01:29:29 +00:00
};
2016-06-21 17:04:43 +00:00
//console.log(options);
2017-01-22 18:37:00 +00:00
let retryCount = 0
let makeRequest = () => {
plugins.request(options, function (err, res, body) {
let responseObj
try {
responseObj = JSON.parse(body);
} catch (err) {
console.log(res.statusCode)
retryRequest()
return
}
if (!err && res.statusCode === 200) {
done.resolve(responseObj);
} else if (!err && res.statusCode === 429) {
console.log('rate limited! Waiting for retry!')
retryRequest()
return
} else {
console.log(res.statusCode)
console.log(responseObj.messages);
console.log(responseObj.errors);
done.reject(err);
};
});
}
let retryRequest = async (delayTimeArg = 15000) => {
console.log(`retry started and waiting for ${delayTimeArg} ms`)
await plugins.smartdelay.delayFor(delayTimeArg)
if (retryCount < 3) {
retryCount++
makeRequest()
return
}
}
makeRequest()
2016-04-27 01:08:14 +00:00
return done.promise;
}
2016-04-27 00:20:27 +00:00
};