@samebase/alchemy-convex 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -80,13 +80,6 @@ export interface ManagementApiService {
80
80
  * Convex CLI selected (CONVEX_DEPLOYMENT). Read once for each service.
81
81
  */
82
82
  readonly teamId: Effect.Effect<number, ConvexApiError>;
83
- /**
84
- * Team id for a numeric id or a slug, for Convex.HubClient, which names the
85
- * team of the hub. With a team access token, a slug is checked against the
86
- * `teamSlug` of the team's projects, so a token of another team fails; with
87
- * the Convex CLI login, a slug goes to the dashboard's team list.
88
- */
89
- readonly resolveTeamId: (team: string | number) => Effect.Effect<number, ConvexApiError>;
90
83
  /**
91
84
  * True when a deploy key that Convex returned carries the token of the
92
85
  * Management API credential itself. With an OAuth token, create_deploy_key
@@ -199,14 +192,6 @@ export const absentAsUndefined = <A, R>(
199
192
  ),
200
193
  );
201
194
 
202
- const TeamRow = Schema.Struct({ id: Schema.Number, slug: Schema.String });
203
- const TeamRows = Schema.Array(TeamRow);
204
-
205
- /** The first page of GET /teams/{team_id}/projects: every row carries the slug of the team. */
206
- const ProjectSlugRows = Schema.Struct({
207
- items: Schema.Array(Schema.Struct({ teamSlug: Schema.String })),
208
- });
209
-
210
195
  /** GET /deployments/{name}: the project of the deployment. */
211
196
  const DeploymentProject = Schema.Struct({ projectId: Schema.Number });
212
197
 
@@ -346,27 +331,18 @@ const makeService = (source: Effect.Effect<ResolvedCredentials>): ManagementApiS
346
331
  ),
347
332
  );
348
333
 
