DnsRead
Source:
src/Cloudflare/Dns/DnsRead.ts
Binding that lets a Worker read Cloudflare DNS records at runtime.
Creates a least-privilege {@link AccountApiToken} with only the DNS Read
permission, scoped to the single zone passed to bind, and binds its value
into the Worker so runtime code can authenticate.
Reading DNS records at runtime
Section titled “Reading DNS records at runtime”Bind the client in the Worker’s Init phase and provide {@link DnsReadLive}.
The zone is fixed by .bind(zone) — the provisioned token only grants
access to that zone, so calls take no zoneId. Pass the {@link Zone}
resource directly (it’s an Effect), or yield* Zone for a resolved value.
import * as Cloudflare from "alchemy/Cloudflare";import * as Effect from "effect/Effect";import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
const Zone = Cloudflare.Zone("MyZone", { name: "example.com" });
export class DnsReaderWorker extends Cloudflare.Worker<DnsReaderWorker>()( "DnsReaderWorker", { main: import.meta.filename }, Effect.gen(function* () { // Init phase — bind the read client scoped to the zone. const dns = yield* Cloudflare.DnsRead.bind(Zone);
return { fetch: Effect.gen(function* () { const { result } = yield* dns.listDnsRecords({ type: "A" }); const record = yield* dns.getDnsRecord(result[0].id); return yield* HttpServerResponse.json({ id: record.id }); }), }; }).pipe(Effect.provide(Cloudflare.DnsReadLive)),) {}