@lark-apaas/client-toolkit 1.2.1-alpha.16 → 1.2.1-alpha.18
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.
|
@@ -9,7 +9,5 @@ declare module 'axios' {
|
|
|
9
9
|
_requestUUID?: string;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
15
|
-
export declare function initAxiosConfig(axiosInstance?: AxiosInstance): AxiosInstance;
|
|
12
|
+
export declare function patchCreate(): void;
|
|
13
|
+
export declare function initAxiosConfig(axiosInstance?: AxiosInstance): void;
|
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,106 @@ async function logResponse(ok, responseOrError) {
|
|
|
129
129
|
});
|
|
130
130
|
}
|
|
131
131
|
const requestStacktraceMap = new Map();
|
|
132
|
-
|
|
133
|
-
|
|
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();
|
|
172
|
-
}
|
|
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);
|
|
132
|
+
let isPrototypeInjected = false;
|
|
133
|
+
function injectAxiosPrototype() {
|
|
134
|
+
if (isPrototypeInjected) return;
|
|
135
|
+
isPrototypeInjected = true;
|
|
136
|
+
const originalRequest = Axios.prototype.request;
|
|
137
|
+
Axios.prototype.request = function(configOrUrl, config) {
|
|
138
|
+
let finalConfig;
|
|
139
|
+
if ('string' == typeof configOrUrl) {
|
|
140
|
+
finalConfig = config || {};
|
|
141
|
+
finalConfig.url = configOrUrl;
|
|
142
|
+
} else finalConfig = configOrUrl || {};
|
|
143
|
+
let span;
|
|
144
|
+
try {
|
|
145
|
+
if (!finalConfig.__span) {
|
|
146
|
+
const url = finalConfig.url || '';
|
|
147
|
+
const actualMethod = (finalConfig.method || 'GET').toUpperCase();
|
|
148
|
+
const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
|
|
149
|
+
span = observable.startSpan(`${actualMethod} ${cleanedPath}`);
|
|
150
|
+
if (span) {
|
|
151
|
+
const spanContext = span.spanContext();
|
|
152
|
+
if (spanContext && spanContext.traceId) {
|
|
153
|
+
const traceInfo = `${spanContext.traceId}-${spanContext.spanId}`;
|
|
154
|
+
if (!finalConfig.headers) finalConfig.headers = {};
|
|
155
|
+
finalConfig.headers['X-Tt-TraceInfo'] = traceInfo;
|
|
156
|
+
finalConfig.__span = span;
|
|
195
157
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
} catch (e) {
|
|
201
|
-
console.error('Failed to init axios trace config wrapper:', e);
|
|
202
|
-
}
|
|
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();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} catch (e) {
|
|
161
|
+
console.error('Failed to start axios trace span in prototype:', e);
|
|
241
162
|
}
|
|
242
|
-
|
|
243
|
-
|
|
163
|
+
const promise = originalRequest.apply(this, arguments);
|
|
164
|
+
return promise.then((response)=>{
|
|
165
|
+
try {
|
|
166
|
+
const currentSpan = response.config?.__span;
|
|
167
|
+
if (currentSpan) {
|
|
168
|
+
const startTime = response.config._startTime;
|
|
169
|
+
const url = response.config.url || "";
|
|
170
|
+
const method = (response.config.method || 'GET').toUpperCase();
|
|
171
|
+
const path = url.split('?')[0] || '/';
|
|
172
|
+
const logData = {
|
|
173
|
+
method,
|
|
174
|
+
path,
|
|
175
|
+
url: response.request?.responseURL || url || "",
|
|
176
|
+
duration_ms: startTime ? Date.now() - startTime : void 0,
|
|
177
|
+
status: response.status
|
|
178
|
+
};
|
|
179
|
+
if ('undefined' != typeof navigator) logData.user_agent = navigator.userAgent;
|
|
180
|
+
if (response.config.data) logData.request_body = response.config.data;
|
|
181
|
+
if (response.data) logData.response = response.data || {};
|
|
182
|
+
observable.log('INFO', safeStringify(logData), {}, currentSpan);
|
|
183
|
+
'function' == typeof currentSpan.end && currentSpan.end();
|
|
184
|
+
}
|
|
185
|
+
} catch (e) {
|
|
186
|
+
console.error('Failed to log success span:', e);
|
|
187
|
+
}
|
|
188
|
+
return response;
|
|
189
|
+
}, (error)=>{
|
|
190
|
+
try {
|
|
191
|
+
const errorConfig = error.config || {};
|
|
192
|
+
const currentSpan = errorConfig.__span;
|
|
193
|
+
if (currentSpan) {
|
|
194
|
+
const errorResponse = error.response || {};
|
|
195
|
+
const startTime = errorConfig._startTime;
|
|
196
|
+
const url = errorConfig.url || "";
|
|
197
|
+
const method = (errorConfig.method || 'GET').toUpperCase();
|
|
198
|
+
const path = url.split('?')[0] || '/';
|
|
199
|
+
const errorMessage = error.message || '未知错误';
|
|
200
|
+
const logData = {
|
|
201
|
+
method,
|
|
202
|
+
path,
|
|
203
|
+
url: errorResponse.request?.responseURL || errorConfig.url || "",
|
|
204
|
+
duration_ms: startTime ? Date.now() - startTime : void 0,
|
|
205
|
+
status: errorResponse.status || 200,
|
|
206
|
+
error_message: errorMessage
|
|
207
|
+
};
|
|
208
|
+
if ('undefined' != typeof navigator) logData.user_agent = navigator.userAgent;
|
|
209
|
+
if (errorConfig.data) logData.request_body = errorConfig.data;
|
|
210
|
+
if (errorResponse.data) logData.response = errorResponse.data || {};
|
|
211
|
+
observable.log('ERROR', safeStringify(logData), {}, currentSpan);
|
|
212
|
+
'function' == typeof currentSpan.end && currentSpan.end();
|
|
213
|
+
}
|
|
214
|
+
} catch (e) {
|
|
215
|
+
console.error('Failed to log error span:', e);
|
|
216
|
+
}
|
|
217
|
+
return Promise.reject(error);
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function patchCreate() {
|
|
222
|
+
const originalCreate = axios.create;
|
|
223
|
+
axios.create = function(config) {
|
|
224
|
+
injectAxiosPrototype();
|
|
225
|
+
const instance = originalCreate.call(axios, config);
|
|
226
|
+
initAxiosConfig(instance);
|
|
227
|
+
return instance;
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function initAxiosConfig(axiosInstance) {
|
|
231
|
+
if (!axiosInstance) axiosInstance = axios;
|
|
244
232
|
axiosInstance.interceptors.request.use((config)=>{
|
|
245
233
|
const requestUUID = crypto.randomUUID();
|
|
246
234
|
if ('production' !== process.env.NODE_ENV) {
|
|
@@ -260,13 +248,5 @@ function initAxiosConfig(axiosInstance) {
|
|
|
260
248
|
logResponse('error', error.response || error);
|
|
261
249
|
return Promise.reject(error);
|
|
262
250
|
});
|
|
263
|
-
if ('function' == typeof axiosInstance) axiosInstance = new Proxy(axiosInstance, {
|
|
264
|
-
apply (target, thisArg, args) {
|
|
265
|
-
const requestMethod = target.request;
|
|
266
|
-
if ('function' != typeof requestMethod) return Reflect.apply(target, thisArg, args);
|
|
267
|
-
return requestMethod.apply(target, args);
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
return axiosInstance;
|
|
271
251
|
}
|
|
272
|
-
export { initAxiosConfig };
|
|
252
|
+
export { initAxiosConfig, patchCreate };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
-
import { initAxiosConfig } from "./axiosConfig.js";
|
|
2
|
+
import { initAxiosConfig, patchCreate } from "./axiosConfig.js";
|
|
3
3
|
let axiosInstance;
|
|
4
4
|
function showToast(message, duration = 3000) {
|
|
5
5
|
return new Promise((resolve)=>{
|
|
@@ -54,6 +54,7 @@ function showToast(message, duration = 3000) {
|
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
function getAxiosForBackend() {
|
|
57
|
+
patchCreate();
|
|
57
58
|
if (!axiosInstance) {
|
|
58
59
|
axiosInstance = axios.create({
|
|
59
60
|
baseURL: process.env.CLIENT_BASE_PATH || '/'
|
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.18",
|
|
4
4
|
"types": "./lib/index.d.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"@data-loom/js": "^0.4.3",
|
|
89
89
|
"@lark-apaas/client-capability": "^0.1.2",
|
|
90
90
|
"@lark-apaas/miaoda-inspector": "^1.0.8",
|
|
91
|
-
"@lark-apaas/observable-web": "1.0.1-alpha.
|
|
91
|
+
"@lark-apaas/observable-web": "1.0.1-alpha.3",
|
|
92
92
|
"@radix-ui/react-avatar": "^1.1.10",
|
|
93
93
|
"@radix-ui/react-popover": "^1.1.15",
|
|
94
94
|
"@radix-ui/react-slot": "^1.2.3",
|