BREAKING CHANGE(API): move to .convenience property

This commit is contained in:
Philipp Kunz 2020-02-09 18:22:34 +00:00
parent 0032292714
commit 3e8cf73877
3 changed files with 119 additions and 117 deletions

View File

@ -19,7 +19,7 @@ tap.test('should create a valid instance of CloudflareAccount', async () => {
tap.test('.listZones() -> should display an entire account', async tools => { tap.test('.listZones() -> should display an entire account', async tools => {
tools.timeout(600000); tools.timeout(600000);
const result = await testCloudflareAccount.listZones(); const result = await testCloudflareAccount.convenience.listZones();
console.log(result); console.log(result);
}); });
@ -35,7 +35,7 @@ tap.test(
'.listRecords(domainName) -> should list all records for a specific Domain Name', '.listRecords(domainName) -> should list all records for a specific Domain Name',
async tools => { async tools => {
tools.timeout(600000); tools.timeout(600000);
await testCloudflareAccount.listRecords('bleu.de').then(async responseArg => { await testCloudflareAccount.convenience.listRecords('bleu.de').then(async responseArg => {
console.log(responseArg); console.log(responseArg);
}); });
} }
@ -43,12 +43,13 @@ tap.test(
tap.test('should create a valid record for a subdomain', async tools => { tap.test('should create a valid record for a subdomain', async tools => {
tools.timeout(600000); tools.timeout(600000);
await testCloudflareAccount.createRecord(`${randomPrefix}subdomain.bleu.de`, 'A', '127.0.0.1'); await testCloudflareAccount.convenience.createRecord(`${randomPrefix}subdomain.bleu.de`, 'A', '127.0.0.1');
}); });
tap.test('should get a record from Cloudflare', async tools => { tap.test('should get a record from Cloudflare', async tools => {
tools.timeout(600000); tools.timeout(600000);
await testCloudflareAccount await testCloudflareAccount
.convenience
.getRecord(`${randomPrefix}subdomain.bleu.de`, 'A') .getRecord(`${randomPrefix}subdomain.bleu.de`, 'A')
.then(responseArg => { .then(responseArg => {
console.log(responseArg); console.log(responseArg);
@ -58,6 +59,7 @@ tap.test('should get a record from Cloudflare', async tools => {
tap.test('should remove a subdomain record from Cloudflare', async tools => { tap.test('should remove a subdomain record from Cloudflare', async tools => {
tools.timeout(600000); tools.timeout(600000);
await testCloudflareAccount await testCloudflareAccount
.convenience
.removeRecord(`${randomPrefix}subdomain.bleu.de`, 'A') .removeRecord(`${randomPrefix}subdomain.bleu.de`, 'A')
.then(async responseArg => { .then(async responseArg => {
console.log(responseArg); console.log(responseArg);
@ -65,7 +67,7 @@ tap.test('should remove a subdomain record from Cloudflare', async tools => {
}); });
tap.test('.purge(some.domain) -> should purge everything', async () => { tap.test('.purge(some.domain) -> should purge everything', async () => {
await testCloudflareAccount.purgeZone('bleu.de'); await testCloudflareAccount.convenience.purgeZone('bleu.de');
}); });
// WORKERS // WORKERS

View File

@ -22,12 +22,14 @@ export class CloudflareAccount {
this.authKey = optionsArg.key; this.authKey = optionsArg.key;
} }
/**
* gets you the account identifier
*/
public async getAccountIdentifier() { public async getAccountIdentifier() {
if (!this.accountIdentifier) { if (!this.accountIdentifier) {
const route = `/accounts?page=1&per_page=20&direction=desc`; const route = `/accounts?page=1&per_page=20&direction=desc`;
const response: any = await this.request('GET', route); const response: any = await this.request('GET', route);
this.accountIdentifier = response.result[0].id; this.accountIdentifier = response.result[0].id;
// console.log('Account identifier is: ' + this.accountIdentifier);
} }
return this.accountIdentifier; return this.accountIdentifier;
} }
@ -38,7 +40,7 @@ export class CloudflareAccount {
*/ */
public async getZoneId(domainName: string) { public async getZoneId(domainName: string) {
const domain = new plugins.smartstring.Domain(domainName); const domain = new plugins.smartstring.Domain(domainName);
const zoneArray = await this.listZones(domain.zoneName); const zoneArray = await this.convenience.listZones(domain.zoneName);
const filteredResponse = zoneArray.filter(zoneArg => { const filteredResponse = zoneArray.filter(zoneArg => {
return zoneArg.name === domainName; return zoneArg.name === domainName;
}); });
@ -53,117 +55,117 @@ export class CloudflareAccount {
} }
} }
/** public convenience = {
* gets a record /**
* @param domainNameArg * gets a record
* @param typeArg * @param domainNameArg
*/ * @param typeArg
public async getRecord( */
domainNameArg: string, getRecord: async (
typeArg: plugins.tsclass.network.TDnsRecord domainNameArg: string,
): Promise<interfaces.ICflareRecord> { typeArg: plugins.tsclass.network.TDnsRecord
const domain = new plugins.smartstring.Domain(domainNameArg); ): Promise<interfaces.ICflareRecord> => {
const recordArrayArg = await this.listRecords(domain.zoneName); const domain = new plugins.smartstring.Domain(domainNameArg);
const filteredResponse = recordArrayArg.filter(recordArg => { const recordArrayArg = await this.convenience.listRecords(domain.zoneName);
return recordArg.type === typeArg && recordArg.name === domainNameArg; const filteredResponse = recordArrayArg.filter(recordArg => {
}); return recordArg.type === typeArg && recordArg.name === domainNameArg;
return filteredResponse[0]; });
} return filteredResponse[0];
},
/**
* creates a record
*/
createRecord: async (
domainNameArg: string,
typeArg: plugins.tsclass.network.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
};
const response = await this.request(
'POST',
'/zones/' + domainIdArg + '/dns_records',
dataObject
);
return response;
},
/**
* removes a record from Cloudflare
* @param domainNameArg
* @param typeArg
*/
removeRecord: async (
domainNameArg: string,
typeArg: plugins.tsclass.network.TDnsRecord
): Promise<any> => {
const domain = new plugins.smartstring.Domain(domainNameArg);
const cflareRecord = await this.convenience.getRecord(domain.fullName, typeArg);
if (cflareRecord) {
const requestRoute: string = `/zones/${cflareRecord.zone_id}/dns_records/${cflareRecord.id}`;
return await this.request('DELETE', requestRoute);
} else {
throw new Error(`could not remove record for ${domainNameArg} with type ${typeArg}`);
}
},
/**
* updates a record
* @param domainNameArg
* @param typeArg
* @param valueArg
*/
updateRecord: async (domainNameArg: string, typeArg: string, valueArg) => {
// TODO: implement
const domain = new plugins.smartstring.Domain(domainNameArg);
},
/**
* list all records of a specified domain name
* @param domainNameArg - the domain name that you want to get the records from
*/
listRecords: async (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'
);
const result: interfaces.ICflareRecord[] = responseArg.result;
return result;
},
/**
* list all zones in the associated authenticated account
* @param domainName
*/
listZones: async (domainName?: string): Promise<interfaces.ICflareZone[]> => {
// TODO: handle pagination
let requestRoute = `/zones?per_page=50`;
public async createRecord( // may be optionally filtered by domain name
domainNameArg: string, if (domainName) {
typeArg: plugins.tsclass.network.TDnsRecord, requestRoute = `${requestRoute}&name=${domainName}`;
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
};
const response = await this.request(
'POST',
'/zones/' + domainIdArg + '/dns_records',
dataObject
);
return response;
}
/** const response: any = await this.request('GET', requestRoute);
* removes a record from Cloudflare const result = response.result;
* @param domainNameArg return result;
* @param typeArg },
*/ /**
public async removeRecord( * purges a zone
domainNameArg: string, */
typeArg: plugins.tsclass.network.TDnsRecord purgeZone: async (domainName: string): Promise<void> => {
): Promise<any> { const domain = new plugins.smartstring.Domain(domainName);
const domain = new plugins.smartstring.Domain(domainNameArg); const domainId = await this.getZoneId(domain.zoneName);
const cflareRecord = await this.getRecord(domain.fullName, typeArg); const requestUrl = `/zones/${domainId}/purge_cache`;
if (cflareRecord) { const payload = {
const requestRoute: string = `/zones/${cflareRecord.zone_id}/dns_records/${cflareRecord.id}`; purge_everything: true
return await this.request('DELETE', requestRoute); };
} else { const respone = await this.request('DELETE', requestUrl, payload);
throw new Error(`could not remove record for ${domainNameArg} with type ${typeArg}`);
} }
} };
/**
* updates a record
* @param domainNameArg
* @param typeArg
* @param valueArg
*/
public updateRecord(domainNameArg: string, typeArg: string, valueArg) {
// TODO: implement
const done = plugins.smartpromise.defer();
const domain = new plugins.smartstring.Domain(domainNameArg);
return done.promise;
}
/**
* list all records of a specified domain name
* @param domainNameArg - the domain name that you want to get the records from
*/
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'
);
const result: interfaces.ICflareRecord[] = responseArg.result;
return result;
}
/**
* list all zones in the associated authenticated account
* @param domainName
*/
public async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
// TODO: handle pagination
let requestRoute = `/zones?per_page=50`;
// may be optionally filtered by domain name
if (domainName) {
requestRoute = `${requestRoute}&name=${domainName}`;
}
const response: any = await this.request('GET', requestRoute);
const result = response.result;
return result;
}
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
};
const respone = await this.request('DELETE', requestUrl, payload);
}
public request( public request(
methodArg: string, methodArg: string,
@ -223,6 +225,4 @@ export class CloudflareAccount {
private authCheck() { private authCheck() {
return this.authEmail && this.authKey; // check if auth is available return this.authEmail && this.authKey; // check if auth is available
} }
// acme convenience functions
} }

View File

@ -38,7 +38,7 @@ export class CloudflareWorker {
* gets all routes for a worker * gets all routes for a worker
*/ */
public async getRoutes() { public async getRoutes() {
const zones = await this.workerManager.cfAccount.listZones(); const zones = await this.workerManager.cfAccount.convenience.listZones();
for (const zone of zones) { for (const zone of zones) {
const requestRoute = `/zones/${zone.id}/workers/routes`; const requestRoute = `/zones/${zone.id}/workers/routes`;
const response: { const response: {