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,28 +55,31 @@ export class CloudflareAccount {
} }
} }
public convenience = {
/** /**
* gets a record * gets a record
* @param domainNameArg * @param domainNameArg
* @param typeArg * @param typeArg
*/ */
public async getRecord( getRecord: async (
domainNameArg: string, domainNameArg: string,
typeArg: plugins.tsclass.network.TDnsRecord typeArg: plugins.tsclass.network.TDnsRecord
): Promise<interfaces.ICflareRecord> { ): Promise<interfaces.ICflareRecord> => {
const domain = new plugins.smartstring.Domain(domainNameArg); const domain = new plugins.smartstring.Domain(domainNameArg);
const recordArrayArg = await this.listRecords(domain.zoneName); const recordArrayArg = await this.convenience.listRecords(domain.zoneName);
const filteredResponse = recordArrayArg.filter(recordArg => { const filteredResponse = recordArrayArg.filter(recordArg => {
return recordArg.type === typeArg && recordArg.name === domainNameArg; return recordArg.type === typeArg && recordArg.name === domainNameArg;
}); });
return filteredResponse[0]; return filteredResponse[0];
} },
/**
public async createRecord( * creates a record
*/
createRecord: async (
domainNameArg: string, domainNameArg: string,
typeArg: plugins.tsclass.network.TDnsRecord, typeArg: plugins.tsclass.network.TDnsRecord,
contentArg: string contentArg: string
): Promise<any> { ): Promise<any> => {
const domain = new plugins.smartstring.Domain(domainNameArg); const domain = new plugins.smartstring.Domain(domainNameArg);
const domainIdArg = await this.getZoneId(domain.zoneName); const domainIdArg = await this.getZoneId(domain.zoneName);
const dataObject = { const dataObject = {
@ -88,45 +93,40 @@ export class CloudflareAccount {
dataObject dataObject
); );
return response; return response;
} },
/** /**
* removes a record from Cloudflare * removes a record from Cloudflare
* @param domainNameArg * @param domainNameArg
* @param typeArg * @param typeArg
*/ */
public async removeRecord( removeRecord: async (
domainNameArg: string, domainNameArg: string,
typeArg: plugins.tsclass.network.TDnsRecord typeArg: plugins.tsclass.network.TDnsRecord
): Promise<any> { ): Promise<any> => {
const domain = new plugins.smartstring.Domain(domainNameArg); const domain = new plugins.smartstring.Domain(domainNameArg);
const cflareRecord = await this.getRecord(domain.fullName, typeArg); const cflareRecord = await this.convenience.getRecord(domain.fullName, typeArg);
if (cflareRecord) { if (cflareRecord) {
const requestRoute: string = `/zones/${cflareRecord.zone_id}/dns_records/${cflareRecord.id}`; const requestRoute: string = `/zones/${cflareRecord.zone_id}/dns_records/${cflareRecord.id}`;
return await this.request('DELETE', requestRoute); return await this.request('DELETE', requestRoute);
} else { } else {
throw new Error(`could not remove record for ${domainNameArg} with type ${typeArg}`); throw new Error(`could not remove record for ${domainNameArg} with type ${typeArg}`);
} }
} },
/** /**
* updates a record * updates a record
* @param domainNameArg * @param domainNameArg
* @param typeArg * @param typeArg
* @param valueArg * @param valueArg
*/ */
public updateRecord(domainNameArg: string, typeArg: string, valueArg) { updateRecord: async (domainNameArg: string, typeArg: string, valueArg) => {
// TODO: implement // TODO: implement
const done = plugins.smartpromise.defer();
const domain = new plugins.smartstring.Domain(domainNameArg); const domain = new plugins.smartstring.Domain(domainNameArg);
return done.promise; },
}
/** /**
* list all records of a specified domain name * list all records of a specified domain name
* @param domainNameArg - the domain name that you want to get the records from * @param domainNameArg - the domain name that you want to get the records from
*/ */
public async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> { listRecords: async (domainNameArg: string): Promise<interfaces.ICflareRecord[]> => {
const domain = new plugins.smartstring.Domain(domainNameArg); const domain = new plugins.smartstring.Domain(domainNameArg);
const domainId = await this.getZoneId(domain.zoneName); const domainId = await this.getZoneId(domain.zoneName);
const responseArg: any = await this.request( const responseArg: any = await this.request(
@ -135,13 +135,12 @@ export class CloudflareAccount {
); );
const result: interfaces.ICflareRecord[] = responseArg.result; const result: interfaces.ICflareRecord[] = responseArg.result;
return result; return result;
} },
/** /**
* list all zones in the associated authenticated account * list all zones in the associated authenticated account
* @param domainName * @param domainName
*/ */
public async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> { listZones: async (domainName?: string): Promise<interfaces.ICflareZone[]> => {
// TODO: handle pagination // TODO: handle pagination
let requestRoute = `/zones?per_page=50`; let requestRoute = `/zones?per_page=50`;
@ -153,9 +152,11 @@ export class CloudflareAccount {
const response: any = await this.request('GET', requestRoute); const response: any = await this.request('GET', requestRoute);
const result = response.result; const result = response.result;
return result; return result;
} },
/**
public async purgeZone(domainName: string) { * purges a zone
*/
purgeZone: async (domainName: string): Promise<void> => {
const domain = new plugins.smartstring.Domain(domainName); const domain = new plugins.smartstring.Domain(domainName);
const domainId = await this.getZoneId(domain.zoneName); const domainId = await this.getZoneId(domain.zoneName);
const requestUrl = `/zones/${domainId}/purge_cache`; const requestUrl = `/zones/${domainId}/purge_cache`;
@ -164,6 +165,7 @@ export class CloudflareAccount {
}; };
const respone = await this.request('DELETE', requestUrl, payload); 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: {