fix(core): update
This commit is contained in:
parent
f510408fce
commit
f652cc72fe
12
test/test.ts
12
test/test.ts
@ -70,11 +70,19 @@ tap.test('.purge(some.domain) -> should purge everything', async () => {
|
|||||||
|
|
||||||
// WORKERS
|
// WORKERS
|
||||||
tap.test('should create a worker', async () => {
|
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 () => {
|
tap.test('should get workers', async () => {
|
||||||
await testCloudflareAccount.workerManager.listWorkers();
|
const workerArray = await testCloudflareAccount.workerManager.listWorkers();
|
||||||
|
console.log(workerArray);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import plugins = require('./cloudflare.plugins');
|
import plugins = require('./cloudflare.plugins');
|
||||||
import * as interfaces from './interfaces/cloudflare.interfaces';
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
// interfaces
|
// interfaces
|
||||||
import { TDnsRecord } from '@tsclass/tsclass';
|
import { TDnsRecord } from '@tsclass/tsclass';
|
||||||
@ -24,10 +24,12 @@ export class CloudflareAccount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getAccountIdentifier() {
|
public async getAccountIdentifier() {
|
||||||
const route = `/accounts?page=1&per_page=20&direction=desc`;
|
if (!this.accountIdentifier) {
|
||||||
const response: any = await this.request('GET', route);
|
const route = `/accounts?page=1&per_page=20&direction=desc`;
|
||||||
this.accountIdentifier = response.result[0].id;
|
const response: any = await this.request('GET', route);
|
||||||
console.log('Account identifier is: ' + this.accountIdentifier);
|
this.accountIdentifier = response.result[0].id;
|
||||||
|
// console.log('Account identifier is: ' + this.accountIdentifier);
|
||||||
|
}
|
||||||
return this.accountIdentifier;
|
return this.accountIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,7 +163,12 @@ export class CloudflareAccount {
|
|||||||
const respone = await this.request('DELETE', requestUrl, payload);
|
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 done = plugins.smartpromise.defer();
|
||||||
const options: plugins.smartrequest.ISmartRequestOptions = {
|
const options: plugins.smartrequest.ISmartRequestOptions = {
|
||||||
method: methodArg,
|
method: methodArg,
|
||||||
@ -172,7 +179,7 @@ export class CloudflareAccount {
|
|||||||
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
||||||
...requestHeadersArg
|
...requestHeadersArg
|
||||||
},
|
},
|
||||||
requestBody: dataArg,
|
requestBody: dataArg
|
||||||
};
|
};
|
||||||
|
|
||||||
// console.log(options);
|
// console.log(options);
|
||||||
|
@ -1,21 +1,32 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
import { WorkerManager } from './cloudflare.classes.workermanager';
|
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||||
|
|
||||||
|
export interface IWorkerRoute extends interfaces.ICflareWorkerRoute {
|
||||||
|
zoneName: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class Worker {
|
export class Worker {
|
||||||
// STATIC
|
// STATIC
|
||||||
public static async fromApiObject(workerManager: WorkerManager, apiObject): Promise<Worker> {
|
public static async fromApiObject(workerManager: WorkerManager, apiObject): Promise<Worker> {
|
||||||
console.log(apiObject);
|
const newWorker = new Worker(workerManager);
|
||||||
return new Worker(workerManager);
|
Object.assign(newWorker, apiObject.result);
|
||||||
|
await newWorker.getRoutes();
|
||||||
|
return newWorker;
|
||||||
}
|
}
|
||||||
|
|
||||||
// INSTANCE
|
// INSTANCE
|
||||||
private workerManager: WorkerManager;
|
private workerManager: WorkerManager;
|
||||||
|
|
||||||
|
public script: string;
|
||||||
public id: string;
|
public id: string;
|
||||||
public etag: string;
|
public etag: string;
|
||||||
public createdOn: string;
|
// tslint:disable-next-line: variable-name
|
||||||
public modifiedOn: string;
|
public created_on: string;
|
||||||
|
// tslint:disable-next-line: variable-name
|
||||||
|
public modified_on: string;
|
||||||
|
|
||||||
public routes: string[] = [];
|
public routes: IWorkerRoute[] = [];
|
||||||
constructor(workerManagerArg: WorkerManager) {
|
constructor(workerManagerArg: WorkerManager) {
|
||||||
this.workerManager = workerManagerArg;
|
this.workerManager = workerManagerArg;
|
||||||
}
|
}
|
||||||
@ -23,12 +34,54 @@ export class Worker {
|
|||||||
/**
|
/**
|
||||||
* 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.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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
ts/interfaces/cloudflare.api.record.ts
Normal file
15
ts/interfaces/cloudflare.api.record.ts
Normal 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;
|
||||||
|
}
|
5
ts/interfaces/cloudflare.api.workerroute.ts
Normal file
5
ts/interfaces/cloudflare.api.workerroute.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface ICflareWorkerRoute {
|
||||||
|
id: string;
|
||||||
|
pattern: string;
|
||||||
|
script: string;
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
import * as plugins from '../cloudflare.plugins';
|
|
||||||
|
|
||||||
export interface ICflareZone {
|
export interface ICflareZone {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@ -41,19 +39,3 @@ export interface ICflareZone {
|
|||||||
type: string;
|
type: string;
|
||||||
checked_on: 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;
|
|
||||||
}
|
|
@ -1 +1,3 @@
|
|||||||
export * from './cloudflare.interfaces';
|
export * from './cloudflare.api.record';
|
||||||
|
export * from './cloudflare.api.zone';
|
||||||
|
export * from './cloudflare.api.workerroute';
|
||||||
|
Loading…
Reference in New Issue
Block a user