Compare commits
41 Commits
Author | SHA1 | Date | |
---|---|---|---|
90c9bfc906 | |||
4f9e81f612 | |||
a4e280f9f0 | |||
52bd80aebd | |||
15e3cdae83 | |||
c72147b469 | |||
d98c2f89c5 | |||
2b722816f6 | |||
91d58277dd | |||
2458da6754 | |||
d4379d19d3 | |||
b0fdd520f3 | |||
a746577945 | |||
bc4cae3333 | |||
e0614b5956 | |||
f568949085 | |||
bee256416f | |||
afa2679501 | |||
7838642fd5 | |||
7a992badf4 | |||
c65790e2f9 | |||
7ec0fe78fc | |||
12f4456ebd | |||
3e8cf73877 | |||
0032292714 | |||
ead87ceb63 | |||
04d70a4d12 | |||
8da43a79d3 | |||
3153021190 | |||
82701c19e7 | |||
d3a68b4fef | |||
c4c1367306 | |||
1f5352d9f5 | |||
f652cc72fe | |||
f510408fce | |||
e250e9b1a2 | |||
5a9b7bbeee | |||
1158b4ff99 | |||
8eb777dd45 | |||
acf5c242d3 | |||
9e9dd8d935 |
@ -3,14 +3,14 @@ image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
@ -18,60 +18,65 @@ stages:
|
|||||||
mirror:
|
mirror:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
snyk:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# metadata stage
|
# metadata stage
|
||||||
@ -81,31 +86,35 @@ codequality:
|
|||||||
allow_failure: true
|
allow_failure: true
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g tslint typescript
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- priv
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
|
- npmci node install lts
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
@ -113,5 +122,5 @@ pages:
|
|||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "current file",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"${relativeFile}"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "test.ts",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"test/test.ts"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
},
|
},
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "mojoio",
|
"gitscope": "mojoio",
|
||||||
|
4952
package-lock.json
generated
4952
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
46
package.json
46
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/cloudflare",
|
"name": "@mojoio/cloudflare",
|
||||||
"version": "2.0.1",
|
"version": "5.0.5",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "easy cloudflare management",
|
"description": "easy cloudflare management",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild)"
|
"build": "(tsbuild --web)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -24,30 +24,30 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/cflare#readme",
|
"homepage": "https://gitlab.com/pushrocks/cflare#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.3",
|
"@pushrocks/smartdelay": "^2.0.9",
|
||||||
"@pushrocks/smartlog": "^2.0.19",
|
"@pushrocks/smartlog": "^2.0.28",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"@pushrocks/smartrequest": "^1.1.16",
|
"@pushrocks/smartrequest": "^1.1.47",
|
||||||
"@pushrocks/smartstring": "^3.0.10",
|
"@pushrocks/smartstring": "^3.0.18",
|
||||||
"@tsclass/tsclass": "^2.0.1"
|
"@tsclass/tsclass": "^3.0.17"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.24",
|
||||||
"@gitzone/tsrun": "^1.2.6",
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.33",
|
||||||
"@pushrocks/qenv": "^4.0.0",
|
"@pushrocks/qenv": "^4.0.10",
|
||||||
"@pushrocks/tapbundle": "^3.0.11",
|
"@pushrocks/tapbundle": "^3.2.1",
|
||||||
"@types/node": "^12.6.6",
|
"@types/node": "^14.0.13",
|
||||||
"tslint": "^5.18.0",
|
"tslint": "^6.1.2",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/*",
|
"ts/**/*",
|
||||||
"ts_web/*",
|
"ts_web/**/*",
|
||||||
"dist/*",
|
"dist/**/*",
|
||||||
"dist_web/*",
|
"dist_web/**/*",
|
||||||
"dist_ts_web/*",
|
"dist_ts_web/**/*",
|
||||||
"assets/*",
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
@ -8,7 +8,7 @@ easy cloudflare management
|
|||||||
* [docs (typedoc)](https://mojoio.gitlab.io/cloudflare/)
|
* [docs (typedoc)](https://mojoio.gitlab.io/cloudflare/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
||||||
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
||||||
[](https://www.npmjs.com/package/@mojoio/cloudflare)
|
[](https://www.npmjs.com/package/@mojoio/cloudflare)
|
||||||
[](https://snyk.io/test/npm/@mojoio/cloudflare)
|
[](https://snyk.io/test/npm/@mojoio/cloudflare)
|
||||||
@ -21,30 +21,25 @@ easy cloudflare management
|
|||||||
Use TypeScript for best in class instellisense.
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import * as cflare from 'cflare'
|
import * as cflare from '@mojoio/cloudflare';
|
||||||
|
|
||||||
let myCflareAccount = new cflare.CflareAccount()
|
const myCflareAccount = new cflare.CflareAccount('mySuperAwesomeAccountToken');
|
||||||
testCflareAccount.auth({
|
|
||||||
email: 'someuser@example.com',
|
|
||||||
key: 'someLongApiKey'
|
|
||||||
})
|
|
||||||
|
|
||||||
let myAsyncCflareManagement = async () => {
|
const myAsyncCflareManagement = async () => {
|
||||||
// get things
|
// get things
|
||||||
let myZones = await myCflareAccount.listZones() // zones are fully typed
|
const myZones = await myCflareAccount.listZones(); // zones are fully typed
|
||||||
let myIdForADomain = await myCflareAccount.getZoneId('example.com') // type number
|
const myIdForADomain = await myCflareAccount.getZoneId('example.com'); // type number
|
||||||
let myRecordsForADomain = await myCflareAccount.listRecords('example.com') // records are fully typed
|
const myRecordsForADomain = await myCflareAccount.listRecords('example.com'); // records are fully typed
|
||||||
|
};
|
||||||
// set things
|
|
||||||
myCflareAccount.updateRecord(...)
|
|
||||||
myCflareAccount.createRecord(...)
|
|
||||||
myCflareAccount.deleteRecord(...)
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Contribution
|
||||||
|
|
||||||
|
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||||
|
|
||||||
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)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
[](https://maintainedby.lossless.com)
|
63
test/test.ts
63
test/test.ts
@ -1,19 +1,22 @@
|
|||||||
|
// 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(testQenv.getEnvVarOnDemand('CF_KEY'));
|
||||||
});
|
});
|
||||||
|
|
||||||
const randomPrefix = Math.floor(Math.random() * 2000);
|
tap.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);
|
||||||
const result = await testCloudflareAccount.listZones();
|
const result = await testCloudflareAccount.convenience.listZones();
|
||||||
console.log(result);
|
console.log(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -21,7 +24,7 @@ tap.test(
|
|||||||
'.getZoneId(domainName) -> should get an Cloudflare Id for a domain string',
|
'.getZoneId(domainName) -> should get an Cloudflare Id for a domain string',
|
||||||
async tools => {
|
async tools => {
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
await testCloudflareAccount.getZoneId('bleu.de');
|
await testCloudflareAccount.convenience.getZoneId('bleu.de');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -29,7 +32,7 @@ tap.test(
|
|||||||
'.listRecords(domainName) -> should list all records for a specific Domain Name',
|
'.listRecords(domainName) -> should list all records for a specific Domain Name',
|
||||||
async tools => {
|
async tools => {
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
await testCloudflareAccount.listRecords('bleu.de').then(async responseArg => {
|
await testCloudflareAccount.convenience.listRecords('bleu.de').then(async responseArg => {
|
||||||
console.log(responseArg);
|
console.log(responseArg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -37,19 +40,25 @@ tap.test(
|
|||||||
|
|
||||||
tap.test('should create a valid record for a subdomain', async tools => {
|
tap.test('should create a valid record for a subdomain', async tools => {
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
await testCloudflareAccount.createRecord(`${randomPrefix}subdomain.bleu.de`, 'A', '127.0.0.1');
|
await testCloudflareAccount.convenience.createRecord(
|
||||||
|
`${randomPrefix}subdomain.bleu.de`,
|
||||||
|
'A',
|
||||||
|
'127.0.0.1'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should get a record from Cloudflare', async tools => {
|
tap.test('should get a record from Cloudflare', async tools => {
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
await testCloudflareAccount.getRecord(`${randomPrefix}subdomain.bleu.de`, 'A').then(responseArg => {
|
await testCloudflareAccount.convenience
|
||||||
console.log(responseArg);
|
.getRecord(`${randomPrefix}subdomain.bleu.de`, 'A')
|
||||||
});
|
.then(responseArg => {
|
||||||
|
console.log(responseArg);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should remove a subdomain record from Cloudflare', async tools => {
|
tap.test('should remove a subdomain record from Cloudflare', async tools => {
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
await testCloudflareAccount
|
await testCloudflareAccount.convenience
|
||||||
.removeRecord(`${randomPrefix}subdomain.bleu.de`, 'A')
|
.removeRecord(`${randomPrefix}subdomain.bleu.de`, 'A')
|
||||||
.then(async responseArg => {
|
.then(async responseArg => {
|
||||||
console.log(responseArg);
|
console.log(responseArg);
|
||||||
@ -57,7 +66,27 @@ tap.test('should remove a subdomain record from Cloudflare', async tools => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('.purge(some.domain) -> should purge everything', async () => {
|
tap.test('.purge(some.domain) -> should purge everything', async () => {
|
||||||
await testCloudflareAccount.purgeZone('bleu.de');
|
await testCloudflareAccount.convenience.purgeZone('bleu.de');
|
||||||
|
});
|
||||||
|
|
||||||
|
// WORKERS
|
||||||
|
tap.test('should create a worker', async () => {
|
||||||
|
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 () => {
|
||||||
|
const workerArray = await testCloudflareAccount.workerManager.listWorkers();
|
||||||
|
console.log(workerArray);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,184 +1,271 @@
|
|||||||
import plugins = require('./cloudflare.plugins');
|
import plugins = require('./cloudflare.plugins');
|
||||||
import * as interfaces from './interfaces/cloudflare.interfaces';
|
import { logger } from './cloudflare.logger';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
// interfaces
|
// interfaces
|
||||||
import { TDnsRecord } from '@tsclass/tsclass';
|
|
||||||
import { WorkerManager } from './cloudflare.classes.workermanager';
|
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||||
import { ZoneManager } from './cloudflare.classes.zonemanager';
|
import { ZoneManager } from './cloudflare.classes.zonemanager';
|
||||||
|
|
||||||
export class CloudflareAccount {
|
export class CloudflareAccount {
|
||||||
private authEmail: string;
|
private authToken: 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(authTokenArg: string) {
|
||||||
this.authEmail = optionsArg.email;
|
this.authToken = authTokenArg;
|
||||||
this.authKey = optionsArg.key;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets a zone id of a domain from cloudflare
|
* gets you the account identifier
|
||||||
* @param domainName
|
|
||||||
*/
|
*/
|
||||||
public async getZoneId(domainName: string) {
|
public async getAccountIdentifier() {
|
||||||
const domain = new plugins.smartstring.Domain(domainName);
|
if (!this.accountIdentifier) {
|
||||||
const zoneArray = await this.listZones(domain.zoneName);
|
const route = `/accounts?page=1&per_page=20&direction=desc`;
|
||||||
const filteredResponse = zoneArray.filter(zoneArg => {
|
const response: any = await this.request('GET', route);
|
||||||
return zoneArg.name === domainName;
|
this.accountIdentifier = response.result[0].id;
|
||||||
});
|
|
||||||
if (filteredResponse.length >= 1) {
|
|
||||||
return filteredResponse[0].id;
|
|
||||||
} else {
|
|
||||||
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!`);
|
|
||||||
}
|
}
|
||||||
|
return this.accountIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public convenience = {
|
||||||
* gets a record
|
/**
|
||||||
* @param domainNameArg
|
* gets a zone id of a domain from cloudflare
|
||||||
* @param typeArg
|
* @param domainName
|
||||||
*/
|
*/
|
||||||
public async getRecord(
|
getZoneId: async (domainName: string) => {
|
||||||
domainNameArg: string,
|
const domain = new plugins.smartstring.Domain(domainName);
|
||||||
typeArg: TDnsRecord
|
const zoneArray = await this.convenience.listZones(domain.zoneName);
|
||||||
): Promise<interfaces.ICflareRecord> {
|
const filteredResponse = zoneArray.filter(zoneArg => {
|
||||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
return zoneArg.name === domainName;
|
||||||
const recordArrayArg = await this.listRecords(domain.zoneName);
|
});
|
||||||
const filteredResponse = recordArrayArg.filter(recordArg => {
|
if (filteredResponse.length >= 1) {
|
||||||
return recordArg.type === typeArg && recordArg.name === domainNameArg;
|
return filteredResponse[0].id;
|
||||||
});
|
|
||||||
return filteredResponse[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
const response = await this.request(
|
|
||||||
'POST',
|
|
||||||
'/zones/' + domainIdArg + '/dns_records',
|
|
||||||
dataObject
|
|
||||||
);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public removeRecord(domainNameArg: string, typeArg: TDnsRecord) {
|
|
||||||
const done = plugins.smartpromise.defer();
|
|
||||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
|
||||||
this.getRecord(domain.fullName, typeArg).then(responseArg => {
|
|
||||||
if (responseArg) {
|
|
||||||
const requestRoute: string =
|
|
||||||
'/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id;
|
|
||||||
this.request('DELETE', requestRoute).then(responseArg => {
|
|
||||||
done.resolve(responseArg);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
done.reject();
|
logger.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!`);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
return done.promise;
|
/**
|
||||||
}
|
* gets a record
|
||||||
|
* @param domainNameArg
|
||||||
|
* @param typeArg
|
||||||
|
*/
|
||||||
|
getRecord: async (
|
||||||
|
domainNameArg: string,
|
||||||
|
typeArg: plugins.tsclass.network.TDnsRecordType
|
||||||
|
): Promise<interfaces.ICflareRecord> => {
|
||||||
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
const recordArrayArg = await this.convenience.listRecords(domain.zoneName);
|
||||||
|
const filteredResponse = recordArrayArg.filter(recordArg => {
|
||||||
|
return recordArg.type === typeArg && recordArg.name === domainNameArg;
|
||||||
|
});
|
||||||
|
return filteredResponse[0];
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* creates a record
|
||||||
|
*/
|
||||||
|
createRecord: async (
|
||||||
|
domainNameArg: string,
|
||||||
|
typeArg: plugins.tsclass.network.TDnsRecordType,
|
||||||
|
contentArg: string
|
||||||
|
): Promise<any> => {
|
||||||
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
const domainIdArg = await this.convenience.getZoneId(domain.zoneName);
|
||||||
|
const dataObject = {
|
||||||
|
name: domain.fullName,
|
||||||
|
type: typeArg,
|
||||||
|
content: contentArg
|
||||||
|
};
|
||||||
|
const response = await this.request(
|
||||||
|
'POST',
|
||||||
|
'/zones/' + domainIdArg + '/dns_records',
|
||||||
|
dataObject
|
||||||
|
);
|
||||||
|
return response;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* removes a record from Cloudflare
|
||||||
|
* @param domainNameArg
|
||||||
|
* @param typeArg
|
||||||
|
*/
|
||||||
|
removeRecord: async (
|
||||||
|
domainNameArg: string,
|
||||||
|
typeArg: plugins.tsclass.network.TDnsRecordType
|
||||||
|
): Promise<any> => {
|
||||||
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
const cflareRecord = await this.convenience.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}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* cleanrecord allows the cleaning of any previous records to avoid unwanted sideeffects
|
||||||
|
*/
|
||||||
|
cleanRecord: async (domainNameArg: string, typeArg: plugins.tsclass.network.TDnsRecordType) => {
|
||||||
|
console.log(`cleaning record for ${domainNameArg}`);
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* updates a record
|
||||||
|
* @param domainNameArg
|
||||||
|
* @param typeArg
|
||||||
|
* @param valueArg
|
||||||
|
*/
|
||||||
|
updateRecord: async (
|
||||||
|
domainNameArg: string,
|
||||||
|
typeArg: plugins.tsclass.network.TDnsRecordType,
|
||||||
|
valueArg
|
||||||
|
) => {
|
||||||
|
// TODO: implement
|
||||||
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* list all records of a specified domain name
|
||||||
|
* @param domainNameArg - the domain name that you want to get the records from
|
||||||
|
*/
|
||||||
|
listRecords: async (domainNameArg: string): Promise<interfaces.ICflareRecord[]> => {
|
||||||
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
const domainId = await this.convenience.getZoneId(domain.zoneName);
|
||||||
|
const responseArg: any = await this.request(
|
||||||
|
'GET',
|
||||||
|
'/zones/' + domainId + '/dns_records?per_page=100'
|
||||||
|
);
|
||||||
|
const result: interfaces.ICflareRecord[] = responseArg.result;
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* list all zones in the associated authenticated account
|
||||||
|
* @param domainName
|
||||||
|
*/
|
||||||
|
listZones: async (domainName?: string): Promise<interfaces.ICflareZone[]> => {
|
||||||
|
// TODO: handle pagination
|
||||||
|
let requestRoute = `/zones?per_page=50`;
|
||||||
|
|
||||||
public updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
// may be optionally filtered by domain name
|
||||||
const done = plugins.smartpromise.defer();
|
if (domainName) {
|
||||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
requestRoute = `${requestRoute}&name=${domainName}`;
|
||||||
return done.promise;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
const response: any = await this.request('GET', requestRoute);
|
||||||
* list all records of a specified domain name
|
const result = response.result;
|
||||||
* @param domainNameArg - the domain name that you want to get the records from
|
return result;
|
||||||
*/
|
},
|
||||||
public async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
/**
|
||||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
* purges a zone
|
||||||
const domainId = await this.getZoneId(domain.zoneName);
|
*/
|
||||||
const responseArg: any = await this.request(
|
purgeZone: async (domainName: string): Promise<void> => {
|
||||||
'GET',
|
const domain = new plugins.smartstring.Domain(domainName);
|
||||||
'/zones/' + domainId + '/dns_records?per_page=100'
|
const domainId = await this.convenience.getZoneId(domain.zoneName);
|
||||||
);
|
const requestUrl = `/zones/${domainId}/purge_cache`;
|
||||||
const result: interfaces.ICflareRecord[] = responseArg.result;
|
const payload = {
|
||||||
return result;
|
purge_everything: true
|
||||||
}
|
};
|
||||||
|
const respone = await this.request('DELETE', requestUrl, payload);
|
||||||
/**
|
},
|
||||||
* list all zones in the associated authenticated account
|
// acme convenience functions
|
||||||
* @param domainName
|
acmeSetDnsChallenge: async (dnsChallenge: plugins.tsclass.network.IDnsChallenge) => {
|
||||||
*/
|
await this.convenience.cleanRecord(dnsChallenge.hostName, 'TXT');
|
||||||
public async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
|
await this.convenience.createRecord(dnsChallenge.hostName, 'TXT', dnsChallenge.challenge);
|
||||||
// TODO: handle pagination
|
},
|
||||||
let requestRoute = `/zones?per_page=50`;
|
acmeRemoveDnsChallenge: async (dnsChallenge: plugins.tsclass.network.IDnsChallenge) => {
|
||||||
|
await this.convenience.removeRecord(dnsChallenge.hostName, 'TXT');
|
||||||
// may be optionally filtered by domain name
|
|
||||||
if (domainName) {
|
|
||||||
requestRoute = `${requestRoute}&name=${domainName}`;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const response: any = await this.request('GET', requestRoute);
|
public async request(
|
||||||
const result = response.result;
|
methodArg: string,
|
||||||
return result;
|
routeArg: string,
|
||||||
}
|
dataArg: any = {},
|
||||||
|
requestHeadersArg = {}
|
||||||
public async purgeZone(domainName: string) {
|
): Promise<any> {
|
||||||
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
|
|
||||||
};
|
|
||||||
const respone = await this.request('DELETE', requestUrl, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
public request(methodArg: string, routeArg: string, dataArg = {}) {
|
|
||||||
const done = plugins.smartpromise.defer();
|
|
||||||
const options: plugins.smartrequest.ISmartRequestOptions = {
|
const options: plugins.smartrequest.ISmartRequestOptions = {
|
||||||
method: methodArg,
|
method: methodArg,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-Auth-Email': this.authEmail,
|
Authorization: `Bearer ${this.authToken}`,
|
||||||
'X-Auth-Key': this.authKey,
|
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
||||||
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg))
|
...requestHeadersArg
|
||||||
},
|
},
|
||||||
requestBody: dataArg
|
requestBody: dataArg
|
||||||
};
|
};
|
||||||
|
|
||||||
let retryCount = 0; // count the amount of retries
|
// route analysis
|
||||||
|
const routeWithoutQuery = routeArg.split('?')[0];
|
||||||
|
let queryParams: string[] = [];
|
||||||
|
if (routeArg.split('?').length > 1) {
|
||||||
|
queryParams = routeArg.split('?')[1].split('&');
|
||||||
|
}
|
||||||
|
|
||||||
const makeRequest = async () => {
|
// console.log(options);
|
||||||
const response: any = await plugins.smartrequest.request(
|
|
||||||
`https://api.cloudflare.com/client/v4${routeArg}`,
|
let retryCount = 0; // count the amount of retries
|
||||||
options
|
let pageCount = 1;
|
||||||
);
|
|
||||||
|
const getQueryParams = () => {
|
||||||
|
let result = '';
|
||||||
|
if (queryParams.length > 0) {
|
||||||
|
result += '?';
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
let isFirst = true;
|
||||||
|
for (const queryParam of queryParams) {
|
||||||
|
if (!isFirst) {
|
||||||
|
result += '&';
|
||||||
|
}
|
||||||
|
isFirst = false;
|
||||||
|
const queryParamSerialized = queryParam.split('=');
|
||||||
|
if (queryParam === 'page') {
|
||||||
|
result += `page=${pageCount}`;
|
||||||
|
} else {
|
||||||
|
result += queryParam;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
const makeRequest = async (): Promise<plugins.smartrequest.IExtendedIncomingMessage> => {
|
||||||
|
const requestUrl = `https://api.cloudflare.com/client/v4${routeWithoutQuery}${getQueryParams()}`;
|
||||||
|
const response = await plugins.smartrequest.request(requestUrl, options);
|
||||||
if (response.statusCode === 200) {
|
if (response.statusCode === 200) {
|
||||||
done.resolve(response.body);
|
if (response.body.result_info) {
|
||||||
|
const rI = response.body.result_info;
|
||||||
|
if (rI.total_count / rI.per_page > pageCount) {
|
||||||
|
pageCount++;
|
||||||
|
const subresponse = await makeRequest();
|
||||||
|
response.body.result = response.body.result.concat(subresponse.body.result);
|
||||||
|
return response;
|
||||||
|
} else {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
} else if (response.statusCode === 429) {
|
} else if (response.statusCode === 429) {
|
||||||
console.log('rate limited! Waiting for retry!');
|
console.log('rate limited! Waiting for retry!');
|
||||||
retryRequest();
|
return await 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 ${requestUrl}!`);
|
||||||
retryRequest();
|
console.log(response.body);
|
||||||
|
throw new Error(`request failed for ${requestUrl}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(response.statusCode);
|
console.log(response.body);
|
||||||
done.reject(new Error('request failed'));
|
throw new Error(`request failed for ${requestUrl} with status${response.statusCode}}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const retryRequest = async (
|
const retryRequest = async (
|
||||||
delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)
|
delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)
|
||||||
) => {
|
) => {
|
||||||
@ -189,11 +276,11 @@ export class CloudflareAccount {
|
|||||||
return await makeRequest();
|
return await makeRequest();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
makeRequest();
|
const response = await makeRequest();
|
||||||
return done.promise;
|
return response.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
private authCheck() {
|
private authCheck() {
|
||||||
return this.authEmail && this.authKey; // check if auth is available
|
return !!this.authToken; // check if auth is available
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
5
ts/cloudflare.classes.record.ts
Normal file
5
ts/cloudflare.classes.record.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
|
||||||
|
export class CloudflareRecord {
|
||||||
|
|
||||||
|
}
|
93
ts/cloudflare.classes.worker.ts
Normal file
93
ts/cloudflare.classes.worker.ts
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||||
|
import { logger } from './cloudflare.logger';
|
||||||
|
|
||||||
|
export interface IWorkerRoute extends interfaces.ICflareWorkerRoute {
|
||||||
|
zoneName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CloudflareWorker {
|
||||||
|
// STATIC
|
||||||
|
public static async fromApiObject(
|
||||||
|
workerManager: WorkerManager,
|
||||||
|
apiObject
|
||||||
|
): Promise<CloudflareWorker> {
|
||||||
|
const newWorker = new CloudflareWorker(workerManager);
|
||||||
|
Object.assign(newWorker, apiObject);
|
||||||
|
await newWorker.getRoutes();
|
||||||
|
return newWorker;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
private workerManager: WorkerManager;
|
||||||
|
|
||||||
|
public script: string;
|
||||||
|
public id: string;
|
||||||
|
public etag: string;
|
||||||
|
// tslint:disable-next-line: variable-name
|
||||||
|
public created_on: string;
|
||||||
|
// tslint:disable-next-line: variable-name
|
||||||
|
public modified_on: string;
|
||||||
|
|
||||||
|
public routes: IWorkerRoute[] = [];
|
||||||
|
constructor(workerManagerArg: WorkerManager) {
|
||||||
|
this.workerManager = workerManagerArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all routes for a worker
|
||||||
|
*/
|
||||||
|
public async getRoutes() {
|
||||||
|
const zones = await this.workerManager.cfAccount.convenience.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';
|
||||||
|
logger.log('info', `route already exists, no update needed`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets care about actually setting routes
|
||||||
|
if (routeStatus === 'new') {
|
||||||
|
const zoneId = await this.workerManager.cfAccount.convenience.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.convenience.getZoneId(newRoute.zoneName);
|
||||||
|
const requestRoute = `/zones/${zoneId}/workers/routes/${routeIdForUpdate}`;
|
||||||
|
await this.workerManager.cfAccount.request('PUT', requestRoute, {
|
||||||
|
pattern: newRoute.pattern,
|
||||||
|
script: this.id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
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 { CloudflareWorker } from './cloudflare.classes.worker';
|
||||||
|
|
||||||
export class WorkerManager {
|
export class WorkerManager {
|
||||||
public cfAccount: CloudflareAccount;
|
public cfAccount: CloudflareAccount;
|
||||||
@ -7,4 +8,29 @@ export class WorkerManager {
|
|||||||
constructor(cfAccountArg: CloudflareAccount) {
|
constructor(cfAccountArg: CloudflareAccount) {
|
||||||
this.cfAccount = cfAccountArg;
|
this.cfAccount = cfAccountArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async createWorker(workerName: string, workerScript: string): Promise<CloudflareWorker> {
|
||||||
|
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 CloudflareWorker.fromApiObject(this, responseBody.result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
const results = response.result;
|
||||||
|
const workers: CloudflareWorker[] = [];
|
||||||
|
for (const apiObject of results) {
|
||||||
|
workers.push(await CloudflareWorker.fromApiObject(this, apiObject));
|
||||||
|
}
|
||||||
|
return workers;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
51
ts/cloudflare.classes.zone.ts
Normal file
51
ts/cloudflare.classes.zone.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
export class CloudflareZone implements interfaces.ICflareZone {
|
||||||
|
public static createFromApiObject(apiObject: interfaces.ICflareZone) {
|
||||||
|
const cloudflareZone = new CloudflareZone();
|
||||||
|
Object.assign(cloudflareZone, apiObject);
|
||||||
|
return cloudflareZone;
|
||||||
|
}
|
||||||
|
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
development_mode: number;
|
||||||
|
original_name_servers: string[];
|
||||||
|
original_registrar: string;
|
||||||
|
original_dnshost: string;
|
||||||
|
created_on: string;
|
||||||
|
modified_on: string;
|
||||||
|
name_servers: string[];
|
||||||
|
owner: {
|
||||||
|
id: string;
|
||||||
|
email: string;
|
||||||
|
owner_type: string;
|
||||||
|
};
|
||||||
|
permissions: string[];
|
||||||
|
plan: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
price: number;
|
||||||
|
currency: string;
|
||||||
|
frequency: string;
|
||||||
|
legacy_id: string;
|
||||||
|
is_subscribed: boolean;
|
||||||
|
can_subscribe: boolean;
|
||||||
|
};
|
||||||
|
plan_pending: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
price: number;
|
||||||
|
currency: string;
|
||||||
|
frequency: string;
|
||||||
|
legacy_id: string;
|
||||||
|
is_subscribed: string;
|
||||||
|
can_subscribe: string;
|
||||||
|
};
|
||||||
|
status: string;
|
||||||
|
paused: boolean;
|
||||||
|
type: string;
|
||||||
|
checked_on: string;
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
import * as plugins from './cloudflare.plugins';
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
import { CloudflareAccount } from './cloudflare.classes.account';
|
import { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
|
import { CloudflareZone } from './cloudflare.classes.zone';
|
||||||
|
|
||||||
export class ZoneManager {
|
export class ZoneManager {
|
||||||
public cfAccount: CloudflareAccount;
|
public cfAccount: CloudflareAccount;
|
||||||
@ -8,4 +10,23 @@ export class ZoneManager {
|
|||||||
constructor(cfAccountArg: CloudflareAccount) {
|
constructor(cfAccountArg: CloudflareAccount) {
|
||||||
this.cfAccount = cfAccountArg;
|
this.cfAccount = cfAccountArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getZones(zoneName: string) {
|
||||||
|
let requestRoute = `/zones?per_page=50`;
|
||||||
|
// may be optionally filtered by domain name
|
||||||
|
|
||||||
|
if (zoneName) {
|
||||||
|
requestRoute = `${requestRoute}&name=${zoneName}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response: any = await this.cfAccount.request('GET', requestRoute);
|
||||||
|
const apiObjects: interfaces.ICflareZone[] = response.result;
|
||||||
|
|
||||||
|
const cloudflareZoneArray = [];
|
||||||
|
for (const apiObject of apiObjects) {
|
||||||
|
cloudflareZoneArray.push(CloudflareZone.createFromApiObject(apiObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
return cloudflareZoneArray;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
3
ts/cloudflare.logger.ts
Normal file
3
ts/cloudflare.logger.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
|
||||||
|
export const logger = new plugins.smartlog.ConsoleLog();
|
@ -1 +1,2 @@
|
|||||||
export { CloudflareAccount } from './cloudflare.classes.account';
|
export { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
|
export { CloudflareWorker } from './cloudflare.classes.worker';
|
||||||
|
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';
|
||||||
|
Reference in New Issue
Block a user