diff --git a/test/test.ts b/test/test.ts index 25c563a..2c286ba 100644 --- a/test/test.ts +++ b/test/test.ts @@ -19,7 +19,7 @@ tap.test('should create a valid instance of CloudflareAccount', async () => { tap.test('.listZones() -> should display an entire account', async tools => { tools.timeout(600000); - const result = await testCloudflareAccount.listZones(); + const result = await testCloudflareAccount.convenience.listZones(); console.log(result); }); @@ -35,7 +35,7 @@ tap.test( '.listRecords(domainName) -> should list all records for a specific Domain Name', async tools => { tools.timeout(600000); - await testCloudflareAccount.listRecords('bleu.de').then(async responseArg => { + await testCloudflareAccount.convenience.listRecords('bleu.de').then(async responseArg => { console.log(responseArg); }); } @@ -43,12 +43,13 @@ tap.test( tap.test('should create a valid record for a subdomain', async tools => { 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 => { tools.timeout(600000); await testCloudflareAccount + .convenience .getRecord(`${randomPrefix}subdomain.bleu.de`, 'A') .then(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 => { tools.timeout(600000); await testCloudflareAccount + .convenience .removeRecord(`${randomPrefix}subdomain.bleu.de`, 'A') .then(async 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 () => { - await testCloudflareAccount.purgeZone('bleu.de'); + await testCloudflareAccount.convenience.purgeZone('bleu.de'); }); // WORKERS diff --git a/ts/cloudflare.classes.account.ts b/ts/cloudflare.classes.account.ts index c3c95ab..506caf4 100644 --- a/ts/cloudflare.classes.account.ts +++ b/ts/cloudflare.classes.account.ts @@ -22,12 +22,14 @@ export class CloudflareAccount { this.authKey = optionsArg.key; } + /** + * gets you the account identifier + */ public async getAccountIdentifier() { if (!this.accountIdentifier) { const route = `/accounts?page=1&per_page=20&direction=desc`; const response: any = await this.request('GET', route); this.accountIdentifier = response.result[0].id; - // console.log('Account identifier is: ' + this.accountIdentifier); } return this.accountIdentifier; } @@ -38,7 +40,7 @@ export class CloudflareAccount { */ public async getZoneId(domainName: string) { 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 => { return zoneArg.name === domainName; }); @@ -53,117 +55,117 @@ export class CloudflareAccount { } } - /** - * gets a record - * @param domainNameArg - * @param typeArg - */ - public async getRecord( - domainNameArg: string, - typeArg: plugins.tsclass.network.TDnsRecord - ): Promise { - 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 filteredResponse[0]; - } + public convenience = { + /** + * gets a record + * @param domainNameArg + * @param typeArg + */ + getRecord: async ( + domainNameArg: string, + typeArg: plugins.tsclass.network.TDnsRecord + ): Promise => { + const domain = new plugins.smartstring.Domain(domainNameArg); + const recordArrayArg = await this.convenience.listRecords(domain.zoneName); + const filteredResponse = recordArrayArg.filter(recordArg => { + return recordArg.type === typeArg && recordArg.name === domainNameArg; + }); + return filteredResponse[0]; + }, + /** + * creates a record + */ + createRecord: async ( + domainNameArg: string, + typeArg: plugins.tsclass.network.TDnsRecord, + contentArg: string + ): Promise => { + 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 => { + 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 => { + 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 => { + // TODO: handle pagination + let requestRoute = `/zones?per_page=50`; - public async createRecord( - domainNameArg: string, - typeArg: plugins.tsclass.network.TDnsRecord, - contentArg: string - ): Promise { - 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; - } + // may be optionally filtered by domain name + if (domainName) { + requestRoute = `${requestRoute}&name=${domainName}`; + } - /** - * removes a record from Cloudflare - * @param domainNameArg - * @param typeArg - */ - public async removeRecord( - domainNameArg: string, - typeArg: plugins.tsclass.network.TDnsRecord - ): Promise { - const domain = new plugins.smartstring.Domain(domainNameArg); - const cflareRecord = await this.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}`); + const response: any = await this.request('GET', requestRoute); + const result = response.result; + return result; + }, + /** + * purges a zone + */ + purgeZone: async (domainName: string): Promise => { + 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); } - } - - /** - * 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 { - 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 { - // 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( methodArg: string, @@ -223,6 +225,4 @@ export class CloudflareAccount { private authCheck() { return this.authEmail && this.authKey; // check if auth is available } - - // acme convenience functions } diff --git a/ts/cloudflare.classes.worker.ts b/ts/cloudflare.classes.worker.ts index 659af4a..97b543c 100644 --- a/ts/cloudflare.classes.worker.ts +++ b/ts/cloudflare.classes.worker.ts @@ -38,7 +38,7 @@ export class CloudflareWorker { * gets all routes for a worker */ public async getRoutes() { - const zones = await this.workerManager.cfAccount.listZones(); + const zones = await this.workerManager.cfAccount.convenience.listZones(); for (const zone of zones) { const requestRoute = `/zones/${zone.id}/workers/routes`; const response: {