Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
f510408fce | |||
e250e9b1a2 | |||
5a9b7bbeee | |||
1158b4ff99 | |||
8eb777dd45 | |||
acf5c242d3 | |||
9e9dd8d935 | |||
b7cc500f4d | |||
8a4126a49c |
22
.gitignore
vendored
22
.gitignore
vendored
@ -1,6 +1,22 @@
|
||||
.nogit/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
docs/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
.nogit/
|
||||
nogit/
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
|
||||
# custom
|
@ -1,5 +1,5 @@
|
||||
# gitzone standard
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
@ -26,6 +26,7 @@ mirror:
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
@ -36,21 +37,11 @@ snyk:
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
@ -58,13 +49,14 @@ testLTS:
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
@ -73,7 +65,7 @@ testSTABLE:
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci node install lts
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
@ -86,19 +78,11 @@ release:
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--volume "$PWD":/code
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||
artifacts:
|
||||
paths: [codeclimate.json]
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
@ -117,9 +101,10 @@ pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
@ -130,13 +115,3 @@ pages:
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
allow_failure: true
|
||||
|
36
README.md
36
README.md
@ -1,25 +1,20 @@
|
||||
# cflare
|
||||
|
||||
# @mojoio/cloudflare
|
||||
easy cloudflare management
|
||||
|
||||
## Availabililty
|
||||
|
||||
[](https://www.npmjs.com/package/cflare)
|
||||
[](https://GitLab.com/mojoio/cflare)
|
||||
[](https://github.com/mojoio/cflare)
|
||||
[](https://mojoio.gitlab.io/cflare/)
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/cloudflare)
|
||||
* [gitlab.com (source)](https://gitlab.com/mojoio/cloudflare)
|
||||
* [github.com (source mirror)](https://github.com/mojoio/cloudflare)
|
||||
* [docs (typedoc)](https://mojoio.gitlab.io/cloudflare/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://GitLab.com/mojoio/cflare/commits/master)
|
||||
[](https://GitLab.com/mojoio/cflare/commits/master)
|
||||
[](https://www.npmjs.com/package/cflare)
|
||||
[](https://david-dm.org/mojoio/cflare)
|
||||
[](https://www.bithound.io/github/mojoio/cflare/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/mojoio/cflare)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
||||
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
||||
[](https://www.npmjs.com/package/@mojoio/cloudflare)
|
||||
[](https://snyk.io/test/npm/@mojoio/cloudflare)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
@ -47,8 +42,9 @@ let myAsyncCflareManagement = async () => {
|
||||
}
|
||||
```
|
||||
|
||||
For further information read the linked docs at the top of this README.
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://mojo.io)
|
||||
[](https://maintainedby.lossless.com)
|
||||
|
@ -2,5 +2,15 @@
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "mojoio",
|
||||
"gitrepo": "cloudflare",
|
||||
"shortDescription": "easy cloudflare management",
|
||||
"npmPackagename": "@mojoio/cloudflare",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
1385
package-lock.json
generated
1385
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
41
package.json
41
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mojoio/cloudflare",
|
||||
"version": "2.0.0",
|
||||
"version": "3.0.2",
|
||||
"private": false,
|
||||
"description": "easy cloudflare management",
|
||||
"main": "dist/index.js",
|
||||
@ -24,19 +24,32 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/cflare#readme",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.2",
|
||||
"@pushrocks/smartlog": "^2.0.1",
|
||||
"@pushrocks/smartpromise": "^2.0.5",
|
||||
"@pushrocks/smartrequest": "^1.1.14",
|
||||
"@pushrocks/smartstring": "^3.0.4",
|
||||
"@tsclass/tsclass": "^1.0.48"
|
||||
"@pushrocks/smartdelay": "^2.0.3",
|
||||
"@pushrocks/smartlog": "^2.0.19",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrequest": "^1.1.16",
|
||||
"@pushrocks/smartstring": "^3.0.10",
|
||||
"@tsclass/tsclass": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.12",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/qenv": "^2.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.5",
|
||||
"@types/node": "^10.5.8"
|
||||
}
|
||||
"@gitzone/tsbuild": "^2.1.11",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.24",
|
||||
"@pushrocks/qenv": "^4.0.0",
|
||||
"@pushrocks/tapbundle": "^3.0.11",
|
||||
"@types/node": "^12.6.6",
|
||||
"tslint": "^5.18.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"dist_ts_web/*",
|
||||
"assets/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
]
|
||||
}
|
||||
|
35
test/test.ts
35
test/test.ts
@ -1,19 +1,27 @@
|
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import cloudflare = require('../ts/index');
|
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
||||
|
||||
let testCloudflareAccount = new cloudflare.CloudflareAccount();
|
||||
testCloudflareAccount.auth({
|
||||
email: process.env.CF_EMAIL,
|
||||
key: process.env.CF_KEY
|
||||
import cloudflare = require('../ts/index');
|
||||
|
||||
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
||||
|
||||
const randomPrefix = Math.floor(Math.random() * 2000);
|
||||
let testCloudflareAccount: cloudflare.CloudflareAccount;
|
||||
|
||||
tap.test('should create a valid instance of CloudflareAccount', async () => {
|
||||
testCloudflareAccount = new cloudflare.CloudflareAccount({
|
||||
email: testQenv.getEnvVarOnDemand('CF_EMAIL'),
|
||||
key: testQenv.getEnvVarOnDemand('CF_KEY')
|
||||
});
|
||||
});
|
||||
|
||||
let randomPrefix = Math.floor(Math.random() * 2000);
|
||||
|
||||
|
||||
tap.skip.test('.listZones() -> should display an entire account', async tools => {
|
||||
tools.timeout(600000);
|
||||
let result = await testCloudflareAccount.listZones();
|
||||
const result = await testCloudflareAccount.listZones();
|
||||
console.log(result);
|
||||
});
|
||||
|
||||
@ -42,7 +50,7 @@ tap.test('should create a valid record for a subdomain', async tools => {
|
||||
|
||||
tap.test('should get a record from Cloudflare', async tools => {
|
||||
tools.timeout(600000);
|
||||
await testCloudflareAccount.getRecord('bleu.de', 'A').then(function(responseArg) {
|
||||
await testCloudflareAccount.getRecord(`${randomPrefix}subdomain.bleu.de`, 'A').then(responseArg => {
|
||||
console.log(responseArg);
|
||||
});
|
||||
});
|
||||
@ -60,4 +68,13 @@ tap.test('.purge(some.domain) -> should purge everything', async () => {
|
||||
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();
|
||||
|
@ -1,35 +1,53 @@
|
||||
import plugins = require('./cloudflare.plugins');
|
||||
import * as interfaces from './cloudflare.interfaces';
|
||||
import * as interfaces from './interfaces/cloudflare.interfaces';
|
||||
|
||||
// interfaces
|
||||
import { TDnsRecord } from '@tsclass/tsclass';
|
||||
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||
import { ZoneManager } from './cloudflare.classes.zonemanager';
|
||||
|
||||
export class CloudflareAccount {
|
||||
private authEmail: string;
|
||||
private authKey: string;
|
||||
constructor() {
|
||||
// Nothing here
|
||||
}
|
||||
private accountIdentifier: string;
|
||||
|
||||
auth(optionsArg: { email: string; key: string }) {
|
||||
public workerManager = new WorkerManager(this);
|
||||
public zoneManager = new ZoneManager(this);
|
||||
|
||||
/**
|
||||
* constructor sets auth information on the CloudflareAccountInstance
|
||||
* @param optionsArg
|
||||
*/
|
||||
constructor(optionsArg: { email: string; key: string }) {
|
||||
this.authEmail = optionsArg.email;
|
||||
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
|
||||
* @param domainName
|
||||
*/
|
||||
async getZoneId(domainName: string) {
|
||||
let domain = new plugins.smartstring.Domain(domainName);
|
||||
let zoneArray = await this.listZones(domain.zoneName);
|
||||
let filteredResponse = zoneArray.filter(zoneArg => {
|
||||
public async getZoneId(domainName: string) {
|
||||
const domain = new plugins.smartstring.Domain(domainName);
|
||||
const zoneArray = await this.listZones(domain.zoneName);
|
||||
const filteredResponse = zoneArray.filter(zoneArg => {
|
||||
return zoneArg.name === domainName;
|
||||
});
|
||||
if (filteredResponse.length >= 1) {
|
||||
return filteredResponse[0].id;
|
||||
} else {
|
||||
plugins.smartlog.defaultLogger.error(`the domain ${domainName} does not appear to be in this account!`);
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
'error',
|
||||
`the domain ${domainName} does not appear to be in this account!`
|
||||
);
|
||||
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
|
||||
}
|
||||
}
|
||||
@ -37,59 +55,66 @@ export class CloudflareAccount {
|
||||
/**
|
||||
* gets a record
|
||||
* @param domainNameArg
|
||||
* @param typeArg
|
||||
* @param typeArg
|
||||
*/
|
||||
async getRecord(domainNameArg: string, typeArg: TDnsRecord): Promise<interfaces.ICflareRecord> {
|
||||
let done = plugins.smartpromise.defer<interfaces.ICflareRecord>();
|
||||
let result: interfaces.ICflareRecord;
|
||||
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
this.listRecords(domain.zoneName).then(recordArrayArg => {
|
||||
let filteredResponse = recordArrayArg.filter(recordArg => {
|
||||
return recordArg.type === typeArg && recordArg.name === domainNameArg;
|
||||
});
|
||||
done.resolve(filteredResponse[0]);
|
||||
public async getRecord(
|
||||
domainNameArg: string,
|
||||
typeArg: TDnsRecord
|
||||
): Promise<interfaces.ICflareRecord> {
|
||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
const recordArrayArg = await this.listRecords(domain.zoneName);
|
||||
const filteredResponse = recordArrayArg.filter(recordArg => {
|
||||
return recordArg.type === typeArg && recordArg.name === domainNameArg;
|
||||
});
|
||||
return done.promise;
|
||||
return filteredResponse[0];
|
||||
}
|
||||
|
||||
async createRecord(domainNameArg: string, typeArg: TDnsRecord, contentArg: string) {
|
||||
let done = plugins.smartpromise.defer();
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
let domainIdArg = await this.getZoneId(domain.zoneName);
|
||||
let dataObject = {
|
||||
public async createRecord(
|
||||
domainNameArg: string,
|
||||
typeArg: TDnsRecord,
|
||||
contentArg: string
|
||||
): Promise<any> {
|
||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
const domainIdArg = await this.getZoneId(domain.zoneName);
|
||||
const dataObject = {
|
||||
name: domain.fullName,
|
||||
type: typeArg,
|
||||
content: contentArg
|
||||
};
|
||||
this.request('POST', '/zones/' + domainIdArg + '/dns_records', dataObject).then(function(
|
||||
responseArg
|
||||
) {
|
||||
done.resolve(responseArg);
|
||||
});
|
||||
return done.promise;
|
||||
const response = await this.request(
|
||||
'POST',
|
||||
'/zones/' + domainIdArg + '/dns_records',
|
||||
dataObject
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
removeRecord(domainNameArg: string, typeArg: TDnsRecord) {
|
||||
let done = plugins.smartpromise.defer();
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
this.getRecord(domain.fullName, typeArg).then(responseArg => {
|
||||
if (responseArg) {
|
||||
let requestRoute: string =
|
||||
'/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id;
|
||||
this.request('DELETE', requestRoute).then(responseArg => {
|
||||
done.resolve(responseArg);
|
||||
});
|
||||
} else {
|
||||
done.reject();
|
||||
}
|
||||
});
|
||||
return done.promise;
|
||||
/**
|
||||
* 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 cflareRecord = await this.getRecord(domain.fullName, typeArg);
|
||||
if (cflareRecord) {
|
||||
const requestRoute: string = `/zones/${cflareRecord.zone_id}/dns_records/${cflareRecord.id}`;
|
||||
return await this.request('DELETE', requestRoute);
|
||||
} else {
|
||||
throw new Error(`could not remove record for ${domainNameArg} with type ${typeArg}`);
|
||||
}
|
||||
}
|
||||
|
||||
updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
||||
let done = plugins.smartpromise.defer();
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
/**
|
||||
* updates a record
|
||||
* @param domainNameArg
|
||||
* @param typeArg
|
||||
* @param valueArg
|
||||
*/
|
||||
public updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
||||
// TODO: implement
|
||||
const done = plugins.smartpromise.defer();
|
||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
@ -97,14 +122,14 @@ export class CloudflareAccount {
|
||||
* list all records of a specified domain name
|
||||
* @param domainNameArg - the domain name that you want to get the records from
|
||||
*/
|
||||
async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
let domainId = await this.getZoneId(domain.zoneName);
|
||||
let responseArg: any = await this.request(
|
||||
public async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
const domainId = await this.getZoneId(domain.zoneName);
|
||||
const responseArg: any = await this.request(
|
||||
'GET',
|
||||
'/zones/' + domainId + '/dns_records?per_page=100'
|
||||
);
|
||||
let result: interfaces.ICflareRecord[] = responseArg.result;
|
||||
const result: interfaces.ICflareRecord[] = responseArg.result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -112,7 +137,7 @@ export class CloudflareAccount {
|
||||
* list all zones in the associated authenticated account
|
||||
* @param domainName
|
||||
*/
|
||||
async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
|
||||
public async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
|
||||
// TODO: handle pagination
|
||||
let requestRoute = `/zones?per_page=50`;
|
||||
|
||||
@ -121,38 +146,41 @@ export class CloudflareAccount {
|
||||
requestRoute = `${requestRoute}&name=${domainName}`;
|
||||
}
|
||||
|
||||
let response: any = await this.request('GET', requestRoute);
|
||||
let result = response.result;
|
||||
const response: any = await this.request('GET', requestRoute);
|
||||
const result = response.result;
|
||||
return result;
|
||||
}
|
||||
|
||||
async purgeZone(domainName: string) {
|
||||
let domain = new plugins.smartstring.Domain(domainName);
|
||||
let domainId = await this.getZoneId(domain.zoneName);
|
||||
let requestUrl = `/zones/${domainId}/purge_cache`;
|
||||
let payload = {
|
||||
public async purgeZone(domainName: string) {
|
||||
const domain = new plugins.smartstring.Domain(domainName);
|
||||
const domainId = await this.getZoneId(domain.zoneName);
|
||||
const requestUrl = `/zones/${domainId}/purge_cache`;
|
||||
const payload = {
|
||||
purge_everything: true
|
||||
};
|
||||
let respone = await this.request('DELETE', requestUrl, payload);
|
||||
const respone = await this.request('DELETE', requestUrl, payload);
|
||||
}
|
||||
|
||||
request(methodArg: string, routeArg: string, dataArg = {}) {
|
||||
let done = plugins.smartpromise.defer();
|
||||
let options: plugins.smartrequest.ISmartRequestOptions = {
|
||||
public request(methodArg: string, routeArg: string, dataArg: any = {}, requestHeadersArg = {}): Promise<any> {
|
||||
const done = plugins.smartpromise.defer();
|
||||
const options: plugins.smartrequest.ISmartRequestOptions = {
|
||||
method: methodArg,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Auth-Email': this.authEmail,
|
||||
'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 makeRequest = async () => {
|
||||
let response: any = await plugins.smartrequest.request(
|
||||
const makeRequest = async () => {
|
||||
const response: any = await plugins.smartrequest.request(
|
||||
`https://api.cloudflare.com/client/v4${routeArg}`,
|
||||
options
|
||||
);
|
||||
@ -163,13 +191,15 @@ export class CloudflareAccount {
|
||||
retryRequest();
|
||||
} else if (response.statusCode === 400) {
|
||||
console.log(`bad request for route ${routeArg}! Going to retry!`);
|
||||
retryRequest();
|
||||
console.log(response.body);
|
||||
} else {
|
||||
console.log(response.statusCode);
|
||||
done.reject(new Error('request failed'));
|
||||
}
|
||||
};
|
||||
let retryRequest = async (delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)) => {
|
||||
const retryRequest = async (
|
||||
delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)
|
||||
) => {
|
||||
console.log(`retry started and waiting for ${delayTimeArg} ms`);
|
||||
await plugins.smartdelay.delayFor(delayTimeArg);
|
||||
if (retryCount < 10) {
|
||||
|
34
ts/cloudflare.classes.worker.ts
Normal file
34
ts/cloudflare.classes.worker.ts
Normal file
@ -0,0 +1,34 @@
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all routes for a worker
|
||||
*/
|
||||
public async getRoutes(){
|
||||
const zones = await this.workerManager.cfAccount.listZones();
|
||||
console.log(zones);
|
||||
}
|
||||
|
||||
public setRoutes(routeArray: string[]) {
|
||||
|
||||
}
|
||||
}
|
31
ts/cloudflare.classes.workermanager.ts
Normal file
31
ts/cloudflare.classes.workermanager.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import * as plugins from './cloudflare.plugins';
|
||||
import { CloudflareAccount } from './cloudflare.classes.account';
|
||||
import { Worker } from './cloudflare.classes.worker';
|
||||
|
||||
export class WorkerManager {
|
||||
public cfAccount: CloudflareAccount;
|
||||
|
||||
constructor(cfAccountArg: CloudflareAccount) {
|
||||
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);
|
||||
}
|
||||
}
|
11
ts/cloudflare.classes.zonemanager.ts
Normal file
11
ts/cloudflare.classes.zonemanager.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import * as plugins from './cloudflare.plugins';
|
||||
import { CloudflareAccount } from './cloudflare.classes.account';
|
||||
|
||||
export class ZoneManager {
|
||||
public cfAccount: CloudflareAccount;
|
||||
public zoneName: string;
|
||||
|
||||
constructor(cfAccountArg: CloudflareAccount) {
|
||||
this.cfAccount = cfAccountArg;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './cloudflare.plugins';
|
||||
import * as plugins from '../cloudflare.plugins';
|
||||
|
||||
export interface ICflareZone {
|
||||
id: string;
|
1
ts/interfaces/index.ts
Normal file
1
ts/interfaces/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './cloudflare.interfaces';
|
17
tslint.json
17
tslint.json
@ -1,3 +1,18 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"no-return-await": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
|
Reference in New Issue
Block a user