The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ConfigError } from "@distilled.cloud/core/errors"; |
| 2 | import { |
| 3 | type Config as PlanetscaleClientConfig, |
| 4 | Credentials, |
| 5 | DEFAULT_API_BASE_URL, |
| 6 | } from "@distilled.cloud/planetscale/Credentials"; |
| 7 | import { lazyCredentials } from "alchemy/Auth/Resolve"; |
| 8 | import * as Config from "effect/Config"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import { |
| 13 | PLANETSCALE_AUTH_PROVIDER_NAME, |
| 14 | type PlanetscaleResolvedCredentials, |
| 15 | } from "./AuthProvider.ts"; |
| 16 | |
| 17 | export { |
| 18 | Credentials, |
| 19 | CredentialsFromEnv, |
| 20 | DEFAULT_API_BASE_URL, |
| 21 | } from "@distilled.cloud/planetscale/Credentials"; |
| 22 | |
| 23 | /** |
| 24 | * Build a PlanetScale `Credentials` Layer from an explicit token. Useful for |
| 25 | * tests or when the caller already has credentials in hand. |
| 26 | * |
| 27 | * @example |
| 28 | * ```ts |
| 29 | * Effect.provide( |
| 30 | * Planetscale.fromToken({ |
| 31 | * tokenId: "abcd1234", |
| 32 | * token: "api-token-secret", |
| 33 | * organization: "my-org", |
| 34 | * }), |
| 35 | * ) |
| 36 | * ``` |
| 37 | */ |
| 38 | export const fromToken = (input: { |
| 39 | tokenId: string | Redacted.Redacted<string>; |
| 40 | token: string | Redacted.Redacted<string>; |
| 41 | organization: string; |
| 42 | apiBaseUrl?: string; |
| 43 | }) => |
| 44 | Layer.succeed( |
| 45 | Credentials, |
| 46 | Effect.succeed({ |
| 47 | tokenId: typeof input.tokenId === "string" ? Redacted.make(input.tokenId) : input.tokenId, |
| 48 | token: typeof input.token === "string" ? Redacted.make(input.token) : input.token, |
| 49 | organization: input.organization, |
| 50 | apiBaseUrl: input.apiBaseUrl ?? DEFAULT_API_BASE_URL, |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * Build a PlanetScale `Credentials` Layer that resolves credentials via the |
| 56 | * Alchemy AuthProvider using the configured profile (defaults to "default", |
| 57 | * selected by the current Alchemy profile). |
| 58 | */ |
| 59 | export const fromAuthProvider = () => |
| 60 | Layer.effect( |
| 61 | Credentials, |
| 62 | Effect.gen(function* () { |
| 63 | const apiBaseUrl = yield* Config.String("PLANETSCALE_API_BASE_URL").pipe( |
| 64 | Config.withDefault(DEFAULT_API_BASE_URL), |
| 65 | ); |
| 66 | // Cache until shortly before the OAuth token expires (service tokens |
| 67 | // never expire) so a long `alchemy dev` session re-resolves — and |
| 68 | // thereby refreshes — the token instead of keeping the first, by then |
| 69 | // dead, resolution forever. |
| 70 | return yield* lazyCredentials<PlanetscaleResolvedCredentials, ConfigError>()( |
| 71 | PLANETSCALE_AUTH_PROVIDER_NAME, |
| 72 | (creds): PlanetscaleClientConfig => |
| 73 | creds.type === "oauth" |
| 74 | ? { |
| 75 | type: "oauth", |
| 76 | accessToken: creds.accessToken, |
| 77 | organization: creds.organization, |
| 78 | apiBaseUrl, |
| 79 | } |
| 80 | : { |
| 81 | type: "serviceToken", |
| 82 | tokenId: creds.tokenId, |
| 83 | token: creds.token, |
| 84 | organization: creds.organization, |
| 85 | apiBaseUrl, |
| 86 | }, |
| 87 | { |
| 88 | expiresAt: (creds) => (creds.type === "oauth" ? creds.expires : undefined), |
| 89 | }, |
| 90 | ); |
| 91 | }), |
| 92 | ); |