349
- /** The slug of the team, from the first project of its listing, or undefined for a team without one. Read once. */
350
- const teamSlug = (teamId: number) =>
351
- request("list projects", (c) =>
352
- c.GET("/teams/{team_id}/projects", { params: { path: { team_id: teamId } } }),
353
- ).pipe(
354
- Effect.map((body) => Schema.decodeUnknownSync(ProjectSlugRows)(body).items[0]?.teamSlug),
355
- );
356
-
357
- const projectTokenError = (projectId: number) =>
358
- new ConvexApiError({
359
- operation: "resolve team",
360
- status: 403,
361
- message: `the access token belongs to project ${projectId}, and a project token cannot create projects. Use a team access token.`,
362
- });
363
-
364
334
  const teamId: ManagementApiService["teamId"] = Effect.runSync(
365
335
  Effect.cached(
366
336
  Effect.gen(function* () {
367
337
  const details = yield* tokenDetails;
368
338
  if (details?.type === "teamToken") return details.teamId;
369
- if (details?.type === "projectToken") return yield* projectTokenError(details.projectId);
339
+ if (details?.type === "projectToken") {
340
+ return yield* new ConvexApiError({
341
+ operation: "resolve team",
342
+ status: 403,
343
+ message: `the access token belongs to project ${details.projectId}, and a project token cannot create projects. Use a team access token.`,
344
+ });
345
+ }
370
346
  // The Convex CLI login names no team, but the deployment that `npx convex dev` selected
371
347
  // belongs to a project, and the project to a team.
372
348
  const selected = selectedDeployment();
@@ -396,65 +372,6 @@ const makeService = (source: Effect.Effect<ResolvedCredentials>): ManagementApiS
396
372
  ),
397
373
  );
398
374
 
399
- // The Management API addresses teams by numeric id. The only slug lookup
400
- // it offers is per project, so a slug falls back to the dashboard's team
401
- // list, which is unofficial. Pass the numeric team id to skip this.
402
- const resolveTeamId: ManagementApiService["resolveTeamId"] = (team) =>
403
- Effect.gen(function* () {
404
- if (typeof team === "number") return team;
405
- if (/^\d+$/.test(team)) return Number(team);
406
- const details = yield* tokenDetails;
407
- if (details?.type === "teamToken") {
408
- // The Management API has no team-by-id call, so the slug of the
409
- // team comes from any of its projects. A team without a project
410
- // cannot be checked: the numeric id, or no team, is the fix.
411
- const slug = yield* teamSlug(details.teamId);
412
- if (slug === undefined) {
413
- return yield* new ConvexApiError({
414
- operation: "resolve team",
415
- status: 0,
416
- message: `team "${team}" cannot be checked against the access token: team ${details.teamId} has no project yet. Set team to ${details.teamId}.`,
417
- });
418
- }
419
- if (slug !== team) {
420
- return yield* new ConvexApiError({
421
- operation: "resolve team",
422
- status: 403,
423
- message: `the access token belongs to team ${details.teamId} ("${slug}"), and team is "${team}". Use a token of "${team}", or set team to ${details.teamId}.`,
424
- });
425
- }
426
- return details.teamId;
427
- }
428
- if (details?.type === "projectToken") return yield* projectTokenError(details.projectId);
429
- const resolved = yield* credentials;
430
- const rows = yield* Effect.tryPromise({
431
- try: async () => {
432
- const response = await fetch("https://api.convex.dev/api/dashboard/teams", {
433
- headers: { Authorization: `Bearer ${Redacted.value(resolved.accessToken)}` },
434
- });
435
- if (!response.ok) {
436
- throw new Error(`team lookup for "${team}" failed with HTTP ${response.status}`);
437
- }
438
- return Schema.decodeUnknownSync(TeamRows)(await response.json());
439
- },
440
- catch: (cause) =>
441
- new ConvexApiError({
442
- operation: "resolve team",
443
- status: 0,
444
- message: cause instanceof Error ? cause.message : String(cause),
445
- }),
446
- });
447
- const match = rows.find((row) => row.slug === team);
448
- if (match === undefined) {
449
- return yield* new ConvexApiError({
450
- operation: "resolve team",
451
- status: 404,
452
- message: `no team with slug "${team}" for this token`,
453
- });
454
- }
455
- return match.id;
456
- });
457
-
458
375
  const isCredentialToken: ManagementApiService["isCredentialToken"] = (secret) =>
459
376
  credentials.pipe(
460
377
  Effect.map(
@@ -474,7 +391,6 @@ const makeService = (source: Effect.Effect<ResolvedCredentials>): ManagementApiS
474
391
  requestVoid,
475
392
  deploymentRequest,
476
393
  teamId,
477
- resolveTeamId,
478
394
  isCredentialToken,
479
395
  isOAuthCredential,
480
396
  withToken,
package/src/Providers.ts CHANGED
@@ -4,7 +4,6 @@ import * as Provider from "alchemy/Provider";
4
4
  import * as Layer from "effect/Layer";
5
5
  import { type Credentials, fromConfig } from "./Credentials.ts";
6
6
  import { DeploymentProvider, DeploymentResource } from "./Deployment.ts";
7
- import { HubClient, HubClientProvider } from "./HubClient.ts";
8
7
  import { LocalDeployment, LocalDeploymentProvider } from "./LocalDeployment.ts";
9
8
  import { ManagementApiLive } from "./ManagementApi.ts";
10
9
  import { Project, ProjectProvider } from "./Project.ts";
@@ -20,14 +19,13 @@ export class Providers extends Provider.ProviderCollection<Providers>()("Convex"
20
19
  export const providers = (credentials: Layer.Layer<Credentials> = fromConfig()) =>
21
20
  Layer.effect(
22
21
  Providers,
23
- Provider.collection([Project, DeploymentResource, LocalDeployment, HubClient, SharedHubClient]),
22
+ Provider.collection([Project, DeploymentResource, LocalDeployment, SharedHubClient]),
24
23
  ).pipe(
25
24
  Layer.provide(
26
25
  Layer.mergeAll(
27
26
  ProjectProvider(),
28
27
  DeploymentProvider(),
29
28
  LocalDeploymentProvider(),
30
- HubClientProvider(),
31
29
  SharedHubClientProvider(),
32
30
  ),
33
31
  ),
package/src/cli.ts CHANGED
@@ -1,82 +1,33 @@
1
1
  #!/usr/bin/env node
2
- // The commands of the package:
2
+ // The command of the package:
3
3
  //
4
- // alchemy-convex hub-client --team <team> --app <app> --callback-path <path> [--env-prefix HUB]
5
4
  // alchemy-convex stack-comment
6
5
  //
7
- // hub-client runs in the directory of a Convex app at each dev start, and renews the client of the
8
- // identity hub of the team that the development deployment of that directory has, or gives it a new
9
- // one: see registerDevelopmentClient. stack-comment runs as a step of a pull request workflow after
10
- // the deploys, and keeps one comment with the stacks of the repository: see src/StackComment.ts. It
11
- // needs GITHUB_TOKEN, and reads the store of the team of the token. Both use CONVEX_ACCESS_TOKEN or
12
- // the Convex CLI login.
13
- import { execFileSync } from "node:child_process";
6
+ // stack-comment runs as a step of a pull request workflow after the deploys, and keeps one comment
7
+ // with the stacks of the repository: see src/StackComment.ts. It needs GITHUB_TOKEN, and reads the
8
+ // store of the team of CONVEX_ACCESS_TOKEN or of the Convex CLI login.
14
9
  import { parseArgs } from "node:util";
15
10
  import * as NodeServices from "@effect/platform-node/NodeServices";
16
11
  import * as Effect from "effect/Effect";
17
12
  import * as Layer from "effect/Layer";
18
- import { fromConfig } from "./Credentials.ts";
19
- import { registerDevelopmentClient } from "./HubClient.ts";
20
- import { ManagementApiLive } from "./ManagementApi.ts";
21
- import { installedCli } from "./Push.ts";
22
13
  import { updateStackComment } from "./StackComment.ts";
23
14
  import { state } from "./State.ts";
24
15
 
25
- const { positionals, values } = parseArgs({
26
- allowPositionals: true,
27
- options: {
28
- team: { type: "string" },
29
- app: { type: "string" },
30
- "callback-path": { type: "string" },
31
- "env-prefix": { type: "string" },
32
- },
33
- });
34
- const { team, app, "callback-path": callbackPath, "env-prefix": envPrefix } = values;
16
+ const { positionals } = parseArgs({ allowPositionals: true, options: {} });
35
17
  const USAGE = `Usage:
36
- alchemy-convex hub-client --team <team> --app <app> --callback-path <path> [--env-prefix HUB]
37
18
  alchemy-convex stack-comment`;
38
19
 
39
- const hubClient = (hub: { team: string; app: string; callbackPath: string; envPrefix?: string }) =>
40
- Effect.gen(function* () {
41
- const [command, ...prefix] = yield* installedCli(process.cwd());
42
- const callbackUrl = yield* registerDevelopmentClient({
43
- ...hub,
44
- convex: (args, input) =>
45
- execFileSync(command, [...prefix, ...args], {
46
- encoding: "utf8",
47
- input,
48
- stdio: ["pipe", "pipe", "inherit"],
49
- }).trim(),
50
- });
51
- console.log(
52
- callbackUrl === undefined
53
- ? "Hub client renewed."
54
- : `Hub client registered for ${callbackUrl}.`,
55
- );
56
- }).pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));
57
-
58
20
  const stackComment = updateStackComment.pipe(
59
21
  Effect.tap((url) => Effect.sync(() => console.log(url))),
60
22
  Effect.provide(state().pipe(Layer.provide(NodeServices.layer))),
61
23
  );
62
24
 
63
- const subcommand = positionals.join(" ");
64
- const program: Effect.Effect<unknown, unknown> | undefined =
65
- // stack-comment reads the store of the team of the credential, so a --team would be ignored.
66
- subcommand === "stack-comment" && team === undefined
67
- ? stackComment
68
- : subcommand === "hub-client" &&
69
- team !== undefined &&
70
- app !== undefined &&
71
- callbackPath !== undefined
72
- ? hubClient({ team, app, callbackPath, ...(envPrefix === undefined ? {} : { envPrefix }) })
73
- : undefined;
74
- if (program === undefined) {
25
+ if (positionals.join(" ") !== "stack-comment") {
75
26
  console.error(USAGE);
76
27
  process.exit(1);
77
28
  }
78
29
 
79
- await Effect.runPromise(program).catch((error: unknown) => {
30
+ await Effect.runPromise(stackComment).catch((error: unknown) => {
80
31
  console.error(error instanceof Error ? error.message : String(error));
81
32
  process.exit(1);
82
33
  });
package/src/index.ts CHANGED
@@ -18,14 +18,6 @@ export {
18
18
  type DeploymentProps,
19
19
  ProductionDeploymentNotFound,
20
20
  } from "./Deployment.ts";
21
- export {
22
- HUB_PROJECT_NAME,
23
- HubClient,
24
- type HubClientAttributes,
25
- HubClientFailed,
26
- type HubClientProps,
27
- type HubDeploymentType,
28
- } from "./HubClient.ts";
29
21
  export {
30
22
  LocalDeployment,
31
23
  type LocalDeploymentAttributes,