The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import type { Account } from "./Account.ts"; |
| 5 | import { |
| 6 | accountLayer, |
| 7 | issueCertificate, |
| 8 | revokeCertificate, |
| 9 | type AccountCredentials, |
| 10 | } from "./Client.ts"; |
| 11 | import { IssueCertificate, type IssueCertificateClient } from "./IssueCertificate.ts"; |
| 12 | |
| 13 | /** |
| 14 | * Implementation of {@link IssueCertificate}: the account's directory |
| 15 | * URL, account URL and private key are bound into the host at deploy time |
| 16 | * and read back at runtime; every call signs with them over HTTPS. |
| 17 | * |
| 18 | * Provide it on the Worker / Service / Action Effect. |
| 19 | * |
| 20 | * ### Provide the layer |
| 21 | * **Example:** On a Fly Service |
| 22 | * ```typescript |
| 23 | * Effect.gen(function* () { |
| 24 | * const acme = yield* ACME.IssueCertificate(LetsEncrypt); |
| 25 | * // ... |
| 26 | * }).pipe(Effect.provide(ACME.IssueCertificateHttp)) |
| 27 | * ``` |
| 28 | * |
| 29 | * @layer |
| 30 | * @product Certificate |
| 31 | * @provides ACME.IssueCertificate |
| 32 | */ |
| 33 | export const IssueCertificateHttp = Layer.succeed( |
| 34 | IssueCertificate, |
| 35 | Effect.fn(function* (account: Account) { |
| 36 | const directoryUrl = yield* account.directoryUrl; |
| 37 | const accountUrl = yield* account.accountUrl; |
| 38 | const trustedRoot = yield* account.trustedRoot.pipe(Output.map((root) => root ?? "")); |
| 39 | const privateKey = yield* account.privateKey; |
| 40 | const credentials = Effect.all({ |
| 41 | directoryUrl, |
| 42 | trustedRoot, |
| 43 | accountUrl, |
| 44 | privateKey, |
| 45 | }).pipe( |
| 46 | Effect.map((bound): AccountCredentials => ({ |
| 47 | ca: { |
| 48 | directoryUrl: bound.directoryUrl, |
| 49 | trustedRoot: bound.trustedRoot || undefined, |
| 50 | }, |
| 51 | accountKey: bound.privateKey, |
| 52 | accountUrl: bound.accountUrl, |
| 53 | })), |
| 54 | ); |
| 55 | const client: IssueCertificateClient = { |
| 56 | issue: Effect.fn("ACME.IssueCertificate.issue")(function* (request) { |
| 57 | const creds = yield* credentials; |
| 58 | return yield* issueCertificate(request).pipe(Effect.provide(accountLayer(creds))); |
| 59 | }), |
| 60 | revoke: Effect.fn("ACME.IssueCertificate.revoke")(function* (request) { |
| 61 | const creds = yield* credentials; |
| 62 | return yield* revokeCertificate(request).pipe(Effect.provide(accountLayer(creds))); |
| 63 | }), |
| 64 | }; |
| 65 | return client; |
| 66 | }), |
| 67 | ); |