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

main
ts 127 lines 3.81 KB
Raw
1 import * as turso from "@distilled.cloud/turso/turso";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import { organization } from "./Credentials.ts";
7 import {
8 diffSqlToken,
9 isTokenCurrent,
10 tokenExpiry,
11 type SqlTokenAttributes,
12 type SqlTokenProps,
13 } from "./DatabaseToken.ts";
14 import type { Providers } from "./Providers.ts";
15
16 export class GroupTokenUnexpectedResponse extends Data.TaggedError("GroupTokenUnexpectedResponse")<{
17 readonly message: string;
18 readonly cause?: unknown;
19 }> {}
20
21 export interface GroupTokenProps extends SqlTokenProps {
22 /** Name of the group whose databases the token grants access to. */
23 group: string;
24 }
25
26 export interface GroupTokenAttributes extends SqlTokenAttributes {
27 /** Name of the group the token grants access to. */
28 group: string;
29 /** UUID of the group the token was minted for. */
30 groupId: string;
31 }
32
33 export type GroupToken = Resource<
34 "Turso.GroupToken",
35 GroupTokenProps,
36 GroupTokenAttributes,
37 never,
38 Providers
39 >;
40
41 /**
42 * A token that lets an app run SQL on every database in a Turso
43 * {@link Group}, even databases created later. Useful when each customer
44 * has their own database.
45 *
46 * Like {@link DatabaseToken}, you can't cancel a single group token. Changing
47 * a prop creates a new token, but the old one keeps working until it expires.
48 * @see https://docs.turso.tech/api-reference/groups/create-token
49 *
50 * ### Creating a Token
51 * **Example:** Full-access token for every database in a group
52 * ```typescript
53 * const token = yield* Turso.GroupToken("Token", {
54 * group: group.name,
55 * });
56 * ```
57 *
58 * **Example:** Read-only token that expires after a week
59 * ```typescript
60 * const token = yield* Turso.GroupToken("ReadOnly", {
61 * group: group.name,
62 * authorization: "read-only",
63 * expiration: "7d",
64 * });
65 * ```
66 *
67 * @resource
68 * @product Group
69 */
70 export const GroupToken = Resource<GroupToken>("Turso.GroupToken");
71
72 export const GroupTokenProvider = () =>
73 Provider.effect(
74 GroupToken,
75 Effect.gen(function* () {
76 return {
77 stables: ["organization"],
78 diff: diffSqlToken,
79 reconcile: Effect.fn(function* ({ news, olds, output }) {
80 const org = output?.organization ?? (yield* organization);
81
82 // Observe — the token is bound to the group's UUID, so a group
83 // replaced under the same name invalidates it.
84 const { group } = yield* turso.getGroup({
85 organizationSlug: org,
86 groupName: news.group,
87 });
88 const groupId = group?.uuid ?? "";
89 if (
90 output !== undefined &&
91 output.group === news.group &&
92 output.groupId === groupId &&
93 (yield* isTokenCurrent(output, olds, news))
94 ) {
95 return output;
96 }
97
98 // Ensure — mint.
99 const authorization = news.authorization ?? "full-access";
100 const { jwt } = yield* turso.createGroupToken({
101 organizationSlug: org,
102 groupName: news.group,
103 authorization,
104 expiration: news.expiration,
105 permissions: news.readAttach
106 ? { read_attach: { databases: news.readAttach } }
107 : undefined,
108 });
109 if (!jwt) {
110 return yield* new GroupTokenUnexpectedResponse({
111 message: `Turso returned no token for group ${news.group}`,
112 });
113 }
114 return {
115 token: jwt,
116 organization: org,
117 authorization,
118 expiresAt: tokenExpiry(jwt),
119 group: news.group,
120 groupId,
121 };
122 }),
123 // Turso cannot revoke a single token; it simply stops being tracked.
124 delete: () => Effect.void,
125 };
126 }),
127 );