@cnbcool/cnb-api-generate 2.4.2 → 2.4.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.
package/client/core.ts CHANGED
@@ -4,6 +4,7 @@ import os from 'os';
4
4
  import { fetchResponseHandler } from "./fetch-response-handler";
5
5
  import { generateUniqueId } from './utils/generate-unique-id';
6
6
  import { resolveToken } from './utils/resolve-token';
7
+ import { resolveApiDomain } from './utils/resolve-api-domain';
7
8
 
8
9
  /**
9
10
  * 格式化 API 响应
@@ -118,7 +119,7 @@ async function formatResponse(data: any, response: any) {
118
119
 
119
120
  async function clientFetch(data: any): Promise<any> {
120
121
  const token = await resolveToken();
121
- const domain = process.env.CNB_API_ENDPOINT || 'https://api.cnb.cool'
122
+ const domain = resolveApiDomain()
122
123
  const url = `${domain}${data.url}`
123
124
  const urlParse = new URL(url);
124
125
  if (data.params) {
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  import { resolveToken } from '../utils/resolve-token';
10
+ import { resolveApiDomain } from '../utils/resolve-api-domain';
10
11
 
11
12
  /** CNB 构建号格式:cnb-xxx-xxxxxxxxx(字母/数字组成的两段) */
12
13
  export const SN_PATTERN = /^cnb-[a-z0-9]+-[a-z0-9]+$/i;
@@ -111,7 +112,7 @@ export async function resolveBuildContext(
111
112
  | { ok: true; ctx: ResolvedContext }
112
113
  | { ok: false; message: string }
113
114
  > {
114
- const domain = process.env.CNB_API_ENDPOINT || 'https://api.cnb.cool';
115
+ const domain = resolveApiDomain();
115
116
  const { repo } = options;
116
117
  let sn = options.sn;
117
118
 
@@ -78,11 +78,15 @@ export function registerLoginCommand(program: Command): void {
78
78
  }
79
79
  // 持久化 token
80
80
  const nowSec = Math.floor(Date.now() / 1000);
81
+ // 从 platformURL 推导 API 域名,例如 https://cnb.cool -> https://api.cnb.cool
82
+ const platformHost = new URL(cfg.platformURL).hostname;
83
+ const loginHost = `https://api.${platformHost}`;
81
84
  const store: TokenStore = {
82
85
  access_token: tok.access_token,
83
86
  expires_at: nowSec + tok.expires_in,
84
87
  platform_url: cfg.platformURL,
85
88
  client_id: cfg.clientID,
89
+ login_host: loginHost,
86
90
  };
87
91
  if (tok.refresh_token) {
88
92
  store.refresh_token = tok.refresh_token;
@@ -0,0 +1,26 @@
1
+ import { loadToken } from './load-token';
2
+
3
+ /**
4
+ * 获取 API 域名。
5
+ *
6
+ * 优先级:
7
+ * 1. 如果设置了环境变量 CNB_TOKEN(即非 OAuth 登录),使用 CNB_API_ENDPOINT 或默认值
8
+ * 2. 如果是 OAuth 登录(token 文件中有 login_host),使用登录时记录的域名
9
+ * 3. 兜底使用 CNB_API_ENDPOINT 或默认值 https://api.cnb.cool
10
+ */
11
+ export function resolveApiDomain(): string {
12
+ const defaultDomain = process.env.CNB_API_ENDPOINT || 'https://api.cnb.cool';
13
+
14
+ // 如果使用环境变量 token,直接走 CNB_API_ENDPOINT
15
+ if (process.env.CNB_TOKEN_FOR_CODEBUDDY || process.env.CNB_TOKEN) {
16
+ return defaultDomain;
17
+ }
18
+
19
+ // OAuth 登录场景:优先使用登录时记录的域名
20
+ const store = loadToken();
21
+ if (store?.login_host) {
22
+ return store.login_host;
23
+ }
24
+
25
+ return defaultDomain;
26
+ }
@@ -42,4 +42,6 @@ export interface TokenStore {
42
42
  platform_url?: string;
43
43
  /** 用于 refresh 时携带 client_id */
44
44
  client_id?: string;
45
+ /** 登录时使用的 API 域名(如 https://api.cnb.cool),用于后续 CLI 调用 */
46
+ login_host?: string;
45
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cnbcool/cnb-api-generate",
3
- "version": "2.4.2",
3
+ "version": "2.4.3",
4
4
  "main": "./built/index.js",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",