The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | apiKeyCredentials, |
| 3 | apiTokenCredentials, |
| 4 | Credentials, |
| 5 | oauthCredentials, |
| 6 | type ResolvedCredentials, |
| 7 | } from "@distilled.cloud/cloudflare/Credentials"; |
| 8 | import { ConfigError } from "@distilled.cloud/core/errors"; |
| 9 | import * as CredentialsCache from "alchemy/Auth/CredentialsCache"; |
| 10 | import { lazyCredentials } from "alchemy/Auth/Resolve"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Layer from "effect/Layer"; |
| 13 | import * as Match from "effect/Match"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | import { |
| 16 | CLOUDFLARE_AUTH_PROVIDER_NAME, |
| 17 | type CloudflareResolvedCredentials, |
| 18 | } from "./Auth/AuthConfig.ts"; |
| 19 | |
| 20 | export { Credentials, fromEnv } from "@distilled.cloud/cloudflare/Credentials"; |
| 21 | |
| 22 | declare module "@distilled.cloud/cloudflare/Credentials" { |
| 23 | interface Credentials { |
| 24 | readonly kind: "Credentials"; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Memoize a credentials-resolution effect until shortly before the resolved |
| 30 | * credentials expire — see {@link CredentialsCache.cacheUntilExpiry} for the |
| 31 | * caching rules. Non-OAuth credentials (API token / global key) never expire |
| 32 | * and cache forever. |
| 33 | */ |
| 34 | export const cacheUntilExpiry = <E>(resolve: Effect.Effect<ResolvedCredentials, E>) => |
| 35 | CredentialsCache.cacheUntilExpiry(resolve, (credentials) => |
| 36 | credentials.type === "oauth" ? credentials.expiresAt : undefined, |
| 37 | ); |
| 38 | |
| 39 | /** OAuth access tokens expire; API tokens and global keys never do. */ |
| 40 | const expiresAt = (creds: CloudflareResolvedCredentials) => |
| 41 | creds.type === "oauth" ? creds.expires : undefined; |
| 42 | |
| 43 | /** |
| 44 | * Build a `Credentials` layer that resolves Cloudflare credentials via the |
| 45 | * Alchemy AuthProvider using the configured profile (defaults to "default", |
| 46 | * selected by the current Alchemy profile). |
| 47 | * |
| 48 | * `auth.read` refreshes and persists expired OAuth tokens when it is re-run, |
| 49 | * so expiry-aware caching (instead of caching the first resolution forever) |
| 50 | * is what keeps long-lived dev sessions authenticated across the ~1h |
| 51 | * access-token lifetime. |
| 52 | */ |
| 53 | export const fromAuthProvider = () => |
| 54 | Layer.effect( |
| 55 | Credentials, |
| 56 | lazyCredentials<CloudflareResolvedCredentials, ConfigError>()( |
| 57 | CLOUDFLARE_AUTH_PROVIDER_NAME, |
| 58 | (creds) => |
| 59 | Match.value(creds).pipe( |
| 60 | Match.when({ type: "apiToken" }, (c) => apiTokenCredentials({ apiToken: c.apiToken })), |
| 61 | Match.when({ type: "apiKey" }, (c) => |
| 62 | apiKeyCredentials({ |
| 63 | apiKey: c.apiKey, |
| 64 | email: Redacted.value(c.email), |
| 65 | }), |
| 66 | ), |
| 67 | Match.when({ type: "oauth" }, (c) => |
| 68 | oauthCredentials({ |
| 69 | accessToken: c.accessToken, |
| 70 | expiresAt: c.expires, |
| 71 | }), |
| 72 | ), |
| 73 | Match.exhaustive, |
| 74 | ), |
| 75 | { expiresAt }, |
| 76 | ), |
| 77 | ); |