fix(core): update

This commit is contained in:
Philipp Kunz 2019-07-18 17:12:03 +02:00
parent f510408fce
commit f652cc72fe
7 changed files with 111 additions and 39 deletions

View File

@ -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();

View File

@ -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<any> {
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<any> {
public request(
methodArg: string,
routeArg: string,
dataArg: any = {},
requestHeadersArg = {}
): Promise<any> {
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);

View File

@ -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<Worker> {
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
});
}
}
}
}

View File

@ -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;
}

View File

@ -0,0 +1,5 @@
export interface ICflareWorkerRoute {
id: string;
pattern: string;
script: string;
}

View File

@ -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;
}

View File

@ -1 +1,3 @@
export * from './cloudflare.interfaces';
export * from './cloudflare.api.record';
export * from './cloudflare.api.zone';
export * from './cloudflare.api.workerroute';