@lark-apaas/client-toolkit 1.2.1-alpha.0 → 1.2.1-alpha.10

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.
@@ -0,0 +1 @@
1
+ export * from '../trace';
@@ -0,0 +1 @@
1
+ export * from "../trace/index.js";
@@ -4,6 +4,7 @@ import { useLocation, useNavigate } from "react-router-dom";
4
4
  import { connectToParent } from "penpal";
5
5
  import { useUpdatingRef } from "../../hooks/useUpdatingRef.js";
6
6
  import { submitPostMessage } from "../../utils/postMessage.js";
7
+ import { getPreviewParentOrigin } from "../../utils/getParentOrigin.js";
7
8
  import { childApi } from "./utils/childApi.js";
8
9
  import "./utils/listenHot.js";
9
10
  var IframeBridge_RouteMessageType = /*#__PURE__*/ function(RouteMessageType) {
@@ -20,10 +21,8 @@ async function connectParent() {
20
21
  type: 'PreviewReady',
21
22
  data: {}
22
23
  });
23
- const parentOrigin = globalThis.process?.env?.FORCE_FRAMEWORK_DOMAIN_MAIN;
24
- if (!parentOrigin) return;
25
24
  const connection = connectToParent({
26
- parentOrigin,
25
+ parentOrigin: getPreviewParentOrigin(),
27
26
  methods: {
28
27
  ...childApi
29
28
  }
@@ -36,13 +35,13 @@ function IframeBridge() {
36
35
  const navigate = useNavigate();
37
36
  const navigateRef = useUpdatingRef(navigate);
38
37
  const isActive = useRef(false);
39
- const historyBack = useCallback((_payload)=>{
38
+ const historyBack = useCallback(()=>{
40
39
  navigateRef.current(-1);
41
40
  isActive.current = true;
42
41
  }, [
43
42
  navigateRef
44
43
  ]);
45
- const historyForward = useCallback((_payload)=>{
44
+ const historyForward = useCallback(()=>{
46
45
  navigateRef.current(1);
47
46
  isActive.current = true;
48
47
  }, [
@@ -70,8 +69,8 @@ function IframeBridge() {
70
69
  location
71
70
  ]);
72
71
  const handleMessage = useCallback((event)=>{
73
- const data = event.data ?? {};
74
- if ('string' == typeof data.type && isRouteMessageType(data.type)) operatorMessage[data.type](data.data);
72
+ const { data } = event;
73
+ if (isRouteMessageType(data?.type)) operatorMessage[data?.type](data?.data);
75
74
  }, [
76
75
  operatorMessage
77
76
  ]);
@@ -6,6 +6,7 @@ const initObservable = ()=>{
6
6
  env: 'development' === process.env.NODE_ENV ? AppEnv.Dev : AppEnv.Prod,
7
7
  collectorUrl: {
8
8
  log: `/spark/app/${window.appId}/runtime/api/v1/observability/logs/collect`,
9
+ trace: `/spark/app/${window.appId}/runtime/api/v1/observability/traces/collect`,
9
10
  metric: `/spark/app/${window.appId}/runtime/api/v1/observability/metrics/collect`
10
11
  }
11
12
  });
@@ -0,0 +1,2 @@
1
+ declare const trace: any;
2
+ export { trace };
@@ -0,0 +1,3 @@
1
+ import { observable } from "@lark-apaas/observable-web";
2
+ const trace = observable.trace.bind(observable);
3
+ export { trace };
@@ -1,4 +1,14 @@
1
1
  import { AxiosInstance } from 'axios';
2
+ declare module 'axios' {
3
+ interface AxiosRequestConfig {
4
+ /** @internal 存储 Span 实例 */
5
+ __span?: any;
6
+ /** @internal 请求开始时间 */
7
+ _startTime?: number;
8
+ /** @internal 请求唯一标识 */
9
+ _requestUUID?: string;
10
+ }
11
+ }
2
12
  /**
3
13
  * axios配置内容:拦截器、日志等
4
14
  */
@@ -1,6 +1,8 @@
1
1
  import axios from "axios";
2
+ import { observable } from "@lark-apaas/observable-web";
2
3
  import { logger } from "../apis/logger.js";
3
4
  import { getStacktrace } from "../logger/selected-logs.js";
5
+ import { safeStringify } from "./safeStringify.js";
4
6
  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;
5
7
  async function logResponse(ok, responseOrError) {
6
8
  if (isValidResponse(responseOrError)) {
@@ -52,7 +54,7 @@ async function logResponse(ok, responseOrError) {
52
54
  logTraceID
53
55
  };
54
56
  if (stacktrace) logMeta.stacktrace = stacktrace;
55
- if ('development' === process.env.NODE_ENV) logger.log({
57
+ logger.debug({
56
58
  level: ok,
57
59
  args: [
58
60
  parts.join(''),
@@ -108,7 +110,7 @@ async function logResponse(ok, responseOrError) {
108
110
  const stacktrace = await requestStacktraceMap.get(requestUUID);
109
111
  const logMeta = {};
110
112
  if (stacktrace) logMeta.stacktrace = stacktrace;
111
- logger.log({
113
+ logger.debug({
112
114
  level: 'error',
113
115
  args: [
114
116
  parts.join(''),
@@ -118,7 +120,7 @@ async function logResponse(ok, responseOrError) {
118
120
  });
119
121
  return;
120
122
  }
121
- logger.log({
123
+ logger.debug({
122
124
  level: 'error',
123
125
  args: [
124
126
  '请求失败:无响应对象或配置信息'
@@ -129,6 +131,61 @@ async function logResponse(ok, responseOrError) {
129
131
  const requestStacktraceMap = new Map();
130
132
  function initAxiosConfig(axiosInstance) {
131
133
  if (!axiosInstance) axiosInstance = axios;
134
+ axiosInstance.interceptors.request.use((config)=>{
135
+ const method = (config.method || 'GET').toUpperCase();
136
+ const url = config.url || '';
137
+ const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
138
+ try {
139
+ const span = observable.startSpan(`${method} ${cleanedPath}`);
140
+ if (span) {
141
+ const traceInfo = `${span.spanContext().traceId}-${span.spanContext().spanId}`;
142
+ config.headers["X-Tt-TraceInfo"] = traceInfo;
143
+ config.__span = span;
144
+ }
145
+ } catch (error) {
146
+ console.error('Trace 配置失败:', error);
147
+ }
148
+ return config;
149
+ });
150
+ axiosInstance.interceptors.response.use((response)=>{
151
+ const { __span: span, _startTime: startTime, url = "" } = response.config || {};
152
+ const method = (response.config.method || 'GET').toUpperCase();
153
+ const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
154
+ if (span) {
155
+ observable.log('INFO', safeStringify({
156
+ method,
157
+ path: cleanedPath,
158
+ url,
159
+ user_agent: response.config.headers["user-agent"],
160
+ duration_ms: startTime ? Date.now() - startTime : void 0,
161
+ status: response.status,
162
+ request_body: response.config.data,
163
+ response: response.data
164
+ }), {}, span);
165
+ 'function' == typeof span.end && span.end();
166
+ }
167
+ return response;
168
+ }, (error)=>{
169
+ const { config: errorConfig = {}, response: errorResponse = {}, message: errorMessage = '未知错误' } = error;
170
+ const { __span: span, _startTime: startTime, url = "" } = errorConfig || {};
171
+ const method = (errorConfig.method || 'GET').toUpperCase();
172
+ const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
173
+ if (span) {
174
+ observable.log('ERROR', safeStringify({
175
+ method,
176
+ path: cleanedPath,
177
+ url,
178
+ user_agent: errorConfig.headers?.["user-agent"],
179
+ duration_ms: startTime ? Date.now() - startTime : void 0,
180
+ status: errorResponse.status || 200,
181
+ request_body: errorConfig.data,
182
+ response: errorResponse.data || {},
183
+ error_message: errorMessage
184
+ }), {}, span);
185
+ 'function' == typeof span.end && span.end();
186
+ }
187
+ return Promise.reject(error);
188
+ });
132
189
  axiosInstance.interceptors.request.use((config)=>{
133
190
  const requestUUID = crypto.randomUUID();
134
191
  if ('production' !== process.env.NODE_ENV) {
@@ -1 +1,6 @@
1
1
  export declare function getEnv(): 'BOE' | 'PRE' | 'ONLINE';
2
+ /**
3
+ * @internal
4
+ * 获取预览环境父级域名
5
+ */
6
+ export declare function getPreviewParentOrigin(): "https://force.feishu.cn" | "https://force.feishu-pre.cn" | "https://force.feishu-boe.cn" | "https://miaoda.feishu.cn" | "https://miaoda.feishu-pre.cn" | "https://miaoda.feishu-boe.cn";
@@ -4,4 +4,13 @@ function getEnv() {
4
4
  if (origin.includes('fsapp.kundou.cn') || origin.includes('miaoda-pre.feishuapp.net')) return 'PRE';
5
5
  return 'BOE';
6
6
  }
7
- export { getEnv };
7
+ function getPreviewParentOrigin() {
8
+ const { origin } = window.location;
9
+ if (origin.includes('force.feishuapp.net')) return 'https://force.feishu.cn';
10
+ if (origin.includes('force-pre.feishuapp.net')) return 'https://force.feishu-pre.cn';
11
+ if (origin.includes('force.byted.org')) return 'https://force.feishu-boe.cn';
12
+ if (origin.includes('feishuapp.cn') || origin.includes('miaoda.feishuapp.net')) return 'https://miaoda.feishu.cn';
13
+ if (origin.includes('fsapp.kundou.cn') || origin.includes('miaoda-pre.feishuapp.net')) return 'https://miaoda.feishu-pre.cn';
14
+ return 'https://miaoda.feishu-boe.cn';
15
+ }
16
+ export { getEnv, getPreviewParentOrigin };
@@ -1,9 +1,7 @@
1
+ import { getPreviewParentOrigin } from "./getParentOrigin.js";
1
2
  function submitPostMessage(message, targetOrigin) {
2
3
  try {
3
- const parentOrigin = globalThis.process?.env?.FORCE_FRAMEWORK_DOMAIN_MAIN;
4
- const origin = targetOrigin ?? parentOrigin;
5
- if (!origin) return;
6
- window.parent.postMessage(message, origin);
4
+ window.parent.postMessage(message, targetOrigin ?? getPreviewParentOrigin());
7
5
  } catch (e) {
8
6
  console.error('postMessage error', e);
9
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.1-alpha.0",
3
+ "version": "1.2.1-alpha.10",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -31,6 +31,11 @@
31
31
  "require": "./lib/apis/logger.js",
32
32
  "types": "./lib/apis/logger.d.ts"
33
33
  },
34
+ "./trace": {
35
+ "import": "./lib/apis/trace.js",
36
+ "require": "./lib/apis/trace.js",
37
+ "types": "./lib/apis/trace.d.ts"
38
+ },
34
39
  "./udt-types": {
35
40
  "import": "./lib/apis/udt-types.js",
36
41
  "require": "./lib/apis/udt-types.js",
@@ -82,8 +87,8 @@
82
87
  "@ant-design/cssinjs": "^1.24.0",
83
88
  "@data-loom/js": "^0.4.3",
84
89
  "@lark-apaas/client-capability": "^0.1.1",
85
- "@lark-apaas/miaoda-inspector": "1.0.10-alpha.0",
86
- "@lark-apaas/observable-web": "^1.0.0",
90
+ "@lark-apaas/miaoda-inspector": "^1.0.8",
91
+ "@lark-apaas/observable-web": "1.0.1-alpha.2",
87
92
  "@radix-ui/react-avatar": "^1.1.10",
88
93
  "@radix-ui/react-popover": "^1.1.15",
89
94
  "@radix-ui/react-slot": "^1.2.3",
@@ -157,4 +162,4 @@
157
162
  "react-router-dom": ">=6.26.2",
158
163
  "styled-jsx": ">=5.0.0"
159
164
  }
160
- }
165
+ }