fix(start supporting workers again): update

This commit is contained in:
Philipp Kunz 2024-06-17 01:36:39 +02:00
parent 902ca30529
commit b0f8d1e4d0
7 changed files with 83 additions and 24 deletions

View File

@ -14,18 +14,23 @@ tap.test('should create a valid instance of CloudflareAccount', async () => {
testCloudflareAccount = new cloudflare.CloudflareAccount(await testQenv.getEnvVarOnDemand('CF_KEY')); testCloudflareAccount = new cloudflare.CloudflareAccount(await testQenv.getEnvVarOnDemand('CF_KEY'));
}); });
tap.test('should preselect an account', async () => {
await testCloudflareAccount.preselectAccountByName('Sandbox Account');
})
tap.test('.listZones() -> should display an entire account', async (tools) => { tap.test('.listZones() -> should display an entire account', async (tools) => {
tools.timeout(600000); tools.timeout(600000);
const result = await testCloudflareAccount.convenience.listZones(); const result = await testCloudflareAccount.convenience.listZones();
console.log(result); console.log(result);
await tools.delayFor(10000); // await tools.delayFor(10000);
}); });
tap.test( 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.convenience.getZoneId('bleu.de'); const id = await testCloudflareAccount.convenience.getZoneId('bleu.de');
console.log(`The account id for bleu.de is: ${id}`);
} }
); );
@ -71,6 +76,11 @@ tap.test('.purge(some.domain) -> should purge everything', async () => {
await testCloudflareAccount.convenience.purgeZone('bleu.de'); await testCloudflareAccount.convenience.purgeZone('bleu.de');
}); });
tap.test('should list workers', async () => {
const workerArray = await testCloudflareAccount.workerManager.listWorkerScripts();
console.log(workerArray);
});
// WORKERS // WORKERS
tap.test('should create a worker', async () => { tap.test('should create a worker', async () => {
const worker = await testCloudflareAccount.workerManager.createWorker( const worker = await testCloudflareAccount.workerManager.createWorker(
@ -86,8 +96,8 @@ tap.test('should create a worker', async () => {
console.log(worker); console.log(worker);
}); });
tap.test('should get workers', async () => { tap.test('should get workers again', async () => {
const workerArray = await testCloudflareAccount.workerManager.listWorkers(); const workerArray = await testCloudflareAccount.workerManager.listWorkerScripts();
console.log(workerArray); console.log(workerArray);
}); });

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@apiclient.xyz/cloudflare', name: '@apiclient.xyz/cloudflare',
version: '6.0.4', version: '6.0.5',
description: 'easy cloudflare management' description: 'A TypeScript client for managing Cloudflare accounts, zones, DNS records, and workers with ease.'
} }

View File

@ -8,7 +8,7 @@ import { ZoneManager } from './cloudflare.classes.zonemanager.js';
export class CloudflareAccount { export class CloudflareAccount {
private authToken: string; private authToken: string;
private accountIdentifier: string; public preselectedAccountId: string;
public workerManager = new WorkerManager(this); public workerManager = new WorkerManager(this);
public zoneManager = new ZoneManager(this); public zoneManager = new ZoneManager(this);
@ -26,7 +26,29 @@ export class CloudflareAccount {
}); });
} }
public async preselectAccountByName(nameArg: string) {
const accounts = await this.convenience.listAccounts();
const account = accounts.find((accountArg) => {
return accountArg.name === nameArg;
});
if (account) {
this.preselectedAccountId = account.id;
} else {
throw new Error(`account with name ${nameArg} not found`);
}
}
public convenience = { public convenience = {
/**
* listAccounts
*/
listAccounts: async () => {
const accounts: plugins.ICloudflareTypes['Account'][] = [];
for await (const account of this.apiAccount.accounts.list()) {
accounts.push(account as interfaces.ICloudflareApiAccountObject);
}
return accounts;
},
/** /**
* gets a zone id of a domain from cloudflare * gets a zone id of a domain from cloudflare
* @param domainName * @param domainName

View File

@ -9,28 +9,30 @@ export class WorkerManager {
this.cfAccount = cfAccountArg; this.cfAccount = cfAccountArg;
} }
public async createWorker(workerName: string, workerScript: string): Promise<CloudflareWorker> { public async createWorker(workerName: string, workerScript: string): Promise<plugins.ICloudflareTypes['Script']> {
const accountIdentifier = await this.cfAccount.getAccountIdentifier(); if (!this.cfAccount.preselectedAccountId) {
const route = `/accounts/${accountIdentifier}/workers/scripts/${workerName}`; throw new Error('No account selected. Please select it first on the account.');
const responseBody = await this.cfAccount.request('PUT', route, workerScript, { }
'Content-Type': 'application/javascript', const worker = await this.cfAccount.apiAccount.workers.scripts.content.update(workerName, {
'Content-Length': Buffer.byteLength(workerScript), account_id: this.cfAccount.preselectedAccountId,
"CF-WORKER-BODY-PART": workerScript,
}); });
return CloudflareWorker.fromApiObject(this, responseBody.result); return worker;
} }
/** /**
* lists workers * lists workers
*/ */
public async listWorkers() { public async listWorkerScripts() {
const accountIdentifier = await this.cfAccount.getAccountIdentifier(); if (!this.cfAccount.preselectedAccountId) {
const route = `/accounts/${accountIdentifier}/workers/scripts`; throw new Error('No account selected. Please select it first on the account.');
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; const workerScripts: plugins.ICloudflareTypes['Script'][] = [];
for await (const scriptArg of this.cfAccount.apiAccount.workers.scripts.list({
account_id: this.cfAccount.preselectedAccountId,
})) {
workerScripts.push(scriptArg);
}
return workerScripts;
} }
} }

View File

@ -9,12 +9,16 @@ export { smartlog, smartpromise, smartdelay, smartrequest, smartstring, tsclass
// third party // third party
import * as cloudflare from 'cloudflare'; import * as cloudflare from 'cloudflare';
import * as interfaces from './interfaces/index.js';
import type { Zone } from 'cloudflare/resources/zones/zones.js'; import type { Zone } from 'cloudflare/resources/zones/zones.js';
import type { Record } from 'cloudflare/resources/dns/records.js'; import type { Record } from 'cloudflare/resources/dns/records.js';
import type { Script } from 'cloudflare/resources/workers/scripts/index.js';
export interface ICloudflareTypes { export interface ICloudflareTypes {
Zone: Zone; Account: interfaces.ICloudflareApiAccountObject;
Record: Record; Record: Record;
Zone: Zone;
Script: Script;
} }
export { cloudflare }; export { cloudflare };

View File

@ -0,0 +1,20 @@
export interface ICloudflareApiAccountObject {
id: string;
name: string;
type: 'standard' | 'enterprise' | 'pro' | 'free'; // Assuming other possible types
settings: {
enforce_twofactor: boolean;
api_access_enabled: boolean | null;
access_approval_expiry: string | null; // Assuming ISO date string or null
use_account_custom_ns_by_default: boolean;
default_nameservers: string;
};
legacy_flags: {
enterprise_zone_quota: {
maximum: number;
current: number;
available: number;
};
};
created_on: string; // Assuming ISO date string
}

View File

@ -1 +1,2 @@
export * from './cloudflare.api.account.js';
export * from './cloudflare.api.workerroute.js'; export * from './cloudflare.api.workerroute.js';