apiblaze 0.1.18 → 0.1.20
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 +179 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51,6 +51,14 @@ function saveCredentials(creds) {
|
|
|
51
51
|
fs.writeFileSync(CREDENTIALS_PATH, JSON.stringify(creds, null, 2), "utf-8");
|
|
52
52
|
fs.chmodSync(CREDENTIALS_PATH, 384);
|
|
53
53
|
}
|
|
54
|
+
function clearCredentials() {
|
|
55
|
+
try {
|
|
56
|
+
fs.unlinkSync(CREDENTIALS_PATH);
|
|
57
|
+
return true;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
54
62
|
function loadCredentials() {
|
|
55
63
|
try {
|
|
56
64
|
const raw = fs.readFileSync(CREDENTIALS_PATH, "utf-8");
|
|
@@ -85,6 +93,7 @@ var init_auth = __esm({
|
|
|
85
93
|
var api_exports = {};
|
|
86
94
|
__export(api_exports, {
|
|
87
95
|
checkProxyName: () => checkProxyName,
|
|
96
|
+
claimProxy: () => claimProxy,
|
|
88
97
|
createProxy: () => createProxy,
|
|
89
98
|
createProxyAnonymous: () => createProxyAnonymous,
|
|
90
99
|
deleteDevTunnel: () => deleteDevTunnel,
|
|
@@ -159,6 +168,12 @@ async function createProxy(payload) {
|
|
|
159
168
|
body: JSON.stringify(payload)
|
|
160
169
|
});
|
|
161
170
|
}
|
|
171
|
+
async function claimProxy(claimCode, teamId) {
|
|
172
|
+
return apiFetch("/api/cli/claim", {
|
|
173
|
+
method: "POST",
|
|
174
|
+
body: JSON.stringify({ claimCode, team_id: teamId })
|
|
175
|
+
});
|
|
176
|
+
}
|
|
162
177
|
async function putDevTunnel(payload) {
|
|
163
178
|
return apiFetch("/api/cli/dev-tunnel", {
|
|
164
179
|
method: "PUT",
|
|
@@ -184,10 +199,10 @@ var init_api = __esm({
|
|
|
184
199
|
|
|
185
200
|
// src/index.ts
|
|
186
201
|
var import_commander = require("commander");
|
|
187
|
-
var
|
|
202
|
+
var import_chalk10 = __toESM(require("chalk"));
|
|
188
203
|
|
|
189
204
|
// package.json
|
|
190
|
-
var version = "0.1.
|
|
205
|
+
var version = "0.1.20";
|
|
191
206
|
|
|
192
207
|
// src/index.ts
|
|
193
208
|
init_types();
|
|
@@ -963,7 +978,7 @@ async function runAnonymousCreate(opts) {
|
|
|
963
978
|
console.log();
|
|
964
979
|
}
|
|
965
980
|
|
|
966
|
-
// src/commands/
|
|
981
|
+
// src/commands/claim.ts
|
|
967
982
|
var import_chalk6 = __toESM(require("chalk"));
|
|
968
983
|
init_auth();
|
|
969
984
|
init_api();
|
|
@@ -971,26 +986,150 @@ function fail2(message) {
|
|
|
971
986
|
console.error(import_chalk6.default.red(`Error: ${message}`));
|
|
972
987
|
process.exit(1);
|
|
973
988
|
}
|
|
974
|
-
async function
|
|
989
|
+
async function runClaim(claimCodeArg, opts) {
|
|
975
990
|
const creds = loadCredentials();
|
|
976
991
|
if (!creds) {
|
|
977
|
-
fail2("Not logged in. Run `apiblaze login` first.");
|
|
992
|
+
fail2("Not logged in. Run `apiblaze login` first, then claim.");
|
|
993
|
+
}
|
|
994
|
+
let claimCode = (claimCodeArg ?? "").trim();
|
|
995
|
+
if (!claimCode && process.stdin.isTTY) {
|
|
996
|
+
const { default: inquirer2 } = await import("inquirer");
|
|
997
|
+
const { code } = await inquirer2.prompt([{
|
|
998
|
+
type: "input",
|
|
999
|
+
name: "code",
|
|
1000
|
+
message: "Claim code (from the create output / claim URL):"
|
|
1001
|
+
}]);
|
|
1002
|
+
claimCode = (code ?? "").trim();
|
|
1003
|
+
}
|
|
1004
|
+
if (!claimCode) {
|
|
1005
|
+
fail2("A claim code is required: `apiblaze claim <code>`.");
|
|
978
1006
|
}
|
|
979
1007
|
const teams = await getTeams().catch(() => []);
|
|
980
1008
|
if (teams.length === 0) {
|
|
981
1009
|
fail2("No teams found for your account.");
|
|
982
1010
|
}
|
|
1011
|
+
let teamId;
|
|
1012
|
+
if (opts.team) {
|
|
1013
|
+
const match = teams.find(
|
|
1014
|
+
(t) => t.teamId === opts.team || t.name.toLowerCase() === opts.team.toLowerCase()
|
|
1015
|
+
);
|
|
1016
|
+
if (!match) {
|
|
1017
|
+
fail2(`Team "${opts.team}" not found. Available: ${teams.map((t) => t.name).join(", ")}.`);
|
|
1018
|
+
}
|
|
1019
|
+
teamId = match.teamId;
|
|
1020
|
+
} else if (creds.teamId && teams.some((t) => t.teamId === creds.teamId)) {
|
|
1021
|
+
teamId = creds.teamId;
|
|
1022
|
+
} else if (teams.length === 1) {
|
|
1023
|
+
teamId = teams[0].teamId;
|
|
1024
|
+
} else if (process.stdin.isTTY) {
|
|
1025
|
+
const { default: inquirer2 } = await import("inquirer");
|
|
1026
|
+
const { picked } = await inquirer2.prompt([{
|
|
1027
|
+
type: "list",
|
|
1028
|
+
name: "picked",
|
|
1029
|
+
message: "Claim into which team?",
|
|
1030
|
+
choices: teams.map((t) => ({ name: t.name, value: t.teamId }))
|
|
1031
|
+
}]);
|
|
1032
|
+
teamId = picked;
|
|
1033
|
+
} else {
|
|
1034
|
+
fail2(`Specify a team: \`apiblaze claim ${claimCode} --team <name|id>\`. Available: ${teams.map((t) => t.name).join(", ")}.`);
|
|
1035
|
+
}
|
|
1036
|
+
const teamName = teams.find((t) => t.teamId === teamId)?.name ?? teamId;
|
|
1037
|
+
const result = await claimProxy(claimCode, teamId);
|
|
1038
|
+
if (opts.json) {
|
|
1039
|
+
console.log(JSON.stringify(result, null, 2));
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
console.log(import_chalk6.default.green(`
|
|
1043
|
+
\u2714 Claimed into ${import_chalk6.default.bold(teamName)}.`));
|
|
1044
|
+
if (result.project_id) console.log(` Project: ${import_chalk6.default.bold(result.project_id)}`);
|
|
1045
|
+
if (result.endpoints?.length) {
|
|
1046
|
+
console.log(" Endpoints:");
|
|
1047
|
+
for (const e of result.endpoints) console.log(` ${e}`);
|
|
1048
|
+
}
|
|
1049
|
+
console.log(`
|
|
1050
|
+
Manage it at ${import_chalk6.default.cyan("https://dashboard.apiblaze.com/dashboard")}`);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
// src/commands/logout.ts
|
|
1054
|
+
var import_chalk7 = __toESM(require("chalk"));
|
|
1055
|
+
init_auth();
|
|
1056
|
+
async function runLogout() {
|
|
1057
|
+
const creds = loadCredentials();
|
|
1058
|
+
const removed = clearCredentials();
|
|
1059
|
+
if (!removed) {
|
|
1060
|
+
console.log(import_chalk7.default.yellow("You were not logged in."));
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
const who = creds?.githubHandle ?? creds?.email;
|
|
1064
|
+
console.log(import_chalk7.default.green(`\u2714 Logged out${who ? ` (${who})` : ""}.`));
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
// src/commands/whoami.ts
|
|
1068
|
+
var import_chalk8 = __toESM(require("chalk"));
|
|
1069
|
+
init_auth();
|
|
1070
|
+
async function runWhoami(opts = {}) {
|
|
1071
|
+
const creds = loadCredentials();
|
|
1072
|
+
if (!creds) {
|
|
1073
|
+
if (opts.json) {
|
|
1074
|
+
console.log(JSON.stringify({ loggedIn: false }, null, 2));
|
|
1075
|
+
} else {
|
|
1076
|
+
console.log(import_chalk8.default.yellow("Not logged in. Run `apiblaze login` first."));
|
|
1077
|
+
}
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
const expired = Date.now() >= creds.expiresAt;
|
|
1081
|
+
if (opts.json) {
|
|
1082
|
+
console.log(JSON.stringify({
|
|
1083
|
+
loggedIn: true,
|
|
1084
|
+
expired,
|
|
1085
|
+
apiblazeUserId: creds.apiblazeUserId ?? null,
|
|
1086
|
+
githubHandle: creds.githubHandle ?? null,
|
|
1087
|
+
email: creds.email ?? null,
|
|
1088
|
+
teamId: creds.teamId ?? null,
|
|
1089
|
+
teamName: creds.teamName ?? null
|
|
1090
|
+
}, null, 2));
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
const who = creds.githubHandle ?? creds.email ?? creds.apiblazeUserId ?? "unknown";
|
|
1094
|
+
console.log(`${import_chalk8.default.cyan("Signed in as")} ${import_chalk8.default.bold(who)}`);
|
|
1095
|
+
if (creds.email && creds.email !== who) console.log(` Email: ${creds.email}`);
|
|
1096
|
+
if (creds.apiblazeUserId) console.log(` User ID: ${creds.apiblazeUserId}`);
|
|
1097
|
+
if (creds.teamName || creds.teamId) {
|
|
1098
|
+
console.log(` Team: ${import_chalk8.default.bold(creds.teamName ?? creds.teamId)}${creds.teamName && creds.teamId ? import_chalk8.default.gray(` (${creds.teamId})`) : ""}`);
|
|
1099
|
+
}
|
|
1100
|
+
if (expired) {
|
|
1101
|
+
console.log(import_chalk8.default.yellow("\n\u26A0 Session expired. Run `apiblaze login` to re-authenticate."));
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
// src/commands/team.ts
|
|
1106
|
+
var import_chalk9 = __toESM(require("chalk"));
|
|
1107
|
+
init_auth();
|
|
1108
|
+
init_api();
|
|
1109
|
+
function fail3(message) {
|
|
1110
|
+
console.error(import_chalk9.default.red(`Error: ${message}`));
|
|
1111
|
+
process.exit(1);
|
|
1112
|
+
}
|
|
1113
|
+
async function runTeam(arg) {
|
|
1114
|
+
const creds = loadCredentials();
|
|
1115
|
+
if (!creds) {
|
|
1116
|
+
fail3("Not logged in. Run `apiblaze login` first.");
|
|
1117
|
+
}
|
|
1118
|
+
const teams = await getTeams().catch(() => []);
|
|
1119
|
+
if (teams.length === 0) {
|
|
1120
|
+
fail3("No teams found for your account.");
|
|
1121
|
+
}
|
|
983
1122
|
let chosen;
|
|
984
1123
|
if (arg) {
|
|
985
1124
|
chosen = teams.find(
|
|
986
1125
|
(t) => t.teamId === arg || t.name.toLowerCase() === arg.toLowerCase()
|
|
987
1126
|
);
|
|
988
1127
|
if (!chosen) {
|
|
989
|
-
|
|
1128
|
+
fail3(`Team "${arg}" not found. Available: ${teams.map((t) => t.name).join(", ")}.`);
|
|
990
1129
|
}
|
|
991
1130
|
} else if (teams.length === 1) {
|
|
992
1131
|
chosen = teams[0];
|
|
993
|
-
console.log(`${
|
|
1132
|
+
console.log(`${import_chalk9.default.cyan("\u2192")} You only have one team: ${import_chalk9.default.bold(chosen.name)}`);
|
|
994
1133
|
} else if (process.stdin.isTTY) {
|
|
995
1134
|
const { default: inquirer2 } = await import("inquirer");
|
|
996
1135
|
const { picked } = await inquirer2.prompt([{
|
|
@@ -1002,11 +1141,11 @@ async function runTeam(arg) {
|
|
|
1002
1141
|
}]);
|
|
1003
1142
|
chosen = teams.find((t) => t.teamId === picked);
|
|
1004
1143
|
} else {
|
|
1005
|
-
|
|
1144
|
+
fail3(`Specify a team: \`apiblaze team <name|id>\`. Available: ${teams.map((t) => t.name).join(", ")}.`);
|
|
1006
1145
|
}
|
|
1007
1146
|
saveCredentials({ ...creds, teamId: chosen.teamId, teamName: chosen.name });
|
|
1008
|
-
console.log(
|
|
1009
|
-
\u2714 Active team: ${
|
|
1147
|
+
console.log(import_chalk9.default.green(`
|
|
1148
|
+
\u2714 Active team: ${import_chalk9.default.bold(chosen.name)}`));
|
|
1010
1149
|
}
|
|
1011
1150
|
|
|
1012
1151
|
// src/index.ts
|
|
@@ -1028,6 +1167,30 @@ program.command("create").description("Create a new API proxy (no login needed \
|
|
|
1028
1167
|
process.exit(1);
|
|
1029
1168
|
}
|
|
1030
1169
|
});
|
|
1170
|
+
program.command("logout").description("Sign out \u2014 remove stored credentials from this machine").action(async () => {
|
|
1171
|
+
try {
|
|
1172
|
+
await runLogout();
|
|
1173
|
+
} catch (err) {
|
|
1174
|
+
printError(err);
|
|
1175
|
+
process.exit(1);
|
|
1176
|
+
}
|
|
1177
|
+
});
|
|
1178
|
+
program.command("whoami").description("Show the signed-in identity and active team").option("--json", "Output machine-readable JSON").action(async (opts) => {
|
|
1179
|
+
try {
|
|
1180
|
+
await runWhoami(opts);
|
|
1181
|
+
} catch (err) {
|
|
1182
|
+
printError(err);
|
|
1183
|
+
process.exit(1);
|
|
1184
|
+
}
|
|
1185
|
+
});
|
|
1186
|
+
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) => {
|
|
1187
|
+
try {
|
|
1188
|
+
await runClaim(code, opts);
|
|
1189
|
+
} catch (err) {
|
|
1190
|
+
printError(err);
|
|
1191
|
+
process.exit(1);
|
|
1192
|
+
}
|
|
1193
|
+
});
|
|
1031
1194
|
program.command("team").description("Switch the active team").argument("[team]", "Team name or id to switch to (omit to choose interactively)").action(async (team) => {
|
|
1032
1195
|
try {
|
|
1033
1196
|
await runTeam(team);
|
|
@@ -1048,7 +1211,7 @@ program.command("dev").description("Start a dev tunnel for your localhost projec
|
|
|
1048
1211
|
try {
|
|
1049
1212
|
const resolved = parseInt(port ?? opts.port, 10);
|
|
1050
1213
|
if (Number.isNaN(resolved)) {
|
|
1051
|
-
console.error(
|
|
1214
|
+
console.error(import_chalk10.default.red(`Invalid port: ${port ?? opts.port}`));
|
|
1052
1215
|
process.exit(1);
|
|
1053
1216
|
}
|
|
1054
1217
|
await runDev({ port: resolved });
|
|
@@ -1072,20 +1235,22 @@ Examples:
|
|
|
1072
1235
|
$ npx apiblaze create --name myapi --target https://api.example.com --auth api_key
|
|
1073
1236
|
|
|
1074
1237
|
# Manage:
|
|
1238
|
+
$ npx apiblaze whoami
|
|
1075
1239
|
$ npx apiblaze projects
|
|
1076
1240
|
$ npx apiblaze team
|
|
1077
1241
|
$ npx apiblaze dev 3000
|
|
1242
|
+
$ npx apiblaze logout
|
|
1078
1243
|
`
|
|
1079
1244
|
);
|
|
1080
1245
|
function printError(err) {
|
|
1081
1246
|
if (err instanceof ApiError) {
|
|
1082
|
-
console.error(
|
|
1247
|
+
console.error(import_chalk10.default.red(`
|
|
1083
1248
|
API error (${err.status}): ${err.message}`));
|
|
1084
1249
|
} else if (err instanceof Error) {
|
|
1085
|
-
console.error(
|
|
1250
|
+
console.error(import_chalk10.default.red(`
|
|
1086
1251
|
Error: ${err.message}`));
|
|
1087
1252
|
} else {
|
|
1088
|
-
console.error(
|
|
1253
|
+
console.error(import_chalk10.default.red("\nUnknown error"));
|
|
1089
1254
|
}
|
|
1090
1255
|
}
|
|
1091
1256
|
program.parse(process.argv);
|