fix(core): update

This commit is contained in:
Philipp Kunz 2019-07-18 14:25:10 +02:00
parent b7cc500f4d
commit 9e9dd8d935
8 changed files with 123 additions and 41 deletions

17
package-lock.json generated
View File

@ -49,15 +49,14 @@
} }
}, },
"@gitzone/tsrun": { "@gitzone/tsrun": {
"version": "1.2.6", "version": "1.2.8",
"resolved": "https://verdaccio.lossless.one/@gitzone%2ftsrun/-/tsrun-1.2.6.tgz", "resolved": "https://verdaccio.lossless.one/@gitzone%2ftsrun/-/tsrun-1.2.8.tgz",
"integrity": "sha512-MTgQ7OiKfMpRQRumJp8aGuLCNNyWGjZSxsF+GfZ7cmg2DUtJRyCL0Ybkai1sPdMWG/zE8RUo50WvBKEChdVyvA==", "integrity": "sha512-G65DNcjegxF6w/O9MYD2mlHh+QVUiLuZvgLD5eamOOuWLhzuiE1tDzv5praJ/woJQzzZinDs+IyRyZ5rWKp/fQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@gitzone/tsbuild": "^2.1.8", "@pushrocks/smartfile": "^7.0.4",
"@pushrocks/smartfile": "^7.0.2", "ts-node": "^8.3.0",
"ts-node": "^8.0.3", "typescript": "^3.5.3"
"typescript": "^3.4.2"
} }
}, },
"@gitzone/tstest": { "@gitzone/tstest": {
@ -568,7 +567,7 @@
}, },
"buffer-from": { "buffer-from": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", "resolved": "https://verdaccio.lossless.one/buffer-from/-/buffer-from-1.1.1.tgz",
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
"dev": true "dev": true
}, },
@ -1527,7 +1526,7 @@
}, },
"source-map": { "source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "resolved": "https://verdaccio.lossless.one/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
"dev": true "dev": true
}, },

View File

@ -33,7 +33,7 @@
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.11", "@gitzone/tsbuild": "^2.1.11",
"@gitzone/tsrun": "^1.2.6", "@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.24", "@gitzone/tstest": "^1.0.24",
"@pushrocks/qenv": "^4.0.0", "@pushrocks/qenv": "^4.0.0",
"@pushrocks/tapbundle": "^3.0.11", "@pushrocks/tapbundle": "^3.0.11",

View File

