@lark-apaas/client-toolkit 1.2.17-alpha.16 → 1.2.17-alpha.17
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.
|
@@ -14,7 +14,7 @@ async function getSourceMap() {
|
|
|
14
14
|
return sourceMapContent;
|
|
15
15
|
}
|
|
16
16
|
const childApi = {
|
|
17
|
-
getRoutes: getRouteDefinitions,
|
|
17
|
+
getRoutes: ()=>Promise.resolve(getRouteDefinitions()),
|
|
18
18
|
updateAppInfo: (appInfo)=>{
|
|
19
19
|
dispatchEvent(new CustomEvent('MiaoDaMetaInfoChanged', {
|
|
20
20
|
detail: appInfo
|
package/lib/utils/axiosConfig.js
CHANGED
|
@@ -16,13 +16,47 @@ function getRefererPath() {
|
|
|
16
16
|
}
|
|
17
17
|
function getApiField(method, response, errorResponse, fallbackPath) {
|
|
18
18
|
const matchRoute = response?.headers?.['x-match-route'] || errorResponse?.headers?.['x-match-route'] || '';
|
|
19
|
-
const routePath = matchRoute || fallbackPath;
|
|
19
|
+
const routePath = matchRoute || matchApiRoute(method, fallbackPath) || fallbackPath;
|
|
20
20
|
return `${method} ${routePath}`;
|
|
21
21
|
}
|
|
22
|
-
function stripBasePath(
|
|
22
|
+
function stripBasePath(urlPath) {
|
|
23
23
|
const base = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
|
24
|
-
if (base &&
|
|
25
|
-
return
|
|
24
|
+
if (base && urlPath.startsWith(base)) return urlPath.slice(base.length) || '/';
|
|
25
|
+
return urlPath;
|
|
26
|
+
}
|
|
27
|
+
let apiRouteDefinitions = null;
|
|
28
|
+
function getApiRouteDefinitions() {
|
|
29
|
+
if (apiRouteDefinitions) return apiRouteDefinitions;
|
|
30
|
+
try {
|
|
31
|
+
const raw = process.env.__API_ROUTE_DEFINITIONS__;
|
|
32
|
+
if (raw) {
|
|
33
|
+
const parsed = JSON.parse(raw);
|
|
34
|
+
if (Array.isArray(parsed)) {
|
|
35
|
+
apiRouteDefinitions = parsed;
|
|
36
|
+
return apiRouteDefinitions;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch {}
|
|
40
|
+
apiRouteDefinitions = [];
|
|
41
|
+
return apiRouteDefinitions;
|
|
42
|
+
}
|
|
43
|
+
function matchApiRoute(method, concretePath) {
|
|
44
|
+
const routes = getApiRouteDefinitions();
|
|
45
|
+
const segments = concretePath.split('/').filter(Boolean);
|
|
46
|
+
for (const route of routes){
|
|
47
|
+
if ('*' !== route.method && route.method !== method) continue;
|
|
48
|
+
const routeSegments = route.path.split('/').filter(Boolean);
|
|
49
|
+
if (routeSegments.length !== segments.length) continue;
|
|
50
|
+
let match = true;
|
|
51
|
+
for(let i = 0; i < routeSegments.length; i++)if (!routeSegments[i].startsWith(':')) {
|
|
52
|
+
if (routeSegments[i] !== segments[i]) {
|
|
53
|
+
match = false;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (match) return route.path;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
26
60
|
}
|
|
27
61
|
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;
|
|
28
62
|
async function logResponse(ok, responseOrError) {
|
|
@@ -272,6 +306,12 @@ function initAxiosConfig(axiosInstance) {
|
|
|
272
306
|
const csrfToken = window.csrfToken;
|
|
273
307
|
if (csrfToken) config.headers['X-Suda-Csrf-Token'] = csrfToken;
|
|
274
308
|
if ('undefined' != typeof window && window.location?.pathname) config.headers['X-Page-Route'] = window.location.pathname;
|
|
309
|
+
const refererPath = getRefererPath();
|
|
310
|
+
config.headers['Rpc-Persist-Apaas-Observability-Referer-Path'] = refererPath;
|
|
311
|
+
const reqMethod = (config.method || 'GET').toUpperCase();
|
|
312
|
+
const requestPath = stripBasePath((config.url || '').split('?')[0]);
|
|
313
|
+
const parameterizedPath = matchApiRoute(reqMethod, requestPath) || requestPath;
|
|
314
|
+
config.headers['Rpc-Persist-Apaas-Observability-Api'] = `${reqMethod} ${parameterizedPath}`;
|
|
275
315
|
return config;
|
|
276
316
|
}, (error)=>Promise.reject(error));
|
|
277
317
|
instance.interceptors.response.use((response)=>response, (error)=>{
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 获取路由定义列表
|
|
3
|
+
* 供 childApi 等外部模块使用
|
|
4
|
+
*/
|
|
5
|
+
export declare function getRouteDefinitions(): {
|
|
2
6
|
path: string;
|
|
3
|
-
}[]
|
|
7
|
+
}[];
|
|
4
8
|
/**
|
|
5
9
|
* 根据当前 pathname 匹配路由模式并更新存储
|
|
6
10
|
* 使用 react-router 的 matchPath 确保匹配逻辑与路由一致
|
|
7
11
|
*/
|
|
8
|
-
export declare function updateRoutePattern(pathname: string):
|
|
12
|
+
export declare function updateRoutePattern(pathname: string): void;
|
|
9
13
|
/**
|
|
10
14
|
* 获取当前匹配的参数化路由模式
|
|
11
15
|
* 供 axiosConfig 等非 React 上下文使用
|
|
@@ -2,41 +2,30 @@ import { matchPath } from "react-router-dom";
|
|
|
2
2
|
import { normalizeBasePath } from "./utils.js";
|
|
3
3
|
let currentRoutePattern = '/';
|
|
4
4
|
let routeDefinitions = null;
|
|
5
|
-
function
|
|
5
|
+
function loadRouteDefinitions() {
|
|
6
|
+
if (routeDefinitions) return routeDefinitions;
|
|
6
7
|
try {
|
|
7
8
|
const raw = process.env.__ROUTE_DEFINITIONS__;
|
|
8
9
|
if (raw) {
|
|
9
10
|
const parsed = JSON.parse(raw);
|
|
10
|
-
if (Array.isArray(parsed) && parsed.length > 0)
|
|
11
|
+
if (Array.isArray(parsed) && parsed.length > 0) {
|
|
12
|
+
routeDefinitions = parsed;
|
|
13
|
+
return routeDefinitions;
|
|
14
|
+
}
|
|
11
15
|
}
|
|
12
16
|
} catch {}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (injected) {
|
|
19
|
-
routeDefinitions = injected;
|
|
20
|
-
return routeDefinitions;
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
|
24
|
-
const res = await fetch(`${basePath}/routes.json`);
|
|
25
|
-
routeDefinitions = await res.json();
|
|
26
|
-
} catch {
|
|
27
|
-
routeDefinitions = [
|
|
28
|
-
{
|
|
29
|
-
path: '/'
|
|
30
|
-
}
|
|
31
|
-
];
|
|
32
|
-
}
|
|
17
|
+
routeDefinitions = [
|
|
18
|
+
{
|
|
19
|
+
path: '/'
|
|
20
|
+
}
|
|
21
|
+
];
|
|
33
22
|
return routeDefinitions;
|
|
34
23
|
}
|
|
35
|
-
|
|
24
|
+
function getRouteDefinitions() {
|
|
36
25
|
return loadRouteDefinitions();
|
|
37
26
|
}
|
|
38
|
-
|
|
39
|
-
const routes =
|
|
27
|
+
function updateRoutePattern(pathname) {
|
|
28
|
+
const routes = loadRouteDefinitions();
|
|
40
29
|
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
|
41
30
|
let relativePath = pathname;
|
|
42
31
|
if (basePath && pathname.startsWith(basePath)) relativePath = pathname.slice(basePath.length) || '/';
|