From 2458da6754cc4aab8de935850cd05b35c1c6b317 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 28 Feb 2020 14:24:36 +0000 Subject: [PATCH] fix(core): update --- test/test.ts | 2 +- ts/cloudflare.classes.account.ts | 51 +++++++++++++++++----------- ts/cloudflare.classes.record.ts | 5 +++ ts/cloudflare.classes.worker.ts | 4 +-- ts/cloudflare.classes.zone.ts | 49 ++++++++++++++++++++++++-- ts/cloudflare.classes.zonemanager.ts | 21 +++++++++++- 6 files changed, 106 insertions(+), 26 deletions(-) diff --git a/test/test.ts b/test/test.ts index 0d05ab1..dfa4332 100644 --- a/test/test.ts +++ b/test/test.ts @@ -24,7 +24,7 @@ tap.test( '.getZoneId(domainName) -> should get an Cloudflare Id for a domain string', async tools => { tools.timeout(600000); - await testCloudflareAccount.getZoneId('bleu.de'); + await testCloudflareAccount.convenience.getZoneId('bleu.de'); } ); diff --git a/ts/cloudflare.classes.account.ts b/ts/cloudflare.classes.account.ts index 25a3ff5..6380e76 100644 --- a/ts/cloudflare.classes.account.ts +++ b/ts/cloudflare.classes.account.ts @@ -32,11 +32,12 @@ export class CloudflareAccount { return this.accountIdentifier; } - /** + public convenience = { + /** * gets a zone id of a domain from cloudflare * @param domainName */ - public async getZoneId(domainName: string) { + getZoneId: async(domainName: string) => { const domain = new plugins.smartstring.Domain(domainName); const zoneArray = await this.convenience.listZones(domain.zoneName); const filteredResponse = zoneArray.filter(zoneArg => { @@ -51,9 +52,7 @@ export class CloudflareAccount { ); throw new Error(`the domain ${domainName} does not appear to be in this account!`); } - } - - public convenience = { + }, /** * gets a record * @param domainNameArg @@ -79,7 +78,7 @@ export class CloudflareAccount { contentArg: string ): Promise => { const domain = new plugins.smartstring.Domain(domainNameArg); - const domainIdArg = await this.getZoneId(domain.zoneName); + const domainIdArg = await this.convenience.getZoneId(domain.zoneName); const dataObject = { name: domain.fullName, type: typeArg, @@ -132,7 +131,7 @@ export class CloudflareAccount { */ listRecords: async (domainNameArg: string): Promise => { const domain = new plugins.smartstring.Domain(domainNameArg); - const domainId = await this.getZoneId(domain.zoneName); + const domainId = await this.convenience.getZoneId(domain.zoneName); const responseArg: any = await this.request( 'GET', '/zones/' + domainId + '/dns_records?per_page=100' @@ -162,7 +161,7 @@ export class CloudflareAccount { */ purgeZone: async (domainName: string): Promise => { const domain = new plugins.smartstring.Domain(domainName); - const domainId = await this.getZoneId(domain.zoneName); + const domainId = await this.convenience.getZoneId(domain.zoneName); const requestUrl = `/zones/${domainId}/purge_cache`; const payload = { purge_everything: true @@ -179,13 +178,12 @@ export class CloudflareAccount { } }; - public request( + public async request( methodArg: string, routeArg: string, dataArg: any = {}, requestHeadersArg = {} ): Promise { - const done = plugins.smartpromise.defer(); const options: plugins.smartrequest.ISmartRequestOptions = { method: methodArg, headers: { @@ -200,25 +198,38 @@ export class CloudflareAccount { // console.log(options); let retryCount = 0; // count the amount of retries - - const makeRequest = async () => { - const response: any = await plugins.smartrequest.request( - `https://api.cloudflare.com/client/v4${routeArg}`, + let pageCount = 1; + const makeRequest = async (): Promise => { + const requestUrl = `https://api.cloudflare.com/client/v4${routeArg}`; + const response = await plugins.smartrequest.request( + requestUrl, options ); if (response.statusCode === 200) { - done.resolve(response.body); + if(response.body.result_info) { + const rI = response.body.result_info; + if ((rI.total_count / rI.per_page) > pageCount) { + pageCount++; + return await makeRequest(); + } else { + return response; + } + } else { + return response; + } } else if (response.statusCode === 429) { console.log('rate limited! Waiting for retry!'); - retryRequest(); + return await retryRequest(); } else if (response.statusCode === 400) { - console.log(`bad request for route ${routeArg}! Going to retry!`); + console.log(`bad request for route ${routeArg}!`); console.log(response.body); + throw new Error(`request failed for ${routeArg}`); } else { console.log(response.statusCode); - done.reject(new Error('request failed')); + throw Error('request failed'); } }; + const retryRequest = async ( delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000) ) => { @@ -229,8 +240,8 @@ export class CloudflareAccount { return await makeRequest(); } }; - makeRequest(); - return done.promise; + const response = await makeRequest(); + return response.body; } private authCheck() { diff --git a/ts/cloudflare.classes.record.ts b/ts/cloudflare.classes.record.ts index e69de29..564d6ee 100644 --- a/ts/cloudflare.classes.record.ts +++ b/ts/cloudflare.classes.record.ts @@ -0,0 +1,5 @@ +import * as plugins from './cloudflare.plugins'; + +export class CloudflareRecord { + +} diff --git a/ts/cloudflare.classes.worker.ts b/ts/cloudflare.classes.worker.ts index 97b543c..00f742b 100644 --- a/ts/cloudflare.classes.worker.ts +++ b/ts/cloudflare.classes.worker.ts @@ -73,14 +73,14 @@ export class CloudflareWorker { // lets care about actually setting routes if (routeStatus === 'new') { - const zoneId = await this.workerManager.cfAccount.getZoneId(newRoute.zoneName); + const zoneId = await this.workerManager.cfAccount.convenience.getZoneId(newRoute.zoneName); const requestRoute = `/zones/${zoneId}/workers/routes`; await this.workerManager.cfAccount.request('POST', requestRoute, { pattern: newRoute.pattern, script: this.id }); } else if (routeStatus === 'needsUpdate') { - const zoneId = await this.workerManager.cfAccount.getZoneId(newRoute.zoneName); + const zoneId = await this.workerManager.cfAccount.convenience.getZoneId(newRoute.zoneName); const requestRoute = `/zones/${zoneId}/workers/routes/${routeIdForUpdate}`; await this.workerManager.cfAccount.request('PUT', requestRoute, { pattern: newRoute.pattern, diff --git a/ts/cloudflare.classes.zone.ts b/ts/cloudflare.classes.zone.ts index 172cc71..c53ee66 100644 --- a/ts/cloudflare.classes.zone.ts +++ b/ts/cloudflare.classes.zone.ts @@ -1,6 +1,51 @@ import * as plugins from './cloudflare.plugins'; import * as interfaces from './interfaces'; -export class CloudflareZone { - public static async createFromApiObject() {} +export class CloudflareZone implements interfaces.ICflareZone { + public static createFromApiObject(apiObject: interfaces.ICflareZone) { + const cloudflareZone = new CloudflareZone(); + Object.assign(cloudflareZone, apiObject); + return cloudflareZone; + } + + id: string; + name: string; + development_mode: number; + original_name_servers: string[]; + original_registrar: string; + original_dnshost: string; + created_on: string; + modified_on: string; + name_servers: string[]; + owner: { + id: string; + email: string; + owner_type: string; + }; + permissions: string[]; + plan: { + id: string; + name: string; + price: number; + currency: string; + frequency: string; + legacy_id: string; + is_subscribed: boolean; + can_subscribe: boolean; + }; + plan_pending: { + id: string; + name: string; + price: number; + currency: string; + frequency: string; + legacy_id: string; + is_subscribed: string; + can_subscribe: string; + }; + status: string; + paused: boolean; + type: string; + checked_on: string; + } diff --git a/ts/cloudflare.classes.zonemanager.ts b/ts/cloudflare.classes.zonemanager.ts index 374e822..e76a75a 100644 --- a/ts/cloudflare.classes.zonemanager.ts +++ b/ts/cloudflare.classes.zonemanager.ts @@ -1,5 +1,7 @@ import * as plugins from './cloudflare.plugins'; +import * as interfaces from './interfaces'; import { CloudflareAccount } from './cloudflare.classes.account'; +import { CloudflareZone } from './cloudflare.classes.zone'; export class ZoneManager { public cfAccount: CloudflareAccount; @@ -9,5 +11,22 @@ export class ZoneManager { this.cfAccount = cfAccountArg; } - public getZones() {} + public async getZones(zoneName: string) { + let requestRoute = `/zones?per_page=50`; + // may be optionally filtered by domain name + + if (zoneName) { + requestRoute = `${requestRoute}&name=${zoneName}`; + } + + const response: any = await this.cfAccount.request('GET', requestRoute); + const apiObjects: interfaces.ICflareZone[] = response.result; + + const cloudflareZoneArray = []; + for (const apiObject of apiObjects) { + cloudflareZoneArray.push(CloudflareZone.createFromApiObject(apiObject)); + } + + return cloudflareZoneArray; + } }