The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as turso from "@distilled.cloud/turso/turso"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { organization } from "./Credentials.ts"; |
| 7 | import type { Providers } from "./Providers.ts"; |
| 8 | |
| 9 | /** Role granted to an invited or added organization member. */ |
| 10 | export type MemberRole = "admin" | "member" | "viewer"; |
| 11 | |
| 12 | export interface OrganizationInviteProps { |
| 13 | /** Email address to invite. Changing it replaces the invite. */ |
| 14 | email: string; |
| 15 | /** |
| 16 | * Role the invitee receives on accepting. |
| 17 | * @default "member" |
| 18 | */ |
| 19 | role?: MemberRole; |
| 20 | } |
| 21 | |
| 22 | export interface OrganizationInviteAttributes { |
| 23 | /** The invited email address. */ |
| 24 | email: string; |
| 25 | /** The organization slug the invite is for. */ |
| 26 | organization: string; |
| 27 | /** Role the invitee receives on accepting. */ |
| 28 | role: string; |
| 29 | } |
| 30 | |
| 31 | export type OrganizationInvite = Resource< |
| 32 | "Turso.OrganizationInvite", |
| 33 | OrganizationInviteProps, |
| 34 | OrganizationInviteAttributes, |
| 35 | never, |
| 36 | Providers |
| 37 | >; |
| 38 | |
| 39 | /** |
| 40 | * An invitation for someone to join your Turso organization by email. |
| 41 | * Changing `role` re-sends the invite with the new role; destroying the |
| 42 | * resource withdraws a pending invite. Requires a team organization on a |
| 43 | * paid plan — the Starter plan rejects invites with `BadRequest`. |
| 44 | * @see https://docs.turso.tech/api-reference/organizations/invites/create |
| 45 | * |
| 46 | * ### Inviting Members |
| 47 | * **Example:** Invite a teammate as an admin |
| 48 | * ```typescript |
| 49 | * yield* Turso.OrganizationInvite("Alice", { |
| 50 | * email: "alice@example.com", |
| 51 | * role: "admin", |
| 52 | * }); |
| 53 | * ``` |
| 54 | * |
| 55 | * @resource |
| 56 | * @product Organization |
| 57 | */ |
| 58 | export const OrganizationInvite = Resource<OrganizationInvite>("Turso.OrganizationInvite"); |
| 59 | |
| 60 | export const OrganizationInviteProvider = () => |
| 61 | Provider.effect( |
| 62 | OrganizationInvite, |
| 63 | Effect.gen(function* () { |
| 64 | const observe = (org: string, email: string) => |
| 65 | turso |
| 66 | .listOrganizationInvitesV2({ organizationSlug: org }) |
| 67 | .pipe( |
| 68 | Effect.map(({ invites }) => |
| 69 | (invites ?? []).find((i) => i.email?.toLowerCase() === email.toLowerCase()), |
| 70 | ), |
| 71 | ); |
| 72 | |
| 73 | const withdraw = (org: string, email: string) => |
| 74 | turso.deleteOrganizationInviteByEmailV2({ organizationSlug: org, email }).pipe( |
| 75 | Effect.asVoid, |
| 76 | Effect.catchTag("NotFound", () => Effect.void), |
| 77 | ); |
| 78 | |
| 79 | return { |
| 80 | stables: ["email", "organization"], |
| 81 | diff: Effect.fn(function* ({ olds, news }) { |
| 82 | if (!isResolved(news)) return undefined; |
| 83 | if (news.email.toLowerCase() !== olds?.email.toLowerCase()) { |
| 84 | return { action: "replace" } as const; |
| 85 | } |
| 86 | }), |
| 87 | read: Effect.fn(function* ({ olds, output }) { |
| 88 | const org = output?.organization ?? (yield* organization); |
| 89 | const invite = yield* observe(org, output?.email ?? olds.email); |
| 90 | if (!invite?.email) return undefined; |
| 91 | return { email: invite.email, organization: org, role: invite.role ?? "member" }; |
| 92 | }), |
| 93 | reconcile: Effect.fn(function* ({ news, output }) { |
| 94 | const org = output?.organization ?? (yield* organization); |
| 95 | const role = news.role ?? "member"; |
| 96 | |
| 97 | // Observe |
| 98 | const invite = yield* observe(org, news.email); |
| 99 | |
| 100 | // Ensure / sync — invites have no update API, so a role change |
| 101 | // withdraws and re-sends. |
| 102 | if (invite?.role === role) { |
| 103 | return { email: invite.email ?? news.email, organization: org, role }; |
| 104 | } |
| 105 | if (invite) yield* withdraw(org, news.email); |
| 106 | const { invited } = yield* turso.inviteOrganizationMemberV2({ |
| 107 | organizationSlug: org, |
| 108 | email: news.email, |
| 109 | role, |
| 110 | }); |
| 111 | return { email: invited?.email ?? news.email, organization: org, role }; |
| 112 | }), |
| 113 | delete: Effect.fn(function* ({ output }) { |
| 114 | yield* withdraw(output.organization, output.email); |
| 115 | }), |
| 116 | }; |
| 117 | }), |
| 118 | ); |