sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Cloudflare/ Dns/ fixtures/ effect.ts

import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Zone } from "./zone.ts";
/**
 * Effect-native Worker fixture that exercises the {@link Cloudflare.DNS.ReadWriteDns}
 * binding (full DNS record CRUD).
 *
 * Binding `DnsReadWrite` in the Init phase provisions a scoped
 * {@link Cloudflare.ApiToken.AccountApiToken} (with `DNS Read` + `DNS Write`, limited to
 * the bound zone) and binds its value plus the zone id into the Worker. The
 * `/dns` route then drives a self-contained create → get → list → update →
 * delete scenario against that zone.
 */
export default class DnsEffectWorker extends Cloudflare.Worker<DnsEffectWorker>()(
  "DnsEffectWorker",
  {
    main: import.meta.url,
  },
  Effect.gen(function* () {
    const dns = yield* Cloudflare.DNS.ReadWriteDns(Zone);

    return {
      fetch: Effect.gen(function* () {
        const request = yield* HttpServerRequest;
        const url = new URL(request.originalUrl);

        if (url.pathname === "/dns") {
          const name = url.searchParams.get("name")!;

          return yield* Effect.gen(function* () {
            // Tests use a deterministic record name — purge any leftover
            // record from a previously crashed run before creating.
            const leftovers = yield* dns.listDnsRecords({
              type: "A",
              name: { exact: name },
            });
            yield* Effect.forEach(
              (leftovers.result ?? []).filter((r) => r.name === name),
              (r) => dns.deleteDnsRecord(r.id),
            );

            const created = yield* dns.createDnsRecord({
              type: "A",
              name,
              content: "192.0.2.1",
              ttl: 1,
            });
            const id = created.id;

            const got = yield* dns.getDnsRecord(id);
            const list = yield* dns.listDnsRecords({ type: "A" });
            const updated = yield* dns.updateDnsRecord(id, {
              type: "A",
              name,
              content: "192.0.2.2",
              ttl: 1,
            });
            yield* dns.deleteDnsRecord(id);

            return yield* HttpServerResponse.json({
              id,
              getName: got.name,
              count: list.result?.length,
              updatedId: updated.id,
              deleted: true,
            });
          }).pipe(
            Effect.catchCause((cause) =>
              HttpServerResponse.json({ error: Cause.pretty(cause) }, { status: 500 }),
            ),
          );
        }

        return HttpServerResponse.text("ok");
      }),
    };
  }).pipe(Effect.provide(Cloudflare.DNS.ReadWriteDnsHttp)),
) {}