@origen-ui/admin-cli 1.0.3 → 2.0.1
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 +11 -7
- package/package.json +19 -3
- package/templates/current/clean/CHANGELOG.md +137 -0
- 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 +90 -46
- package/templates/current/clean/package.json +55 -41
- package/templates/current/clean/playwright.config.ts +1 -0
- package/templates/current/clean/playwright.demo.config.ts +1 -1
- package/templates/current/clean/scaffold.lock.json +2 -2
- 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 +84 -8
- package/templates/current/clean/scripts/generate-api.ts +87 -3
- package/templates/current/clean/scripts/vite-build-policy.spec.ts +72 -0
- package/templates/current/clean/src/App.vue +10 -3
- package/templates/current/clean/src/api/client.ts +90 -10
- package/templates/current/clean/src/api/generated/data-contracts.ts +0 -3
- 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 +6 -9
- 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 +7 -10
- 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 +6 -9
- 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 +6 -9
- package/templates/current/clean/src/api/generated//350/256/244/350/257/201.ts +3 -6
- package/templates/current/clean/src/app/adminLocale.ts +90 -0
- package/templates/current/clean/src/app/createAdminUi.spec.ts +65 -9
- package/templates/current/clean/src/app/createAdminUi.ts +14 -29
- package/templates/current/clean/src/app/embedded-token.spec.ts +40 -0
- package/templates/current/clean/src/app/embedded.spec.ts +135 -0
- package/templates/current/clean/src/app/embedded.ts +255 -0
- 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/localeFormat.ts +52 -0
- package/templates/current/clean/src/app/menu-host.spec.ts +77 -0
- package/templates/current/clean/src/app/menu.spec.ts +65 -0
- package/templates/current/clean/src/app/menu.ts +52 -12
- package/templates/current/clean/src/app/menuTypes.ts +1 -0
- package/templates/current/clean/src/app/origenUiLocale.spec.ts +6 -5
- package/templates/current/clean/src/app/origenUiLocale.ts +3 -58
- package/templates/current/clean/src/app/permission.spec.ts +26 -0
- package/templates/current/clean/src/app/runtimeConfig.ts +9 -8
- package/templates/current/clean/src/demo-api.spec.ts +311 -0
- package/templates/current/clean/src/env.d.ts +18 -0
- package/templates/current/clean/src/features/appearance/components/ThemePanel.vue +21 -17
- package/templates/current/clean/src/features/appearance/store/theme.ts +2 -1
- package/templates/current/clean/src/features/auth/pages/Login.vue +22 -12
- package/templates/current/clean/src/features/auth/store/auth.ts +6 -9
- 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/security/components/DevtoolsProtectionOverlay.vue +5 -2
- package/templates/current/clean/src/features/security/devtoolsProtection.spec.ts +7 -10
- package/templates/current/clean/src/features/security/devtoolsProtection.ts +2 -1
- package/templates/current/clean/src/features/system/access/api/admin-repository.spec.ts +14 -0
- package/templates/current/clean/src/features/system/access/api/admin-repository.ts +34 -15
- package/templates/current/clean/src/features/system/access/api/rbac.ts +52 -32
- 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/Empty.vue +4 -1
- package/templates/current/clean/src/features/workspace/pages/Profile.vue +14 -12
- package/templates/current/clean/src/layouts/AdminLayout.vue +6 -1
- package/templates/current/clean/src/layouts/TopBar.vue +60 -11
- 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 +517 -0
- package/templates/current/clean/src/locales/en-US.runtime.ts +22 -0
- package/templates/current/clean/src/locales/index.ts +144 -47
- package/templates/current/clean/src/locales/messages-types.ts +62 -0
- package/templates/current/clean/src/locales/messages.spec.ts +113 -6
- package/templates/current/clean/src/locales/vue-i18n.d.ts +5 -0
- package/templates/current/clean/src/locales/zh-CN.json +517 -0
- package/templates/current/clean/src/locales/zh-CN.runtime.ts +22 -0
- package/templates/current/clean/src/locales/zh-TW.json +517 -0
- package/templates/current/clean/src/locales/zh-TW.runtime.ts +22 -0
- package/templates/current/clean/src/main.ts +23 -14
- package/templates/current/clean/src/router/bootstrap.ts +20 -11
- 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 +19 -4
- 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 +30 -4
- package/templates/current/clean/src/router/queryEncryption.ts +48 -44
- package/templates/current/clean/src/router/staticRoutes.ts +6 -6
- package/templates/current/clean/src/router/useRouteQueryDefaults.spec.ts +2 -1
- package/templates/current/clean/src/router/useRouteQueryDefaults.types.ts +2 -1
- package/templates/current/clean/src/templates/og-table.ts +79 -37
- package/templates/current/clean/src/templates/templates.spec.ts +19 -0
- package/templates/current/clean/tsconfig.json +0 -1
- package/templates/current/clean/vite.build-policy.ts +161 -289
- package/templates/current/clean/vite.config.ts +34 -5
- package/templates/current/examples/CHANGELOG.md +137 -0
- 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 +90 -46
- package/templates/current/examples/package.json +55 -41
- package/templates/current/examples/playwright.config.ts +1 -0
- package/templates/current/examples/playwright.demo.config.ts +1 -1
- package/templates/current/examples/scaffold.lock.json +2 -2
- 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 +84 -8
- package/templates/current/examples/scripts/generate-api.ts +87 -3
- package/templates/current/examples/scripts/vite-build-policy.spec.ts +72 -0
- package/templates/current/examples/src/App.vue +10 -3
- package/templates/current/examples/src/api/client.ts +90 -10
- package/templates/current/examples/src/api/generated/data-contracts.ts +0 -3
- 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 +6 -9
- 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 +7 -10
- 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 +6 -9
- 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 +6 -9
- package/templates/current/examples/src/api/generated//350/256/244/350/257/201.ts +3 -6
- package/templates/current/examples/src/app/adminLocale.ts +90 -0
- package/templates/current/examples/src/app/createAdminUi.spec.ts +65 -9
- package/templates/current/examples/src/app/createAdminUi.ts +14 -29
- package/templates/current/examples/src/app/embedded-token.spec.ts +40 -0
- package/templates/current/examples/src/app/embedded.spec.ts +135 -0
- package/templates/current/examples/src/app/embedded.ts +255 -0
- 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/localeFormat.ts +52 -0
- package/templates/current/examples/src/app/menu-host.spec.ts +77 -0
- package/templates/current/examples/src/app/menu.spec.ts +65 -0
- package/templates/current/examples/src/app/menu.ts +52 -12
- package/templates/current/examples/src/app/menuTypes.ts +1 -0
- package/templates/current/examples/src/app/origenUiLocale.spec.ts +6 -5
- package/templates/current/examples/src/app/origenUiLocale.ts +3 -58
- package/templates/current/examples/src/app/permission.spec.ts +26 -0
- package/templates/current/examples/src/app/runtimeConfig.ts +9 -8
- package/templates/current/examples/src/demo-api.spec.ts +311 -0
- package/templates/current/examples/src/env.d.ts +18 -0
- package/templates/current/examples/src/features/appearance/components/ThemePanel.vue +21 -17
- package/templates/current/examples/src/features/appearance/store/theme.ts +2 -1
- package/templates/current/examples/src/features/auth/pages/Login.vue +22 -12
- package/templates/current/examples/src/features/auth/store/auth.ts +6 -9
- package/templates/current/examples/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/examples/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/examples/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/examples/src/features/errors/pages/error-pages.spec.ts +9 -1
- package/templates/current/examples/src/features/feature-composables.spec.ts +47 -10
- package/templates/current/examples/src/features/security/components/DevtoolsProtectionOverlay.vue +5 -2
- package/templates/current/examples/src/features/security/devtoolsProtection.spec.ts +7 -10
- package/templates/current/examples/src/features/security/devtoolsProtection.ts +2 -1
- package/templates/current/examples/src/features/system/access/api/admin-repository.spec.ts +14 -0
- package/templates/current/examples/src/features/system/access/api/admin-repository.ts +34 -15
- package/templates/current/examples/src/features/system/access/api/rbac.ts +52 -32
- 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/audit/composables/useAuditManagement.ts +43 -38
- package/templates/current/examples/src/features/system/menus/components/MenuEditorForm.vue +137 -47
- package/templates/current/examples/src/features/system/menus/composables/useMenuManagement.ts +215 -107
- 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/menus/pages/MenuList.vue +3 -1
- package/templates/current/examples/src/features/system/roles/components/RoleEditorForm.vue +19 -15
- package/templates/current/examples/src/features/system/roles/composables/useRoleManagement.ts +49 -53
- package/templates/current/examples/src/features/system/roles/pages/RoleList.vue +3 -1
- package/templates/current/examples/src/features/system/users/components/UserEditorForm.vue +22 -19
- package/templates/current/examples/src/features/system/users/composables/useUserManagement.ts +59 -64
- package/templates/current/examples/src/features/system/users/pages/UserList.vue +3 -1
- package/templates/current/examples/src/features/workspace/pages/Dashboard.spec.ts +9 -0
- package/templates/current/examples/src/features/workspace/pages/Dashboard.vue +54 -26
- package/templates/current/examples/src/features/workspace/pages/Empty.vue +4 -1
- package/templates/current/examples/src/features/workspace/pages/Profile.vue +14 -12
- package/templates/current/examples/src/layouts/AdminLayout.vue +6 -1
- package/templates/current/examples/src/layouts/TopBar.vue +60 -11
- 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 +517 -0
- package/templates/current/examples/src/locales/en-US.runtime.ts +22 -0
- package/templates/current/examples/src/locales/index.ts +144 -47
- package/templates/current/examples/src/locales/messages-types.ts +62 -0
- package/templates/current/examples/src/locales/messages.spec.ts +113 -6
- package/templates/current/examples/src/locales/vue-i18n.d.ts +5 -0
- package/templates/current/examples/src/locales/zh-CN.json +517 -0
- package/templates/current/examples/src/locales/zh-CN.runtime.ts +22 -0
- package/templates/current/examples/src/locales/zh-TW.json +517 -0
- package/templates/current/examples/src/locales/zh-TW.runtime.ts +22 -0
- package/templates/current/examples/src/main.ts +43 -15
- package/templates/current/examples/src/router/bootstrap.ts +20 -11
- 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 +37 -2
- package/templates/current/examples/src/router/dynamicRoutes.ts +19 -4
- 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 +30 -4
- package/templates/current/examples/src/router/queryEncryption.ts +48 -44
- package/templates/current/examples/src/router/staticRoutes.ts +4 -4
- package/templates/current/examples/src/router/useRouteQueryDefaults.spec.ts +2 -1
- package/templates/current/examples/src/router/useRouteQueryDefaults.types.ts +2 -1
- package/templates/current/examples/src/templates/og-table.ts +79 -37
- package/templates/current/examples/src/templates/templates.spec.ts +19 -0
- package/templates/current/examples/tsconfig.json +0 -1
- package/templates/current/examples/vite.build-policy.ts +161 -289
- package/templates/current/examples/vite.config.ts +34 -5
- package/templates/current/clean/scripts/check-admin-bundle.mjs +0 -214
- package/templates/current/clean/scripts/performance/baselines/bundle-gzip-v2-pre-entry-unification.json +0 -38
- package/templates/current/clean/src/locales/en-US.ts +0 -55
- package/templates/current/clean/src/locales/zh-CN.ts +0 -55
- package/templates/current/clean/src/locales/zh-TW.ts +0 -44
- package/templates/current/examples/scripts/check-admin-bundle.mjs +0 -214
- package/templates/current/examples/scripts/performance/baselines/bundle-gzip-v2-pre-entry-unification.json +0 -38
- package/templates/current/examples/src/locales/en-US.ts +0 -55
- package/templates/current/examples/src/locales/zh-CN.ts +0 -55
- package/templates/current/examples/src/locales/zh-TW.ts +0 -44
|
@@ -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,52 @@
|
|
|
1
|
+
import { currentAdminLocale } from './adminLocale'
|
|
2
|
+
|
|
3
|
+
export { currentAdminLocale }
|
|
4
|
+
|
|
5
|
+
type DateTimeFormatOptions = Intl.DateTimeFormatOptions
|
|
6
|
+
|
|
7
|
+
const formatterCache = new Map<string, Intl.DateTimeFormat>()
|
|
8
|
+
|
|
9
|
+
function formatter(options: DateTimeFormatOptions, key: string): Intl.DateTimeFormat {
|
|
10
|
+
const cacheKey = `${currentAdminLocale()}|${key}`
|
|
11
|
+
const cached = formatterCache.get(cacheKey)
|
|
12
|
+
if (cached) return cached
|
|
13
|
+
const created = new Intl.DateTimeFormat(currentAdminLocale(), options)
|
|
14
|
+
formatterCache.set(cacheKey, created)
|
|
15
|
+
return created
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function format(value: unknown, options: DateTimeFormatOptions, key: string): string {
|
|
19
|
+
const date = new Date(String(value))
|
|
20
|
+
return Number.isNaN(date.valueOf()) ? '-' : formatter(options, key).format(date)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Formats a calendar date (year-month-day) in the active locale. */
|
|
24
|
+
export function formatAdminDate(value: unknown): string {
|
|
25
|
+
return format(value, { year: 'numeric', month: '2-digit', day: '2-digit' }, 'date')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Formats a full timestamp (date plus hours-minutes-seconds) in the active locale. */
|
|
29
|
+
export function formatAdminDateTime(value: unknown): string {
|
|
30
|
+
return format(
|
|
31
|
+
value,
|
|
32
|
+
{
|
|
33
|
+
year: 'numeric',
|
|
34
|
+
month: '2-digit',
|
|
35
|
+
day: '2-digit',
|
|
36
|
+
hour: '2-digit',
|
|
37
|
+
minute: '2-digit',
|
|
38
|
+
second: '2-digit',
|
|
39
|
+
hour12: false,
|
|
40
|
+
},
|
|
41
|
+
'dateTime',
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Formats a compact timestamp (month-day hours-minutes) in the active locale. */
|
|
46
|
+
export function formatAdminShortDateTime(value: unknown): string {
|
|
47
|
+
return format(
|
|
48
|
+
value,
|
|
49
|
+
{ month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false },
|
|
50
|
+
'shortDateTime',
|
|
51
|
+
)
|
|
52
|
+
}
|
|
@@ -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
|
+
})
|
|
@@ -5,6 +5,42 @@ import { fetchMenus, parseMenuResponse } from './menu'
|
|
|
5
5
|
const timestamps = { createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z' }
|
|
6
6
|
|
|
7
7
|
describe('parseMenuResponse', () => {
|
|
8
|
+
it('preserves an optional titleKey on backend menu nodes', () => {
|
|
9
|
+
expect(
|
|
10
|
+
parseMenuResponse([
|
|
11
|
+
{ code: 'dashboard', path: '/dashboard', title: 'Dashboard', titleKey: 'menu.dashboard' },
|
|
12
|
+
]),
|
|
13
|
+
).toEqual([
|
|
14
|
+
{
|
|
15
|
+
code: 'dashboard',
|
|
16
|
+
path: '/dashboard',
|
|
17
|
+
title: 'Dashboard',
|
|
18
|
+
titleKey: 'menu.dashboard',
|
|
19
|
+
componentKey: undefined,
|
|
20
|
+
icon: undefined,
|
|
21
|
+
permissions: undefined,
|
|
22
|
+
parentPath: undefined,
|
|
23
|
+
menu: undefined,
|
|
24
|
+
hidden: undefined,
|
|
25
|
+
order: undefined,
|
|
26
|
+
cache: undefined,
|
|
27
|
+
tabHidden: undefined,
|
|
28
|
+
redirect: undefined,
|
|
29
|
+
children: undefined,
|
|
30
|
+
},
|
|
31
|
+
])
|
|
32
|
+
})
|
|
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
|
+
|
|
8
44
|
it('maps a paged RBAC response into runtime menu nodes', () => {
|
|
9
45
|
const menus = parseMenuResponse({
|
|
10
46
|
list: [
|
|
@@ -109,6 +145,33 @@ describe('parseMenuResponse', () => {
|
|
|
109
145
|
])
|
|
110
146
|
})
|
|
111
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
|
+
|
|
112
175
|
it('rejects malformed paged menu payloads', () => {
|
|
113
176
|
expect(() => parseMenuResponse({ list: {}, total: 1, page: 1, pageSize: 20 })).toThrow(
|
|
114
177
|
'Menu page response',
|
|
@@ -146,6 +209,7 @@ describe('parseMenuResponse', () => {
|
|
|
146
209
|
kind: 'directory',
|
|
147
210
|
code: 'system',
|
|
148
211
|
title: 'System',
|
|
212
|
+
titleKey: 'menu.system',
|
|
149
213
|
path: '/system',
|
|
150
214
|
order: 20,
|
|
151
215
|
status: 'active',
|
|
@@ -166,6 +230,7 @@ describe('parseMenuResponse', () => {
|
|
|
166
230
|
kind: 'page',
|
|
167
231
|
code: 'users',
|
|
168
232
|
title: 'Users',
|
|
233
|
+
titleKey: 'menu.users',
|
|
169
234
|
path: 'users',
|
|
170
235
|
componentKey: 'user-list',
|
|
171
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,10 +40,13 @@ 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,
|
|
45
48
|
title: value.title,
|
|
49
|
+
...(typeof value.titleKey === 'string' && value.titleKey ? { titleKey: value.titleKey } : {}),
|
|
46
50
|
componentKey: typeof value.componentKey === 'string' ? value.componentKey : undefined,
|
|
47
51
|
icon: typeof value.icon === 'string' ? value.icon : undefined,
|
|
48
52
|
permissions: Array.isArray(value.permissions)
|
|
@@ -57,7 +61,7 @@ function readNode(value: unknown, index: string): BackendMenuNode {
|
|
|
57
61
|
redirect: typeof value.redirect === 'string' ? value.redirect : undefined,
|
|
58
62
|
children: Array.isArray(value.children)
|
|
59
63
|
? value.children.map((child, childIndex) =>
|
|
60
|
-
readNode(child, `${index}.children[${childIndex}]
|
|
64
|
+
readNode(child, `${index}.children[${childIndex}]`, requireTitleKey),
|
|
61
65
|
)
|
|
62
66
|
: undefined,
|
|
63
67
|
}
|
|
@@ -71,6 +75,7 @@ interface RbacMenuNode {
|
|
|
71
75
|
kind: RbacMenuKind
|
|
72
76
|
code: string
|
|
73
77
|
title: string
|
|
78
|
+
titleKey?: string
|
|
74
79
|
path?: string
|
|
75
80
|
componentKey?: string
|
|
76
81
|
icon?: string
|
|
@@ -84,7 +89,11 @@ interface RbacMenuNode {
|
|
|
84
89
|
children?: readonly RbacMenuNode[]
|
|
85
90
|
}
|
|
86
91
|
|
|
87
|
-
function readRbacMenuNode(
|
|
92
|
+
function readRbacMenuNode(
|
|
93
|
+
value: unknown,
|
|
94
|
+
index: string,
|
|
95
|
+
requireTitleKey = false,
|
|
96
|
+
): RbacMenuNode {
|
|
88
97
|
if (!isRecord(value)) throw new TypeError(`Invalid RBAC menu at ${index}`)
|
|
89
98
|
|
|
90
99
|
const kind = value.kind
|
|
@@ -111,6 +120,8 @@ function readRbacMenuNode(value: unknown, index: string): RbacMenuNode {
|
|
|
111
120
|
throw new TypeError(`Invalid RBAC menu path at ${index}`)
|
|
112
121
|
if (value.children !== undefined && !Array.isArray(value.children))
|
|
113
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}`)
|
|
114
125
|
|
|
115
126
|
return {
|
|
116
127
|
id: value.id,
|
|
@@ -118,6 +129,7 @@ function readRbacMenuNode(value: unknown, index: string): RbacMenuNode {
|
|
|
118
129
|
kind,
|
|
119
130
|
code: value.code,
|
|
120
131
|
title: value.title,
|
|
132
|
+
titleKey: typeof value.titleKey === 'string' && value.titleKey ? value.titleKey : undefined,
|
|
121
133
|
path: value.path,
|
|
122
134
|
componentKey: typeof value.componentKey === 'string' ? value.componentKey : undefined,
|
|
123
135
|
icon: typeof value.icon === 'string' ? value.icon : undefined,
|
|
@@ -130,12 +142,15 @@ function readRbacMenuNode(value: unknown, index: string): RbacMenuNode {
|
|
|
130
142
|
tabHidden: value.tabHidden,
|
|
131
143
|
redirect: typeof value.redirect === 'string' ? value.redirect : undefined,
|
|
132
144
|
children: value.children?.map((child, childIndex) =>
|
|
133
|
-
readRbacMenuNode(child, `${index}.children[${childIndex}]
|
|
145
|
+
readRbacMenuNode(child, `${index}.children[${childIndex}]`, requireTitleKey),
|
|
134
146
|
),
|
|
135
147
|
}
|
|
136
148
|
}
|
|
137
149
|
|
|
138
|
-
function parseRbacMenuPage(
|
|
150
|
+
function parseRbacMenuPage(
|
|
151
|
+
value: Record<string, unknown>,
|
|
152
|
+
options: { requireTitleKey?: boolean } = {},
|
|
153
|
+
): readonly BackendMenuNode[] {
|
|
139
154
|
if (
|
|
140
155
|
!Array.isArray(value.list) ||
|
|
141
156
|
typeof value.total !== 'number' ||
|
|
@@ -154,7 +169,9 @@ function parseRbacMenuPage(value: Record<string, unknown>): readonly BackendMenu
|
|
|
154
169
|
}
|
|
155
170
|
}
|
|
156
171
|
collect(
|
|
157
|
-
value.list.map((row, index) =>
|
|
172
|
+
value.list.map((row, index) =>
|
|
173
|
+
readRbacMenuNode(row, `menus.list[${index}]`, options.requireTitleKey === true),
|
|
174
|
+
),
|
|
158
175
|
undefined,
|
|
159
176
|
)
|
|
160
177
|
|
|
@@ -183,6 +200,7 @@ function parseRbacMenuPage(value: Record<string, unknown>): readonly BackendMenu
|
|
|
183
200
|
code: menu.code,
|
|
184
201
|
path: menu.path,
|
|
185
202
|
title: menu.title,
|
|
203
|
+
...(menu.titleKey ? { titleKey: menu.titleKey } : {}),
|
|
186
204
|
componentKey: menu.componentKey,
|
|
187
205
|
icon: menu.icon,
|
|
188
206
|
permissions: menu.permission ? [menu.permission] : undefined,
|
|
@@ -205,8 +223,11 @@ function parseRbacMenuPage(value: Record<string, unknown>): readonly BackendMenu
|
|
|
205
223
|
})
|
|
206
224
|
}
|
|
207
225
|
|
|
208
|
-
export function parseMenuResponse(
|
|
209
|
-
|
|
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)
|
|
210
231
|
const rows = Array.isArray(value)
|
|
211
232
|
? value
|
|
212
233
|
: isRecord(value) && Array.isArray(value.items)
|
|
@@ -214,7 +235,9 @@ export function parseMenuResponse(value: unknown): readonly BackendMenuNode[] {
|
|
|
214
235
|
: undefined
|
|
215
236
|
if (!rows)
|
|
216
237
|
throw new TypeError('Menu response must be an array, contain items, or be a menu page.')
|
|
217
|
-
return rows.map((row, index) =>
|
|
238
|
+
return rows.map((row, index) =>
|
|
239
|
+
readNode(row, `menus[${index}]`, options.requireTitleKey === true),
|
|
240
|
+
)
|
|
218
241
|
}
|
|
219
242
|
|
|
220
243
|
export async function fetchMenus(
|
|
@@ -229,7 +252,7 @@ export async function fetchMenus(
|
|
|
229
252
|
})
|
|
230
253
|
const firstResponse = await request(1, 100)
|
|
231
254
|
const firstPage = readMenuPageResponse(firstResponse)
|
|
232
|
-
if (!firstPage) return parseMenuResponse(firstResponse)
|
|
255
|
+
if (!firstPage) return parseMenuResponse(firstResponse, { requireTitleKey: true })
|
|
233
256
|
if (firstPage.page !== 1)
|
|
234
257
|
throw new TypeError('Menu response did not return the requested first page.')
|
|
235
258
|
if (
|
|
@@ -241,7 +264,7 @@ export async function fetchMenus(
|
|
|
241
264
|
}
|
|
242
265
|
|
|
243
266
|
const pageCount = Math.ceil(firstPage.total / firstPage.pageSize)
|
|
244
|
-
if (pageCount <= 1) return parseMenuResponse(firstResponse)
|
|
267
|
+
if (pageCount <= 1) return parseMenuResponse(firstResponse, { requireTitleKey: true })
|
|
245
268
|
|
|
246
269
|
const responses = await Promise.all(
|
|
247
270
|
Array.from({ length: pageCount - 1 }, (_, index) => request(index + 2, firstPage.pageSize)),
|
|
@@ -264,5 +287,22 @@ export async function fetchMenus(
|
|
|
264
287
|
return page
|
|
265
288
|
})
|
|
266
289
|
const rows = [...firstPage.list, ...pages.flatMap((page) => page.list)]
|
|
267
|
-
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())]
|
|
268
308
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { resolveAdminLocale
|
|
2
|
+
import { resolveAdminLocale } from './origenUiLocale'
|
|
3
3
|
|
|
4
|
-
describe('admin
|
|
5
|
-
it('
|
|
4
|
+
describe('admin locale resolution', () => {
|
|
5
|
+
it('keeps the supported application locales', () => {
|
|
6
6
|
expect(resolveAdminLocale('zh-CN')).toBe('zh-CN')
|
|
7
|
-
expect(
|
|
8
|
-
expect(
|
|
7
|
+
expect(resolveAdminLocale('en-US')).toBe('en-US')
|
|
8
|
+
expect(resolveAdminLocale('zh-TW')).toBe('zh-TW')
|
|
9
9
|
})
|
|
10
10
|
|
|
11
11
|
it('falls back unknown locales to zh-CN', () => {
|
|
12
12
|
expect(resolveAdminLocale('fr-FR')).toBe('zh-CN')
|
|
13
|
+
expect(resolveAdminLocale(undefined)).toBe('zh-CN')
|
|
13
14
|
})
|
|
14
15
|
})
|
|
@@ -1,65 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { OrigenUiMessages } from 'origen-ui'
|
|
3
|
-
import {
|
|
4
|
-
OG_DIALOG_LOCALE_EN_US,
|
|
5
|
-
OG_DIALOG_LOCALE_ZH_CN,
|
|
6
|
-
OG_DIALOG_LOCALE_ZH_TW,
|
|
7
|
-
} from '@origen-ui/dialog/runtime'
|
|
8
|
-
import {
|
|
9
|
-
OG_FORM_LOCALE_EN_US,
|
|
10
|
-
OG_FORM_LOCALE_ZH_CN,
|
|
11
|
-
OG_FORM_LOCALE_ZH_TW,
|
|
12
|
-
} from '@origen-ui/form/runtime'
|
|
13
|
-
import {
|
|
14
|
-
OG_SEARCH_LOCALE_EN_US,
|
|
15
|
-
OG_SEARCH_LOCALE_ZH_CN,
|
|
16
|
-
OG_SEARCH_LOCALE_ZH_TW,
|
|
17
|
-
} from '@origen-ui/search/runtime'
|
|
18
|
-
import {
|
|
19
|
-
OG_TABLE_LOCALE_EN_US,
|
|
20
|
-
OG_TABLE_LOCALE_ZH_CN,
|
|
21
|
-
OG_TABLE_LOCALE_ZH_TW,
|
|
22
|
-
} from '@origen-ui/table/runtime'
|
|
23
|
-
import {
|
|
24
|
-
OG_TABS_LOCALE_EN_US,
|
|
25
|
-
OG_TABS_LOCALE_ZH_CN,
|
|
26
|
-
OG_TABS_LOCALE_ZH_TW,
|
|
27
|
-
} from '@origen-ui/tabs/runtime'
|
|
1
|
+
import type { AdminLocale } from './adminLocale'
|
|
28
2
|
|
|
29
|
-
export type AdminLocale
|
|
30
|
-
export type OrigenUiLocaleBundle = DeepPartial<OrigenUiMessages>
|
|
3
|
+
export type { AdminLocale } from './adminLocale'
|
|
31
4
|
|
|
32
|
-
/** Resolves
|
|
5
|
+
/** Resolves the application locale selected for vue-i18n and the Origen UI language runtime. */
|
|
33
6
|
export function resolveAdminLocale(locale: string | undefined): AdminLocale {
|
|
34
7
|
if (locale === 'en-US') return 'en-US'
|
|
35
8
|
if (locale === 'zh-TW') return 'zh-TW'
|
|
36
9
|
return 'zh-CN'
|
|
37
10
|
}
|
|
38
|
-
|
|
39
|
-
export function resolveAdminOrigenUiLocale(locale: string | undefined): OrigenUiLocaleBundle {
|
|
40
|
-
if (resolveAdminLocale(locale) === 'en-US') {
|
|
41
|
-
return {
|
|
42
|
-
dialog: OG_DIALOG_LOCALE_EN_US,
|
|
43
|
-
form: OG_FORM_LOCALE_EN_US,
|
|
44
|
-
search: OG_SEARCH_LOCALE_EN_US,
|
|
45
|
-
table: OG_TABLE_LOCALE_EN_US,
|
|
46
|
-
tabs: OG_TABS_LOCALE_EN_US,
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (resolveAdminLocale(locale) === 'zh-TW') {
|
|
50
|
-
return {
|
|
51
|
-
dialog: OG_DIALOG_LOCALE_ZH_TW,
|
|
52
|
-
form: OG_FORM_LOCALE_ZH_TW,
|
|
53
|
-
search: OG_SEARCH_LOCALE_ZH_TW,
|
|
54
|
-
table: OG_TABLE_LOCALE_ZH_TW,
|
|
55
|
-
tabs: OG_TABS_LOCALE_ZH_TW,
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
dialog: OG_DIALOG_LOCALE_ZH_CN,
|
|
60
|
-
form: OG_FORM_LOCALE_ZH_CN,
|
|
61
|
-
search: OG_SEARCH_LOCALE_ZH_CN,
|
|
62
|
-
table: OG_TABLE_LOCALE_ZH_CN,
|
|
63
|
-
tabs: OG_TABS_LOCALE_ZH_CN,
|
|
64
|
-
}
|
|
65
|
-
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { setActivePinia } from 'pinia'
|
|
2
|
+
import { beforeEach, describe, expect, it } from 'vitest'
|
|
3
|
+
import { useAuthStore } from '../features/auth/store/auth'
|
|
4
|
+
import { pinia } from '../store'
|
|
5
|
+
import { permission } from './permission'
|
|
6
|
+
|
|
7
|
+
describe('admin permission runtime', () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
// runtime 实例在 app.use(pinia) 之前创建,因此不能读取激活的 Pinia 实例。
|
|
10
|
+
setActivePinia(undefined)
|
|
11
|
+
useAuthStore(pinia).$reset()
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('reads permissions from the explicit application Pinia instance before installation', () => {
|
|
15
|
+
useAuthStore(pinia).session = {
|
|
16
|
+
token: 'test-token',
|
|
17
|
+
user: { id: '1', username: 'admin', displayName: 'Admin' },
|
|
18
|
+
permissions: ['system:users:read'],
|
|
19
|
+
roleIds: [],
|
|
20
|
+
authorizationRevision: 3,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
expect(permission.can('system:users:read')).toBe(true)
|
|
24
|
+
expect(permission.can('system:users:write')).toBe(false)
|
|
25
|
+
})
|
|
26
|
+
})
|