@lark-apaas/client-toolkit 1.2.28-alpha.9 → 1.2.29

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,5 +1,11 @@
1
1
  import { IUserProfile } from '../apis/udt-types';
2
- export interface ICompatibilityUserProfile extends IUserProfile {
2
+ /**
3
+ * useCurrentUserProfile 的返回类型。
4
+ * 初始状态为空对象 `{}`(所有字段均为 undefined),异步获取用户信息后字段才会被填充。
5
+ * 使用时必须通过可选链或空值合并进行安全访问,如 `userInfo?.user_id`。
6
+ * 判断是否已加载完成应使用 `if (!userInfo?.user_id)` 而非 `if (!userInfo)`(因为空对象是 truthy)。
7
+ */
8
+ export type ICompatibilityUserProfile = Partial<IUserProfile & {
3
9
  /**
4
10
  * @deprecated please use `name`
5
11
  */
@@ -8,5 +14,14 @@ export interface ICompatibilityUserProfile extends IUserProfile {
8
14
  * @deprecated please use `avatar`
9
15
  */
10
16
  userAvatar: string;
11
- }
12
- export declare const useCurrentUserProfile: () => ICompatibilityUserProfile;
17
+ }>;
18
+ export declare const useCurrentUserProfile: () => Partial<IUserProfile & {
19
+ /**
20
+ * @deprecated please use `name`
21
+ */
22
+ userName: string;
23
+ /**
24
+ * @deprecated please use `avatar`
25
+ */
26
+ userAvatar: string;
27
+ }>;
@@ -1,5 +1,11 @@
1
1
  import { IUserProfile } from '../apis/udt-types';
2
- export declare function getCurrentUserProfile(): IUserProfile;
2
+ /**
3
+ * 获取当前用户信息。
4
+ * 返回 Partial<IUserProfile>,因为初始值来自 window._userInfo,
5
+ * 该值在运行时始终为空对象 {},所有字段均为 undefined。
6
+ * 异步获取用户信息后才会被填充为完整的 IUserProfile。
7
+ */
8
+ export declare function getCurrentUserProfile(): Partial<IUserProfile>;
3
9
  /**
4
10
  * @deprecated 请使用 getCurrentUserProfile 代替
5
11
  */
@@ -4,6 +4,92 @@ import { logger } from "../apis/logger.js";
4
4
  import { getStacktrace } from "../logger/selected-logs.js";
5
5
  import { safeStringify } from "./safeStringify.js";
6
6
  import { slardar } from "@lark-apaas/internal-slardar";
7
+ import { normalizeBasePath } from "./utils.js";
8
+ const APP_CLIENT_API_LOG_TYPE = 'app_client_api_log';
9
+ function stripBasePath(urlPath) {
10
+ const base = normalizeBasePath(process.env.CLIENT_BASE_PATH);
11
+ if (base && urlPath.startsWith(base)) return urlPath.slice(base.length) || '/';
12
+ return urlPath;
13
+ }
14
+ let _pageRoutes = null;
15
+ function getPageRouteDefinitions() {
16
+ if (_pageRoutes) return _pageRoutes;
17
+ try {
18
+ const raw = process.env.__PAGE_ROUTE_DEFINITIONS__;
19
+ if (raw) {
20
+ const parsed = JSON.parse(raw);
21
+ if (Array.isArray(parsed)) {
22
+ _pageRoutes = parsed;
23
+ return _pageRoutes;
24
+ }
25
+ }
26
+ } catch {}
27
+ _pageRoutes = [];
28
+ return _pageRoutes;
29
+ }
30
+ let _apiRoutes = null;
31
+ function getApiRouteDefinitions() {
32
+ if (_apiRoutes) return _apiRoutes;
33
+ try {
34
+ const raw = process.env.__API_ROUTE_DEFINITIONS__;
35
+ if (raw) {
36
+ const parsed = JSON.parse(raw);
37
+ if (Array.isArray(parsed)) {
38
+ _apiRoutes = parsed;
39
+ return _apiRoutes;
40
+ }
41
+ }
42
+ } catch {}
43
+ _apiRoutes = [];
44
+ return _apiRoutes;
45
+ }
46
+ function matchRoute(concretePath, routes) {
47
+ const segments = concretePath.split('/').filter(Boolean);
48
+ for (const route of routes){
49
+ const routeSegments = route.path.split('/').filter(Boolean);
50
+ if (routeSegments.length !== segments.length) continue;
51
+ let match = true;
52
+ for(let i = 0; i < routeSegments.length; i++)if (!routeSegments[i].startsWith(':')) {
53
+ if (routeSegments[i] !== segments[i]) {
54
+ match = false;
55
+ break;
56
+ }
57
+ }
58
+ if (match) return route.path;
59
+ }
60
+ return null;
61
+ }
62
+ function matchApiRoute(method, concretePath) {
63
+ const routes = getApiRouteDefinitions();
64
+ const segments = concretePath.split('/').filter(Boolean);
65
+ for (const route of routes){
66
+ if ('*' !== route.method && route.method !== method) continue;
67
+ const routeSegments = route.path.split('/').filter(Boolean);
68
+ if (routeSegments.length !== segments.length) continue;
69
+ let match = true;
70
+ for(let i = 0; i < routeSegments.length; i++)if (!routeSegments[i].startsWith(':')) {
71
+ if (routeSegments[i] !== segments[i]) {
72
+ match = false;
73
+ break;
74
+ }
75
+ }
76
+ if (match) return route.path;
77
+ }
78
+ return null;
79
+ }
80
+ function getRefererPath() {
81
+ try {
82
+ if ('undefined' == typeof window || !window.location?.pathname) return '/';
83
+ const rawPath = stripBasePath(window.location.pathname);
84
+ return matchRoute(rawPath, getPageRouteDefinitions()) || rawPath;
85
+ } catch {
86
+ return '/';
87
+ }
88
+ }
89
+ function getApiField(method, path) {
90
+ const matched = matchApiRoute(method, path);
91
+ return `${method} ${matched || path}`;
92
+ }
7
93
  const isValidResponse = (resp)=>null != resp && 'object' == typeof resp && 'config' in resp && null !== resp.config && void 0 !== resp.config && 'object' == typeof resp.config && 'status' in resp && 'number' == typeof resp.status && 'data' in resp;
8
94
  async function logResponse(ok, responseOrError) {
9
95
  if (isValidResponse(responseOrError)) {
@@ -139,13 +225,20 @@ function handleSpanEnd(cfg, response, error) {
139
225
  const errorMessage = error?.message || '未知错误';
140
226
  const url = response?.request?.responseURL || errorResponse?.request?.responseURL || cfg.url || "";
141
227
  const method = (cfg.method || 'GET').toUpperCase();
142
- const path = url.split('?')[0].replace(/^https?:\/\/[^/]+/, '') || '/';
228
+ const rawPath = url.split('?')[0].replace(/^https?:\/\/[^/]+/, '') || '/';
229
+ const path = stripBasePath(rawPath);
230
+ const durationMs = startTime ? Date.now() - startTime : void 0;
231
+ const referer_path = getRefererPath();
232
+ const api = getApiField(method, path);
233
+ const type = APP_CLIENT_API_LOG_TYPE;
143
234
  const logData = {
144
235
  method,
145
236
  path,
146
237
  url,
147
- duration_ms: startTime ? Date.now() - startTime : void 0,
148
- status: response ? response.status : errorResponse.status || 0
238
+ duration_ms: durationMs,
239
+ status: response ? response.status : errorResponse.status || 0,
240
+ referer_path,
241
+ type
149
242
  };
150
243
  if (error) logData.error_message = errorMessage;
151
244
  if ('undefined' != typeof navigator) logData.user_agent = navigator.userAgent;
@@ -158,7 +251,19 @@ function handleSpanEnd(cfg, response, error) {
158
251
  const responseData = response?.data || errorResponse?.data;
159
252
  if (responseData) logData.response = responseData;
160
253
  const level = error ? 'ERROR' : 'INFO';
161
- observable.log(level, safeStringify(logData), {}, currentSpan);
254
+ observable.log(level, safeStringify(logData), {
255
+ referer_path,
256
+ api,
257
+ type,
258
+ duration_ms: durationMs
259
+ }, currentSpan);
260
+ if ('function' == typeof currentSpan.setAttributes) currentSpan.setAttributes({
261
+ referer_path,
262
+ api,
263
+ duration_ms: durationMs,
264
+ module: 'app_web',
265
+ source_type: 'platform'
266
+ });
162
267
  'function' == typeof currentSpan.end && currentSpan.end();
163
268
  } catch (e) {
164
269
  console.error('[AxiosTrace] Log span failed:', e);
@@ -240,6 +345,11 @@ function initAxiosConfig(axiosInstance) {
240
345
  const csrfToken = window.csrfToken;
241
346
  if (csrfToken) config.headers['X-Suda-Csrf-Token'] = csrfToken;
242
347
  if ('undefined' != typeof window && window.location?.pathname) config.headers['X-Page-Route'] = window.location.pathname;
348
+ const refererPath = getRefererPath();
349
+ config.headers['Rpc-Persist-Apaas-Observability-Referer-Path'] = refererPath;
350
+ const reqMethod = (config.method || 'GET').toUpperCase();
351
+ const requestPath = stripBasePath((config.url || '').split('?')[0]);
352
+ config.headers['Rpc-Persist-Apaas-Observability-Api'] = getApiField(reqMethod, requestPath);
243
353
  return config;
244
354
  }, (error)=>Promise.reject(error));
245
355
  instance.interceptors.response.use((response)=>response, (error)=>{
@@ -9,6 +9,12 @@
9
9
  * @see docs/RFC_HMR_API.md
10
10
  */
11
11
  export interface HmrApi {
12
+ /**
13
+ * 注册 HMR 更新前回调(模块替换之前触发)
14
+ * @param callback 回调函数
15
+ * @returns cleanup 函数,用于取消注册
16
+ */
17
+ onBeforeApply?(callback: () => void): () => void;
12
18
  /**
13
19
  * 注册 HMR 成功回调
14
20
  * @param callback 成功回调函数
@@ -24,7 +30,7 @@ export interface HmrApi {
24
30
  }
25
31
  declare global {
26
32
  interface Window {
27
- __VITE_HMR__?: HmrApi;
33
+ __VITE_HMR__?: Required<HmrApi>;
28
34
  }
29
35
  }
30
36
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.28-alpha.9",
3
+ "version": "1.2.29",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -103,7 +103,7 @@
103
103
  "@lark-apaas/auth-sdk": "^0.1.2",
104
104
  "@lark-apaas/client-capability": "^0.1.6",
105
105
  "@lark-apaas/internal-slardar": "^0.0.3",
106
- "@lark-apaas/miaoda-inspector": "^1.0.20",
106
+ "@lark-apaas/miaoda-inspector": "^1.0.21",
107
107
  "@lark-apaas/observable-web": "^1.0.5",
108
108
  "@radix-ui/react-avatar": "^1.1.10",
109
109
  "@radix-ui/react-popover": "^1.1.15",