Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

announce-connect-ga.ts

226 lines6.5 KB

1#!/usr/bin/env node
2​
3import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import * as Config from "effect/Config";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as Logger from "effect/Logger";
9import * as Option from "effect/Option";
10import * as Schema from "effect/Schema";
11import { Command, Flag } from "effect/unstable/cli";
12import {
13 FetchHttpClient,
14 HttpClient,
15 HttpClientRequest,
16 HttpClientResponse,
17} from "effect/unstable/http";
18​
19const CLERK_API_URL = "https://api.clerk.com/v1";
20const PAGE_SIZE = 500;
21​
22export class WaitlistEntry extends Schema.Class<WaitlistEntry>("WaitlistEntry")({
23 id: Schema.String,
24 email_address: Schema.String,
25 status: Schema.Literals(["pending", "invited", "completed", "rejected"]),
26}) {}
27​
28const ClerkWaitlistResponse = Schema.Struct({
29 data: Schema.Array(WaitlistEntry),
30 total_count: Schema.Int,
31});
32const PositiveInteger = Schema.Int.check(Schema.isGreaterThan(0));
33const ClerkSecretKey = Config.string("CLERK_SECRET_KEY");
34​
35export interface ConnectGaOptions {
36 readonly invite: boolean;
37 readonly limit: number | undefined;
39​
40export class ConnectGaRequestError extends Schema.TaggedErrorClass<ConnectGaRequestError>()(
41 "ConnectGaRequestError",
42 {
43 operation: Schema.String,
44 cause: Schema.Defect(),
45 },
46) {
47 override get message(): string {
48 return `Clerk ${this.operation} request failed.`;
49 }
51​
52export class ConnectGaResponseError extends Schema.TaggedErrorClass<ConnectGaResponseError>()(
53 "ConnectGaResponseError",
54 {
55 operation: Schema.String,
56 status: Schema.Int,
57 cause: Schema.Defect(),
58 },
59) {
60 override get message(): string {
61 return `Clerk ${this.operation} returned status ${this.status}.`;
62 }
64​
65const executeClerkJsonRequest = Effect.fn("executeClerkJsonRequest")(function* <
66 S extends Schema.Top,
67>(request: HttpClientRequest.HttpClientRequest, schema: S, operation: string) {
68 const client = (yield* HttpClient.HttpClient).pipe(
69 HttpClient.retryTransient({
70 retryOn: "errors-and-responses",
71 times: 3,
72 }),
73 );
74 const response = yield* client
75 .execute(request)
76 .pipe(Effect.mapError((cause) => new ConnectGaRequestError({ operation, cause })));
77 const success = yield* HttpClientResponse.filterStatusOk(response).pipe(
78 Effect.mapError(
79 (cause) =>
80 new ConnectGaResponseError({
81 operation,
82 status: response.status,
83 cause,
84 }),
85 ),
86 );
87 return yield* HttpClientResponse.schemaBodyJson(schema)(success).pipe(
88 Effect.mapError(
89 (cause) =>
90 new ConnectGaResponseError({
91 operation,
92 status: response.status,
93 cause,
94 }),
95 ),
96 );
97});
98​
99const fetchWaitlistPage = Effect.fn("fetchWaitlistPage")(function* (
100 secretKey: string,
101 offset: number,
102) {
103 const url = new URL(`${CLERK_API_URL}/waitlist_entries`);
104 url.searchParams.set("status", "pending");
105 url.searchParams.set("limit", String(PAGE_SIZE));
106 url.searchParams.set("offset", String(offset));
107 url.searchParams.set("order_by", "+created_at");
108 const request = HttpClientRequest.get(url.href).pipe(
109 HttpClientRequest.bearerToken(secretKey),
110 HttpClientRequest.setHeader("Clerk-API-Version", "2026-05-12"),
111 );
112 return yield* executeClerkJsonRequest(
113 request,
114 ClerkWaitlistResponse,
115 "list pending waitlist entries",
116 );
117});
118​
119export const fetchPendingWaitlistEntries = Effect.fn("fetchPendingWaitlistEntries")(function* (
120 secretKey: string,
121 limit?: number,
122) {
123 const entries: Array<WaitlistEntry> = [];
124 while (true) {
125 if (limit !== undefined && entries.length >= limit) break;
126 const page = yield* fetchWaitlistPage(secretKey, entries.length);
127 entries.push(...page.data);
128 if (entries.length >= page.total_count || page.data.length === 0) break;
129 }
130 return limit === undefined ? entries : entries.slice(0, limit);
131});
132​
133export const inviteWaitlistEntry = Effect.fn("inviteWaitlistEntry")(function* (
134 secretKey: string,
135 entry: WaitlistEntry,
136) {
137 const request = HttpClientRequest.post(
138 `${CLERK_API_URL}/waitlist_entries/${encodeURIComponent(entry.id)}/invite`,
139 ).pipe(
140 HttpClientRequest.bearerToken(secretKey),
141 HttpClientRequest.setHeader("Clerk-API-Version", "2026-05-12"),
142 );
143 return yield* executeClerkJsonRequest(
144 request,
145 WaitlistEntry,
146 `invite waitlist entry ${entry.id}`,
147 );
148});
149​
150export const announceConnectGa = Effect.fn("announceConnectGa")(function* (
151 options: ConnectGaOptions,
152) {
153 const clerkSecretKey = yield* ClerkSecretKey;
154 const entries = yield* fetchPendingWaitlistEntries(clerkSecretKey, options.limit);
155​
156 yield* Effect.logInfo(
157 options.invite ? "Connect GA waitlist invitations starting" : "Connect GA dry run",
158 ).pipe(
159 Effect.annotateLogs({
160 pendingEntries: entries.length,
161 }),
162 );
163​
164 if (!options.invite) {
165 for (const entry of entries) {
166 yield* Effect.logInfo("pending waitlist entry").pipe(
167 Effect.annotateLogs({
168 waitlistEntryId: entry.id,
169 emailAddress: entry.email_address,
170 }),
171 );
172 }
173 yield* Effect.logInfo("No invitation was sent. Re-run with --invite after reviewing the list.");
174 return;
175 }
176​
177 for (const [index, entry] of entries.entries()) {
178 const invited = yield* inviteWaitlistEntry(clerkSecretKey, entry);
179 yield* Effect.logInfo("Clerk waitlist invitation sent").pipe(
180 Effect.annotateLogs({
181 waitlistEntryId: invited.id,
182 completed: index + 1,
183 total: entries.length,
184 }),
185 );
186 }
187});
188​
189export const announceConnectGaCommand = Command.make(
190 "announce-connect-ga",
191 {
192 invite: Flag.boolean("invite").pipe(
193 Flag.withDefault(false),
194 Flag.withDescription(
195 "Invite pending entries through Clerk. Without this flag, only print a dry-run list.",
196 ),
197 ),
198 limit: Flag.integer("limit").pipe(
199 Flag.withSchema(PositiveInteger),
200 Flag.optional,
201 Flag.withDescription("Process at most this many pending waitlist entries."),
202 ),
203 },
204 ({ invite, limit }) =>
205 announceConnectGa({
206 invite,
207 limit: Option.getOrUndefined(limit),
208 }),
209).pipe(
210 Command.withDescription(
211 "Invite pending Clerk waitlist members now that T3 Connect is generally available.",
212 ),
213);
214​
215if (import.meta.main) {
216 Command.run(announceConnectGaCommand, { version: "0.0.0" }).pipe(
217 Effect.provide(
218 Layer.mergeAll(
219 Logger.layer([Logger.consolePretty()]),
220 NodeServices.layer,
221 FetchHttpClient.layer,
222 ),
223 ),
224 NodeRuntime.runMain,
225 );