From b0f8d1e4d0d5c88c37dd57177a0c7df1eb9ab6fe Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Mon, 17 Jun 2024 01:36:39 +0200 Subject: [PATCH] fix(start supporting workers again): update --- test/test.ts | 18 ++++++++++--- ts/00_commitinfo_data.ts | 4 +-- ts/cloudflare.classes.account.ts | 24 ++++++++++++++++- ts/cloudflare.classes.workermanager.ts | 34 +++++++++++++------------ ts/cloudflare.plugins.ts | 6 ++++- ts/interfaces/cloudflare.api.account.ts | 20 +++++++++++++++ ts/interfaces/index.ts | 1 + 7 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 ts/interfaces/cloudflare.api.account.ts diff --git a/test/test.ts b/test/test.ts index 91ea13f..6430cb7 100644 --- a/test/test.ts +++ b/test/test.ts @@ -14,18 +14,23 @@ tap.test('should create a valid instance of CloudflareAccount', async () => { 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) => { tools.timeout(600000); const result = await testCloudflareAccount.convenience.listZones(); console.log(result); - await tools.delayFor(10000); + // await tools.delayFor(10000); }); tap.test( '.getZoneId(domainName) -> should get an Cloudflare Id for a domain string', async (tools) => { 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'); }); +tap.test('should list workers', async () => { + const workerArray = await testCloudflareAccount.workerManager.listWorkerScripts(); + console.log(workerArray); +}); + // WORKERS tap.test('should create a worker', async () => { const worker = await testCloudflareAccount.workerManager.createWorker( @@ -86,8 +96,8 @@ tap.test('should create a worker', async () => { console.log(worker); }); -tap.test('should get workers', async () => { - const workerArray = await testCloudflareAccount.workerManager.listWorkers(); +tap.test('should get workers again', async () => { + const workerArray = await testCloudflareAccount.workerManager.listWorkerScripts(); console.log(workerArray); }); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 0db81af..c3beb53 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/cloudflare', - version: '6.0.4', - description: 'easy cloudflare management' + version: '6.0.5', + description: 'A TypeScript client for managing Cloudflare accounts, zones, DNS records, and workers with ease.' } diff --git a/ts/cloudflare.classes.account.ts b/ts/cloudflare.classes.account.ts index 4d6fef0..c143b90 100644 --- a/ts/cloudflare.classes.account.ts +++ b/ts/cloudflare.classes.account.ts @@ -8,7 +8,7 @@ import { ZoneManager } from './cloudflare.classes.zonemanager.js'; export class CloudflareAccount { private authToken: string; - private accountIdentifier: string; + public preselectedAccountId: string; public workerManager = new WorkerManager(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 = { + /** + * 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 * @param domainName diff --git a/ts/cloudflare.classes.workermanager.ts b/ts/cloudflare.classes.workermanager.ts index fce8996..38f667a 100644 --- a/ts/cloudflare.classes.workermanager.ts +++ b/ts/cloudflare.classes.workermanager.ts @@ -9,28 +9,30 @@ export class WorkerManager { this.cfAccount = cfAccountArg; } - public async createWorker(workerName: string, workerScript: string): Promise { - 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), + public async createWorker(workerName: string, workerScript: string): Promise { + if (!this.cfAccount.preselectedAccountId) { + throw new Error('No account selected. Please select it first on the account.'); + } + const worker = await this.cfAccount.apiAccount.workers.scripts.content.update(workerName, { + account_id: this.cfAccount.preselectedAccountId, + "CF-WORKER-BODY-PART": workerScript, }); - return CloudflareWorker.fromApiObject(this, responseBody.result); + return worker; } /** * 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)); + public async listWorkerScripts() { + if (!this.cfAccount.preselectedAccountId) { + throw new Error('No account selected. Please select it first on the account.'); } - 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; } } diff --git a/ts/cloudflare.plugins.ts b/ts/cloudflare.plugins.ts index de30a91..acbac90 100644 --- a/ts/cloudflare.plugins.ts +++ b/ts/cloudflare.plugins.ts @@ -9,12 +9,16 @@ export { smartlog, smartpromise, smartdelay, smartrequest, smartstring, tsclass // third party import * as cloudflare from 'cloudflare'; +import * as interfaces from './interfaces/index.js'; import type { Zone } from 'cloudflare/resources/zones/zones.js'; import type { Record } from 'cloudflare/resources/dns/records.js'; +import type { Script } from 'cloudflare/resources/workers/scripts/index.js'; export interface ICloudflareTypes { - Zone: Zone; + Account: interfaces.ICloudflareApiAccountObject; Record: Record; + Zone: Zone; + Script: Script; } export { cloudflare }; diff --git a/ts/interfaces/cloudflare.api.account.ts b/ts/interfaces/cloudflare.api.account.ts new file mode 100644 index 0000000..6be78f2 --- /dev/null +++ b/ts/interfaces/cloudflare.api.account.ts @@ -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 +} \ No newline at end of file diff --git a/ts/interfaces/index.ts b/ts/interfaces/index.ts index 1c8a339..c2b7bfa 100644 --- a/ts/interfaces/index.ts +++ b/ts/interfaces/index.ts @@ -1 +1,2 @@ +export * from './cloudflare.api.account.js'; export * from './cloudflare.api.workerroute.js';