@ -1,15 +1,23 @@
// tslint:disable-next-line: no-implicit-dependencies
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import cloudflare = require('../ts/index'); // tslint:disable-next-line: no-implicit-dependencies
import { Qenv } from '@pushrocks/qenv'; import { Qenv } from '@pushrocks/qenv';
import cloudflare = require('../ts/index');
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit'); const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
const testCloudflareAccount = new cloudflare.CloudflareAccount(); const randomPrefix = Math.floor(Math.random() * 2000);
testCloudflareAccount.auth({ let testCloudflareAccount: cloudflare.CloudflareAccount;
email: testQenv.getEnvVarOnDemand('CF_EMAIL'),
key: testQenv.getEnvVarOnDemand('CF_KEY') tap.test('should create a valid instance of CloudflareAccount', async () => {
testCloudflareAccount = new cloudflare.CloudflareAccount({
email: testQenv.getEnvVarOnDemand('CF_EMAIL'),
key: testQenv.getEnvVarOnDemand('CF_KEY')
});
}); });
const randomPrefix = Math.floor(Math.random() * 2000);
tap.skip.test('.listZones() -> should display an entire account', async tools => { tap.skip.test('.listZones() -> should display an entire account', async tools => {
tools.timeout(600000); tools.timeout(600000);
@ -60,4 +68,13 @@ tap.test('.purge(some.domain) -> should purge everything', async () => {
await testCloudflareAccount.purgeZone('bleu.de'); await testCloudflareAccount.purgeZone('bleu.de');
}); });
// WORKERS
tap.test('should create a worker', async () => {
await testCloudflareAccount.workerManager.createWorker('myawesomescript', `addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })`);
});
tap.test('should get workers', async () => {
await testCloudflareAccount.workerManager.listWorkers();
});
tap.start(); tap.start();

View File

@ -9,19 +9,28 @@ import { ZoneManager } from './cloudflare.classes.zonemanager';
export class CloudflareAccount { export class CloudflareAccount {
private authEmail: string; private authEmail: string;
private authKey: string; private authKey: string;
private accountIdentifier: string;
public workerManager = new WorkerManager(this); public workerManager = new WorkerManager(this);
public zoneManager = new ZoneManager(this); public zoneManager = new ZoneManager(this);
constructor() { /**
// Nothing here * constructor sets auth information on the CloudflareAccountInstance
} * @param optionsArg
*/
public auth(optionsArg: { email: string; key: string }) { constructor(optionsArg: { email: string; key: string }) {
this.authEmail = optionsArg.email; this.authEmail = optionsArg.email;
this.authKey = optionsArg.key; this.authKey = optionsArg.key;
} }
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);
return this.accountIdentifier;
}
/** /**
* gets a zone id of a domain from cloudflare * gets a zone id of a domain from cloudflare
* @param domainName * @param domainName
@ -80,24 +89,30 @@ export class CloudflareAccount {
return response; return response;
} }
public removeRecord(domainNameArg: string, typeArg: TDnsRecord) { /**
const done = plugins.smartpromise.defer(); * removes a record from Cloudflare
* @param domainNameArg
* @param typeArg
*/
public async removeRecord(domainNameArg: string, typeArg: TDnsRecord): Promise<any> {
const domain = new plugins.smartstring.Domain(domainNameArg); const domain = new plugins.smartstring.Domain(domainNameArg);
this.getRecord(domain.fullName, typeArg).then(responseArg => { const cflareRecord = await this.getRecord(domain.fullName, typeArg);
if (responseArg) { if (cflareRecord) {
const requestRoute: string = const requestRoute: string = `/zones/${cflareRecord.zone_id}/dns_records/${cflareRecord.id}`;
'/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id; return await this.request('DELETE', requestRoute);
this.request('DELETE', requestRoute).then(responseArg => { } else {
done.resolve(responseArg); throw new Error(`could not remove record for ${domainNameArg} with type ${typeArg}`);
}); }
} else {
done.reject();
}
});
return done.promise;
} }
/**
* updates a record
* @param domainNameArg
* @param typeArg
* @param valueArg
*/
public updateRecord(domainNameArg: string, typeArg: string, valueArg) { public updateRecord(domainNameArg: string, typeArg: string, valueArg) {
// TODO: implement
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const domain = new plugins.smartstring.Domain(domainNameArg); const domain = new plugins.smartstring.Domain(domainNameArg);
return done.promise; return done.promise;
@ -146,7 +161,7 @@ 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 = {}) { 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,
@ -154,11 +169,14 @@ export class CloudflareAccount {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-Auth-Email': this.authEmail, 'X-Auth-Email': this.authEmail,
'X-Auth-Key': this.authKey, 'X-Auth-Key': this.authKey,
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)) 'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
...requestHeadersArg
}, },
requestBody: dataArg requestBody: dataArg,
}; };
// console.log(options);
let retryCount = 0; // count the amount of retries let retryCount = 0; // count the amount of retries
const makeRequest = async () => { const makeRequest = async () => {
@ -173,7 +191,7 @@ export class CloudflareAccount {
retryRequest(); retryRequest();
} else if (response.statusCode === 400) { } else if (response.statusCode === 400) {
console.log(`bad request for route ${routeArg}! Going to retry!`); console.log(`bad request for route ${routeArg}! Going to retry!`);
retryRequest(); console.log(response.body);
} else { } else {
console.log(response.statusCode); console.log(response.statusCode);
done.reject(new Error('request failed')); done.reject(new Error('request failed'));

View File

@ -0,0 +1,26 @@
import { WorkerManager } from './cloudflare.classes.workermanager';
export class Worker {
// STATIC
public static async fromApiObject(workerManager: WorkerManager, apiObject): Promise<Worker> {
console.log(apiObject);
return new Worker(workerManager);
}
// INSTANCE
private workerManager: WorkerManager;
public id: string;
public etag: string;
public createdOn: string;
public modifiedOn: string;
public routes: string[] = [];
constructor(workerManagerArg: WorkerManager) {
this.workerManager = workerManagerArg;
}
public setRoutes(routeArray: string[]) {
}
}

View File

@ -1,10 +1,31 @@
import * as plugins from './cloudflare.plugins'; import * as plugins from './cloudflare.plugins';
import { CloudflareAccount } from './cloudflare.classes.account'; import { CloudflareAccount } from './cloudflare.classes.account';
import { Worker } from './cloudflare.classes.worker';
export class WorkerManager { export class WorkerManager {
public cfAccount: CloudflareAccount; private cfAccount: CloudflareAccount;
constructor(cfAccountArg: CloudflareAccount) { constructor(cfAccountArg: CloudflareAccount) {
this.cfAccount = cfAccountArg; this.cfAccount = cfAccountArg;
} }
public async createWorker(workerName: string, workerScript: string): Promise<Worker> {
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
const route = `/accounts/${accountIdentifier}/workers/scripts/${workerName}`;
const responseBody = await this.cfAccount.request('PUT', route, workerScript, {
'Content-Type': 'application/javascript',
'Content-Length': Buffer.byteLength(workerScript)
});
return Worker.fromApiObject(this, responseBody);
}
/**
* lists workers
*/
public async listWorkers() {
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
const route = `/accounts/${accountIdentifier}/workers/scripts`;
const response = await this.cfAccount.request('GET', route);
console.log(response);
}
} }

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 {
public cfAccount: CloudflareAccount; private cfAccount: CloudflareAccount;
public zoneName: string; public zoneName: string;
constructor(cfAccountArg: CloudflareAccount) { constructor(cfAccountArg: CloudflareAccount) {

View File

@ -3,6 +3,7 @@
"rules": { "rules": {
"semicolon": [true, "always"], "semicolon": [true, "always"],
"no-console": false, "no-console": false,
"no-return-await": false,
"ordered-imports": false, "ordered-imports": false,
"object-literal-sort-keys": false, "object-literal-sort-keys": false,
"member-ordering": { "member-ordering": {