From f652cc72fe861c8a4830abd41bc5f5390cf4b685 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Thu, 18 Jul 2019 17:12:03 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 12 ++- ts/cloudflare.classes.account.ts | 23 ++++-- ts/cloudflare.classes.worker.ts | 73 ++++++++++++++++--- ts/interfaces/cloudflare.api.record.ts | 15 ++++ ts/interfaces/cloudflare.api.workerroute.ts | 5 ++ ...e.interfaces.ts => cloudflare.api.zone.ts} | 18 ----- ts/interfaces/index.ts | 4 +- 7 files changed, 111 insertions(+), 39 deletions(-) create mode 100644 ts/interfaces/cloudflare.api.record.ts create mode 100644 ts/interfaces/cloudflare.api.workerroute.ts rename ts/interfaces/{cloudflare.interfaces.ts => cloudflare.api.zone.ts} (71%) diff --git a/test/test.ts b/test/test.ts index d9d86ea..652cdf9 100644 --- a/test/test.ts +++ b/test/test.ts @@ -70,11 +70,19 @@ tap.test('.purge(some.domain) -> should purge everything', async () => { // WORKERS tap.test('should create a worker', async () => { - await testCloudflareAccount.workerManager.createWorker('myawesomescript', `addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })`); + const worker = await testCloudflareAccount.workerManager.createWorker('myawesomescript', `addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })`); + await worker.setRoutes([ + { + zoneName: 'bleu.de', + pattern: 'https://*bleu.de/hello' + } + ]); + console.log(worker); }); tap.test('should get workers', async () => { - await testCloudflareAccount.workerManager.listWorkers(); + const workerArray = await testCloudflareAccount.workerManager.listWorkers(); + console.log(workerArray); }); tap.start(); diff --git a/ts/cloudflare.classes.account.ts b/ts/cloudflare.classes.account.ts index 595b8d1..c58f493 100644 --- a/ts/cloudflare.classes.account.ts +++ b/ts/cloudflare.classes.account.ts @@ -1,5 +1,5 @@ import plugins = require('./cloudflare.plugins'); -import * as interfaces from './interfaces/cloudflare.interfaces'; +import * as interfaces from './interfaces'; // interfaces import { TDnsRecord } from '@tsclass/tsclass'; @@ -24,10 +24,12 @@ export class CloudflareAccount { } public async getAccountIdentifier() { - 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); + 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; } @@ -92,7 +94,7 @@ export class CloudflareAccount { /** * removes a record from Cloudflare * @param domainNameArg - * @param typeArg + * @param typeArg */ public async removeRecord(domainNameArg: string, typeArg: TDnsRecord): Promise { const domain = new plugins.smartstring.Domain(domainNameArg); @@ -161,7 +163,12 @@ export class CloudflareAccount { const respone = await this.request('DELETE', requestUrl, payload); } - public request(methodArg: string, routeArg: string, dataArg: any = {}, requestHeadersArg = {}): Promise { + public request( + methodArg: string, + routeArg: string, + dataArg: any = {}, + requestHeadersArg = {} + ): Promise { const done = plugins.smartpromise.defer(); const options: plugins.smartrequest.ISmartRequestOptions = { method: methodArg, @@ -172,7 +179,7 @@ export class CloudflareAccount { 'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)), ...requestHeadersArg }, - requestBody: dataArg, + requestBody: dataArg }; // console.log(options); diff --git a/ts/cloudflare.classes.worker.ts b/ts/cloudflare.classes.worker.ts index 2edb240..2b11422 100644 --- a/ts/cloudflare.classes.worker.ts +++ b/ts/cloudflare.classes.worker.ts @@ -1,21 +1,32 @@ +import * as plugins from './cloudflare.plugins'; +import * as interfaces from './interfaces'; import { WorkerManager } from './cloudflare.classes.workermanager'; +export interface IWorkerRoute extends interfaces.ICflareWorkerRoute { + zoneName: string; +} + export class Worker { // STATIC public static async fromApiObject(workerManager: WorkerManager, apiObject): Promise { - console.log(apiObject); - return new Worker(workerManager); + const newWorker = new Worker(workerManager); + Object.assign(newWorker, apiObject.result); + await newWorker.getRoutes(); + return newWorker; } - + // INSTANCE private workerManager: WorkerManager; + public script: string; public id: string; public etag: string; - public createdOn: string; - public modifiedOn: string; + // tslint:disable-next-line: variable-name + public created_on: string; + // tslint:disable-next-line: variable-name + public modified_on: string; - public routes: string[] = []; + public routes: IWorkerRoute[] = []; constructor(workerManagerArg: WorkerManager) { this.workerManager = workerManagerArg; } @@ -23,12 +34,54 @@ export class Worker { /** * gets all routes for a worker */ - public async getRoutes(){ + public async getRoutes() { const zones = await this.workerManager.cfAccount.listZones(); - console.log(zones); + for (const zone of zones) { + const requestRoute = `/zones/${zone.id}/workers/routes`; + const response: {result: interfaces.ICflareWorkerRoute[]} = await this.workerManager.cfAccount.request('GET', requestRoute); + for (const route of response.result) { + console.log('hey'); + console.log(route); + console.log(this.id); + if (route.script === this.id) { + this.routes.push({...route, zoneName: zone.name}); + } + } + } } - - public setRoutes(routeArray: string[]) { + public async setRoutes(routeArray: Array<{zoneName: string, pattern: string}>) { + for (const newRoute of routeArray) { + // lets determine wether a route is new, needs an update or already up to date. + let routeStatus: 'new' | 'needsUpdate' | 'alreadyUpToDate' = 'new'; + let routeIdForUpdate: string; + for (const existingRoute of this.routes) { + if (existingRoute.pattern === newRoute.pattern) { + routeStatus = 'needsUpdate'; + routeIdForUpdate = existingRoute.id; + if (existingRoute.script === this.id) { + routeStatus = 'alreadyUpToDate'; + plugins.smartlog.defaultLogger.log('info', `route already exists, no update needed`); + } + } + } + + // lets care about actually setting routes + if (routeStatus === 'new') { + const zoneId = await this.workerManager.cfAccount.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 requestRoute = `/zones/${zoneId}/workers/routes/${routeIdForUpdate}`; + await this.workerManager.cfAccount.request('PUT', requestRoute, { + pattern: newRoute.pattern, + script: this.id + }); + } + } } } diff --git a/ts/interfaces/cloudflare.api.record.ts b/ts/interfaces/cloudflare.api.record.ts new file mode 100644 index 0000000..6152cb4 --- /dev/null +++ b/ts/interfaces/cloudflare.api.record.ts @@ -0,0 +1,15 @@ +export interface ICflareRecord { + id: string; + type: string; + name: string; + content: string; + proxiable: boolean; + proxied: boolean; + ttl: number; + locked: boolean; + zone_id: string; + zone_name: string; + created_on: string; + modified_on: string; + data: any; +} \ No newline at end of file diff --git a/ts/interfaces/cloudflare.api.workerroute.ts b/ts/interfaces/cloudflare.api.workerroute.ts new file mode 100644 index 0000000..490a0cd --- /dev/null +++ b/ts/interfaces/cloudflare.api.workerroute.ts @@ -0,0 +1,5 @@ +export interface ICflareWorkerRoute { + id: string; + pattern: string; + script: string; +} diff --git a/ts/interfaces/cloudflare.interfaces.ts b/ts/interfaces/cloudflare.api.zone.ts similarity index 71% rename from ts/interfaces/cloudflare.interfaces.ts rename to ts/interfaces/cloudflare.api.zone.ts index 79c23b6..052a317 100644 --- a/ts/interfaces/cloudflare.interfaces.ts +++ b/ts/interfaces/cloudflare.api.zone.ts @@ -1,5 +1,3 @@ -import * as plugins from '../cloudflare.plugins'; - export interface ICflareZone { id: string; name: string; @@ -41,19 +39,3 @@ export interface ICflareZone { type: string; checked_on: string; } - -export interface ICflareRecord { - id: string; - type: string; - name: string; - content: string; - proxiable: boolean; - proxied: boolean; - ttl: number; - locked: boolean; - zone_id: string; - zone_name: string; - created_on: string; - modified_on: string; - data: any; -} diff --git a/ts/interfaces/index.ts b/ts/interfaces/index.ts index ca7d620..816ccf0 100644 --- a/ts/interfaces/index.ts +++ b/ts/interfaces/index.ts @@ -1 +1,3 @@ -export * from './cloudflare.interfaces'; +export * from './cloudflare.api.record'; +export * from './cloudflare.api.zone'; +export * from './cloudflare.api.workerroute';