@lark-apaas/client-toolkit 1.2.1-alpha.15 → 1.2.1-alpha.17
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/lib/index.js +3 -1
- package/lib/utils/axiosConfig.d.ts +0 -3
- package/lib/utils/axiosConfig.js +85 -107
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createClient } from "@lark-apaas/client-capability";
|
|
2
2
|
import { normalizeBasePath } from "./utils/utils.js";
|
|
3
|
+
import { logger } from "./logger/index.js";
|
|
3
4
|
import { version } from "../package.json";
|
|
4
5
|
const capabilityClient = createClient({
|
|
5
6
|
baseURL: normalizeBasePath(process.env.CLIENT_BASE_PATH),
|
|
@@ -7,7 +8,8 @@ const capabilityClient = createClient({
|
|
|
7
8
|
headers: {
|
|
8
9
|
'X-Suda-Csrf-Token': window.csrfToken ?? ''
|
|
9
10
|
}
|
|
10
|
-
}
|
|
11
|
+
},
|
|
12
|
+
logger: logger
|
|
11
13
|
});
|
|
12
14
|
const src = {
|
|
13
15
|
version: version
|
package/lib/utils/axiosConfig.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import axios from "axios";
|
|
1
|
+
import axios, { Axios } from "axios";
|
|
2
2
|
import { observable } from "@lark-apaas/observable-web";
|
|
3
3
|
import { logger } from "../apis/logger.js";
|
|
4
4
|
import { getStacktrace } from "../logger/selected-logs.js";
|
|
@@ -129,118 +129,96 @@ async function logResponse(ok, responseOrError) {
|
|
|
129
129
|
});
|
|
130
130
|
}
|
|
131
131
|
const requestStacktraceMap = new Map();
|
|
132
|
+
let isPrototypeInjected = false;
|
|
132
133
|
function initAxiosConfig(axiosInstance) {
|
|
133
134
|
if (!axiosInstance) axiosInstance = axios;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const originalMethod = axiosInstance[methodName];
|
|
158
|
-
if ('function' != typeof originalMethod) return;
|
|
159
|
-
axiosInstance[methodName] = function() {
|
|
160
|
-
const args = Array.prototype.slice.call(arguments);
|
|
161
|
-
try {
|
|
162
|
-
let config;
|
|
163
|
-
let url = '';
|
|
164
|
-
let actualMethod = methodName.toUpperCase();
|
|
165
|
-
if ('request' === methodName) if ('string' == typeof args[0]) {
|
|
166
|
-
url = args[0];
|
|
167
|
-
config = args[1] = args[1] || {};
|
|
168
|
-
} else {
|
|
169
|
-
config = args[0] = args[0] || {};
|
|
170
|
-
url = config.url || '';
|
|
171
|
-
actualMethod = (config.method || 'GET').toUpperCase();
|
|
135
|
+
if (!isPrototypeInjected) {
|
|
136
|
+
isPrototypeInjected = true;
|
|
137
|
+
const originalRequest = Axios.prototype.request;
|
|
138
|
+
Axios.prototype.request = function(configOrUrl, config) {
|
|
139
|
+
let finalConfig;
|
|
140
|
+
if ('string' == typeof configOrUrl) {
|
|
141
|
+
finalConfig = config || {};
|
|
142
|
+
finalConfig.url = configOrUrl;
|
|
143
|
+
} else finalConfig = configOrUrl || {};
|
|
144
|
+
let span;
|
|
145
|
+
try {
|
|
146
|
+
if (!finalConfig.__span) {
|
|
147
|
+
const url = finalConfig.url || '';
|
|
148
|
+
const actualMethod = (finalConfig.method || 'GET').toUpperCase();
|
|
149
|
+
const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
|
|
150
|
+
span = observable.startSpan(`${actualMethod} ${cleanedPath}`);
|
|
151
|
+
if (span) {
|
|
152
|
+
const spanContext = span.spanContext();
|
|
153
|
+
if (spanContext && spanContext.traceId) {
|
|
154
|
+
const traceInfo = `${spanContext.traceId}-${spanContext.spanId}`;
|
|
155
|
+
if (!finalConfig.headers) finalConfig.headers = {};
|
|
156
|
+
finalConfig.headers['X-Tt-TraceInfo'] = traceInfo;
|
|
157
|
+
finalConfig.__span = span;
|
|
172
158
|
}
|
|
173
|
-
else if (methodsWithNoData.includes(methodName)) {
|
|
174
|
-
url = args[0];
|
|
175
|
-
config = args[1] = args[1] || {};
|
|
176
|
-
} else if (methodsWithData.includes(methodName)) {
|
|
177
|
-
url = args[0];
|
|
178
|
-
config = args[2] = args[2] || {};
|
|
179
|
-
}
|
|
180
|
-
if (config && !config.__span) {
|
|
181
|
-
const cleanedPath = (url || '').split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
|
|
182
|
-
const span = observable.startSpan(`${actualMethod} ${cleanedPath}`);
|
|
183
|
-
if (span) {
|
|
184
|
-
const spanContext = span.spanContext();
|
|
185
|
-
if (spanContext && spanContext.traceId) {
|
|
186
|
-
const traceInfo = `${spanContext.traceId}-${spanContext.spanId}`;
|
|
187
|
-
if (!config.headers) config.headers = {};
|
|
188
|
-
config.headers['X-Tt-TraceInfo'] = traceInfo;
|
|
189
|
-
config.__span = span;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
} catch (innerError) {
|
|
194
|
-
console.error(`Trace wrapper inner error [${methodName}]:`, innerError);
|
|
195
159
|
}
|
|
196
|
-
|
|
197
|
-
|
|
160
|
+
}
|
|
161
|
+
} catch (e) {
|
|
162
|
+
console.error('Failed to start axios trace span in prototype:', e);
|
|
163
|
+
}
|
|
164
|
+
const promise = originalRequest.apply(this, arguments);
|
|
165
|
+
return promise.then((response)=>{
|
|
166
|
+
try {
|
|
167
|
+
const currentSpan = response.config?.__span;
|
|
168
|
+
if (currentSpan) {
|
|
169
|
+
const startTime = response.config._startTime;
|
|
170
|
+
const url = response.config.url || "";
|
|
171
|
+
const method = (response.config.method || 'GET').toUpperCase();
|
|
172
|
+
const path = url.split('?')[0] || '/';
|
|
173
|
+
const logData = {
|
|
174
|
+
method,
|
|
175
|
+
path,
|
|
176
|
+
url: response.request?.responseURL || url || "",
|
|
177
|
+
duration_ms: startTime ? Date.now() - startTime : void 0,
|
|
178
|
+
status: response.status
|
|
179
|
+
};
|
|
180
|
+
if ('undefined' != typeof navigator) logData.user_agent = navigator.userAgent;
|
|
181
|
+
if (response.config.data) logData.request_body = response.config.data;
|
|
182
|
+
if (response.data) logData.response = response.data || {};
|
|
183
|
+
observable.log('INFO', safeStringify(logData), {}, currentSpan);
|
|
184
|
+
'function' == typeof currentSpan.end && currentSpan.end();
|
|
185
|
+
}
|
|
186
|
+
} catch (e) {
|
|
187
|
+
console.error('Failed to log success span:', e);
|
|
188
|
+
}
|
|
189
|
+
return response;
|
|
190
|
+
}, (error)=>{
|
|
191
|
+
try {
|
|
192
|
+
const errorConfig = error.config || {};
|
|
193
|
+
const currentSpan = errorConfig.__span;
|
|
194
|
+
if (currentSpan) {
|
|
195
|
+
const errorResponse = error.response || {};
|
|
196
|
+
const startTime = errorConfig._startTime;
|
|
197
|
+
const url = errorConfig.url || "";
|
|
198
|
+
const method = (errorConfig.method || 'GET').toUpperCase();
|
|
199
|
+
const path = url.split('?')[0] || '/';
|
|
200
|
+
const errorMessage = error.message || '未知错误';
|
|
201
|
+
const logData = {
|
|
202
|
+
method,
|
|
203
|
+
path,
|
|
204
|
+
url: errorResponse.request?.responseURL || errorConfig.url || "",
|
|
205
|
+
duration_ms: startTime ? Date.now() - startTime : void 0,
|
|
206
|
+
status: errorResponse.status || 200,
|
|
207
|
+
error_message: errorMessage
|
|
208
|
+
};
|
|
209
|
+
if ('undefined' != typeof navigator) logData.user_agent = navigator.userAgent;
|
|
210
|
+
if (errorConfig.data) logData.request_body = errorConfig.data;
|
|
211
|
+
if (errorResponse.data) logData.response = errorResponse.data || {};
|
|
212
|
+
observable.log('ERROR', safeStringify(logData), {}, currentSpan);
|
|
213
|
+
'function' == typeof currentSpan.end && currentSpan.end();
|
|
214
|
+
}
|
|
215
|
+
} catch (e) {
|
|
216
|
+
console.error('Failed to log error span:', e);
|
|
217
|
+
}
|
|
218
|
+
return Promise.reject(error);
|
|
198
219
|
});
|
|
199
|
-
}
|
|
200
|
-
} catch (e) {
|
|
201
|
-
console.error('Failed to init axios trace config wrapper:', e);
|
|
220
|
+
};
|
|
202
221
|
}
|
|
203
|
-
axiosInstance.interceptors.response.use((response)=>{
|
|
204
|
-
const { __span: span, _startTime: startTime, url = "" } = response.config || {};
|
|
205
|
-
const method = (response.config.method || 'GET').toUpperCase();
|
|
206
|
-
const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
|
|
207
|
-
if (span) {
|
|
208
|
-
const logData = {
|
|
209
|
-
method,
|
|
210
|
-
path: cleanedPath,
|
|
211
|
-
url: response.request?.responseURL || url || "",
|
|
212
|
-
duration_ms: startTime ? Date.now() - startTime : void 0,
|
|
213
|
-
status: response.status
|
|
214
|
-
};
|
|
215
|
-
if ('undefined' != typeof navigator) logData.user_agent = navigator.userAgent;
|
|
216
|
-
if (response.config.data) logData.request_body = response.config.data;
|
|
217
|
-
if (response.data) logData.response = response.data || {};
|
|
218
|
-
observable.log('INFO', safeStringify(logData), {}, span);
|
|
219
|
-
'function' == typeof span.end && span.end();
|
|
220
|
-
}
|
|
221
|
-
return response;
|
|
222
|
-
}, (error)=>{
|
|
223
|
-
const { config: errorConfig = {}, response: errorResponse = {}, message: errorMessage = '未知错误' } = error;
|
|
224
|
-
const { __span: span, _startTime: startTime, url = "" } = errorConfig || {};
|
|
225
|
-
const method = (errorConfig.method || 'GET').toUpperCase();
|
|
226
|
-
const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
|
|
227
|
-
if (span) {
|
|
228
|
-
const logData = {
|
|
229
|
-
method,
|
|
230
|
-
path: cleanedPath,
|
|
231
|
-
url: errorResponse.request?.responseURL || errorConfig.url || "",
|
|
232
|
-
duration_ms: startTime ? Date.now() - startTime : void 0,
|
|
233
|
-
status: errorResponse.status || 200,
|
|
234
|
-
error_message: errorMessage
|
|
235
|
-
};
|
|
236
|
-
if ('undefined' != typeof navigator) logData.user_agent = navigator.userAgent;
|
|
237
|
-
if (errorConfig.data) logData.request_body = errorConfig.data;
|
|
238
|
-
if (errorResponse.data) logData.response = errorResponse.data || {};
|
|
239
|
-
observable.log('ERROR', safeStringify(logData), {}, span);
|
|
240
|
-
'function' == typeof span.end && span.end();
|
|
241
|
-
}
|
|
242
|
-
return Promise.reject(error);
|
|
243
|
-
});
|
|
244
222
|
axiosInstance.interceptors.request.use((config)=>{
|
|
245
223
|
const requestUUID = crypto.randomUUID();
|
|
246
224
|
if ('production' !== process.env.NODE_ENV) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/client-toolkit",
|
|
3
|
-
"version": "1.2.1-alpha.
|
|
3
|
+
"version": "1.2.1-alpha.17",
|
|
4
4
|
"types": "./lib/index.d.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
"@ant-design/colors": "^7.2.1",
|
|
87
87
|
"@ant-design/cssinjs": "^1.24.0",
|
|
88
88
|
"@data-loom/js": "^0.4.3",
|
|
89
|
-
"@lark-apaas/client-capability": "^0.1.
|
|
89
|
+
"@lark-apaas/client-capability": "^0.1.2",
|
|
90
90
|
"@lark-apaas/miaoda-inspector": "^1.0.8",
|
|
91
91
|
"@lark-apaas/observable-web": "1.0.1-alpha.2",
|
|
92
92
|
"@radix-ui/react-avatar": "^1.1.10",
|