@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 113 lines 3.71 KB
Raw
1 import { DEFAULT_API_BASE_URL } from "@distilled.cloud/turso/Credentials";
2 import { AuthError } from "alchemy/Auth/AuthProvider";
3 import { getEnv, getEnvRedacted } from "alchemy/Auth/Env";
4 import {
5 makeStoredAuthProvider,
6 storedSecret,
7 storedValueText,
8 type StoredAuthConfig,
9 } from "alchemy/Auth/StoredAuthProvider";
10 import * as Effect from "effect/Effect";
11 import * as Redacted from "effect/Redacted";
12
13 /**
14 * Canonical name registered in the AuthProviders registry. Use this key to
15 * look up the Turso AuthProvider from inside provider Layers.
16 */
17 export const TURSO_AUTH_PROVIDER_NAME = "Turso";
18
19 export type TursoAuthConfig = StoredAuthConfig;
20
21 /**
22 * Resolved Turso Platform API credentials.
23 *
24 * Turso has no OAuth application flow — the Platform API authenticates
25 * with API tokens only (`turso auth api-tokens mint <name>`). The
26 * organization is optional: when omitted it is discovered from the token
27 * (an organization-scoped token, or an account with a single organization).
28 */
29 export interface TursoResolvedCredentials {
30 type: "apiToken";
31 apiToken: Redacted.Redacted<string>;
32 apiBaseUrl: string;
33 organization: string | undefined;
34 source: { type: TursoAuthConfig["method"] | "env"; details?: string };
35 }
36
37 const readEnvironment = Effect.gen(function* () {
38 const apiToken = yield* getEnvRedacted("TURSO_API_TOKEN");
39 const apiKey = yield* getEnvRedacted("TURSO_API_KEY");
40 const token = apiToken ?? apiKey;
41 if (token === undefined || Redacted.value(token).trim().length === 0) {
42 return yield* AuthError.make({
43 message: "Turso CI credentials not found. Set TURSO_API_TOKEN or TURSO_API_KEY.",
44 });
45 }
46 return {
47 type: "apiToken" as const,
48 apiToken: Redacted.make(Redacted.value(token).trim()),
49 apiBaseUrl: (yield* getEnv("TURSO_API_BASE_URL")) ?? DEFAULT_API_BASE_URL,
50 organization: yield* getEnv("TURSO_ORGANIZATION"),
51 source: {
52 type: "env" as const,
53 details: apiToken === undefined ? "TURSO_API_KEY" : "TURSO_API_TOKEN",
54 },
55 } satisfies TursoResolvedCredentials;
56 });
57
58 const tursoAuth = makeStoredAuthProvider<TursoResolvedCredentials>({
59 provider: TURSO_AUTH_PROVIDER_NAME,
60 fields: [
61 {
62 name: "apiToken",
63 label: "Turso API Token",
64 description: "Mint one with `turso auth api-tokens mint alchemy`.",
65 secret: true,
66 validate: (value) => (value.trim().length === 0 ? "Required" : undefined),
67 },
68 {
69 name: "organization",
70 label: "Turso Organization (slug)",
71 description: "Defaults to the token's organization when it has exactly one.",
72 optional: true,
73 },
74 ],
75 toResolved: (values, source) => ({
76 type: "apiToken",
77 apiToken: storedSecret(values.apiToken) ?? Redacted.make(""),
78 apiBaseUrl: DEFAULT_API_BASE_URL,
79 organization: storedValueText(values.organization),
80 source: { type: source },
81 }),
82 readEnvironment,
83 environment: [
84 {
85 name: "TURSO_API_TOKEN",
86 required: true,
87 secret: true,
88 alternatives: ["TURSO_API_KEY"],
89 description: "Turso Platform API token.",
90 },
91 {
92 name: "TURSO_ORGANIZATION",
93 required: false,
94 description: "Organization slug; defaults to the token's only organization.",
95 },
96 {
97 name: "TURSO_API_BASE_URL",
98 required: false,
99 description: "Platform API base URL override.",
100 },
101 ],
102 });
103
104 /**
105 * Layer that registers the Turso AuthProvider into the AuthProviders
106 * registry when built. Included in `Turso.providers()` so the alchemy CLI
107 * (`alchemy profile edit`) can configure it.
108 */
109 export const TursoAuth = tursoAuth.layer;
110
111 /** Schema of Turso's inline API-token values. */
112 export const TursoStoredCredentials = tursoAuth.storedSchema;
113 export type TursoStoredCredentials = typeof TursoStoredCredentials.Type;