@lark-apaas/client-toolkit 1.2.24-alpha.12 → 1.2.24-alpha.14
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.js +79 -8
- package/package.json +1 -1
package/lib/utils/axiosConfig.js
CHANGED
|
@@ -6,18 +6,89 @@ import { safeStringify } from "./safeStringify.js";
|
|
|
6
6
|
import { slardar } from "@lark-apaas/internal-slardar";
|
|
7
7
|
import { normalizeBasePath } from "./utils.js";
|
|
8
8
|
const APP_CLIENT_API_LOG_TYPE = 'app_client_api_log';
|
|
9
|
+
function stripBasePath(urlPath) {
|
|
10
|
+
const base = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
|
11
|
+
if (base && urlPath.startsWith(base)) return urlPath.slice(base.length) || '/';
|
|
12
|
+
return urlPath;
|
|
13
|
+
}
|
|
14
|
+
let _pageRoutes = null;
|
|
15
|
+
function getPageRouteDefinitions() {
|
|
16
|
+
if (_pageRoutes) return _pageRoutes;
|
|
17
|
+
try {
|
|
18
|
+
const raw = process.env.__PAGE_ROUTE_DEFINITIONS__;
|
|
19
|
+
if (raw) {
|
|
20
|
+
const parsed = JSON.parse(raw);
|
|
21
|
+
if (Array.isArray(parsed)) {
|
|
22
|
+
_pageRoutes = parsed;
|
|
23
|
+
return _pageRoutes;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
} catch {}
|
|
27
|
+
_pageRoutes = [];
|
|
28
|
+
return _pageRoutes;
|
|
29
|
+
}
|
|
30
|
+
let _apiRoutes = null;
|
|
31
|
+
function getApiRouteDefinitions() {
|
|
32
|
+
if (_apiRoutes) return _apiRoutes;
|
|
33
|
+
try {
|
|
34
|
+
const raw = process.env.__API_ROUTE_DEFINITIONS__;
|
|
35
|
+
if (raw) {
|
|
36
|
+
const parsed = JSON.parse(raw);
|
|
37
|
+
if (Array.isArray(parsed)) {
|
|
38
|
+
_apiRoutes = parsed;
|
|
39
|
+
return _apiRoutes;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} catch {}
|
|
43
|
+
_apiRoutes = [];
|
|
44
|
+
return _apiRoutes;
|
|
45
|
+
}
|
|
46
|
+
function matchRoute(concretePath, routes) {
|
|
47
|
+
const segments = concretePath.split('/').filter(Boolean);
|
|
48
|
+
for (const route of routes){
|
|
49
|
+
const routeSegments = route.path.split('/').filter(Boolean);
|
|
50
|
+
if (routeSegments.length !== segments.length) continue;
|
|
51
|
+
let match = true;
|
|
52
|
+
for(let i = 0; i < routeSegments.length; i++)if (!routeSegments[i].startsWith(':')) {
|
|
53
|
+
if (routeSegments[i] !== segments[i]) {
|
|
54
|
+
match = false;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (match) return route.path;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
function matchApiRoute(method, concretePath) {
|
|
63
|
+
const routes = getApiRouteDefinitions();
|
|
64
|
+
const segments = concretePath.split('/').filter(Boolean);
|
|
65
|
+
for (const route of routes){
|
|
66
|
+
if ('*' !== route.method && route.method !== method) continue;
|
|
67
|
+
const routeSegments = route.path.split('/').filter(Boolean);
|
|
68
|
+
if (routeSegments.length !== segments.length) continue;
|
|
69
|
+
let match = true;
|
|
70
|
+
for(let i = 0; i < routeSegments.length; i++)if (!routeSegments[i].startsWith(':')) {
|
|
71
|
+
if (routeSegments[i] !== segments[i]) {
|
|
72
|
+
match = false;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (match) return route.path;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
9
80
|
function getRefererPath() {
|
|
10
81
|
try {
|
|
11
|
-
if ('undefined'
|
|
12
|
-
|
|
82
|
+
if ('undefined' == typeof window || !window.location?.pathname) return '/';
|
|
83
|
+
const rawPath = stripBasePath(window.location.pathname);
|
|
84
|
+
return matchRoute(rawPath, getPageRouteDefinitions()) || rawPath;
|
|
13
85
|
} catch {
|
|
14
86
|
return '/';
|
|
15
87
|
}
|
|
16
88
|
}
|
|
17
|
-
function
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
return urlPath;
|
|
89
|
+
function getApiField(method, path) {
|
|
90
|
+
const matched = matchApiRoute(method, path);
|
|
91
|
+
return `${method} ${matched || path}`;
|
|
21
92
|
}
|
|
22
93
|
const isValidResponse = (resp)=>null != resp && 'object' == typeof resp && 'config' in resp && null !== resp.config && void 0 !== resp.config && 'object' == typeof resp.config && 'status' in resp && 'number' == typeof resp.status && 'data' in resp;
|
|
23
94
|
async function logResponse(ok, responseOrError) {
|
|
@@ -158,7 +229,7 @@ function handleSpanEnd(cfg, response, error) {
|
|
|
158
229
|
const path = stripBasePath(rawPath);
|
|
159
230
|
const durationMs = startTime ? Date.now() - startTime : void 0;
|
|
160
231
|
const referer_path = getRefererPath();
|
|
161
|
-
const api =
|
|
232
|
+
const api = getApiField(method, path);
|
|
162
233
|
const type = APP_CLIENT_API_LOG_TYPE;
|
|
163
234
|
const logData = {
|
|
164
235
|
method,
|
|
@@ -270,7 +341,7 @@ function initAxiosConfig(axiosInstance) {
|
|
|
270
341
|
config.headers['Rpc-Persist-Apaas-Observability-Referer-Path'] = refererPath;
|
|
271
342
|
const reqMethod = (config.method || 'GET').toUpperCase();
|
|
272
343
|
const requestPath = stripBasePath((config.url || '').split('?')[0]);
|
|
273
|
-
config.headers['Rpc-Persist-Apaas-Observability-Api'] =
|
|
344
|
+
config.headers['Rpc-Persist-Apaas-Observability-Api'] = getApiField(reqMethod, requestPath);
|
|
274
345
|
return config;
|
|
275
346
|
}, (error)=>Promise.reject(error));
|
|
276
347
|
instance.interceptors.response.use((response)=>response, (error)=>{
|