| 1 | import * as Cloudflare from "@/Cloudflare/index.ts"; |
| 2 | import { remote } from "@/ProviderMode.ts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | |
| 7 | const senderAddress = process.env.CLOUDFLARE_TEST_EMAIL_FROM; |
| 8 | const destinationAddress = process.env.CLOUDFLARE_TEST_EMAIL_TO; |
| 9 | |
| 10 | /** |
| 11 | * `send_email` descriptor opted into the REAL binding under `alchemy dev` |
| 12 | * via `Alchemy.remote()`: the local worker proxies `send()` through the |
| 13 | * remote-bindings preview session to Cloudflare's live Email service, so it |
| 14 | * needs a verified sender/destination pair from the env. |
| 15 | */ |
| 16 | export const RemoteEmail = Cloudflare.Email.SendEmail("RemoteEmail", { |
| 17 | allowedSenderAddresses: senderAddress ? [senderAddress] : undefined, |
| 18 | destinationAddress, |
| 19 | }).pipe(remote()); |
| 20 | |
| 21 | export default class RemoteEmailWorker extends Cloudflare.Worker<RemoteEmailWorker>()( |
| 22 | "SendEmailRemoteDevTestWorker", |
| 23 | { |
| 24 | main: import.meta.url, |
| 25 | }, |
| 26 | Effect.gen(function* () { |
| 27 | const email = yield* Cloudflare.Email.Send(RemoteEmail); |
| 28 | |
| 29 | return { |
| 30 | fetch: Effect.gen(function* () { |
| 31 | const request = yield* HttpServerRequest; |
| 32 | const url = new URL(request.url, "http://x"); |
| 33 | |
| 34 | if (url.pathname === "/send") { |
| 35 | const from = url.searchParams.get("from")!; |
| 36 | const to = url.searchParams.get("to")!; |
| 37 | const result = yield* email |
| 38 | .send({ |
| 39 | from, |
| 40 | to, |
| 41 | subject: "alchemy dev remote send_email test", |
| 42 | text: `sent at ${new Date().toISOString()}`, |
| 43 | }) |
| 44 | .pipe( |
| 45 | Effect.match({ |
| 46 | onSuccess: () => ({ ok: true as const }), |
| 47 | onFailure: (err) => ({ |
| 48 | ok: false as const, |
| 49 | message: err.message, |
| 50 | }), |
| 51 | }), |
| 52 | ); |
| 53 | return yield* HttpServerResponse.json(result); |
| 54 | } |
| 55 | |
| 56 | return HttpServerResponse.text("ok"); |
| 57 | }), |
| 58 | }; |
| 59 | }).pipe(Effect.provide(Cloudflare.Email.SendBinding)), |
| 60 | ) {} |