@origen-ui/admin-cli 1.1.0 → 2.0.2
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/README.md +41 -9
- package/package.json +26 -2
- package/templates/current/clean/.node-version +1 -0
- package/templates/current/clean/CHANGELOG.md +125 -0
- package/templates/current/clean/README.md +2 -1
- package/templates/current/clean/component-catalog.json +58 -17
- package/templates/current/clean/e2e/admin-shell.spec.ts +6 -0
- package/templates/current/clean/e2e/demo-shell.spec.ts +94 -50
- package/templates/current/clean/package.json +39 -25
- package/templates/current/clean/playwright.config.ts +1 -0
- package/templates/current/clean/playwright.demo.config.ts +1 -1
- package/templates/current/clean/pnpm-workspace.yaml +18 -0
- package/templates/current/clean/scaffold.lock.json +2 -2
- package/templates/current/clean/scripts/check-admin-scaffold.mjs +5 -1
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/error-pages.spec.ts +11 -2
- package/templates/current/clean/scripts/clean-overlay/src/main.ts +23 -14
- package/templates/current/clean/scripts/clean-overlay/src/router/componentCache.ts +12 -0
- package/templates/current/clean/scripts/clean-overlay/src/router/componentRegistry.ts +2 -13
- package/templates/current/clean/scripts/clean-overlay/src/router/dynamicRoutes.spec.ts +34 -4
- package/templates/current/clean/scripts/clean-overlay/src/router/paths.ts +21 -0
- package/templates/current/clean/scripts/clean-overlay/src/router/staticRoutes.ts +6 -6
- package/templates/current/clean/scripts/demo-api.ts +88 -12
- package/templates/current/clean/scripts/generate-api.ts +87 -3
- package/templates/current/clean/scripts/remove-examples.mjs +4 -4
- package/templates/current/clean/scripts/vite-build-policy.spec.ts +1 -0
- package/templates/current/clean/src/api/generated/data-contracts.ts +2 -5
- package/templates/current/clean/src/api/generated/http-client.ts +28 -26
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/206/205/345/256/271/346/226/207/347/253/240.ts +5 -8
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/256/241/350/256/241.ts +1 -4
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/346/226/207/344/273/266/344/270/212/344/274/240.ts +2 -5
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/347/224/250/346/210/267.ts +8 -10
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/217/234/345/215/225.ts +11 -12
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/247/222/350/211/262.ts +8 -10
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/256/242/345/215/225/350/215/211/347/250/277.ts +8 -10
- package/templates/current/clean/src/api/generated//350/256/244/350/257/201.ts +3 -6
- package/templates/current/clean/src/app/errorMessage.spec.ts +76 -0
- package/templates/current/clean/src/app/errorMessage.ts +53 -0
- package/templates/current/clean/src/app/menu-host.spec.ts +77 -0
- package/templates/current/clean/src/app/menu.spec.ts +39 -0
- package/templates/current/clean/src/app/menu.ts +48 -12
- package/templates/current/clean/src/app/runtimeConfig.ts +1 -1
- package/templates/current/clean/src/demo-api.spec.ts +314 -3
- package/templates/current/clean/src/features/auth/pages/Login.vue +9 -2
- package/templates/current/clean/src/features/auth/store/auth.ts +6 -19
- package/templates/current/clean/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/clean/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/clean/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/clean/src/features/errors/pages/error-pages.spec.ts +11 -2
- package/templates/current/clean/src/features/system/access/api/admin-repository.ts +2 -6
- package/templates/current/clean/src/features/system/access/api/rbac.ts +19 -9
- package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
- package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
- package/templates/current/clean/src/features/workspace/pages/Profile.vue +1 -1
- package/templates/current/clean/src/layouts/AdminLayout.vue +3 -0
- package/templates/current/clean/src/layouts/TopBar.vue +37 -6
- package/templates/current/clean/src/layouts/topBarMenuSearch.spec.ts +24 -1
- package/templates/current/clean/src/layouts/topBarMenuSearch.ts +10 -0
- package/templates/current/clean/src/locales/en-US.json +30 -3
- package/templates/current/clean/src/locales/en-US.runtime.ts +2 -0
- package/templates/current/clean/src/locales/index.ts +39 -26
- package/templates/current/clean/src/locales/zh-CN.json +30 -3
- package/templates/current/clean/src/locales/zh-CN.runtime.ts +2 -0
- package/templates/current/clean/src/locales/zh-TW.json +30 -3
- package/templates/current/clean/src/locales/zh-TW.runtime.ts +2 -0
- package/templates/current/clean/src/main.ts +23 -14
- package/templates/current/clean/src/router/bootstrap.ts +10 -16
- package/templates/current/clean/src/router/componentCache.ts +12 -0
- package/templates/current/clean/src/router/componentRegistry.ts +2 -13
- package/templates/current/clean/src/router/dynamicMenuRefresh.spec.ts +101 -0
- package/templates/current/clean/src/router/dynamicMenuRefresh.ts +24 -0
- package/templates/current/clean/src/router/dynamicRoutes.spec.ts +34 -4
- package/templates/current/clean/src/router/dynamicRoutes.ts +14 -3
- package/templates/current/clean/src/router/paths.spec.ts +50 -0
- package/templates/current/clean/src/router/paths.ts +21 -0
- package/templates/current/clean/src/router/queryEncryption.spec.ts +10 -1
- package/templates/current/clean/src/router/routeQuery.spec.ts +2 -1
- package/templates/current/clean/src/router/routeQuery.ts +4 -1
- package/templates/current/clean/src/router/staticRoutes.ts +6 -6
- package/templates/current/clean/src/templates/og-table.ts +2 -4
- package/templates/current/clean/tsconfig.json +0 -1
- package/templates/current/clean/vite.build-policy.ts +55 -8
- package/templates/current/clean/vite.config.ts +30 -0
- package/templates/current/examples/.node-version +1 -0
- package/templates/current/examples/CHANGELOG.md +125 -0
- package/templates/current/examples/README.md +2 -1
- package/templates/current/examples/component-catalog.json +58 -17
- package/templates/current/examples/e2e/admin-shell.spec.ts +6 -0
- package/templates/current/examples/e2e/demo-shell.spec.ts +94 -50
- package/templates/current/examples/package.json +39 -25
- package/templates/current/examples/playwright.config.ts +1 -0
- package/templates/current/examples/playwright.demo.config.ts +1 -1
- package/templates/current/examples/pnpm-workspace.yaml +18 -0
- package/templates/current/examples/scaffold.lock.json +2 -2
- package/templates/current/examples/scripts/check-admin-scaffold.mjs +5 -1
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/error-pages.spec.ts +11 -2
- package/templates/current/examples/scripts/clean-overlay/src/main.ts +23 -14
- package/templates/current/examples/scripts/clean-overlay/src/router/componentCache.ts +12 -0
- package/templates/current/examples/scripts/clean-overlay/src/router/componentRegistry.ts +2 -13
- package/templates/current/examples/scripts/clean-overlay/src/router/dynamicRoutes.spec.ts +34 -4
- package/templates/current/examples/scripts/clean-overlay/src/router/paths.ts +21 -0
- package/templates/current/examples/scripts/clean-overlay/src/router/staticRoutes.ts +6 -6
- package/templates/current/examples/scripts/demo-api.ts +88 -12
- package/templates/current/examples/scripts/generate-api.ts +87 -3
- package/templates/current/examples/scripts/remove-examples.mjs +4 -4
- package/templates/current/examples/scripts/vite-build-policy.spec.ts +1 -0
- package/templates/current/examples/src/api/generated/data-contracts.ts +2 -5
- package/templates/current/examples/src/api/generated/http-client.ts +28 -26
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/206/205/345/256/271/346/226/207/347/253/240.ts +5 -8
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/256/241/350/256/241.ts +1 -4
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/346/226/207/344/273/266/344/270/212/344/274/240.ts +2 -5
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/347/224/250/346/210/267.ts +8 -10
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/217/234/345/215/225.ts +11 -12
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/247/222/350/211/262.ts +8 -10
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/256/242/345/215/225/350/215/211/347/250/277.ts +8 -10
- package/templates/current/examples/src/api/generated//350/256/244/350/257/201.ts +3 -6
- package/templates/current/examples/src/app/errorMessage.spec.ts +76 -0
- package/templates/current/examples/src/app/errorMessage.ts +53 -0
- package/templates/current/examples/src/app/menu-host.spec.ts +77 -0
- package/templates/current/examples/src/app/menu.spec.ts +39 -0
- package/templates/current/examples/src/app/menu.ts +48 -12
- package/templates/current/examples/src/app/runtimeConfig.ts +1 -1
- package/templates/current/examples/src/demo-api.spec.ts +314 -3
- package/templates/current/examples/src/features/auth/pages/Login.vue +9 -2
- package/templates/current/examples/src/features/auth/store/auth.ts +6 -19
- package/templates/current/examples/src/features/errors/pages/Forbidden.vue +2 -1
- package/templates/current/examples/src/features/errors/pages/NotFound.vue +2 -1
- package/templates/current/examples/src/features/errors/pages/ServerError.vue +2 -1
- package/templates/current/examples/src/features/feature-composables.spec.ts +11 -1
- package/templates/current/examples/src/features/system/access/api/admin-repository.ts +2 -6
- package/templates/current/examples/src/features/system/access/api/rbac.ts +19 -9
- package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
- package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
- package/templates/current/examples/src/features/system/menus/components/MenuEditorForm.vue +115 -28
- package/templates/current/examples/src/features/system/menus/composables/useMenuManagement.ts +162 -66
- package/templates/current/examples/src/features/system/menus/menuDraft.spec.ts +204 -0
- package/templates/current/examples/src/features/system/menus/menuDraft.ts +116 -0
- package/templates/current/examples/src/features/system/menus/menuTreePrecheck.spec.ts +268 -0
- package/templates/current/examples/src/features/system/menus/menuTreePrecheck.ts +317 -0
- package/templates/current/examples/src/features/system/roles/components/RoleEditorForm.vue +1 -0
- package/templates/current/examples/src/features/workspace/pages/Profile.vue +1 -1
- package/templates/current/examples/src/layouts/AdminLayout.vue +3 -0
- package/templates/current/examples/src/layouts/TopBar.vue +37 -6
- package/templates/current/examples/src/layouts/topBarMenuSearch.spec.ts +24 -1
- package/templates/current/examples/src/layouts/topBarMenuSearch.ts +10 -0
- package/templates/current/examples/src/locales/en-US.json +30 -3
- package/templates/current/examples/src/locales/en-US.runtime.ts +2 -0
- package/templates/current/examples/src/locales/index.ts +39 -26
- package/templates/current/examples/src/locales/zh-CN.json +30 -3
- package/templates/current/examples/src/locales/zh-CN.runtime.ts +2 -0
- package/templates/current/examples/src/locales/zh-TW.json +30 -3
- package/templates/current/examples/src/locales/zh-TW.runtime.ts +2 -0
- package/templates/current/examples/src/main.ts +24 -22
- package/templates/current/examples/src/router/bootstrap.ts +10 -16
- package/templates/current/examples/src/router/componentCache.ts +18 -0
- package/templates/current/examples/src/router/componentRegistry.ts +1 -18
- package/templates/current/examples/src/router/dynamicMenuRefresh.spec.ts +101 -0
- package/templates/current/examples/src/router/dynamicMenuRefresh.ts +24 -0
- package/templates/current/examples/src/router/dynamicRoutes.spec.ts +20 -2
- package/templates/current/examples/src/router/dynamicRoutes.ts +14 -3
- package/templates/current/examples/src/router/paths.spec.ts +50 -0
- package/templates/current/examples/src/router/paths.ts +21 -0
- package/templates/current/examples/src/router/queryEncryption.spec.ts +10 -1
- package/templates/current/examples/src/router/routeQuery.spec.ts +2 -1
- package/templates/current/examples/src/router/routeQuery.ts +4 -1
- package/templates/current/examples/src/router/staticRoutes.ts +3 -3
- package/templates/current/examples/src/templates/og-table.ts +2 -4
- package/templates/current/examples/tsconfig.json +0 -1
- package/templates/current/examples/vite.build-policy.ts +55 -8
- package/templates/current/examples/vite.config.ts +30 -0
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars */
|
|
2
|
-
/* tslint:disable */
|
|
3
|
-
// @ts-nocheck
|
|
4
1
|
/*
|
|
5
2
|
* ---------------------------------------------------------------
|
|
6
3
|
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
|
|
@@ -35,7 +32,7 @@ export class 认证<SecurityDataType = unknown> {
|
|
|
35
32
|
* @response `200` `LoginResponse` 登录成功
|
|
36
33
|
*/
|
|
37
34
|
loginCreate = (data: LoginCredentials, params: RequestParams = {}) =>
|
|
38
|
-
this.http.request<LoginResponse,
|
|
35
|
+
this.http.request<LoginResponse, unknown>({
|
|
39
36
|
path: `/auth/login`,
|
|
40
37
|
method: "POST",
|
|
41
38
|
body: data,
|
|
@@ -55,7 +52,7 @@ export class 认证<SecurityDataType = unknown> {
|
|
|
55
52
|
* @response `200` `ProfileResponse` 成功
|
|
56
53
|
*/
|
|
57
54
|
profileList = (params: RequestParams = {}) =>
|
|
58
|
-
this.http.request<ProfileResponse,
|
|
55
|
+
this.http.request<ProfileResponse, unknown>({
|
|
59
56
|
path: `/auth/profile`,
|
|
60
57
|
method: "GET",
|
|
61
58
|
secure: true,
|
|
@@ -85,7 +82,7 @@ export class 认证<SecurityDataType = unknown> {
|
|
|
85
82
|
{
|
|
86
83
|
token?: string;
|
|
87
84
|
},
|
|
88
|
-
|
|
85
|
+
unknown
|
|
89
86
|
>({
|
|
90
87
|
path: `/auth/refresh`,
|
|
91
88
|
method: "POST",
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { ApiErrorKind, RequestContext } from '@origen-ui/http'
|
|
2
|
+
import { ApiError } from '@origen-ui/http'
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
import {
|
|
5
|
+
adminErrorKey,
|
|
6
|
+
AdminLocalizedError,
|
|
7
|
+
createAdminError,
|
|
8
|
+
translateAdminError,
|
|
9
|
+
} from './errorMessage'
|
|
10
|
+
|
|
11
|
+
const context: RequestContext = {
|
|
12
|
+
attempt: 0,
|
|
13
|
+
authRetried: false,
|
|
14
|
+
method: 'POST',
|
|
15
|
+
traceId: 'trace-test',
|
|
16
|
+
url: '/auth/login',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function apiError(kind: ApiErrorKind): ApiError {
|
|
20
|
+
return new ApiError({ context, kind, message: 'backend detail must stay diagnostic' })
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('admin error message mapping', () => {
|
|
24
|
+
it.each(['network', 'timeout'] as const)('maps %s auth failures to a safe locale key', (kind) => {
|
|
25
|
+
expect(adminErrorKey(apiError(kind), { domain: 'auth', fallbackKey: 'auth.loginFailed' })).toBe(
|
|
26
|
+
'auth.networkError',
|
|
27
|
+
)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('maps invalid responses while keeping business and HTTP details out of the UI key', () => {
|
|
31
|
+
expect(
|
|
32
|
+
adminErrorKey(apiError('response'), { domain: 'auth', fallbackKey: 'auth.loginFailed' }),
|
|
33
|
+
).toBe('auth.invalidResponse')
|
|
34
|
+
expect(
|
|
35
|
+
adminErrorKey(apiError('business'), { domain: 'auth', fallbackKey: 'auth.loginFailed' }),
|
|
36
|
+
).toBe('auth.loginFailed')
|
|
37
|
+
expect(
|
|
38
|
+
adminErrorKey(apiError('http'), {
|
|
39
|
+
domain: 'runtime',
|
|
40
|
+
fallbackKey: 'common.errors.unknownPageError',
|
|
41
|
+
}),
|
|
42
|
+
).toBe('common.errors.unknownPageError')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('uses runtime transport keys and safe fallbacks for non-API errors', () => {
|
|
46
|
+
expect(
|
|
47
|
+
adminErrorKey(apiError('network'), {
|
|
48
|
+
domain: 'runtime',
|
|
49
|
+
fallbackKey: 'common.errors.unknownPageError',
|
|
50
|
+
}),
|
|
51
|
+
).toBe('common.errors.networkError')
|
|
52
|
+
expect(
|
|
53
|
+
adminErrorKey(apiError('response'), {
|
|
54
|
+
domain: 'runtime',
|
|
55
|
+
fallbackKey: 'common.errors.unknownPageError',
|
|
56
|
+
}),
|
|
57
|
+
).toBe('common.errors.invalidResponse')
|
|
58
|
+
expect(
|
|
59
|
+
translateAdminError(new Error('backend English detail'), {
|
|
60
|
+
fallbackKey: 'common.errors.unknownPageError',
|
|
61
|
+
fallback: 'Safe message',
|
|
62
|
+
}),
|
|
63
|
+
).toBe('Safe message')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('preserves a deliberate locale key and keeps the cause diagnostic', () => {
|
|
67
|
+
const cause = new Error('backend detail')
|
|
68
|
+
const error = createAdminError('auth.invalidResponse', cause)
|
|
69
|
+
expect(error).toBeInstanceOf(AdminLocalizedError)
|
|
70
|
+
expect(error.localeKey).toBe('auth.invalidResponse')
|
|
71
|
+
expect(error.cause).toBe(cause)
|
|
72
|
+
expect(adminErrorKey(error, { domain: 'auth', fallbackKey: 'auth.loginFailed' })).toBe(
|
|
73
|
+
'auth.invalidResponse',
|
|
74
|
+
)
|
|
75
|
+
})
|
|
76
|
+
})
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { isApiError } from '@origen-ui/http'
|
|
2
|
+
import { translateAdminMessage } from '../locales'
|
|
3
|
+
|
|
4
|
+
export type AdminErrorDomain = 'auth' | 'runtime'
|
|
5
|
+
|
|
6
|
+
export interface AdminErrorMessageOptions {
|
|
7
|
+
fallbackKey: string
|
|
8
|
+
fallback?: string
|
|
9
|
+
domain?: AdminErrorDomain
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Error carrying a locale key without exposing the underlying technical message. */
|
|
13
|
+
export class AdminLocalizedError extends Error {
|
|
14
|
+
readonly localeKey: string
|
|
15
|
+
|
|
16
|
+
constructor(localeKey: string, cause?: unknown) {
|
|
17
|
+
super(localeKey)
|
|
18
|
+
this.name = 'AdminLocalizedError'
|
|
19
|
+
this.localeKey = localeKey
|
|
20
|
+
if (cause !== undefined) this.cause = cause
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createAdminError(localeKey: string, cause?: unknown): AdminLocalizedError {
|
|
25
|
+
return new AdminLocalizedError(localeKey, cause)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Maps transport failures to a safe user-facing key; backend messages stay diagnostic only. */
|
|
29
|
+
export function adminErrorKey(
|
|
30
|
+
error: unknown,
|
|
31
|
+
options: Pick<AdminErrorMessageOptions, 'fallbackKey' | 'domain'>,
|
|
32
|
+
): string {
|
|
33
|
+
if (error instanceof AdminLocalizedError) return error.localeKey
|
|
34
|
+
if (!isApiError(error)) return options.fallbackKey
|
|
35
|
+
|
|
36
|
+
if (options.domain === 'auth') {
|
|
37
|
+
if (error.kind === 'network' || error.kind === 'timeout') return 'auth.networkError'
|
|
38
|
+
if (error.kind === 'response') return 'auth.invalidResponse'
|
|
39
|
+
return options.fallbackKey
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (error.kind === 'network' || error.kind === 'timeout') return 'common.errors.networkError'
|
|
43
|
+
if (error.kind === 'response') return 'common.errors.invalidResponse'
|
|
44
|
+
return options.fallbackKey
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function translateAdminError(
|
|
48
|
+
error: unknown,
|
|
49
|
+
options: AdminErrorMessageOptions,
|
|
50
|
+
): string {
|
|
51
|
+
const key = adminErrorKey(error, options)
|
|
52
|
+
return translateAdminMessage(key, { fallback: options.fallback })
|
|
53
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
// loadMenusFromHost 依赖嵌入探测(模块加载期求值),每个用例重置注册表后
|
|
5
|
+
// 动态 import,与 embedded.spec.ts 的模式一致。
|
|
6
|
+
type MenuModule = typeof import('./menu')
|
|
7
|
+
|
|
8
|
+
async function loadMenuModule(): Promise<MenuModule> {
|
|
9
|
+
vi.resetModules()
|
|
10
|
+
return await import('./menu')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function stubHostMenus(menus: unknown): void {
|
|
14
|
+
;(window as { microApp?: unknown }).microApp = { getData: () => ({ backendMenus: menus }) }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const validHostMenus = [
|
|
18
|
+
{ code: 'dashboard', path: '/dashboard', title: '工作台', order: 1 },
|
|
19
|
+
{ code: 'users', path: 'users', title: '用户管理', parentPath: '/dashboard', order: 2 },
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
vi.stubEnv('VITE_EMBEDDED', 'true')
|
|
24
|
+
;(window as { __MICRO_APP_ENVIRONMENT__?: boolean }).__MICRO_APP_ENVIRONMENT__ = true
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
vi.unstubAllEnvs()
|
|
29
|
+
vi.resetModules()
|
|
30
|
+
delete (window as { microApp?: unknown }).microApp
|
|
31
|
+
delete (window as { __MICRO_APP_ENVIRONMENT__?: boolean }).__MICRO_APP_ENVIRONMENT__
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('loadMenusFromHost', () => {
|
|
35
|
+
it('parses host-injected menus through the strict shared pipeline', async () => {
|
|
36
|
+
stubHostMenus(validHostMenus)
|
|
37
|
+
const { loadMenusFromHost } = await loadMenuModule()
|
|
38
|
+
|
|
39
|
+
await expect(loadMenusFromHost()).resolves.toEqual([
|
|
40
|
+
expect.objectContaining({ code: 'dashboard', path: '/dashboard' }),
|
|
41
|
+
expect.objectContaining({ code: 'users', parentPath: '/dashboard' }),
|
|
42
|
+
])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('rejects malformed host payloads instead of trusting the cast', async () => {
|
|
46
|
+
stubHostMenus([{ path: '/broken', title: '缺少 code' }])
|
|
47
|
+
const { loadMenusFromHost } = await loadMenuModule()
|
|
48
|
+
|
|
49
|
+
await expect(loadMenusFromHost()).rejects.toThrow('Invalid menu node fields')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('falls back to fetchMenus when the host did not inject menus', async () => {
|
|
53
|
+
// 先加载 menu 模块(resetModules 之后),再取同一 registry 里的
|
|
54
|
+
// apiClient 实例打 spy,否则 spy 会落在旧实例上。
|
|
55
|
+
const { loadMenusFromHost } = await loadMenuModule()
|
|
56
|
+
const { apiClient } = await import('../api/client')
|
|
57
|
+
const get = vi
|
|
58
|
+
.spyOn(apiClient, 'get')
|
|
59
|
+
.mockResolvedValue([{ code: 'fetched', path: '/fetched', title: 'Fetched', titleKey: 'menu.fetched' }])
|
|
60
|
+
|
|
61
|
+
await expect(loadMenusFromHost()).resolves.toEqual([
|
|
62
|
+
expect.objectContaining({ code: 'fetched' }),
|
|
63
|
+
])
|
|
64
|
+
expect(get).toHaveBeenCalledTimes(1)
|
|
65
|
+
get.mockRestore()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('treats injected nodes as plain data even when they carry extra fields', async () => {
|
|
69
|
+
stubHostMenus([{ ...validHostMenus[0], status: 'suspended', id: 'hacked', kind: 'page' }])
|
|
70
|
+
const { loadMenusFromHost } = await loadMenuModule()
|
|
71
|
+
|
|
72
|
+
const menus = await loadMenusFromHost()
|
|
73
|
+
expect(menus[0]).not.toHaveProperty('status')
|
|
74
|
+
expect(menus[0]).not.toHaveProperty('id')
|
|
75
|
+
expect(menus[0]).not.toHaveProperty('kind')
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -31,6 +31,16 @@ describe('parseMenuResponse', () => {
|
|
|
31
31
|
])
|
|
32
32
|
})
|
|
33
33
|
|
|
34
|
+
it('requires titleKey for array and items system menu responses', () => {
|
|
35
|
+
const menu = { code: 'dashboard', path: '/dashboard', title: 'Dashboard' }
|
|
36
|
+
expect(() => parseMenuResponse([menu], { requireTitleKey: true })).toThrow(
|
|
37
|
+
'Missing RBAC menu title key',
|
|
38
|
+
)
|
|
39
|
+
expect(() => parseMenuResponse({ items: [menu] }, { requireTitleKey: true })).toThrow(
|
|
40
|
+
'Missing RBAC menu title key',
|
|
41
|
+
)
|
|
42
|
+
})
|
|
43
|
+
|
|
34
44
|
it('maps a paged RBAC response into runtime menu nodes', () => {
|
|
35
45
|
const menus = parseMenuResponse({
|
|
36
46
|
list: [
|
|
@@ -135,6 +145,33 @@ describe('parseMenuResponse', () => {
|
|
|
135
145
|
])
|
|
136
146
|
})
|
|
137
147
|
|
|
148
|
+
it('requires titleKey when parsing paginated system menus', () => {
|
|
149
|
+
expect(() =>
|
|
150
|
+
parseMenuResponse(
|
|
151
|
+
{
|
|
152
|
+
list: [
|
|
153
|
+
{
|
|
154
|
+
id: 'users',
|
|
155
|
+
kind: 'page',
|
|
156
|
+
code: 'users',
|
|
157
|
+
title: 'Users',
|
|
158
|
+
path: '/users',
|
|
159
|
+
order: 1,
|
|
160
|
+
status: 'active',
|
|
161
|
+
visible: true,
|
|
162
|
+
cache: false,
|
|
163
|
+
tabHidden: false,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
total: 1,
|
|
167
|
+
page: 1,
|
|
168
|
+
pageSize: 20,
|
|
169
|
+
},
|
|
170
|
+
{ requireTitleKey: true },
|
|
171
|
+
),
|
|
172
|
+
).toThrow('Missing RBAC menu title key')
|
|
173
|
+
})
|
|
174
|
+
|
|
138
175
|
it('rejects malformed paged menu payloads', () => {
|
|
139
176
|
expect(() => parseMenuResponse({ list: {}, total: 1, page: 1, pageSize: 20 })).toThrow(
|
|
140
177
|
'Menu page response',
|
|
@@ -172,6 +209,7 @@ describe('parseMenuResponse', () => {
|
|
|
172
209
|
kind: 'directory',
|
|
173
210
|
code: 'system',
|
|
174
211
|
title: 'System',
|
|
212
|
+
titleKey: 'menu.system',
|
|
175
213
|
path: '/system',
|
|
176
214
|
order: 20,
|
|
177
215
|
status: 'active',
|
|
@@ -192,6 +230,7 @@ describe('parseMenuResponse', () => {
|
|
|
192
230
|
kind: 'page',
|
|
193
231
|
code: 'users',
|
|
194
232
|
title: 'Users',
|
|
233
|
+
titleKey: 'menu.users',
|
|
195
234
|
path: 'users',
|
|
196
235
|
componentKey: 'user-list',
|
|
197
236
|
order: 10,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BackendMenuNode } from './menuTypes'
|
|
2
2
|
import { apiClient } from '../api/client'
|
|
3
|
+
import { readHostInjection } from './embedded'
|
|
3
4
|
|
|
4
5
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
5
6
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
@@ -30,7 +31,7 @@ function readMenuPageResponse(value: unknown): MenuPageResponse | undefined {
|
|
|
30
31
|
return { list: value.list, total: value.total, page: value.page, pageSize: value.pageSize }
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
function readNode(value: unknown, index: string): BackendMenuNode {
|
|
34
|
+
function readNode(value: unknown, index: string, requireTitleKey = false): BackendMenuNode {
|
|
34
35
|
if (!isRecord(value)) throw new TypeError(`Invalid menu node at ${index}`)
|
|
35
36
|
if (
|
|
36
37
|
typeof value.code !== 'string' ||
|
|
@@ -39,6 +40,8 @@ function readNode(value: unknown, index: string): BackendMenuNode {
|
|
|
39
40
|
) {
|
|
40
41
|
throw new TypeError(`Invalid menu node fields at ${index}`)
|
|
41
42
|
}
|
|
43
|
+
if (requireTitleKey && !(typeof value.titleKey === 'string' && value.titleKey))
|
|
44
|
+
throw new TypeError(`Missing RBAC menu title key at ${index}`)
|
|
42
45
|
return {
|
|
43
46
|
code: value.code,
|
|
44
47
|
path: value.path,
|
|
@@ -58,7 +61,7 @@ function readNode(value: unknown, index: string): BackendMenuNode {
|
|
|
58
61
|
redirect: typeof value.redirect === 'string' ? value.redirect : undefined,
|
|
59
62
|
children: Array.isArray(value.children)
|
|
60
63
|
? value.children.map((child, childIndex) =>
|
|
61
|
-
readNode(child, `${index}.children[${childIndex}]
|
|
64
|
+
readNode(child, `${index}.children[${childIndex}]`, requireTitleKey),
|
|
62
65
|
)
|
|
63
66
|
: undefined,
|
|
64
67
|
}
|
|
@@ -86,7 +89,11 @@ interface RbacMenuNode {
|
|
|
86
89
|
children?: readonly RbacMenuNode[]
|
|
87
90
|
}
|
|
88
91
|
|
|
89
|
-
function readRbacMenuNode(
|
|
92
|
+
function readRbacMenuNode(
|
|
93
|
+
value: unknown,
|
|
94
|
+
index: string,
|
|
95
|
+
requireTitleKey = false,
|
|
96
|
+
): RbacMenuNode {
|
|
90
97
|
if (!isRecord(value)) throw new TypeError(`Invalid RBAC menu at ${index}`)
|
|
91
98
|
|
|
92
99
|
const kind = value.kind
|
|
@@ -113,6 +120,8 @@ function readRbacMenuNode(value: unknown, index: string): RbacMenuNode {
|
|
|
113
120
|
throw new TypeError(`Invalid RBAC menu path at ${index}`)
|
|
114
121
|
if (value.children !== undefined && !Array.isArray(value.children))
|
|
115
122
|
throw new TypeError(`Invalid RBAC menu children at ${index}`)
|
|
123
|
+
if (requireTitleKey && kind !== 'button' && !(typeof value.titleKey === 'string' && value.titleKey))
|
|
124
|
+
throw new TypeError(`Missing RBAC menu title key at ${index}`)
|
|
116
125
|
|
|
117
126
|
return {
|
|
118
127
|
id: value.id,
|
|
@@ -133,12 +142,15 @@ function readRbacMenuNode(value: unknown, index: string): RbacMenuNode {
|
|
|
133
142
|
tabHidden: value.tabHidden,
|
|
134
143
|
redirect: typeof value.redirect === 'string' ? value.redirect : undefined,
|
|
135
144
|
children: value.children?.map((child, childIndex) =>
|
|
136
|
-
readRbacMenuNode(child, `${index}.children[${childIndex}]
|
|
145
|
+
readRbacMenuNode(child, `${index}.children[${childIndex}]`, requireTitleKey),
|
|
137
146
|
),
|
|
138
147
|
}
|
|
139
148
|
}
|
|
140
149
|
|
|
141
|
-
function parseRbacMenuPage(
|
|
150
|
+
function parseRbacMenuPage(
|
|
151
|
+
value: Record<string, unknown>,
|
|
152
|
+
options: { requireTitleKey?: boolean } = {},
|
|
153
|
+
): readonly BackendMenuNode[] {
|
|
142
154
|
if (
|
|
143
155
|
!Array.isArray(value.list) ||
|
|
144
156
|
typeof value.total !== 'number' ||
|
|
@@ -157,7 +169,9 @@ function parseRbacMenuPage(value: Record<string, unknown>): readonly BackendMenu
|
|
|
157
169
|
}
|
|
158
170
|
}
|
|
159
171
|
collect(
|
|
160
|
-
value.list.map((row, index) =>
|
|
172
|
+
value.list.map((row, index) =>
|
|
173
|
+
readRbacMenuNode(row, `menus.list[${index}]`, options.requireTitleKey === true),
|
|
174
|
+
),
|
|
161
175
|
undefined,
|
|
162
176
|
)
|
|
163
177
|
|
|
@@ -209,8 +223,11 @@ function parseRbacMenuPage(value: Record<string, unknown>): readonly BackendMenu
|
|
|
209
223
|
})
|
|
210
224
|
}
|
|
211
225
|
|
|
212
|
-
export function parseMenuResponse(
|
|
213
|
-
|
|
226
|
+
export function parseMenuResponse(
|
|
227
|
+
value: unknown,
|
|
228
|
+
options: { requireTitleKey?: boolean } = {},
|
|
229
|
+
): readonly BackendMenuNode[] {
|
|
230
|
+
if (isRecord(value) && Object.hasOwn(value, 'list')) return parseRbacMenuPage(value, options)
|
|
214
231
|
const rows = Array.isArray(value)
|
|
215
232
|
? value
|
|
216
233
|
: isRecord(value) && Array.isArray(value.items)
|
|
@@ -218,7 +235,9 @@ export function parseMenuResponse(value: unknown): readonly BackendMenuNode[] {
|
|
|
218
235
|
: undefined
|
|
219
236
|
if (!rows)
|
|
220
237
|
throw new TypeError('Menu response must be an array, contain items, or be a menu page.')
|
|
221
|
-
return rows.map((row, index) =>
|
|
238
|
+
return rows.map((row, index) =>
|
|
239
|
+
readNode(row, `menus[${index}]`, options.requireTitleKey === true),
|
|
240
|
+
)
|
|
222
241
|
}
|
|
223
242
|
|
|
224
243
|
export async function fetchMenus(
|
|
@@ -233,7 +252,7 @@ export async function fetchMenus(
|
|
|
233
252
|
})
|
|
234
253
|
const firstResponse = await request(1, 100)
|
|
235
254
|
const firstPage = readMenuPageResponse(firstResponse)
|
|
236
|
-
if (!firstPage) return parseMenuResponse(firstResponse)
|
|
255
|
+
if (!firstPage) return parseMenuResponse(firstResponse, { requireTitleKey: true })
|
|
237
256
|
if (firstPage.page !== 1)
|
|
238
257
|
throw new TypeError('Menu response did not return the requested first page.')
|
|
239
258
|
if (
|
|
@@ -245,7 +264,7 @@ export async function fetchMenus(
|
|
|
245
264
|
}
|
|
246
265
|
|
|
247
266
|
const pageCount = Math.ceil(firstPage.total / firstPage.pageSize)
|
|
248
|
-
if (pageCount <= 1) return parseMenuResponse(firstResponse)
|
|
267
|
+
if (pageCount <= 1) return parseMenuResponse(firstResponse, { requireTitleKey: true })
|
|
249
268
|
|
|
250
269
|
const responses = await Promise.all(
|
|
251
270
|
Array.from({ length: pageCount - 1 }, (_, index) => request(index + 2, firstPage.pageSize)),
|
|
@@ -268,5 +287,22 @@ export async function fetchMenus(
|
|
|
268
287
|
return page
|
|
269
288
|
})
|
|
270
289
|
const rows = [...firstPage.list, ...pages.flatMap((page) => page.list)]
|
|
271
|
-
return parseMenuResponse(
|
|
290
|
+
return parseMenuResponse(
|
|
291
|
+
{ list: rows, total: rows.length, page: 1, pageSize: rows.length || 1 },
|
|
292
|
+
{ requireTitleKey: true },
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 嵌入模式菜单装载:优先取主壳聚合下发的 backendMenus。主壳注入属于不可信
|
|
298
|
+
* 输入,与 fetchMenus 走同一套严格解析,畸形数据在进入 routeTransformer 前
|
|
299
|
+
* 统一失败,而不是静默生成错误路由;主壳未下发时回落到经主壳网关拉取自身
|
|
300
|
+
* 菜单,独立模式不受影响。
|
|
301
|
+
*/
|
|
302
|
+
export async function loadMenusFromHost(): Promise<BackendMenuNode[]> {
|
|
303
|
+
const menus = readHostInjection().backendMenus
|
|
304
|
+
if (Array.isArray(menus) && menus.length > 0) {
|
|
305
|
+
return [...parseMenuResponse(menus)]
|
|
306
|
+
}
|
|
307
|
+
return [...(await fetchMenus())]
|
|
272
308
|
}
|