feat(core): Update dependencies, enhance documentation, and improve error handling with clearer API usage examples

This commit is contained in:
2025-03-19 11:31:50 +00:00
parent d1788dc626
commit 2b51df90e6
12 changed files with 6073 additions and 2111 deletions

View File

@@ -14,17 +14,33 @@ export class WorkerManager {
* 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
* @returns CloudflareWorker instance for the created/updated worker
*/
public async createWorker(workerName: string, workerScript: string): Promise<plugins.ICloudflareTypes['Script']> {
public async createWorker(workerName: string, workerScript: string): Promise<CloudflareWorker> {
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 worker;
try {
// Create or update the worker script
await this.cfAccount.apiAccount.workers.scripts.content.update(workerName, {
account_id: this.cfAccount.preselectedAccountId,
"CF-WORKER-BODY-PART": workerScript,
metadata: {} // Required empty object
});
// Create a new worker instance directly
const worker = new CloudflareWorker(this);
worker.id = workerName;
// Initialize the worker and get its routes
await worker.getRoutes();
return worker;
} catch (error) {
logger.log('error', `Failed to create worker ${workerName}: ${error.message}`);
throw error;
}
}
/**
@@ -38,11 +54,19 @@ export class WorkerManager {
}
try {
const script = await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
// Check if the worker exists
await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
account_id: this.cfAccount.preselectedAccountId
});
return CloudflareWorker.fromApiObject(this, script);
// Create a new worker instance directly
const worker = new CloudflareWorker(this);
worker.id = workerName;
// Initialize the worker and get its routes
await worker.getRoutes();
return worker;
} catch (error) {
logger.log('warn', `Worker '${workerName}' not found: ${error.message}`);
return undefined;
@@ -57,13 +81,29 @@ export class WorkerManager {
if (!this.cfAccount.preselectedAccountId) {
throw new Error('No account selected. Please select it first on the account.');
}
const workerScripts: plugins.ICloudflareTypes['Script'][] = [];
for await (const scriptArg of this.cfAccount.apiAccount.workers.scripts.list({
account_id: this.cfAccount.preselectedAccountId,
})) {
workerScripts.push(scriptArg);
try {
const result = await this.cfAccount.apiAccount.workers.scripts.list({
account_id: this.cfAccount.preselectedAccountId,
});
// Check if the result has a 'result' property (API response format)
if (result && result.result && Array.isArray(result.result)) {
return result.result;
}
// Otherwise collect from async iterator (new client format)
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;
} catch (error) {
logger.log('error', `Failed to list worker scripts: ${error.message}`);
return [];
}
return workerScripts;
}
/**