fix(core): update

This commit is contained in:
2019-07-18 11:51:56 +02:00
parent b86391ca00
commit 8a4126a49c
14 changed files with 1351 additions and 377 deletions

View File

@ -1,17 +1,23 @@
import plugins = require('./cloudflare.plugins');
import * as interfaces from './cloudflare.interfaces';
import * as interfaces from './interfaces/cloudflare.interfaces';
// interfaces
import { TDnsRecord } from '@tsclass/tsclass';
import { WorkerManager } from './cloudflare.classes.workermanager';
import { ZoneManager } from './cloudflare.classes.zonemanager';
export class CloudflareAccount {
private authEmail: string;
private authKey: string;
public workerManager = new WorkerManager(this);
public zoneManager = new ZoneManager(this);
constructor() {
// Nothing here
}
auth(optionsArg: { email: string; key: string }) {
public auth(optionsArg: { email: string; key: string }) {
this.authEmail = optionsArg.email;
this.authKey = optionsArg.key;
}
@ -20,16 +26,19 @@ export class CloudflareAccount {
* gets a zone id of a domain from cloudflare
* @param domainName
*/
async getZoneId(domainName: string) {
let domain = new plugins.smartstring.Domain(domainName);
let zoneArray = await this.listZones(domain.zoneName);
let filteredResponse = zoneArray.filter(zoneArg => {
public async getZoneId(domainName: string) {
const domain = new plugins.smartstring.Domain(domainName);
const zoneArray = await this.listZones(domain.zoneName);
const filteredResponse = zoneArray.filter(zoneArg => {
return zoneArg.name === domainName;
});
if (filteredResponse.length >= 1) {
return filteredResponse[0].id;
} else {
plugins.smartlog.defaultLogger.error(`the domain ${domainName} does not appear to be in this account!`);
plugins.smartlog.defaultLogger.log(
'error',
`the domain ${domainName} does not appear to be in this account!`
);
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
}
}
@ -37,45 +46,46 @@ export class CloudflareAccount {
/**
* gets a record
* @param domainNameArg
* @param typeArg
* @param typeArg
*/
async getRecord(domainNameArg: string, typeArg: TDnsRecord): Promise<interfaces.ICflareRecord> {
let done = plugins.smartpromise.defer<interfaces.ICflareRecord>();
let result: interfaces.ICflareRecord;
let domain = new plugins.smartstring.Domain(domainNameArg);
this.listRecords(domain.zoneName).then(recordArrayArg => {
let filteredResponse = recordArrayArg.filter(recordArg => {
return recordArg.type === typeArg && recordArg.name === domainNameArg;
});
done.resolve(filteredResponse[0]);
public async getRecord(
domainNameArg: string,
typeArg: TDnsRecord
): Promise<interfaces.ICflareRecord> {
const domain = new plugins.smartstring.Domain(domainNameArg);
const recordArrayArg = await this.listRecords(domain.zoneName);
const filteredResponse = recordArrayArg.filter(recordArg => {
return recordArg.type === typeArg && recordArg.name === domainNameArg;
});
return done.promise;
return filteredResponse[0];
}
async createRecord(domainNameArg: string, typeArg: TDnsRecord, contentArg: string) {
let done = plugins.smartpromise.defer();
let domain = new plugins.smartstring.Domain(domainNameArg);
let domainIdArg = await this.getZoneId(domain.zoneName);
let dataObject = {
public async createRecord(
domainNameArg: string,
typeArg: TDnsRecord,
contentArg: string
): Promise<any> {
const domain = new plugins.smartstring.Domain(domainNameArg);
const domainIdArg = await this.getZoneId(domain.zoneName);
const dataObject = {
name: domain.fullName,
type: typeArg,
content: contentArg
};
this.request('POST', '/zones/' + domainIdArg + '/dns_records', dataObject).then(function(
responseArg
) {
done.resolve(responseArg);
});
return done.promise;
const response = await this.request(
'POST',
'/zones/' + domainIdArg + '/dns_records',
dataObject
);
return response;
}
removeRecord(domainNameArg: string, typeArg: TDnsRecord) {
let done = plugins.smartpromise.defer();
let domain = new plugins.smartstring.Domain(domainNameArg);
public removeRecord(domainNameArg: string, typeArg: TDnsRecord) {
const done = plugins.smartpromise.defer();
const domain = new plugins.smartstring.Domain(domainNameArg);
this.getRecord(domain.fullName, typeArg).then(responseArg => {
if (responseArg) {
let requestRoute: string =
const requestRoute: string =
'/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id;
this.request('DELETE', requestRoute).then(responseArg => {
done.resolve(responseArg);
@ -87,9 +97,9 @@ export class CloudflareAccount {
return done.promise;
}
updateRecord(domainNameArg: string, typeArg: string, valueArg) {
let done = plugins.smartpromise.defer();
let domain = new plugins.smartstring.Domain(domainNameArg);
public updateRecord(domainNameArg: string, typeArg: string, valueArg) {
const done = plugins.smartpromise.defer();
const domain = new plugins.smartstring.Domain(domainNameArg);
return done.promise;
}
@ -97,14 +107,14 @@ export class CloudflareAccount {
* list all records of a specified domain name
* @param domainNameArg - the domain name that you want to get the records from
*/
async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
let domain = new plugins.smartstring.Domain(domainNameArg);
let domainId = await this.getZoneId(domain.zoneName);
let responseArg: any = await this.request(
public async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
const domain = new plugins.smartstring.Domain(domainNameArg);
const domainId = await this.getZoneId(domain.zoneName);
const responseArg: any = await this.request(
'GET',
'/zones/' + domainId + '/dns_records?per_page=100'
);
let result: interfaces.ICflareRecord[] = responseArg.result;
const result: interfaces.ICflareRecord[] = responseArg.result;
return result;
}
@ -112,7 +122,7 @@ export class CloudflareAccount {
* list all zones in the associated authenticated account
* @param domainName
*/
async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
public async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
// TODO: handle pagination
let requestRoute = `/zones?per_page=50`;
@ -121,24 +131,24 @@ export class CloudflareAccount {
requestRoute = `${requestRoute}&name=${domainName}`;
}
let response: any = await this.request('GET', requestRoute);
let result = response.result;
const response: any = await this.request('GET', requestRoute);
const result = response.result;
return result;
}
async purgeZone(domainName: string) {
let domain = new plugins.smartstring.Domain(domainName);
let domainId = await this.getZoneId(domain.zoneName);
let requestUrl = `/zones/${domainId}/purge_cache`;
let payload = {
public async purgeZone(domainName: string) {
const domain = new plugins.smartstring.Domain(domainName);
const domainId = await this.getZoneId(domain.zoneName);
const requestUrl = `/zones/${domainId}/purge_cache`;
const payload = {
purge_everything: true
};
let respone = await this.request('DELETE', requestUrl, payload);
const respone = await this.request('DELETE', requestUrl, payload);
}
request(methodArg: string, routeArg: string, dataArg = {}) {
let done = plugins.smartpromise.defer();
let options: plugins.smartrequest.ISmartRequestOptions = {
public request(methodArg: string, routeArg: string, dataArg = {}) {
const done = plugins.smartpromise.defer();
const options: plugins.smartrequest.ISmartRequestOptions = {
method: methodArg,
headers: {
'Content-Type': 'application/json',
@ -151,8 +161,8 @@ export class CloudflareAccount {
let retryCount = 0; // count the amount of retries
let makeRequest = async () => {
let response: any = await plugins.smartrequest.request(
const makeRequest = async () => {
const response: any = await plugins.smartrequest.request(
`https://api.cloudflare.com/client/v4${routeArg}`,
options
);
@ -169,7 +179,9 @@ export class CloudflareAccount {
done.reject(new Error('request failed'));
}
};
let retryRequest = async (delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)) => {
const retryRequest = async (
delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)
) => {
console.log(`retry started and waiting for ${delayTimeArg} ms`);
await plugins.smartdelay.delayFor(delayTimeArg);
if (retryCount < 10) {

View File

@ -0,0 +1,10 @@
import * as plugins from './cloudflare.plugins';
import { CloudflareAccount } from './cloudflare.classes.account';
export class WorkerManager {
public cfAccount: CloudflareAccount;
constructor(cfAccountArg: CloudflareAccount) {
this.cfAccount = cfAccountArg;
}
}

View File

@ -0,0 +1,11 @@
import * as plugins from './cloudflare.plugins';
import { CloudflareAccount } from './cloudflare.classes.account';
export class ZoneManager {
public cfAccount: CloudflareAccount;
public zoneName: string;
constructor(cfAccountArg: CloudflareAccount) {
this.cfAccount = cfAccountArg;
}
}

View File

@ -1,4 +1,4 @@
import * as plugins from './cloudflare.plugins';
import * as plugins from '../cloudflare.plugins';
export interface ICflareZone {
id: string;

1
ts/interfaces/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './cloudflare.interfaces';