apiblaze 0.1.1 → 0.1.3

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 +162 -62
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -6,6 +6,13 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __esm = (fn, res) => function __init() {
10
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
+ };
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
9
16
  var __copyProps = (to, from, except, desc) => {
10
17
  if (from && typeof from === "object" || typeof from === "function") {
11
18
  for (let key of __getOwnPropNames(from))
@@ -23,30 +30,22 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
30
  mod
24
31
  ));
25
32
 
26
- // src/index.ts
27
- var import_commander = require("commander");
28
- var import_chalk4 = __toESM(require("chalk"));
29
-
30
33
  // src/types.ts
31
- var ApiError = class extends Error {
32
- constructor(status, message) {
33
- super(message);
34
- this.status = status;
35
- this.name = "ApiError";
34
+ var ApiError;
35
+ var init_types = __esm({
36
+ "src/types.ts"() {
37
+ "use strict";
38
+ ApiError = class extends Error {
39
+ constructor(status, message) {
40
+ super(message);
41
+ this.status = status;
42
+ this.name = "ApiError";
43
+ }
44
+ };
36
45
  }
37
- };
38
-
39
- // src/commands/login.ts
40
- var import_chalk = __toESM(require("chalk"));
41
- var import_ora = __toESM(require("ora"));
42
- var crypto = __toESM(require("crypto"));
46
+ });
43
47
 
44
48
  // src/lib/auth.ts
45
- var fs = __toESM(require("fs"));
46
- var os = __toESM(require("os"));
47
- var path = __toESM(require("path"));
48
- var APIBLAZE_DIR = path.join(os.homedir(), ".apiblaze");
49
- var CREDENTIALS_PATH = path.join(APIBLAZE_DIR, "credentials.json");
50
49
  function saveCredentials(creds) {
51
50
  fs.mkdirSync(APIBLAZE_DIR, { recursive: true });
52
51
  fs.writeFileSync(CREDENTIALS_PATH, JSON.stringify(creds, null, 2), "utf-8");
@@ -73,8 +72,87 @@ function getAccessToken() {
73
72
  function getApiblazeDir() {
74
73
  return APIBLAZE_DIR;
75
74
  }
75
+ var fs, os, path, APIBLAZE_DIR, CREDENTIALS_PATH;
76
+ var init_auth = __esm({
77
+ "src/lib/auth.ts"() {
78
+ "use strict";
79
+ fs = __toESM(require("fs"));
80
+ os = __toESM(require("os"));
81
+ path = __toESM(require("path"));
82
+ APIBLAZE_DIR = path.join(os.homedir(), ".apiblaze");
83
+ CREDENTIALS_PATH = path.join(APIBLAZE_DIR, "credentials.json");
84
+ }
85
+ });
86
+
87
+ // src/lib/api.ts
88
+ var api_exports = {};
89
+ __export(api_exports, {
90
+ deleteDevTunnel: () => deleteDevTunnel,
91
+ getLocalhostTargets: () => getLocalhostTargets,
92
+ getTeams: () => getTeams,
93
+ putDevTunnel: () => putDevTunnel
94
+ });
95
+ async function apiFetch(path3, options = {}) {
96
+ const token = getAccessToken();
97
+ const url = `${API_BASE}${path3}`;
98
+ const res = await fetch(url, {
99
+ ...options,
100
+ headers: {
101
+ "Content-Type": "application/json",
102
+ Authorization: `Bearer ${token}`,
103
+ ...options.headers ?? {}
104
+ }
105
+ });
106
+ if (!res.ok) {
107
+ let message = res.statusText;
108
+ try {
109
+ const body = await res.json();
110
+ message = body.message ?? body.error ?? message;
111
+ } catch {
112
+ }
113
+ throw new ApiError(res.status, message);
114
+ }
115
+ if (res.status === 204) {
116
+ return void 0;
117
+ }
118
+ return res.json();
119
+ }
120
+ async function getTeams() {
121
+ return apiFetch("/me/teams");
122
+ }
123
+ async function getLocalhostTargets(teamId) {
124
+ return apiFetch(`/projects/localhost-targets?team_id=${encodeURIComponent(teamId)}`);
125
+ }
126
+ async function putDevTunnel(payload) {
127
+ return apiFetch("/dev-tunnel", {
128
+ method: "PUT",
129
+ body: JSON.stringify(payload)
130
+ });
131
+ }
132
+ async function deleteDevTunnel() {
133
+ return apiFetch("/dev-tunnel", { method: "DELETE" });
134
+ }
135
+ var API_BASE;
136
+ var init_api = __esm({
137
+ "src/lib/api.ts"() {
138
+ "use strict";
139
+ init_auth();
140
+ init_types();
141
+ API_BASE = "https://cli.admin.apiblaze.com";
142
+ }
143
+ });
144
+
145
+ // src/index.ts
146
+ var import_commander = require("commander");
147
+ var import_chalk4 = __toESM(require("chalk"));
148
+ init_types();
76
149
 
77
150
  // src/commands/login.ts
151
+ var import_chalk = __toESM(require("chalk"));
152
+ var import_ora = __toESM(require("ora"));
153
+ var crypto = __toESM(require("crypto"));
154
+ init_auth();
155
+ init_api();
78
156
  var AUTH_BASE = "https://auth.apiblaze.com";
79
157
  var CLIENT_ID = "emdE4-Pt9LGOAXL5MA1zEQ";
80
158
  var SCOPE = "openid email profile offline_access";
@@ -164,53 +242,50 @@ async function runLogin() {
164
242
  expiresAt: Date.now() + token.expires_in * 1e3,
165
243
  tokenType: token.token_type
166
244
  });
167
- spinner.succeed(import_chalk.default.green("Logged in successfully!"));
245
+ spinner.succeed(import_chalk.default.green("Authorized!"));
246
+ let teamId;
247
+ let teamName;
248
+ try {
249
+ const teams = await getTeams();
250
+ if (teams.length === 0) {
251
+ console.log(import_chalk.default.yellow("\nNo teams found \u2014 you can still use apiblaze dev with a personal workspace."));
252
+ } else if (teams.length === 1) {
253
+ teamId = teams[0].teamId;
254
+ teamName = teams[0].name;
255
+ console.log(`
256
+ ${import_chalk.default.cyan("\u2192")} Using team: ${import_chalk.default.bold(teamName)}`);
257
+ } else {
258
+ const { default: inquirer2 } = await import("inquirer");
259
+ const { chosen } = await inquirer2.prompt([{
260
+ type: "list",
261
+ name: "chosen",
262
+ message: "Which team do you want to use?",
263
+ choices: teams.map((t) => ({ name: t.name, value: t.teamId }))
264
+ }]);
265
+ teamId = chosen;
266
+ teamName = teams.find((t) => t.teamId === chosen)?.name;
267
+ }
268
+ } catch {
269
+ }
270
+ if (teamId) {
271
+ saveCredentials({
272
+ accessToken: token.access_token,
273
+ refreshToken: token.refresh_token,
274
+ expiresAt: Date.now() + token.expires_in * 1e3,
275
+ tokenType: token.token_type,
276
+ teamId,
277
+ teamName
278
+ });
279
+ }
280
+ console.log(import_chalk.default.green("\n\u2714 Logged in successfully!"));
168
281
  }
169
282
 
170
283
  // src/commands/dev.ts
171
284
  var import_chalk3 = __toESM(require("chalk"));
172
285
  var import_ora3 = __toESM(require("ora"));
173
286
  var import_inquirer = __toESM(require("inquirer"));
174
-
175
- // src/lib/api.ts
176
- var API_BASE = "https://adminapioauth.abz.run/1.0.0/prod";
177
- async function apiFetch(path3, options = {}) {
178
- const token = getAccessToken();
179
- const url = `${API_BASE}${path3}`;
180
- const res = await fetch(url, {
181
- ...options,
182
- headers: {
183
- "Content-Type": "application/json",
184
- Authorization: `Bearer ${token}`,
185
- ...options.headers ?? {}
186
- }
187
- });
188
- if (!res.ok) {
189
- let message = res.statusText;
190
- try {
191
- const body = await res.json();
192
- message = body.message ?? body.error ?? message;
193
- } catch {
194
- }
195
- throw new ApiError(res.status, message);
196
- }
197
- if (res.status === 204) {
198
- return void 0;
199
- }
200
- return res.json();
201
- }
202
- async function getLocalhostTargets() {
203
- return apiFetch("/projects/localhost-targets");
204
- }
205
- async function putDevTunnel(payload) {
206
- return apiFetch("/dev-tunnel", {
207
- method: "PUT",
208
- body: JSON.stringify(payload)
209
- });
210
- }
211
- async function deleteDevTunnel() {
212
- return apiFetch("/dev-tunnel", { method: "DELETE" });
213
- }
287
+ init_auth();
288
+ init_api();
214
289
 
215
290
  // src/lib/cloudflared.ts
216
291
  var fs2 = __toESM(require("fs"));
@@ -218,6 +293,7 @@ var path2 = __toESM(require("path"));
218
293
  var readline = __toESM(require("readline"));
219
294
  var import_child_process = require("child_process");
220
295
  var import_ora2 = __toESM(require("ora"));
296
+ init_auth();
221
297
  var TUNNEL_URL_RE = /https:\/\/[a-z0-9-]+\.trycloudflare\.com/;
222
298
  var DOWNLOAD_TIMEOUT_MS = 3e4;
223
299
  var TUNNEL_START_TIMEOUT_MS = 3e4;
@@ -328,6 +404,7 @@ function killCloudflared(proc) {
328
404
  // src/lib/traffic.ts
329
405
  var import_ws = __toESM(require("ws"));
330
406
  var import_chalk2 = __toESM(require("chalk"));
407
+ init_auth();
331
408
  var METHOD_COLORS = {
332
409
  GET: import_chalk2.default.cyan,
333
410
  POST: import_chalk2.default.green,
@@ -397,11 +474,34 @@ async function runDev(options) {
397
474
  console.error(import_chalk3.default.red("Not logged in. Run `apiblaze login` first."));
398
475
  process.exit(1);
399
476
  }
477
+ let teamId = creds.teamId;
478
+ if (!teamId) {
479
+ const { getTeams: getTeams2 } = await Promise.resolve().then(() => (init_api(), api_exports));
480
+ const teams = await getTeams2().catch(() => []);
481
+ if (teams.length === 1) {
482
+ teamId = teams[0].teamId;
483
+ } else if (teams.length > 1) {
484
+ const { chosen } = await import_inquirer.default.prompt([{
485
+ type: "list",
486
+ name: "chosen",
487
+ message: "Which team?",
488
+ choices: teams.map((t) => ({ name: t.name, value: t.teamId }))
489
+ }]);
490
+ teamId = chosen;
491
+ }
492
+ }
493
+ if (creds.teamName) {
494
+ console.log(`${import_chalk3.default.cyan("\u2192")} Team: ${import_chalk3.default.bold(creds.teamName)}`);
495
+ }
496
+ if (!teamId) {
497
+ console.error(import_chalk3.default.red("No team selected. Run `apiblaze login` to set up your team."));
498
+ process.exit(1);
499
+ }
400
500
  let targets;
401
501
  {
402
502
  const spinner = (0, import_ora3.default)("Fetching your localhost projects...").start();
403
503
  try {
404
- targets = await getLocalhostTargets();
504
+ targets = await getLocalhostTargets(teamId);
405
505
  spinner.stop();
406
506
  } catch (err) {
407
507
  spinner.fail("Failed to fetch projects.");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apiblaze",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Dev tunnel CLI for APIblaze — route localhost projects through Cloudflare tunnels",
5
5
  "keywords": [
6
6
  "apiblaze",