xapi-to 0.1.12 → 0.1.13

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/README.md CHANGED
@@ -21,6 +21,9 @@ cd xapi-cli && bun install
21
21
  # 1. Register a new account (apiKey saved automatically)
22
22
  xapi register
23
23
 
24
+ # 1b. Or register with an inviter's referral code (please replace xapito to your referral code)
25
+ xapi register --referral-code xapito
26
+
24
27
  # 2. Or set an existing key
25
28
  xapi config set apiKey=sk-xxx
26
29
 
@@ -76,6 +79,8 @@ xapi oauth providers # list available providers
76
79
 
77
80
  ```bash
78
81
  xapi register # create account, saves apiKey automatically
82
+ xapi register --referral-code xapito # register with an inviter's referral code (please replace xapito to your referral code)
83
+ xapi register xapito # positional shorthand for --referral-code
79
84
  xapi balance # show USD balance
80
85
  xapi topup # generate payment URL
81
86
  xapi topup --method stripe --amount 10 # stripe, $10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xapi-to",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "Agent-friendly CLI for xapi - discover and call capabilities and APIs",
5
5
  "type": "module",
6
6
  "bin": {
@@ -6,6 +6,25 @@ import { getConfig, saveConfig, showConfig } from '../config.ts';
6
6
  import { healthCheck } from '../client.ts';
7
7
  import { output, err } from '../format.ts';
8
8
 
9
+ export const CONFIG_HELP = `xapi config - Manage CLI configuration
10
+
11
+ USAGE
12
+ xapi config <command> [flags]
13
+
14
+ COMMANDS
15
+ show Show current config (host, apiKey path, etc.)
16
+ set apiKey=<key> Save API key to ~/.xapi/config.json
17
+ health Check backend connectivity (alias: xapi health)
18
+
19
+ FLAGS
20
+ --format json|pretty|table Output format
21
+
22
+ EXAMPLES
23
+ xapi config show
24
+ xapi config set apiKey=xapi_abc123
25
+ xapi config health
26
+ `;
27
+
9
28
  export async function configShow(args: string[], flags: Record<string, string>) {
10
29
  showConfig();
11
30
  }
@@ -2,34 +2,43 @@
2
2
  * register command: create a new user account
3
3
  *
4
4
  * POST /auth/register — no auth required
5
- * Returns apiKey (shown once), claimCode, claimUrl, tweetTemplate
5
+ * Returns apiKey (shown once), claimCode, referralCode, claimUrl, tweetTemplate
6
6
  * Automatically saves apiKey to ~/.xapi/config.json
7
+ *
8
+ * Optional referral code (please replace xapito to the actual referral code):
9
+ * xapi register --referral-code xapito
10
+ * xapi register --referralCode xapito # alias
11
+ * xapi register xapito # positional shorthand
7
12
  */
8
13
 
9
14
  import { XAPI_API_HOST, saveConfig, scheme } from '../config.ts';
10
15
  import { output, err } from '../format.ts';
11
16
 
12
- async function registerAccount() {
17
+ interface RegisterResponse {
18
+ apiKey: string;
19
+ claimCode: string;
20
+ referralCode?: string;
21
+ claimSessionId: string;
22
+ claimUrl: string;
23
+ tweetTemplate: string;
24
+ user: { id: string; accountType: string };
25
+ }
26
+
27
+ async function registerAccount(referralCode?: string): Promise<RegisterResponse> {
13
28
  const controller = new AbortController();
14
29
  const timer = setTimeout(() => controller.abort(), 15_000);
15
30
  try {
16
31
  const res = await fetch(`${scheme(XAPI_API_HOST)}://${XAPI_API_HOST}/api/auth/register`, {
17
32
  method: 'POST',
18
33
  headers: { 'Content-Type': 'application/json' },
34
+ body: JSON.stringify(referralCode ? { referralCode } : {}),
19
35
  signal: controller.signal,
20
36
  });
21
37
  if (!res.ok) {
22
38
  const text = await res.text();
23
39
  throw new Error(`HTTP ${res.status}: ${text.slice(0, 300)}`);
24
40
  }
25
- return res.json() as Promise<{
26
- apiKey: string;
27
- claimCode: string;
28
- claimSessionId: string;
29
- claimUrl: string;
30
- tweetTemplate: string;
31
- user: { id: string; accountType: string };
32
- }>;
41
+ return res.json() as Promise<RegisterResponse>;
33
42
  } finally {
34
43
  clearTimeout(timer);
35
44
  }
