t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/Cloudflare/Dns/fixtures/effect.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

84 lines
2.8 KiB
TypeScript

import * as Cloudflare from "@/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)),
) {}