@codespring-app/cli 0.1.0 → 0.2.0

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 +78 -68
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -62,6 +62,79 @@ var init_credentials = __esm(() => {
62
62
  CREDENTIALS_PATH = join(CODESPRING_DIR, "credentials.json");
63
63
  });
64
64
 
65
+ // src/lib/api.ts
66
+ function setCurrentCommand(cmd) {
67
+ currentCommand = cmd;
68
+ }
69
+ async function refreshToken(creds) {
70
+ if (!creds.refreshToken) {
71
+ throw new Error("No refresh token available. Run: codespring auth login");
72
+ }
73
+ const response = await fetch(`${creds.apiUrl}/api/auth/oauth2/token`, {
74
+ method: "POST",
75
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
76
+ body: new URLSearchParams({
77
+ grant_type: "refresh_token",
78
+ refresh_token: creds.refreshToken,
79
+ client_id: "codespring-cli"
80
+ })
81
+ });
82
+ if (!response.ok) {
83
+ throw new Error("Token refresh failed. Run: codespring auth login");
84
+ }
85
+ const tokens = await response.json();
86
+ const updated = {
87
+ ...creds,
88
+ accessToken: tokens.access_token,
89
+ refreshToken: tokens.refresh_token || creds.refreshToken,
90
+ expiresAt: new Date(Date.now() + tokens.expires_in * 1000).toISOString()
91
+ };
92
+ await saveCredentials(updated);
93
+ return updated;
94
+ }
95
+ async function apiCall(options) {
96
+ let creds = await loadCredentials();
97
+ if (!creds) {
98
+ throw new Error("Not authenticated. Run: codespring auth login");
99
+ }
100
+ if (creds.type === "oauth" && creds.expiresAt && new Date(creds.expiresAt) < new Date) {
101
+ creds = await refreshToken(creds);
102
+ }
103
+ const headers = {
104
+ "Content-Type": "application/json",
105
+ "x-codespring-client": `cli/${CLI_VERSION}`
106
+ };
107
+ const command = options.command || currentCommand;
108
+ if (command) {
109
+ headers["x-codespring-command"] = command;
110
+ }
111
+ if (creds.type === "api-key" && creds.apiKey) {
112
+ headers["x-api-key"] = creds.apiKey;
113
+ } else if (creds.type === "oauth" && creds.accessToken) {
114
+ headers["Authorization"] = `Bearer ${creds.accessToken}`;
115
+ }
116
+ const apiUrl = creds.apiUrl || DEFAULT_API_URL;
117
+ const baseUrl = apiUrl.replace(/\/api\/?$/, "");
118
+ const response = await fetch(`${baseUrl}/api${options.path}`, {
119
+ method: options.method,
120
+ headers,
121
+ body: options.body ? JSON.stringify(options.body) : undefined
122
+ });
123
+ const text = await response.text();
124
+ if (!response.ok) {
125
+ throw new Error(`API error (${response.status}): ${text.slice(0, 200)}`);
126
+ }
127
+ try {
128
+ return { data: JSON.parse(text), status: response.status };
129
+ } catch {
130
+ return { data: text, status: response.status };
131
+ }
132
+ }
133
+ var DEFAULT_API_URL = "https://server.codespring.app", CLI_VERSION = "0.2.0", currentCommand = "";
134
+ var init_api = __esm(() => {
135
+ init_credentials();
136
+ });
137
+
65
138
  // src/lib/prompts.ts
66
139
  import * as readline from "readline";
