The best way to control Claude Code, Codex and more from any computer or phone

main
ts 68 lines 2.14 KB
Raw
1 import { DEFAULT_API_BASE_URL, normalizeApiBaseUrl } from "@distilled.cloud/fly-io";
2 import { getEnv, getEnvRedactedRequired } from "alchemy/Auth/Env";
3 import {
4 makeStoredAuthProvider,
5 storedSecret,
6 storedValueText,
7 type StoredAuthConfig,
8 } from "alchemy/Auth/StoredAuthProvider";
9 import * as Effect from "effect/Effect";
10 import * as Redacted from "effect/Redacted";
11
12 export const FLY_AUTH_PROVIDER_NAME = "Fly";
13 export const FLY_API_TOKEN_ENV = "FLY_API_TOKEN";
14 export const FLY_API_HOSTNAME_ENV = "FLY_API_HOSTNAME";
15
16 export type FlyAuthConfig = StoredAuthConfig;
17
18 export type FlyResolvedCredentials = {
19 type: "token";
20 apiKey: Redacted.Redacted<string>;
21 apiBaseUrl: string;
22 source: { type: FlyAuthConfig["method"] | "env"; details?: string };
23 };
24
25 const resolveApiBaseUrl = (explicit?: string) =>
26 getEnv(FLY_API_HOSTNAME_ENV).pipe(
27 Effect.map((fromEnv) => normalizeApiBaseUrl(explicit ?? fromEnv)),
28 );
29
30 const flyAuth = makeStoredAuthProvider<FlyResolvedCredentials>({
31 provider: FLY_AUTH_PROVIDER_NAME,
32 fields: [
33 { name: "apiKey", label: "Fly.io API Token", secret: true },
34 {
35 name: "apiBaseUrl",
36 label: "Fly API hostname",
37 placeholder: DEFAULT_API_BASE_URL,
38 optional: true,
39 },
40 ],
41 toResolved: (values) => ({
42 type: "token",
43 apiKey: storedSecret(values.apiKey) ?? Redacted.make(""),
44 apiBaseUrl: normalizeApiBaseUrl(storedValueText(values.apiBaseUrl)),
45 source: { type: "stored" },
46 }),
47 readEnvironment: Effect.gen(function* () {
48 const apiKey = yield* getEnvRedactedRequired(FLY_API_TOKEN_ENV);
49 const apiBaseUrl = yield* resolveApiBaseUrl();
50 return {
51 type: "token" as const,
52 apiKey,
53 apiBaseUrl,
54 source: { type: "env" as const, details: FLY_API_TOKEN_ENV },
55 };
56 }),
57 environment: [
58 { name: FLY_API_TOKEN_ENV, required: true, secret: true },
59 { name: FLY_API_HOSTNAME_ENV, required: false },
60 ],
61 });
62
63 /**
64 * Layer that registers the Fly {@link AuthProvider} into the
65 * {@link AuthProviders} registry. Fly uses a stored API token locally and
66 * `FLY_API_TOKEN` in CI; `FLY_API_HOSTNAME` optionally overrides the API root.
67 */
68 export const FlyAuth = flyAuth.layer;