Compare commits

..

7 Commits

Author SHA1 Message Date
1f5352d9f5 3.0.3 2019-07-18 17:12:04 +02:00
f652cc72fe fix(core): update 2019-07-18 17:12:03 +02:00
f510408fce 3.0.2 2019-07-18 15:31:25 +02:00
e250e9b1a2 fix(core): update 2019-07-18 15:31:24 +02:00
5a9b7bbeee 3.0.1 2019-07-18 14:44:45 +02:00
1158b4ff99 fix(core): update 2019-07-18 14:44:45 +02:00
8eb777dd45 3.0.0 2019-07-18 14:25:40 +02:00
11 changed files with 121 additions and 41 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/cloudflare", "name": "@mojoio/cloudflare",
"version": "2.0.2", "version": "3.0.3",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/cloudflare", "name": "@mojoio/cloudflare",
"version": "2.0.2", "version": "3.0.3",
"private": false, "private": false,
"description": "easy cloudflare management", "description": "easy cloudflare management",
"main": "dist/index.js", "main": "dist/index.js",

View File

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

View File

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

View File

@ -1,26 +1,87 @@
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;
} }
public setRoutes(routeArray: string[]) { /**
* gets all routes for a worker
*/
public async getRoutes() {
const zones = await this.workerManager.cfAccount.listZones();
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 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

@ -3,7 +3,7 @@ import { CloudflareAccount } from './cloudflare.classes.account';
import { Worker } from './cloudflare.classes.worker'; import { Worker } from './cloudflare.classes.worker';
export class WorkerManager { export class WorkerManager {
private cfAccount: CloudflareAccount; public cfAccount: CloudflareAccount;
constructor(cfAccountArg: CloudflareAccount) { constructor(cfAccountArg: CloudflareAccount) {
this.cfAccount = cfAccountArg; this.cfAccount = cfAccountArg;

View File

@ -2,7 +2,7 @@ import * as plugins from './cloudflare.plugins';
import { CloudflareAccount } from './cloudflare.classes.account'; import { CloudflareAccount } from './cloudflare.classes.account';
export class ZoneManager { export class ZoneManager {
private cfAccount: CloudflareAccount; public cfAccount: CloudflareAccount;
public zoneName: string; public zoneName: string;
constructor(cfAccountArg: CloudflareAccount) { constructor(cfAccountArg: CloudflareAccount) {

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

View File

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