67
140
  function isTTY() {
@@ -334,7 +407,7 @@ __export(exports_login, {
334
407
  authLogin: () => authLogin
335
408
  });
336
409
  function getApiUrl() {
337
- return process.env.CODESPRING_API_URL || DEFAULT_API_URL;
410
+ return process.env.CODESPRING_API_URL || DEFAULT_API_URL2;
338
411
  }
339
412
  async function authLogin(flags) {
340
413
  const apiUrl = flags.url || getApiUrl();
@@ -417,7 +490,7 @@ async function oauthLogin(apiUrl) {
417
490
  stop();
418
491
  }
419
492
  }
420
- var DEFAULT_API_URL = "https://server.codespring.app", CALLBACK_PORT = 8923;
493
+ var DEFAULT_API_URL2 = "https://server.codespring.app", CALLBACK_PORT = 8923;
421
494
  var init_login = __esm(() => {
422
495
  init_credentials();
423
496
  init_prompts();
@@ -505,71 +578,6 @@ async function ensureGitignore(cwd) {
505
578
  }
506
579
  var init_config = () => {};
507
580
 
508
- // src/lib/api.ts
509
- async function refreshToken(creds) {
510
- if (!creds.refreshToken) {
511
- throw new Error("No refresh token available. Run: codespring auth login");
512
- }
513
- const response = await fetch(`${creds.apiUrl}/api/auth/oauth2/token`, {
514
- method: "POST",
515
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
516
- body: new URLSearchParams({
517
- grant_type: "refresh_token",
518
- refresh_token: creds.refreshToken,
519
- client_id: "codespring-cli"
520
- })
521
- });
522
- if (!response.ok) {
523
- throw new Error("Token refresh failed. Run: codespring auth login");
524
- }
525
- const tokens = await response.json();
526
- const updated = {
527
- ...creds,
528
- accessToken: tokens.access_token,
529
- refreshToken: tokens.refresh_token || creds.refreshToken,
530
- expiresAt: new Date(Date.now() + tokens.expires_in * 1000).toISOString()
531
- };
532
- await saveCredentials(updated);
533
- return updated;
534
- }
535
- async function apiCall(options) {
536
- let creds = await loadCredentials();
537
- if (!creds) {
538
- throw new Error("Not authenticated. Run: codespring auth login");
539
- }
540
- if (creds.type === "oauth" && creds.expiresAt && new Date(creds.expiresAt) < new Date) {
541
- creds = await refreshToken(creds);
542
- }
543
- const headers = {
544
- "Content-Type": "application/json"
545
- };
546
- if (creds.type === "api-key" && creds.apiKey) {
547
- headers["x-api-key"] = creds.apiKey;
548
- } else if (creds.type === "oauth" && creds.accessToken) {
549
- headers["Authorization"] = `Bearer ${creds.accessToken}`;
550
- }
551
- const apiUrl = creds.apiUrl || DEFAULT_API_URL2;
552
- const baseUrl = apiUrl.replace(/\/api\/?$/, "");
553
- const response = await fetch(`${baseUrl}/api${options.path}`, {
554
- method: options.method,
555
- headers,
556
- body: options.body ? JSON.stringify(options.body) : undefined
557
- });
558
- const text = await response.text();
559
- if (!response.ok) {
560
- throw new Error(`API error (${response.status}): ${text.slice(0, 200)}`);
561
- }
562
- try {
563
- return { data: JSON.parse(text), status: response.status };
564
- } catch {
565
- return { data: text, status: response.status };
566
- }
567
- }
568
- var DEFAULT_API_URL2 = "https://server.codespring.app";
569
- var init_api = __esm(() => {
570
- init_credentials();
571
- });
572
-
573
581
  // src/commands/init.ts
574
582
  var exports_init = {};
575
583
  __export(exports_init, {
@@ -1174,7 +1182,8 @@ var init_node_types = __esm(() => {
1174
1182
  });
1175
1183
 
1176
1184
  // src/index.ts
1177
- var VERSION = "0.1.0";
1185
+ init_api();
1186
+ var VERSION = "0.2.0";
1178
1187
  function parseArgs(argv) {
1179
1188
  const positional = [];
1180
1189
  const flags = {};
@@ -1266,6 +1275,7 @@ async function main() {
1266
1275
  showHelp();
1267
1276
  return;
1268
1277
  }
1278
+ setCurrentCommand(subcommand ? `${command} ${subcommand}` : command);
1269
1279
  try {
1270
1280
  switch (command) {
1271
1281
  case "auth": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codespring-app/cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "CodeSpring CLI — project planning and management from the terminal",
5
5
  "type": "module",
6
6
  "bin": {