apiblaze 0.1.19 → 0.1.21

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 +109 -27
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -36,9 +36,10 @@ var init_types = __esm({
36
36
  "src/types.ts"() {
37
37
  "use strict";
38
38
  ApiError = class extends Error {
39
- constructor(status, message) {
39
+ constructor(status, message, body) {
40
40
  super(message);
41
41
  this.status = status;
42
+ this.body = body;
42
43
  this.name = "ApiError";
43
44
  }
44
45
  };
@@ -132,12 +133,14 @@ async function apiFetch(path2, options = {}) {
132
133
  });
133
134
  if (!res.ok) {
134
135
  let message = res.statusText;
136
+ let parsed;
135
137
  try {
136
- const body = await res.json();
137
- message = body.message ?? body.error ?? message;
138
+ parsed = await res.json();
139
+ const b = parsed;
140
+ message = b.message ?? b.error ?? message;
138
141
  } catch {
139
142
  }
140
- throw new ApiError(res.status, message);
143
+ throw new ApiError(res.status, message, parsed);
141
144
  }
142
145
  if (res.status === 204) {
143
146
  return void 0;
@@ -158,9 +161,10 @@ async function getLocalhostTargets(teamId) {
158
161
  async function getProjects(teamId) {
159
162
  return apiFetch(`/api/cli/projects?team_id=${encodeURIComponent(teamId)}`);
160
163
  }
161
- async function checkProxyName(name, teamId) {
164
+ async function checkProxyName(name, teamId, apiVersion) {
162
165
  const q = teamId ? `&team_id=${encodeURIComponent(teamId)}` : "";
163
- return apiFetch(`/api/cli/check-name?name=${encodeURIComponent(name)}${q}`);
166
+ const v = apiVersion ? `&api_version=${encodeURIComponent(apiVersion)}` : "";
167
+ return apiFetch(`/api/cli/check-name?name=${encodeURIComponent(name)}${q}${v}`);
164
168
  }
165
169
  async function createProxy(payload) {
166
170
  return apiFetch("/api/cli/create-proxy", {
@@ -199,10 +203,10 @@ var init_api = __esm({
199
203
 
200
204
  // src/index.ts
201
205
  var import_commander = require("commander");
202
- var import_chalk9 = __toESM(require("chalk"));
206
+ var import_chalk10 = __toESM(require("chalk"));
203
207
 
204
208
  // package.json
205
- var version = "0.1.19";
209
+ var version = "0.1.21";
206
210
 
207
211
  // src/index.ts
208
212
  init_types();
@@ -747,7 +751,7 @@ async function runCreate(opts = {}) {
747
751
  if (opts.name !== void 0) {
748
752
  name = normalizeName(opts.name);
749
753
  if (name.length < 3) fail("Proxy name must be at least 3 characters (letters and digits only).");
750
- const check = await checkProxyName(name, teamId).catch(() => null);
754
+ const check = await checkProxyName(name, teamId, opts.apiversion).catch(() => null);
751
755
  if (check && (!check.canUseProjectName || !check.canUseApiVersion)) {
752
756
  fail(`Proxy name "${name}" is not available${check.message ? ` \u2014 ${check.message}` : ""}.`);
753
757
  }
@@ -767,7 +771,7 @@ async function runCreate(opts = {}) {
767
771
  }
768
772
  const spinner2 = (0, import_ora4.default)("Checking availability...").start();
769
773
  try {
770
- const check = await checkProxyName(name, teamId);
774
+ const check = await checkProxyName(name, teamId, opts.apiversion);
771
775
  spinner2.stop();
772
776
  if (!check.canUseProjectName || !check.canUseApiVersion) {
773
777
  console.log(import_chalk5.default.yellow(` "${name}" is not available${check.message ? ` \u2014 ${check.message}` : ""}. Try another.
@@ -824,11 +828,46 @@ async function runCreate(opts = {}) {
824
828
  const spinner = !opts.json ? (0, import_ora4.default)("Creating proxy (tenant, keys, dev portal)...").start() : null;
825
829
  let result;
826
830
  try {
827
- result = await createProxy({ name, target_url: targetUrl, auth_type: auth, team_id: teamId });
831
+ result = await createProxy({ name, target_url: targetUrl, auth_type: auth, team_id: teamId, ...opts.apiversion ? { api_version: opts.apiversion } : {} });
828
832
  spinner?.succeed(import_chalk5.default.green("Proxy created!"));
829
833
  } catch (err) {
834
+ const e = err;
835
+ const ownedByYou = e?.status === 409 && e?.body?.reason === "slug_owned_by_your_other_team" && !opts.team;
830
836
  spinner?.fail("Failed to create proxy.");
831
- throw err;
837
+ if (ownedByYou) {
838
+ const ownerId = e.body.owning_team_id;
839
+ const ownerName = e.body.owning_team_name || ownerId;
840
+ if (!opts.json) console.log(import_chalk5.default.yellow(`
841
+ You already own "${e.body.product_slug}" under your team "${ownerName}".`));
842
+ let doSwitch = false;
843
+ if (interactive && !opts.yes) {
844
+ const { default: inquirer2 } = await import("inquirer");
845
+ const { ok } = await inquirer2.prompt([{
846
+ type: "confirm",
847
+ name: "ok",
848
+ message: `Switch to team "${ownerName}" and create "${name}" there?`,
849
+ default: true
850
+ }]);
851
+ doSwitch = ok;
852
+ }
853
+ if (!doSwitch) {
854
+ if (!opts.json) console.log(import_chalk5.default.dim(` Run \`apiblaze team ${ownerId}\` to switch, then retry \u2014 or pick another --name.`));
855
+ throw err;
856
+ }
857
+ const creds2 = loadCredentials();
858
+ if (creds2) saveCredentials({ ...creds2, teamId: ownerId, teamName: ownerName });
859
+ teamId = ownerId;
860
+ const spinner2 = !opts.json ? (0, import_ora4.default)(`Creating proxy under "${ownerName}"...`).start() : null;
861
+ try {
862
+ result = await createProxy({ name, target_url: targetUrl, auth_type: auth, team_id: ownerId, ...opts.apiversion ? { api_version: opts.apiversion } : {} });
863
+ spinner2?.succeed(import_chalk5.default.green(`Proxy created \u2014 active team switched to "${ownerName}".`));
864
+ } catch (err2) {
865
+ spinner2?.fail("Failed to create proxy after switching team.");
866
+ throw err2;
867
+ }
868
+ } else {
869
+ throw err;
870
+ }
832
871
  }
833
872
  const version2 = result.api_version || "1.0.0";
834
873
  const keys = result.api_keys ?? {};
@@ -934,6 +973,7 @@ async function runAnonymousCreate(opts) {
934
973
  if (opts.tenant) body.tenant = normalizeName(opts.tenant);
935
974
  if (opts.product) body.product_slug = normalizeName(opts.product);
936
975
  if (opts.displayName) body.display_name = opts.displayName;
976
+ if (opts.apiversion) body.api_version = opts.apiversion;
937
977
  if (opts.auth && opts.auth !== "api_key") body.auth_type = opts.auth;
938
978
  const spinner = !opts.json ? (0, import_ora4.default)("Creating proxy...").start() : null;
939
979
  let result;
@@ -1064,12 +1104,50 @@ async function runLogout() {
1064
1104
  console.log(import_chalk7.default.green(`\u2714 Logged out${who ? ` (${who})` : ""}.`));
1065
1105
  }
1066
1106
 
1067
- // src/commands/team.ts
1107
+ // src/commands/whoami.ts
1068
1108
  var import_chalk8 = __toESM(require("chalk"));
1069
1109
  init_auth();
1110
+ async function runWhoami(opts = {}) {
1111
+ const creds = loadCredentials();
1112
+ if (!creds) {
1113
+ if (opts.json) {
1114
+ console.log(JSON.stringify({ loggedIn: false }, null, 2));
1115
+ } else {
1116
+ console.log(import_chalk8.default.yellow("Not logged in. Run `apiblaze login` first."));
1117
+ }
1118
+ return;
1119
+ }
1120
+ const expired = Date.now() >= creds.expiresAt;
1121
+ if (opts.json) {
1122
+ console.log(JSON.stringify({
1123
+ loggedIn: true,
1124
+ expired,
1125
+ apiblazeUserId: creds.apiblazeUserId ?? null,
1126
+ githubHandle: creds.githubHandle ?? null,
1127
+ email: creds.email ?? null,
1128
+ teamId: creds.teamId ?? null,
1129
+ teamName: creds.teamName ?? null
1130
+ }, null, 2));
1131
+ return;
1132
+ }
1133
+ const who = creds.githubHandle ?? creds.email ?? creds.apiblazeUserId ?? "unknown";
1134
+ console.log(`${import_chalk8.default.cyan("Signed in as")} ${import_chalk8.default.bold(who)}`);
1135
+ if (creds.email && creds.email !== who) console.log(` Email: ${creds.email}`);
1136
+ if (creds.apiblazeUserId) console.log(` User ID: ${creds.apiblazeUserId}`);
1137
+ if (creds.teamName || creds.teamId) {
1138
+ console.log(` Team: ${import_chalk8.default.bold(creds.teamName ?? creds.teamId)}${creds.teamName && creds.teamId ? import_chalk8.default.gray(` (${creds.teamId})`) : ""}`);
1139
+ }
1140
+ if (expired) {
1141
+ console.log(import_chalk8.default.yellow("\n\u26A0 Session expired. Run `apiblaze login` to re-authenticate."));
1142
+ }
1143
+ }
1144
+
1145
+ // src/commands/team.ts
1146
+ var import_chalk9 = __toESM(require("chalk"));
1147
+ init_auth();
1070
1148
  init_api();
1071
1149
  function fail3(message) {
1072
- console.error(import_chalk8.default.red(`Error: ${message}`));
1150
+ console.error(import_chalk9.default.red(`Error: ${message}`));
1073
1151
  process.exit(1);
1074
1152
  }
1075
1153
  async function runTeam(arg) {
@@ -1091,7 +1169,7 @@ async function runTeam(arg) {
1091
1169
  }
1092
1170
  } else if (teams.length === 1) {
1093
1171
  chosen = teams[0];
1094
- console.log(`${import_chalk8.default.cyan("\u2192")} You only have one team: ${import_chalk8.default.bold(chosen.name)}`);
1172
+ console.log(`${import_chalk9.default.cyan("\u2192")} You only have one team: ${import_chalk9.default.bold(chosen.name)}`);
1095
1173
  } else if (process.stdin.isTTY) {
1096
1174
  const { default: inquirer2 } = await import("inquirer");
1097
1175
  const { picked } = await inquirer2.prompt([{
@@ -1106,8 +1184,8 @@ async function runTeam(arg) {
1106
1184
  fail3(`Specify a team: \`apiblaze team <name|id>\`. Available: ${teams.map((t) => t.name).join(", ")}.`);
1107
1185
  }
1108
1186
  saveCredentials({ ...creds, teamId: chosen.teamId, teamName: chosen.name });
1109
- console.log(import_chalk8.default.green(`
1110
- \u2714 Active team: ${import_chalk8.default.bold(chosen.name)}`));
1187
+ console.log(import_chalk9.default.green(`
1188
+ \u2714 Active team: ${import_chalk9.default.bold(chosen.name)}`));
1111
1189
  }
1112
1190
 
1113
1191
  // src/index.ts
@@ -1121,7 +1199,7 @@ program.command("login").description("Authenticate with APIblaze").action(async
1121
1199
  process.exit(1);
1122
1200
  }
1123
1201
  });
1124
- program.command("create").description("Create a new API proxy (no login needed \u2014 without auth it creates an anonymous proxy and prints a claim URL)").option("--name <name>", "Proxy name (becomes <name>.apiblaze.com)").option("--target <url>", "Target URL to forward requests to").option("--team <id|name>", "Team to create under (defaults to your active team)").option("--auth <type>", "Auth type: api_key | none | oauth", "api_key").option("--tenant <slug>", "Tenant slug (anonymous create; generated if omitted)").option("--product <slug>", "Product slug (anonymous create; defaults to the proxy name)").option("--display-name <name>", "Human-friendly display name").option("--subdomain <slug>", "Explicit subdomain (defaults to --name)").option("--config <file>", "JSON file with the full request body (anonymous create): requests_auth, login providers + client/server token types, scopes, callback URLs, etc. See apiblaze_anonymous.yaml. Flags override its fields.").option("-y, --yes", "Skip the confirmation prompt").option("--json", "Output machine-readable JSON (non-interactive)").action(async (opts) => {
1202
+ program.command("create").description("Create a new API proxy (no login needed \u2014 without auth it creates an anonymous proxy and prints a claim URL)").option("--name <name>", "Proxy name (becomes <name>.apiblaze.com)").option("--target <url>", "Target URL to forward requests to").option("--team <id|name>", "Team to create under (defaults to your active team)").option("--auth <type>", "Auth type: api_key | none | oauth", "api_key").option("--apiversion <version>", "API version to create (e.g. 2.0.0). Creating a new version of a proxy you own adds a version to the existing project.").option("--tenant <slug>", "Tenant slug (anonymous create; generated if omitted)").option("--product <slug>", "Product slug (anonymous create; defaults to the proxy name)").option("--display-name <name>", "Human-friendly display name").option("--subdomain <slug>", "Explicit subdomain (defaults to --name)").option("--config <file>", "JSON file with the full request body (anonymous create): requests_auth, login providers + client/server token types, scopes, callback URLs, etc. See apiblaze_anonymous.yaml. Flags override its fields.").option("-y, --yes", "Skip the confirmation prompt").option("--json", "Output machine-readable JSON (non-interactive)").action(async (opts) => {
1125
1203
  try {
1126
1204
  await runCreate(opts);
1127
1205
  } catch (err) {
@@ -1137,6 +1215,14 @@ program.command("logout").description("Sign out \u2014 remove stored credentials
1137
1215
  process.exit(1);
1138
1216
  }
1139
1217
  });
1218
+ program.command("whoami").description("Show the signed-in identity and active team").option("--json", "Output machine-readable JSON").action(async (opts) => {
1219
+ try {
1220
+ await runWhoami(opts);
1221
+ } catch (err) {
1222
+ printError(err);
1223
+ process.exit(1);
1224
+ }
1225
+ });
1140
1226
  program.command("claim").description("Claim an anonymously-created proxy into one of your teams (requires login)").argument("[code]", "Claim code from `create` output / claim URL (prompted if omitted)").option("--team <id|name>", "Team to claim into (defaults to your active team)").option("--json", "Output machine-readable JSON (non-interactive)").action(async (code, opts) => {
1141
1227
  try {
1142
1228
  await runClaim(code, opts);
@@ -1165,7 +1251,7 @@ program.command("dev").description("Start a dev tunnel for your localhost projec
1165
1251
  try {
1166
1252
  const resolved = parseInt(port ?? opts.port, 10);
1167
1253
  if (Number.isNaN(resolved)) {
1168
- console.error(import_chalk9.default.red(`Invalid port: ${port ?? opts.port}`));
1254
+ console.error(import_chalk10.default.red(`Invalid port: ${port ?? opts.port}`));
1169
1255
  process.exit(1);
1170
1256
  }
1171
1257
  await runDev({ port: resolved });
@@ -1184,16 +1270,12 @@ Examples:
1184
1270
  # Non-interactive (CI / scripts):
1185
1271
  $ npx apiblaze create --target https://api.example.com --name myapi --json
1186
1272
 
1187
- # Claim that anonymous proxy into your team (after signing in):
1188
- $ npx apiblaze login
1189
- $ npx apiblaze claim G7QN-62JB-VN3D-RQNR-F6EZ
1190
- $ npx apiblaze claim G7QN-62JB-VN3D-RQNR-F6EZ --team my-team
1191
-
1192
1273
  # Sign in, then create under your team:
1193
1274
  $ npx apiblaze login
1194
1275
  $ npx apiblaze create --name myapi --target https://api.example.com --auth api_key
1195
1276
 
1196
1277
  # Manage:
1278
+ $ npx apiblaze whoami
1197
1279
  $ npx apiblaze projects
1198
1280
  $ npx apiblaze team
1199
1281
  $ npx apiblaze dev 3000
@@ -1202,13 +1284,13 @@ Examples:
1202
1284
  );
1203
1285
  function printError(err) {
1204
1286
  if (err instanceof ApiError) {
1205
- console.error(import_chalk9.default.red(`
1287
+ console.error(import_chalk10.default.red(`
1206
1288
  API error (${err.status}): ${err.message}`));
1207
1289
  } else if (err instanceof Error) {
1208
- console.error(import_chalk9.default.red(`
1290
+ console.error(import_chalk10.default.red(`
1209
1291
  Error: ${err.message}`));
1210
1292
  } else {
1211
- console.error(import_chalk9.default.red("\nUnknown error"));
1293
+ console.error(import_chalk10.default.red("\nUnknown error"));
1212
1294
  }
1213
1295
  }
1214
1296
  program.parse(process.argv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apiblaze",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "description": "Dev tunnel CLI for APIblaze — route localhost projects through your APIblaze endpoints",
5
5
  "keywords": [
6
6
  "apiblaze",