@runtypelabs/cli 0.2.2 → 0.2.4

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.
package/dist/index.js CHANGED
@@ -186,19 +186,20 @@ var CallbackServer = class {
186
186
  this.codeReject = reject;
187
187
  });
188
188
  this.app.get("/callback", (req, res) => {
189
- const { code, error } = req.query;
189
+ const { token, code, error } = req.query;
190
190
  if (error) {
191
191
  res.send(this.errorHTML(error));
192
192
  this.codeReject(new Error(error));
193
193
  return;
194
194
  }
195
- if (!code) {
196
- res.send(this.errorHTML("No authorization code received"));
197
- this.codeReject(new Error("No authorization code received"));
195
+ const authToken = token || code;
196
+ if (!authToken) {
197
+ res.send(this.errorHTML("No authentication token received"));
198
+ this.codeReject(new Error("No authentication token received"));
198
199
  return;
199
200
  }
200
201
  res.send(this.successHTML());
201
- this.codeResolve(code);
202
+ this.codeResolve(authToken);
202
203
  setTimeout(() => this.stop(), 1e3);
203
204
  });
204
205
  this.app.get("/health", (_req, res) => {
@@ -547,7 +548,7 @@ function getDefaultTemperature() {
547
548
  }
548
549
 
549
550
  // src/auth/api-key-manager.ts
550
- var isDashboardAuthResponse = (value) => {
551
+ var isCliTokenResponse = (value) => {
551
552
  if (!value || typeof value !== "object") {
552
553
  return false;
553
554
  }
@@ -562,29 +563,36 @@ var isAuthMeResponse = (value) => {
562
563
  return typeof record.user_id === "string";
563
564
  };
564
565
  var ApiKeyManager = class {
565
- async exchangeSessionForApiKey(authCode, apiUrl, dashboardUrl) {
566
- void apiUrl;
567
- const base = dashboardUrl ?? getDashboardUrl();
568
- const authResponse = await fetch(`${base.replace(/\/$/, "")}/api/cli/auth?code=${encodeURIComponent(authCode)}`, {
569
- method: "GET",
566
+ /**
567
+ * Exchange a Clerk session JWT for a persistent API key.
568
+ *
569
+ * Calls POST /v1/auth/cli-token on the Runtype API directly,
570
+ * passing the Clerk JWT as a Bearer token. The API verifies the JWT
571
+ * with Clerk and creates an API key for the CLI.
572
+ */
573
+ async exchangeSessionForApiKey(clerkJwt, apiUrl) {
574
+ const baseUrl = apiUrl || getApiUrl();
575
+ const response = await fetch(`${baseUrl}/${getApiVersion()}/auth/cli-token`, {
576
+ method: "POST",
570
577
  headers: {
571
- "Content-Type": "application/json"
578
+ "Content-Type": "application/json",
579
+ Authorization: `Bearer ${clerkJwt}`
572
580
  }
573
581
  });
574
- if (!authResponse.ok) {
575
- const error = await authResponse.text();
582
+ if (!response.ok) {
583
+ const error = await response.text();
576
584
  throw new Error(`Authentication failed: ${error}`);
577
585
  }
578
- const authData = await authResponse.json();
579
- if (!isDashboardAuthResponse(authData)) {
586
+ const data = await response.json();
587
+ if (!isCliTokenResponse(data)) {
580
588
  throw new Error("Invalid authentication response format");
581
589
  }
582
590
  const result = {
583
- key: authData.apiKey,
584
- userId: authData.userId
591
+ key: data.apiKey,
592
+ userId: data.userId
585
593
  };
586
- if (authData.orgId) {
587
- result.orgId = authData.orgId;
594
+ if (data.orgId) {
595
+ result.orgId = data.orgId;
588
596
  }
589
597
  return result;
590
598
  }
@@ -637,8 +645,7 @@ authCommand.command("signup").description("Create a new Runtype account").option
637
645
  const apiKeyManager = new ApiKeyManager();
638
646
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
639
647
  sessionToken,
640
- options.apiUrl || getApiUrl(),
641
- options.dashboardUrl || getDashboardUrl()
648
+ options.apiUrl || getApiUrl()
642
649
  );
643
650
  spinner.text = "Storing credentials securely...";
644
651
  const store = new CredentialStore();
@@ -704,8 +711,7 @@ authCommand.command("login").description("Login to existing account").option("--
704
711
  const apiKeyManager = new ApiKeyManager();
705
712
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
706
713
  sessionToken,
707
- options.apiUrl || getApiUrl(),
708
- options.dashboardUrl || getDashboardUrl()
714
+ options.apiUrl || getApiUrl()
709
715
  );
710
716
  spinner.text = "Storing credentials...";
711
717
  await store.saveCredentials({
@@ -981,8 +987,7 @@ async function handleBrowserLogin(store, apiUrl) {
981
987
  const apiKeyManager = new ApiKeyManager();
982
988
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
983
989
  sessionToken,
984
- apiUrl || getApiUrl(),
985
- getDashboardUrl()
990
+ apiUrl || getApiUrl()
986
991
  );
987
992
  spinner.text = "Storing credentials...";
988
993
  await store.saveCredentials({
@@ -2596,8 +2601,7 @@ async function handleBrowserAuth(store, mode, apiUrl) {
2596
2601
  const apiKeyManager = new ApiKeyManager();
2597
2602
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
2598
2603
  sessionToken,
2599
- apiUrl || getApiUrl(),
2600
- getDashboardUrl()
2604
+ apiUrl || getApiUrl()
2601
2605
  );
2602
2606
  await store.saveCredentials({
2603
2607
  apiKey: key,