subapp.ts
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
/**
* 子应用发给主壳的请求描述。
* 字段刻意与 axios 的 request config 对齐:host 侧实现只需 `http.request(req)`,
* 让加签 / 验签 / 401 统一登出 / context-path 拼接全部复用同一套拦截器。
*
* transformResponse 允许传函数:storeDownload.ts 会用它从 content-disposition
* 取文件名,并保留 ArrayBuffer 原始字节。若走 postMessage 序列化这条路就断了。
*/
export interface RemoteRequest {
method: HttpMethod;
url: string;
params?: Record<string, unknown>;
data?: unknown;
responseType?: 'json' | 'blob' | 'arraybuffer' | 'text';
transformResponse?: Array<(raw: unknown, headers: Record<string, string>) => unknown>;
_noVerify?: boolean;
/** 中止信号:wujie props 是同源引用传递,可原样跨到主壳 axios(不走 postMessage 序列化)。 */
signal?: AbortSignal;
[key: string]: unknown;
}
/** host 注入的请求代理。返回值已过响应拦截器:JSON 是 ApiResult,文件流是 { data, headers }。 */
export type RemoteRequestFn = (req: RemoteRequest) => Promise<unknown>;
/** 登录会话快照(字段名与老项目 src/access/session.ts 的 LoginSession 对齐)。 */
export interface SessionSnapshot {
token: string;
tokenType: string;
expireAt: string;
userId: number;
account: string;
name: string;
orgId: number;
dataScope: number;
roleCodes: string[];
permCodes: string[];
}
/**
* 主壳 -> 子应用的显式契约。
* 用显式 props 取代隐式 React Context:wujie 下 Context 必断,
* 与其假装它还在,不如把跨应用共享收口成一份可校验的类型。
*/
export interface SubAppProps {
/** 唯一的后端出入口。子应用不打包 axios / 签名 / PEM / VITE_* 密钥 */
request: RemoteRequestFn;
session: SessionSnapshot | null;
permCodes: string[];
/** themeConfig 是纯对象,可直接跨 iframe 传,不需要共享实例 */
theme: Record<string, unknown>;
/** 首屏页名:只在首次 startApp 注入,之后的页面切换一律走 bus */
page: string;
/** 子应用请求主壳跳转(例如详情页想回列表) */
navigate: (path: string) => void;
shortName: string;
}
/** 子应用注册信息;生产由运行时 manifest 覆盖 entry。 */
export interface SubAppMeta {
name: string;
shortName: string;
route: string;
entry: string;
version?: string;
}
/** 部署系统写的运行时清单。只发一个子应用 = 上传产物 + 改这份 JSON,host 不重建。 */
export interface SubAppManifest {
version: string;
updatedAt?: string;
subapps: Record<
string,
{
url: string;
version?: string;
/**
* 路由首段,缺省时 = key(shortName)。
* 供主壳运行时动态注册使用:host 不重建时读不到新子应用的 package.json,
* 只能靠清单把 URL 首段反查回 shortName。与构建期 uj.route 覆写保持同一语义。
*/
route?: string;
}
>;
}