fix(core): Improve logging consistency, record update functionality, and API error handling in Cloudflare modules

This commit is contained in:
2025-03-19 07:07:08 +00:00
parent 4600749442
commit ba49c42dd8
12 changed files with 937 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
import * as plugins from './cloudflare.plugins.js';
import { CloudflareAccount } from './cloudflare.classes.account.js';
import { CloudflareWorker } from './cloudflare.classes.worker.js';
import { logger } from './cloudflare.logger.js';
export class WorkerManager {
public cfAccount: CloudflareAccount;
@@ -9,6 +10,12 @@ export class WorkerManager {
this.cfAccount = cfAccountArg;
}
/**
* Creates a new worker or updates an existing one
* @param workerName Name of the worker
* @param workerScript JavaScript content of the worker
* @returns The created or updated worker
*/
public async createWorker(workerName: string, workerScript: string): Promise<plugins.ICloudflareTypes['Script']> {
if (!this.cfAccount.preselectedAccountId) {
throw new Error('No account selected. Please select it first on the account.');
@@ -21,7 +28,30 @@ export class WorkerManager {
}
/**
* lists workers
* Get a worker by name
* @param workerName Name of the worker to retrieve
* @returns CloudflareWorker instance or undefined if not found
*/
public async getWorker(workerName: string): Promise<CloudflareWorker | undefined> {
if (!this.cfAccount.preselectedAccountId) {
throw new Error('No account selected. Please select it first on the account.');
}
try {
const script = await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
account_id: this.cfAccount.preselectedAccountId
});
return CloudflareWorker.fromApiObject(this, script);
} catch (error) {
logger.log('warn', `Worker '${workerName}' not found: ${error.message}`);
return undefined;
}
}
/**
* Lists all worker scripts
* @returns Array of worker scripts
*/
public async listWorkerScripts() {
if (!this.cfAccount.preselectedAccountId) {
@@ -35,4 +65,26 @@ export class WorkerManager {
}
return workerScripts;
}
}
/**
* Deletes a worker script
* @param workerName Name of the worker to delete
* @returns True if deletion was successful
*/
public async deleteWorker(workerName: string): Promise<boolean> {
if (!this.cfAccount.preselectedAccountId) {
throw new Error('No account selected. Please select it first on the account.');
}
try {
await this.cfAccount.apiAccount.workers.scripts.delete(workerName, {
account_id: this.cfAccount.preselectedAccountId
});
logger.log('info', `Worker '${workerName}' deleted successfully`);
return true;
} catch (error) {
logger.log('error', `Failed to delete worker '${workerName}': ${error.message}`);
return false;
}
}
}