cloudflare/ts/cloudflare.classes.workermanager.ts

39 lines
1.3 KiB
TypeScript

import * as plugins from './cloudflare.plugins.js';
import { CloudflareAccount } from './cloudflare.classes.account.js';
import { CloudflareWorker } from './cloudflare.classes.worker.js';
export class WorkerManager {
public cfAccount: CloudflareAccount;
constructor(cfAccountArg: CloudflareAccount) {
this.cfAccount = cfAccountArg;
}
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.');
}
const worker = await this.cfAccount.apiAccount.workers.scripts.content.update(workerName, {
account_id: this.cfAccount.preselectedAccountId,
"CF-WORKER-BODY-PART": workerScript,
});
return worker;
}
/**
* lists workers
*/
public async listWorkerScripts() {
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);
}
return workerScripts;
}
}