@lark-apaas/client-toolkit 1.2.1-alpha.20 → 1.2.1-alpha.22
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/utils/axiosConfig.d.ts +1 -1
- package/lib/utils/axiosConfig.js +14 -13
- package/package.json +1 -1
|
@@ -14,4 +14,4 @@ declare module 'axios' {
|
|
|
14
14
|
* @param originalInstance 原始的 axios 实例
|
|
15
15
|
*/
|
|
16
16
|
export declare function wrapAxiosWithTrace(originalInstance: AxiosInstance): AxiosInstance;
|
|
17
|
-
export declare function initAxiosConfig(axiosInstance?: AxiosInstance):
|
|
17
|
+
export declare function initAxiosConfig(axiosInstance?: AxiosInstance): void;
|
package/lib/utils/axiosConfig.js
CHANGED
|
@@ -130,6 +130,7 @@ async function logResponse(ok, responseOrError) {
|
|
|
130
130
|
}
|
|
131
131
|
const requestStacktraceMap = new Map();
|
|
132
132
|
function wrapAxiosWithTrace(originalInstance) {
|
|
133
|
+
const originalRequestMethod = originalInstance.request;
|
|
133
134
|
const tracedRequest = function(configOrUrl, config) {
|
|
134
135
|
let finalConfig;
|
|
135
136
|
if ('string' == typeof configOrUrl) {
|
|
@@ -141,8 +142,8 @@ function wrapAxiosWithTrace(originalInstance) {
|
|
|
141
142
|
if (!finalConfig.__span) {
|
|
142
143
|
const url = finalConfig.url || '';
|
|
143
144
|
const actualMethod = (finalConfig.method || 'GET').toUpperCase();
|
|
144
|
-
const
|
|
145
|
-
span = observable.startSpan(`${actualMethod} ${
|
|
145
|
+
const path = url.split('?')[0] || '/';
|
|
146
|
+
span = observable.startSpan(`${actualMethod} ${path}`);
|
|
146
147
|
if (span) {
|
|
147
148
|
const spanContext = span.spanContext();
|
|
148
149
|
if (spanContext && spanContext.traceId) {
|
|
@@ -150,15 +151,16 @@ function wrapAxiosWithTrace(originalInstance) {
|
|
|
150
151
|
if (!finalConfig.headers) finalConfig.headers = {};
|
|
151
152
|
finalConfig.headers['X-Tt-TraceInfo'] = traceInfo;
|
|
152
153
|
finalConfig.__span = span;
|
|
154
|
+
finalConfig._startTime = finalConfig._startTime || Date.now();
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
157
|
}
|
|
156
158
|
} catch (e) {
|
|
157
159
|
console.error('[ProxyTrace] Start axios trace span failed:', e);
|
|
158
160
|
}
|
|
159
|
-
const promise =
|
|
161
|
+
const promise = originalRequestMethod.call(originalInstance, finalConfig);
|
|
160
162
|
return promise.then((response)=>{
|
|
161
|
-
handleSpanEnd(response.config, response, null);
|
|
163
|
+
handleSpanEnd(response.config || finalConfig, response, null);
|
|
162
164
|
return response;
|
|
163
165
|
}, (error)=>{
|
|
164
166
|
handleSpanEnd(error.config || finalConfig, null, error);
|
|
@@ -169,12 +171,12 @@ function wrapAxiosWithTrace(originalInstance) {
|
|
|
169
171
|
try {
|
|
170
172
|
const currentSpan = cfg?.__span;
|
|
171
173
|
if (!currentSpan) return;
|
|
172
|
-
const startTime = cfg._startTime;
|
|
173
|
-
const url = cfg.url || "";
|
|
174
|
-
const method = (cfg.method || 'GET').toUpperCase();
|
|
175
|
-
const path = url.split('?')[0] || '/';
|
|
176
174
|
const errorResponse = error?.response || {};
|
|
177
175
|
const errorMessage = error?.message || '未知错误';
|
|
176
|
+
const startTime = cfg._startTime;
|
|
177
|
+
const url = response?.request?.responseURL || errorResponse?.request?.responseURL || cfg.url || "";
|
|
178
|
+
const method = (cfg.method || 'GET').toUpperCase();
|
|
179
|
+
const path = url.split('?')[0].replace(/^https?:\/\/[^/]+/, '') || '/';
|
|
178
180
|
const logData = {
|
|
179
181
|
method,
|
|
180
182
|
path,
|
|
@@ -207,26 +209,25 @@ function wrapAxiosWithTrace(originalInstance) {
|
|
|
207
209
|
});
|
|
208
210
|
}
|
|
209
211
|
function initAxiosConfig(axiosInstance) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
+
if (!axiosInstance) axiosInstance = axios;
|
|
213
|
+
axiosInstance.interceptors.request.use((config)=>{
|
|
212
214
|
const requestUUID = crypto.randomUUID();
|
|
213
215
|
if ('production' !== process.env.NODE_ENV) {
|
|
214
216
|
const stacktrace = getStacktrace();
|
|
215
217
|
requestStacktraceMap.set(requestUUID, stacktrace);
|
|
216
218
|
}
|
|
217
219
|
config._requestUUID = requestUUID;
|
|
218
|
-
config._startTime = Date.now();
|
|
220
|
+
config._startTime = config._startTime || Date.now();
|
|
219
221
|
const csrfToken = window.csrfToken;
|
|
220
222
|
if (csrfToken) config.headers['X-Suda-Csrf-Token'] = csrfToken;
|
|
221
223
|
return config;
|
|
222
224
|
}, (error)=>Promise.reject(error));
|
|
223
|
-
'production' !== process.env.NODE_ENV &&
|
|
225
|
+
'production' !== process.env.NODE_ENV && axiosInstance.interceptors.response.use((response)=>{
|
|
224
226
|
logResponse('success', response);
|
|
225
227
|
return response;
|
|
226
228
|
}, (error)=>{
|
|
227
229
|
logResponse('error', error.response || error);
|
|
228
230
|
return Promise.reject(error);
|
|
229
231
|
});
|
|
230
|
-
return wrapAxiosWithTrace(instance);
|
|
231
232
|
}
|
|
232
233
|
export { initAxiosConfig, wrapAxiosWithTrace };
|