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

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.
@@ -131,22 +131,75 @@ async function logResponse(ok, responseOrError) {
131
131
  const requestStacktraceMap = new Map();
132
132
  function initAxiosConfig(axiosInstance) {
133
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);
134
+ try {
135
+ if (axiosInstance && !axiosInstance._isTraced) {
136
+ axiosInstance._isTraced = true;
137
+ const methodsWithNoData = [
138
+ 'get',
139
+ 'delete',
140
+ 'head',
141
+ 'options'
142
+ ];
143
+ const methodsWithData = [
144
+ 'post',
145
+ 'put',
146
+ 'patch',
147
+ "postForm",
148
+ "putForm",
149
+ "patchForm"
150
+ ];
151
+ const allMethods = [
152
+ 'request',
153
+ ...methodsWithNoData,
154
+ ...methodsWithData
155
+ ];
156
+ allMethods.forEach((methodName)=>{
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);
195
+ }
196
+ return originalMethod.apply(this, args);
197
+ };
198
+ });
147
199
  }
148
- return config;
149
- });
200
+ } catch (e) {
201
+ console.error('Failed to init axios trace config wrapper:', e);
202
+ }
150
203
  axiosInstance.interceptors.response.use((response)=>{
151
204
  const { __span: span, _startTime: startTime, url = "" } = response.config || {};
152
205
  const method = (response.config.method || 'GET').toUpperCase();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.1-alpha.10",
3
+ "version": "1.2.1-alpha.12",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [