@lark-apaas/client-toolkit 1.2.1-alpha.11 → 1.2.1-alpha.13

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,30 +131,74 @@ async function logResponse(ok, responseOrError) {
131
131
  const requestStacktraceMap = new Map();
132
132
  function initAxiosConfig(axiosInstance) {
133
133
  if (!axiosInstance) axiosInstance = axios;
134
- if (axiosInstance && !axiosInstance._isTraced) {
135
- axiosInstance._isTraced = true;
136
- const originalRequest = axiosInstance.request;
137
- axiosInstance.request = function(urlOrConfig, config) {
138
- try {
139
- const finalConfig = 'string' == typeof urlOrConfig ? config = config || {} : urlOrConfig = urlOrConfig || {};
140
- const method = (finalConfig.method || 'GET').toUpperCase();
141
- const url = finalConfig.url || ('string' == typeof urlOrConfig ? urlOrConfig : '');
142
- const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
143
- const span = observable.startSpan(`${method} ${cleanedPath}`);
144
- if (span) {
145
- const spanContext = span.spanContext();
146
- if (spanContext && spanContext.traceId) {
147
- const traceInfo = `${spanContext.traceId}-${spanContext.spanId}`;
148
- if (!finalConfig.headers) finalConfig.headers = {};
149
- finalConfig.headers['X-Tt-TraceInfo'] = traceInfo;
150
- finalConfig.__span = span;
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);
151
195
  }
152
- }
153
- } catch (error) {
154
- console.error('Trace start failed:', error);
155
- }
156
- return originalRequest.apply(this, arguments);
157
- };
196
+ return originalMethod.apply(this, args);
197
+ };
198
+ });
199
+ }
200
+ } catch (e) {
201
+ console.error('Failed to init axios trace config wrapper:', e);
158
202
  }
159
203
  axiosInstance.interceptors.response.use((response)=>{
160
204
  const { __span: span, _startTime: startTime, url = "" } = response.config || {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.1-alpha.11",
3
+ "version": "1.2.1-alpha.13",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [