feat(core): Release 6.2.0: Improved async iterator support, enhanced error handling and refined API interfaces for better type safety and consistent behavior.
This commit is contained in:
@@ -22,19 +22,25 @@ export class WorkerManager {
|
||||
}
|
||||
|
||||
try {
|
||||
// Create or update the worker script
|
||||
// Use the official client to create/update the worker
|
||||
await this.cfAccount.apiAccount.workers.scripts.content.update(workerName, {
|
||||
account_id: this.cfAccount.preselectedAccountId,
|
||||
"CF-WORKER-BODY-PART": workerScript,
|
||||
metadata: {} // Required empty object
|
||||
metadata: {}
|
||||
});
|
||||
|
||||
// Create a new worker instance directly
|
||||
// Create a new worker instance
|
||||
const worker = new CloudflareWorker(this);
|
||||
worker.id = workerName;
|
||||
worker.script = workerScript;
|
||||
|
||||
// Initialize the worker and get its routes
|
||||
await worker.getRoutes();
|
||||
try {
|
||||
await worker.getRoutes();
|
||||
} catch (routeError) {
|
||||
logger.log('warn', `Failed to get routes for worker ${workerName}: ${routeError.message}`);
|
||||
// Continue anyway since the worker was created
|
||||
}
|
||||
|
||||
return worker;
|
||||
} catch (error) {
|
||||
@@ -54,17 +60,27 @@ export class WorkerManager {
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if the worker exists
|
||||
await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
|
||||
// Get the worker script using the official client
|
||||
const workerScript = await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
|
||||
account_id: this.cfAccount.preselectedAccountId
|
||||
});
|
||||
|
||||
// Create a new worker instance directly
|
||||
// Create a new worker instance
|
||||
const worker = new CloudflareWorker(this);
|
||||
worker.id = workerName;
|
||||
|
||||
// Save script content if available
|
||||
if (workerScript && typeof workerScript === 'object') {
|
||||
Object.assign(worker, workerScript);
|
||||
}
|
||||
|
||||
// Initialize the worker and get its routes
|
||||
await worker.getRoutes();
|
||||
try {
|
||||
await worker.getRoutes();
|
||||
} catch (routeError) {
|
||||
logger.log('warn', `Failed to get routes for worker ${workerName}: ${routeError.message}`);
|
||||
// Continue anyway since we found the worker
|
||||
}
|
||||
|
||||
return worker;
|
||||
} catch (error) {
|
||||
@@ -83,23 +99,35 @@ export class WorkerManager {
|
||||
}
|
||||
|
||||
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)
|
||||
// Collect all scripts using the new client's async iterator
|
||||
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 {
|
||||
for await (const script of this.cfAccount.apiAccount.workers.scripts.list({
|
||||
account_id: this.cfAccount.preselectedAccountId,
|
||||
})) {
|
||||
workerScripts.push(script);
|
||||
}
|
||||
|
||||
logger.log('info', `Found ${workerScripts.length} worker scripts`);
|
||||
return workerScripts;
|
||||
} catch (error) {
|
||||
logger.log('warn', `Error while listing workers with async iterator: ${error.message}`);
|
||||
|
||||
// Try alternative approach if the async iterator fails
|
||||
const result = await this.cfAccount.apiAccount.workers.scripts.list({
|
||||
account_id: this.cfAccount.preselectedAccountId,
|
||||
}) as any;
|
||||
|
||||
// Check if the result has a 'result' property (older API response format)
|
||||
if (result && result.result && Array.isArray(result.result)) {
|
||||
logger.log('info', `Found ${result.result.length} worker scripts using direct result`);
|
||||
return result.result;
|
||||
}
|
||||
}
|
||||
return workerScripts;
|
||||
|
||||
logger.log('warn', 'Could not retrieve worker scripts');
|
||||
return [];
|
||||
} catch (error) {
|
||||
logger.log('error', `Failed to list worker scripts: ${error.message}`);
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user