apiblaze 0.19.21 → 0.19.23

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.
Files changed (2) hide show
  1. package/dist/index.js +45 -23
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -932,7 +932,7 @@ var import_commander = require("commander");
932
932
  var import_chalk44 = __toESM(require("chalk"));
933
933
 
934
934
  // package.json
935
- var version = "0.19.21";
935
+ var version = "0.19.23";
936
936
 
937
937
  // src/index.ts
938
938
  init_types();
@@ -6067,12 +6067,13 @@ async function dataPlaneAuth(p) {
6067
6067
  if (fresh.accessToken !== stored.accessToken) saveConsumer(fresh);
6068
6068
  return { Authorization: `Bearer ${fresh.accessToken}` };
6069
6069
  }
6070
- async function ensureConsumerLogin(teamId, tenant2) {
6070
+ async function ensureConsumerLogin(teamId, tenant2, version2) {
6071
+ const resource = `https://${tenant2}.portal.apiblaze.com/${version2}`;
6071
6072
  const existing = loadConsumer();
6072
- if (existing && existing.tenant === tenant2) {
6073
+ if (existing && existing.tenant === tenant2 && existing.resource === resource) {
6073
6074
  const fresh = await validConsumerToken(existing);
6074
6075
  if (fresh) {
6075
- if (fresh.accessToken !== existing.accessToken) saveConsumer(fresh);
6076
+ if (fresh.accessToken !== existing.accessToken) saveConsumer({ ...fresh, resource });
6076
6077
  console.log(import_chalk39.default.dim(` Using your consumer session on ${import_chalk39.default.bold(tenant2)}${fresh.email ? ` (${fresh.email})` : ""}.`));
6077
6078
  return fresh;
6078
6079
  }
@@ -6099,7 +6100,7 @@ async function ensureConsumerLogin(teamId, tenant2) {
6099
6100
  console.log(` Code: ${import_chalk39.default.bold(userCode)}
6100
6101
  `);
6101
6102
  console.log(import_chalk39.default.dim(" (opening your browser\u2026 waiting for you to finish)"));
6102
- }, `https://${tenant2}.portal.apiblaze.com/1.0.0`);
6103
+ }, resource);
6103
6104
  const claims = result.idToken && decodeJwt2(result.idToken) || (decodeJwt2(result.accessToken) ?? {});
6104
6105
  const creds = {
6105
6106
  tenant: tenant2,
@@ -6110,7 +6111,8 @@ async function ensureConsumerLogin(teamId, tenant2) {
6110
6111
  expiresAt: Date.now() + (result.expiresIn ?? 3600) * 1e3,
6111
6112
  scope: result.scope,
6112
6113
  email: typeof claims.email === "string" ? claims.email : void 0,
6113
- obtainedAt: Date.now()
6114
+ obtainedAt: Date.now(),
6115
+ resource
6114
6116
  };
6115
6117
  saveConsumer(creds);
6116
6118
  console.log(` ${import_chalk39.default.green("\u2714")} Signed in as${creds.email ? ` ${import_chalk39.default.bold(creds.email)}` : " a consumer"} on ${tenant2}.`);
@@ -6413,7 +6415,14 @@ async function replTurn(p, messages, userText) {
6413
6415
  return;
6414
6416
  }
6415
6417
  if (res.status === 401) {
6416
- console.log(import_chalk39.default.red(" The proxy rejected the API key (401). The key may have been revoked; re-run `apiblaze apichat` to re-provision."));
6418
+ const reason = data && (data.error || data.message) || null;
6419
+ const usingKey = !p.consumerAuth;
6420
+ console.log(import_chalk39.default.red(` The proxy rejected the request (401)${reason ? `: ${reason}` : "."}`));
6421
+ console.log(
6422
+ import_chalk39.default.dim(
6423
+ usingKey ? " Sent an API key. If the key was revoked, re-run `apiblaze apichat` to re-provision." : " Sent your consumer OAuth token. Re-run `apiblaze apichat` to sign in again."
6424
+ )
6425
+ );
6417
6426
  return;
6418
6427
  }
6419
6428
  if (!res.ok || !data) {
@@ -6516,7 +6525,7 @@ async function mintDurableProxyKey(teamId, tenant2) {
6516
6525
  if (!out?.key) throw new Error("key mint returned no key");
6517
6526
  return out.key;
6518
6527
  }
6519
- async function fetchAcceptedMethods(teamId, projectId, apiVersion) {
6528
+ async function fetchProxyDoor(teamId, projectId, apiVersion) {
6520
6529
  try {
6521
6530
  const listing = await admin({
6522
6531
  method: "GET",
@@ -6525,11 +6534,15 @@ async function fetchAcceptedMethods(teamId, projectId, apiVersion) {
6525
6534
  });
6526
6535
  const rows = listing?.projects ?? [];
6527
6536
  const row = rows.find((r) => r.project_id === projectId && r.api_version === apiVersion) ?? rows.find((r) => r.project_id === projectId);
6537
+ const tenant2 = row?.config?.default_tenant ?? row?.config?.tenant ?? (typeof row?.urls?.portal === "string" ? row.urls.portal.replace(/^https?:\/\//, "").split(".")[0] : void 0);
6528
6538
  const policy = row?.config?.requests_policy;
6529
- if (!policy || policy.mode !== "authenticate") return null;
6530
- return Array.isArray(policy.methods) && policy.methods.length ? policy.methods : null;
6539
+ if (!policy || policy.mode !== "authenticate") return { methods: null, tenant: tenant2 };
6540
+ return {
6541
+ methods: Array.isArray(policy.methods) && policy.methods.length ? policy.methods : null,
6542
+ tenant: tenant2
6543
+ };
6531
6544
  } catch {
6532
- return null;
6545
+ return { methods: null };
6533
6546
  }
6534
6547
  }
6535
6548
  async function openServerProxy(project) {
@@ -6539,8 +6552,8 @@ async function openServerProxy(project) {
6539
6552
  const tenant2 = project.tenant || project.projectId;
6540
6553
  const me = loadCredentials()?.apiblazeUserId;
6541
6554
  const prior = loadApichats().find((a) => a.projectId === project.projectId);
6542
- const acceptedMethods = await fetchAcceptedMethods(project.teamId, project.projectId, project.apiVersion);
6543
- const acceptsApiKey = acceptedMethods === null || acceptedMethods.includes("api_key");
6555
+ const door = await fetchProxyDoor(project.teamId, project.projectId, project.apiVersion);
6556
+ const acceptsApiKey = door.methods === null || door.methods.includes("api_key");
6544
6557
  let dpKey = prior?.dpKey;
6545
6558
  let consumerAuth = false;
6546
6559
  if (acceptsApiKey) {
@@ -6552,7 +6565,7 @@ async function openServerProxy(project) {
6552
6565
  } else {
6553
6566
  consumerAuth = true;
6554
6567
  dpKey = void 0;
6555
- await ensureConsumerLogin(project.teamId, tenant2);
6568
+ await ensureConsumerLogin(project.teamId, door.tenant ?? tenant2, version2);
6556
6569
  }
6557
6570
  const p = {
6558
6571
  projectId: project.projectId,
@@ -6571,14 +6584,14 @@ async function openServerProxy(project) {
6571
6584
  const raw = await admin({
6572
6585
  method: "GET",
6573
6586
  path: `/projects/${project.projectId}/${version2}/openapi`,
6574
- summary: "Fetch OpenAPI spec"
6587
+ summary: "Check the proxy has a spec"
6575
6588
  }).catch(() => null);
6576
6589
  const spec2 = raw && (raw.spec ?? raw);
6577
6590
  spinner.stop();
6578
- if (spec2 && (spec2.paths || spec2.openapi)) {
6579
- await publishMcp(p, spec2);
6580
- } else {
6591
+ if (!spec2 || !(spec2.paths || spec2.openapi)) {
6581
6592
  console.log(import_chalk39.default.yellow(" This proxy has no OpenAPI spec yet \u2014 chat will have no tools. Build one with `apiblaze agent openapi`."));
6593
+ } else {
6594
+ console.log(import_chalk39.default.dim(" Using the proxy's existing MCP catalogue (`apiblaze mcp` to rebuild it)."));
6582
6595
  }
6583
6596
  } catch (err) {
6584
6597
  spinner.fail("Could not open the proxy.");
@@ -6674,19 +6687,28 @@ async function noArgsMenu(opts) {
6674
6687
  }
6675
6688
  let savedKey = a.dpKey;
6676
6689
  let savedConsumerAuth = a.consumerAuth;
6690
+ let savedTenant = a.tenant;
6677
6691
  const resumeTeamId = a.teamId ?? creds?.teamId;
6678
6692
  if (!a.anon && resumeTeamId) {
6679
- const methods = await fetchAcceptedMethods(resumeTeamId, a.projectId, a.version);
6680
- const acceptsApiKey = methods === null || methods.includes("api_key");
6693
+ const door = await fetchProxyDoor(resumeTeamId, a.projectId, a.version);
6694
+ const acceptsApiKey = door.methods === null || door.methods.includes("api_key");
6681
6695
  if (!acceptsApiKey) {
6682
6696
  savedConsumerAuth = true;
6683
6697
  savedKey = void 0;
6684
- await ensureConsumerLogin(resumeTeamId, a.tenant ?? a.projectId);
6698
+ const loginTenant = door.tenant ?? a.tenant;
6699
+ if (!loginTenant) {
6700
+ fail4(
6701
+ `Could not work out which tenant signs consumers in for "${a.projectId}".`,
6702
+ 'Re-open it from the "your proxy" list instead of Recent apichats.'
6703
+ );
6704
+ }
6705
+ savedTenant = loginTenant;
6706
+ await ensureConsumerLogin(resumeTeamId, loginTenant, a.version);
6685
6707
  } else if (savedConsumerAuth && !savedKey) {
6686
6708
  savedConsumerAuth = false;
6687
6709
  }
6688
- if (savedConsumerAuth !== a.consumerAuth || savedKey !== a.dpKey) {
6689
- upsertApichat({ ...a, teamId: resumeTeamId, dpKey: savedKey, consumerAuth: savedConsumerAuth });
6710
+ if (savedConsumerAuth !== a.consumerAuth || savedKey !== a.dpKey || savedTenant !== a.tenant) {
6711
+ upsertApichat({ ...a, teamId: resumeTeamId, tenant: savedTenant, dpKey: savedKey, consumerAuth: savedConsumerAuth });
6690
6712
  }
6691
6713
  }
6692
6714
  const p = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apiblaze",
3
- "version": "0.19.21",
3
+ "version": "0.19.23",
4
4
  "description": "APIblaze CLI — Chat with your APIs, Manage your API keys, users and groups with the APIblaze serverless proxy",
5
5
  "keywords": [
6
6
  "apiblaze",