cloudflare/ts/cloudflare.classes.workermanager.ts

37 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2019-07-18 09:51:56 +00:00
import * as plugins from './cloudflare.plugins';
import { CloudflareAccount } from './cloudflare.classes.account';
2019-07-19 10:39:39 +00:00
import { CloudflareWorker } from './cloudflare.classes.worker';
2019-07-18 09:51:56 +00:00
export class WorkerManager {
2019-07-18 12:44:45 +00:00
public cfAccount: CloudflareAccount;
2019-07-18 09:51:56 +00:00
constructor(cfAccountArg: CloudflareAccount) {
this.cfAccount = cfAccountArg;
}
2019-07-18 12:25:10 +00:00
2019-07-19 10:39:39 +00:00
public async createWorker(workerName: string, workerScript: string): Promise<CloudflareWorker> {
2019-07-18 12:25:10 +00:00
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)
});
2019-07-19 10:39:39 +00:00
return CloudflareWorker.fromApiObject(this, responseBody);
2019-07-18 12:25:10 +00:00
}
/**
* 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);
2020-02-09 17:36:29 +00:00
const results = response.result;
const workers: CloudflareWorker[] = [];
for (const apiObject of results) {
workers.push(await CloudflareWorker.fromApiObject(this, apiObject));
}
return workers;
2019-07-18 12:25:10 +00:00
}
2019-07-18 09:51:56 +00:00
}