@@ -37,20 +46,29 @@ async function registerAccount() {
37
46
 
38
47
  export async function register(args: string[], flags: Record<string, string>) {
39
48
  try {
40
- const res = await registerAccount();
49
+ // 邀请码来源优先级:--referral-code > --referralCode > 第一个位置参数
50
+ const rawReferral =
51
+ flags['referral-code'] ?? flags['referralCode'] ?? args[0];
52
+ const referralCode =
53
+ typeof rawReferral === 'string' && rawReferral !== 'true' && rawReferral.length > 0
54
+ ? rawReferral
55
+ : undefined;
56
+
57
+ const res = await registerAccount(referralCode);
41
58
 
42
- // auto-save apiKey
43
59
  saveConfig({ apiKey: res.apiKey });
44
60
 
45
61
  output({
46
62
  apiKey: res.apiKey,
47
63
  user: res.user,
64
+ referralCode: res.referralCode,
48
65
  claim: {
49
66
  code: res.claimCode,
50
67
  sessionId: res.claimSessionId,
51
68
  url: res.claimUrl,
52
69
  },
53
70
  tweetTemplate: res.tweetTemplate,
71
+ ...(referralCode ? { referredBy: referralCode } : {}),
54
72
  note: 'apiKey saved to ~/.xapi/config.json',
55
73
  }, flags.format as any);
56
74
  } catch (e: any) {
package/src/index.ts CHANGED
@@ -12,6 +12,7 @@
12
12
  *
13
13
  * xapi config show
14
14
  * xapi config set apiKey=<key>
15
+ * xapi config health
15
16
  * xapi health
16
17
  *
17
18
  * Global flags:
@@ -26,6 +27,7 @@
26
27
 
27
28
  import * as actionCmds from './commands/action.ts';
28
29
  import * as cfgCmds from './commands/config.ts';
30
+ const { CONFIG_HELP } = cfgCmds;
29
31
  import * as regCmds from './commands/register.ts';
30
32
  import * as topupCmds from './commands/topup.ts';
31
33
  import * as balanceCmds from './commands/balance.ts';
@@ -97,7 +99,8 @@ COMMANDS
97
99
  oauth unbind <binding-id> Remove an OAuth binding
98
100
  oauth providers List available OAuth providers
99
101
 
100
- register Create a new user account (apiKey saved automatically)
102
+ register [referral-code] Create a new user account (apiKey saved automatically)
103
+ --referral-code <code> Register with an inviter's referral code (also: --referralCode, or as positional arg)
101
104
  balance Show current account balance
102
105
  topup [--amount <usd>] [--method stripe|x402] Generate payment URL
103
106
 
@@ -105,6 +108,7 @@ COMMANDS
105
108
 
106
109
  config show Show current config
107
110
  config set apiKey=<key> Save API key to ~/.xapi/config.json
111
+ config health Check backend connectivity (alias: xapi health)
108
112
 
109
113
  GLOBAL FLAGS
110
114
  --format json|pretty|table Output format (default: json)
@@ -117,6 +121,8 @@ ENV VARS
117
121
 
118
122
  EXAMPLES
119
123
  xapi register
124
+ xapi register --referral-code xapito # register with an inviter's referral code
125
+ xapi register xapito # positional shorthand
120
126
  xapi list --format table
121
127
  xapi list --source capability
122
128
  xapi search twitter --source api
@@ -182,12 +188,17 @@ async function main() {
182
188
 
183
189
  // ── Config commands ──
184
190
  case 'config': {
191
+ if (flags.help || rest.length === 0) {
192
+ console.log(CONFIG_HELP);
193
+ process.exit(0);
194
+ }
185
195
  const [subCmd, ...subRest] = rest;
186
196
  switch (subCmd) {
187
197
  case 'show': return cfgCmds.configShow(subRest, flags);
188
198
  case 'set': return cfgCmds.configSet(subRest, flags);
199
+ case 'health': return cfgCmds.configHealth(subRest, flags);
189
200
  default:
190
- console.error(JSON.stringify({ error: `unknown config command: ${subCmd}` }));
201
+ console.error(JSON.stringify({ error: `unknown config command: ${subCmd}`, hint: 'valid commands: show, set, health' }));
191
202
  process.exit(1);
192
203
  }
193
204
  break;