@cnbcool/cnb-api-generate 2.17.2 → 2.17.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.
@@ -1,3 +1,5 @@
1
+ import { resolveApiDomain } from '../utils/resolve-api-domain';
2
+
1
3
  /**
2
4
  * 把文本里的相对路径转换为绝对路径
3
5
  * 与 src/helpers/convertLink.ts 逻辑一致
@@ -110,8 +112,7 @@ export default function convertLink(text: string, repoSlug: string | undefined,
110
112
  * @returns {String}
111
113
  */
112
114
  function normaliseLink(link: string, repoSlug: string | undefined, branchOrSha = 'HEAD') {
113
- const { CNB_WEB_ENDPOINT } = process.env;
114
- const baseURL = `${CNB_WEB_ENDPOINT}/${repoSlug}`;
115
+ const baseURL = `${resolveWebEndpoint()}/${repoSlug}`;
115
116
 
116
117
  if (link.startsWith('/-/')) {
117
118
  return `${baseURL}${link}`;
@@ -137,3 +138,31 @@ function normaliseLink(link: string, repoSlug: string | undefined, branchOrSha =
137
138
 
138
139
  return link;
139
140
  }
141
+
142
+ /**
143
+ * 解析 Web 平台地址(形如 https://cnb.cool)。
144
+ *
145
+ * 直接以 CLI 实际调用的 OpenAPI 地址(resolveApiDomain)为准,
146
+ * 去掉主机名的 `api.` 前缀即得到 Web 地址;保留协议与自定义端口。
147
+ * 不再依赖 CNB_WEB_ENDPOINT 环境变量。
148
+ *
149
+ * @returns {String} Web 平台地址,解析失败返回空字符串
150
+ */
151
+ function resolveWebEndpoint(): string {
152
+ let apiDomain = '';
153
+ try {
154
+ apiDomain = resolveApiDomain() || '';
155
+ } catch {
156
+ return '';
157
+ }
158
+ const trimmed = String(apiDomain).trim();
159
+ if (!trimmed) return '';
160
+ try {
161
+ const u = new URL(trimmed);
162
+ // 只去掉主机名的 api. 前缀,保留协议与自定义端口
163
+ return `${u.protocol}//${u.host.replace(/^api\./, '')}`;
164
+ } catch {
165
+ // 非法地址:按纯主机名处理,仍尝试去 api. 前缀
166
+ return trimmed.replace(/^api\./, '');
167
+ }
168
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cnbcool/cnb-api-generate",
3
- "version": "2.17.2",
3
+ "version": "2.17.3",
4
4
  "main": "./built/index.js",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",