import type { CfDnsRecord, CfHealthCheck, CfZone, CreateDnsRecordPayload, PatchDnsRecordPayload, } from "@cfdm/shared"; import { createDnsAdapter } from "./cloudflare/dns-service.js"; import { createHealthCheckAdapter, type CfHealthCheckPayload } from "./cloudflare/healthcheck-service.js"; import { createKvAdapter } from "./cloudflare/kv-service.js"; import { createWorkersAdapter } from "./cloudflare/workers-service.js"; import { createZoneAdapter } from "./cloudflare/zone-service.js"; export type { CfHealthCheckPayload }; export class CloudflareClient { private readonly zones; private readonly dns; private readonly healthchecks; private readonly kv; private readonly workers; private readonly token; constructor(token: string) { this.token = token.trim(); this.zones = createZoneAdapter(token); this.dns = createDnsAdapter(token); this.healthchecks = createHealthCheckAdapter(token); this.kv = createKvAdapter(token); this.workers = createWorkersAdapter(token); } get isConfigured(): boolean { return this.token.length > 0; } listZones(): Promise { return this.zones.listZones(); } getZone(zoneId: string): Promise { return this.zones.getZone(zoneId); } listDnsRecords(zoneId: string): Promise { return this.dns.listDnsRecords(zoneId); } createDnsRecord(zoneId: string, payload: CreateDnsRecordPayload): Promise { return this.dns.createDnsRecord(zoneId, payload); } updateDnsRecord( zoneId: string, recordId: string, payload: CreateDnsRecordPayload, ): Promise { return this.dns.updateDnsRecord(zoneId, recordId, payload); } patchDnsRecord( zoneId: string, recordId: string, payload: PatchDnsRecordPayload, ): Promise { return this.dns.patchDnsRecord(zoneId, recordId, payload); } deleteDnsRecord(zoneId: string, recordId: string): Promise { return this.dns.deleteDnsRecord(zoneId, recordId); } listHealthChecks(zoneId: string): Promise { return this.healthchecks.listHealthChecks(zoneId); } getHealthCheck(zoneId: string, id: string): Promise { return this.healthchecks.getHealthCheck(zoneId, id); } createHealthCheck(zoneId: string, payload: CfHealthCheckPayload): Promise { return this.healthchecks.createHealthCheck(zoneId, payload); } updateHealthCheck( zoneId: string, id: string, payload: CfHealthCheckPayload, ): Promise { return this.healthchecks.updateHealthCheck(zoneId, id, payload); } deleteHealthCheck(zoneId: string, id: string): Promise { return this.healthchecks.deleteHealthCheck(zoneId, id); } listAccounts() { return this.workers.listAccounts(); } listKvNamespaces(accountId: string) { return this.kv.listNamespaces(accountId); } createKvNamespace(accountId: string, title: string) { return this.kv.createNamespace(accountId, title); } kvGet(accountId: string, namespaceId: string, key: string) { return this.kv.getValue(accountId, namespaceId, key); } kvPut(accountId: string, namespaceId: string, key: string, value: string) { return this.kv.putValue(accountId, namespaceId, key, value); } putWorkerScript(opts: { accountId: string; scriptName: string; source: string; kvNamespaceId: string; }) { return this.workers.putScript(opts); } putWorkerSchedules(accountId: string, scriptName: string, crons: string[]) { return this.workers.putSchedules(accountId, scriptName, crons); } enableWorkersDev(accountId: string, scriptName: string) { return this.workers.enableWorkersDev(accountId, scriptName); } getWorkersSubdomain(accountId: string) { return this.workers.getWorkersSubdomain(accountId); } }