@optima-chat/tiktok-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.
package/README.md CHANGED
@@ -10,14 +10,15 @@ Optima agent 用来操作 TikTok 店铺的 CLI。**薄客户端**:不直连 Ti
10
10
  tiktok <domain> <verb> --shop <shopId> [--flags]
11
11
  ```
12
12
 
13
- - `--shop` **必填**(多店显式路由,对应 A 轨「多店必须显式选店」教训)
13
+ - `--shop` **必填**(多店显式路由,对应 A 轨「多店必须显式选店」教训;`auth link` 除外)
14
14
  - 默认 **JSON 输出**,统一信封 `{ ok, data?, error?, meta? }`
15
- - `TIKTOK_BACKEND_URL` 指向 backend(默认 `http://127.0.0.1:3000`)
15
+ - `TIKTOK_BACKEND_URL` 解析顺序:显式环境变量 > `OPTIMA_ENV` 映射(`cn-stage` → `https://tiktok-api.stage.optima.chat`;`cn-prod` → `https://tiktok-api.yzsgo.com`)> 本地默认 `http://127.0.0.1:3000`
16
16
 
17
17
  ## 命令(v1 只读)
18
18
 
19
19
  | 命令 | 说明 |
20
20
  |---|---|
21
+ | `auth link` | 生成店铺绑定授权链接(无需 --shop) |
21
22
  | `shops --merchant <id>` | 查商家已授权店铺 |
22
23
  | `orders list` | 订单列表(`--status --page-size --since`) |
23
24
  | `orders get --id <id>` | 订单详情 |
package/dist/index.js CHANGED
@@ -4,7 +4,20 @@
4
4
  import { readFileSync } from "fs";
5
5
  import { homedir } from "os";
6
6
  import { join } from "path";
7
- var BACKEND = process.env.TIKTOK_BACKEND_URL ?? "http://127.0.0.1:3000";
7
+
8
+ // src/backendUrl.ts
9
+ var ENV_URLS = {
10
+ "cn-stage": "https://tiktok-api.stage.optima.chat",
11
+ "cn-prod": "https://tiktok-api.yzsgo.com"
12
+ };
13
+ function resolveBackendUrl(env2) {
14
+ if (env2.TIKTOK_BACKEND_URL) return env2.TIKTOK_BACKEND_URL;
15
+ const mapped = env2.OPTIMA_ENV && ENV_URLS[env2.OPTIMA_ENV];
16
+ return mapped || "http://127.0.0.1:3000";
17
+ }
18
+
19
+ // src/client.ts
20
+ var BACKEND = resolveBackendUrl(process.env);
8
21
  function authHeaders() {
9
22
  const explicit = process.env.OPTIMA_TOKEN;
10
23
  if (explicit) return { Authorization: `Bearer ${explicit}` };
@@ -58,6 +71,16 @@ async function listShops() {
58
71
  return { ok: false, error: { code: "BACKEND_UNREACHABLE", message: e.message, retryable: true } };
59
72
  }
60
73
  }
74
+ async function createAuthLink() {
75
+ try {
76
+ const r = await fetch(`${BACKEND}/auth/link`, { method: "POST", headers: authHeaders() });
77
+ if (r.status === 401) return { ok: false, error: { code: "AUTH_EXPIRED", message: "\u672A\u767B\u5F55\u6216 token \u5931\u6548", retryable: false } };
78
+ const body = await r.json();
79
+ return { ok: true, data: { authorize_url: body.authorize_url, expires_in_minutes: 15 } };
80
+ } catch (e) {
81
+ return { ok: false, error: { code: "BACKEND_UNREACHABLE", message: e.message, retryable: true } };
82
+ }
83
+ }
61
84
 
62
85
  // src/commands.ts
63
86
  var today = () => (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
@@ -268,6 +291,7 @@ function usage() {
268
291
  const lines = ["tiktok <domain> <verb> --shop <shopId> [--flags]", "", "\u547D\u4EE4\uFF1A"];
269
292
  for (const c of COMMANDS) lines.push(` ${c.name.padEnd(22)} ${c.summary}`);
270
293
  lines.push(` ${"shops".padEnd(22)} \u67E5\u5F53\u524D\u767B\u5F55\u7528\u6237\u5DF2\u6388\u6743\u5E97\u94FA`);
294
+ lines.push(` ${"auth link".padEnd(22)} \u751F\u6210\u5E97\u94FA\u7ED1\u5B9A\u6388\u6743\u94FE\u63A5\uFF08\u53D1\u7ED9\u5546\u5BB6\u70B9\u51FB\uFF09`);
271
295
  lines.push("", "\u901A\u7528\uFF1A--shop \u5FC5\u586B\uFF08\u9664 shops\uFF09\uFF1B\u9ED8\u8BA4 JSON \u8F93\u51FA\uFF1BTIKTOK_BACKEND_URL \u6307\u5411 backend\u3002");
272
296
  process.stderr.write(lines.join("\n") + "\n");
273
297
  }
@@ -281,6 +305,10 @@ if (positional[0] === "shops") {
281
305
  out(await listShops());
282
306
  process.exit(0);
283
307
  }
308
+ if (positional[0] === "auth" && positional[1] === "link") {
309
+ out(await createAuthLink());
310
+ process.exit(0);
311
+ }
284
312
  var match = findCommand(positional);
285
313
  if (!match) {
286
314
  out({ ok: false, error: { code: "VALIDATION", message: `\u672A\u77E5\u547D\u4EE4: ${positional.join(" ")}`, retryable: false } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optima-chat/tiktok-cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "TikTok Shop CLI for Optima agents — 经 tiktok-backend 操作店铺",
5
5
  "type": "module",
6
6
  "bin": {
@@ -10,6 +10,7 @@
10
10
  "build": "tsup src/index.ts --format esm --clean",
11
11
  "dev": "tsx src/index.ts",
12
12
  "typecheck": "tsc --noEmit",
13
+ "test": "node --import tsx --test tests/*.test.mts",
13
14
  "prepublishOnly": "npm run build"
14
15
  },
15
16
  "author": "Optima Commerce Team",