| 1 | import * as zoneRrsets from "@distilled.cloud/hetzner/zone_rrsets"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import { type DnsAuth, makeHttpDnsBinding } from "./DnsHttp.ts"; |
| 5 | import { ReadDns, type ReadDnsClient } from "./ReadDns.ts"; |
| 6 | |
| 7 | /** Runtime layer for {@link ReadDns}. */ |
| 8 | export const ReadDnsHttp = Layer.effect( |
| 9 | ReadDns, |
| 10 | Effect.suspend(() => |
| 11 | makeHttpDnsBinding({ |
| 12 | makeClient: dnsReadClient, |
| 13 | }), |
| 14 | ), |
| 15 | ); |
| 16 | |
| 17 | /** Build the read-only client over an injectable auth and zone id. */ |
| 18 | export const dnsReadClient = ( |
| 19 | auth: DnsAuth, |
| 20 | zoneId: Effect.Effect<number>, |
| 21 | ): ReadDnsClient => { |
| 22 | const authorize = auth.authorize; |
| 23 | return { |
| 24 | getRecordSet: Effect.fn("Hetzner.DNS.getRecordSet")(function* (name, type) { |
| 25 | return yield* authorize( |
| 26 | zoneRrsets.getZoneRrset({ |
| 27 | id_or_name: String(yield* zoneId), |
| 28 | rr_name: name, |
| 29 | rr_type: type, |
| 30 | }), |
| 31 | ); |
| 32 | }), |
| 33 | listRecordSets: Effect.fn("Hetzner.DNS.listRecordSets")( |
| 34 | function* (request) { |
| 35 | return yield* authorize( |
| 36 | zoneRrsets.listZoneRrsets({ |
| 37 | id_or_name: String(yield* zoneId), |
| 38 | ...request, |
| 39 | }), |
| 40 | ); |
| 41 | }, |
| 42 | ), |
| 43 | }; |
| 44 | }; |