@qlover/create-app 0.7.14 → 0.8.0
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/CHANGELOG.md +27 -0
- package/dist/configs/_common/.gitignore.template +6 -0
- package/dist/configs/_common/.prettierignore +17 -5
- package/dist/configs/_common/.vscode/settings.json +6 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/templates/next-app/.env.template +1 -1
- package/dist/templates/next-app/README.en.md +130 -0
- package/dist/templates/next-app/README.md +114 -20
- package/dist/templates/next-app/config/Identifier/api.ts +5 -5
- package/dist/templates/next-app/config/Identifier/common/admint.table.ts +69 -0
- package/dist/templates/next-app/config/Identifier/common/common.ts +76 -0
- package/dist/templates/next-app/config/Identifier/common/index.ts +3 -0
- package/dist/templates/next-app/config/Identifier/{validator.ts → common/validators.ts} +5 -5
- package/dist/templates/next-app/config/Identifier/index.ts +2 -12
- package/dist/templates/next-app/config/Identifier/pages/index.ts +6 -0
- package/dist/templates/next-app/config/Identifier/pages/page.admin.home.ts +27 -0
- package/dist/templates/next-app/config/Identifier/pages/page.admin.locales.ts +266 -0
- package/dist/templates/next-app/config/Identifier/pages/page.admin.user.ts +293 -0
- package/dist/templates/{react-app/config/Identifier → next-app/config/Identifier/pages}/page.home.ts +15 -22
- package/dist/templates/next-app/config/Identifier/{page.login.ts → pages/page.login.ts} +28 -34
- package/dist/templates/next-app/config/Identifier/{page.register.ts → pages/page.register.ts} +30 -29
- package/dist/templates/next-app/config/adminNavs.ts +19 -0
- package/dist/templates/next-app/config/common.ts +22 -13
- package/dist/templates/next-app/config/i18n/HomeI18n.ts +5 -5
- package/dist/templates/next-app/config/i18n/admin18n.ts +61 -19
- package/dist/templates/next-app/config/i18n/i18nConfig.ts +2 -0
- package/dist/templates/next-app/config/i18n/i18nKeyScheam.ts +36 -0
- package/dist/templates/next-app/config/i18n/loginI18n.ts +22 -22
- package/dist/templates/next-app/config/i18n/register18n.ts +23 -24
- package/dist/templates/next-app/docs/en/api.md +387 -0
- package/dist/templates/next-app/docs/en/component.md +544 -0
- package/dist/templates/next-app/docs/en/database.md +496 -0
- package/dist/templates/next-app/docs/en/development-guide.md +727 -0
- package/dist/templates/next-app/docs/en/env.md +563 -0
- package/dist/templates/next-app/docs/en/i18n.md +287 -0
- package/dist/templates/next-app/docs/en/index.md +165 -0
- package/dist/templates/next-app/docs/en/page.md +457 -0
- package/dist/templates/next-app/docs/en/project-structure.md +176 -0
- package/dist/templates/next-app/docs/en/router.md +427 -0
- package/dist/templates/next-app/docs/en/theme.md +532 -0
- package/dist/templates/next-app/docs/en/validator.md +478 -0
- package/dist/templates/next-app/docs/zh/api.md +387 -0
- package/dist/templates/next-app/docs/zh/component.md +544 -0
- package/dist/templates/next-app/docs/zh/database.md +496 -0
- package/dist/templates/next-app/docs/zh/development-guide.md +727 -0
- package/dist/templates/next-app/docs/zh/env.md +563 -0
- package/dist/templates/next-app/docs/zh/i18n.md +287 -0
- package/dist/templates/next-app/docs/zh/index.md +165 -0
- package/dist/templates/next-app/docs/zh/page.md +457 -0
- package/dist/templates/next-app/docs/zh/project-structure.md +176 -0
- package/dist/templates/next-app/docs/zh/router.md +427 -0
- package/dist/templates/next-app/docs/zh/theme.md +532 -0
- package/dist/templates/next-app/docs/zh/validator.md +476 -0
- package/dist/templates/next-app/make/generateLocales.ts +19 -12
- package/dist/templates/next-app/migrations/schema/LocalesSchema.ts +15 -0
- package/dist/templates/next-app/migrations/sql/1694244000000.sql +11 -0
- package/dist/templates/next-app/package.json +7 -3
- package/dist/templates/next-app/public/locales/en.json +172 -207
- package/dist/templates/next-app/public/locales/zh.json +172 -207
- package/dist/templates/next-app/src/app/[locale]/admin/locales/page.tsx +153 -0
- package/dist/templates/next-app/src/app/[locale]/admin/users/page.tsx +48 -50
- package/dist/templates/next-app/src/app/[locale]/login/LoginForm.tsx +2 -2
- package/dist/templates/next-app/src/app/api/admin/locales/create/route.ts +34 -0
- package/dist/templates/next-app/src/app/api/admin/locales/import/route.ts +40 -0
- package/dist/templates/next-app/src/app/api/admin/locales/route.ts +42 -0
- package/dist/templates/next-app/src/app/api/admin/locales/update/route.ts +32 -0
- package/dist/templates/next-app/src/app/api/locales/json/route.ts +44 -0
- package/dist/templates/next-app/src/base/cases/AdminPageManager.ts +1 -13
- package/dist/templates/next-app/src/base/cases/Datetime.ts +18 -0
- package/dist/templates/next-app/src/base/cases/DialogErrorPlugin.ts +12 -6
- package/dist/templates/next-app/src/base/cases/ResourceState.ts +17 -0
- package/dist/templates/next-app/src/base/cases/TranslateI18nInterface.ts +25 -0
- package/dist/templates/next-app/src/base/cases/ZodColumnBuilder.ts +200 -0
- package/dist/templates/next-app/src/base/port/ZodBuilderInterface.ts +8 -0
- package/dist/templates/next-app/src/base/services/AdminLocalesService.ts +20 -0
- package/dist/templates/next-app/src/base/services/AdminPageEvent.ts +26 -0
- package/dist/templates/next-app/src/base/services/AdminPageScheduler.ts +42 -0
- package/dist/templates/next-app/src/base/services/ResourceService.ts +122 -0
- package/dist/templates/next-app/src/base/services/adminApi/AdminLocalesApi.ts +104 -0
- package/dist/templates/next-app/src/base/services/adminApi/AdminUserApi.ts +38 -5
- package/dist/templates/next-app/src/base/services/appApi/AppApiPlugin.ts +1 -1
- package/dist/templates/next-app/src/i18n/request.ts +30 -1
- package/dist/templates/next-app/src/server/PageParams.ts +2 -10
- package/dist/templates/next-app/src/server/port/DBBridgeInterface.ts +5 -0
- package/dist/templates/next-app/src/server/port/DBTableInterface.ts +2 -0
- package/dist/templates/next-app/src/server/port/LocalesRepositoryInterface.ts +43 -0
- package/dist/templates/next-app/src/server/repositorys/LocalesRepository.ts +197 -0
- package/dist/templates/next-app/src/server/services/ApiLocaleService.ts +122 -0
- package/dist/templates/next-app/src/server/sqlBridges/SupabaseBridge.ts +60 -11
- package/dist/templates/next-app/src/server/validators/ExtendedExecutorError.ts +6 -0
- package/dist/templates/next-app/src/server/validators/LocalesValidator.ts +131 -0
- package/dist/templates/next-app/src/server/validators/LoginValidator.ts +2 -5
- package/dist/templates/next-app/src/server/validators/PaginationValidator.ts +32 -16
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/_default.css +2 -1
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/dark.css +28 -29
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/pink.css +2 -1
- package/dist/templates/next-app/src/uikit/components/AdminLayout.tsx +17 -3
- package/dist/templates/next-app/src/uikit/components/BaseHeader.tsx +5 -4
- package/dist/templates/next-app/src/uikit/components/BaseLayout.tsx +5 -4
- package/dist/templates/next-app/src/uikit/components/BootstrapsProvider.tsx +3 -2
- package/dist/templates/next-app/src/uikit/components/ComboProvider.tsx +1 -1
- package/dist/templates/next-app/src/uikit/components/EditableCell.tsx +118 -0
- package/dist/templates/next-app/src/uikit/components/LogoutButton.tsx +5 -6
- package/dist/templates/next-app/src/uikit/components/ThemeSwitcher.tsx +1 -1
- package/dist/templates/next-app/src/uikit/components/With.tsx +2 -2
- package/dist/templates/next-app/src/uikit/components/localesImportButton/LocalesImportButton.tsx +62 -0
- package/dist/templates/next-app/src/uikit/components/localesImportButton/LocalesImportEvent.ts +28 -0
- package/dist/templates/next-app/src/uikit/components/localesImportButton/import.module.css +6 -0
- package/dist/templates/next-app/src/uikit/hook/useI18nInterface.ts +8 -14
- package/dist/templates/next-app/src/uikit/hook/useWarnTranslations.ts +25 -0
- package/dist/templates/react-app/.prettierignore +17 -0
- package/dist/templates/react-app/README.en.md +71 -54
- package/dist/templates/react-app/README.md +35 -18
- package/dist/templates/react-app/__tests__/__mocks__/BootstrapTest.ts +14 -0
- package/dist/templates/react-app/__tests__/__mocks__/MockAppConfit.ts +1 -1
- package/dist/templates/react-app/__tests__/__mocks__/MockDialogHandler.ts +2 -2
- package/dist/templates/react-app/__tests__/__mocks__/MockLogger.ts +1 -1
- package/dist/templates/react-app/__tests__/__mocks__/components/TestApp.tsx +45 -0
- package/dist/templates/react-app/__tests__/__mocks__/components/TestBootstrapsProvider.tsx +34 -0
- package/dist/templates/react-app/__tests__/__mocks__/components/TestRouter.tsx +46 -0
- package/dist/templates/react-app/__tests__/__mocks__/components/index.ts +12 -0
- package/dist/templates/react-app/__tests__/__mocks__/createMockGlobals.ts +1 -2
- package/dist/templates/react-app/__tests__/__mocks__/testIOC/TestIOC.ts +51 -0
- package/dist/templates/react-app/__tests__/__mocks__/testIOC/TestIOCRegister.ts +69 -0
- package/dist/templates/react-app/__tests__/setup/index.ts +1 -51
- package/dist/templates/react-app/__tests__/setup/setupGlobal.ts +51 -0
- package/dist/templates/react-app/__tests__/src/App.structure.test.tsx +115 -0
- package/dist/templates/react-app/__tests__/src/base/cases/AppConfig.test.ts +2 -2
- package/dist/templates/react-app/__tests__/src/base/cases/AppError.test.ts +1 -1
- package/dist/templates/react-app/__tests__/src/base/cases/DialogHandler.test.ts +3 -5
- package/dist/templates/react-app/__tests__/src/base/cases/I18nKeyErrorPlugin.test.ts +13 -2
- package/dist/templates/react-app/__tests__/src/base/cases/InversifyContainer.test.ts +1 -1
- package/dist/templates/react-app/__tests__/src/base/cases/PublicAssetsPath.test.ts +1 -1
- package/dist/templates/react-app/__tests__/src/base/cases/RequestLogger.test.ts +5 -5
- package/dist/templates/react-app/__tests__/src/base/cases/RequestStatusCatcher.test.ts +1 -2
- package/dist/templates/react-app/__tests__/src/base/cases/RouterLoader.test.ts +25 -15
- package/dist/templates/react-app/__tests__/src/base/services/I18nService.test.ts +29 -15
- package/dist/templates/react-app/__tests__/src/core/IOC.test.ts +19 -9
- package/dist/templates/react-app/__tests__/src/core/bootstraps/BootstrapClient.test.ts +153 -0
- package/dist/templates/react-app/__tests__/src/core/bootstraps/BootstrapsApp.test.ts +9 -7
- package/dist/templates/react-app/__tests__/src/main.integration.test.tsx +4 -5
- package/dist/templates/react-app/__tests__/src/main.test.tsx +4 -4
- package/dist/templates/react-app/__tests__/src/uikit/components/BaseHeader.test.tsx +68 -59
- package/dist/templates/react-app/config/IOCIdentifier.ts +8 -8
- package/dist/templates/react-app/config/Identifier/{common.error.ts → common/common.error.ts} +5 -5
- package/dist/templates/react-app/config/Identifier/{common.ts → common/common.ts} +9 -9
- package/dist/templates/react-app/config/Identifier/common/index.ts +2 -0
- package/dist/templates/react-app/config/Identifier/index.ts +1 -9
- package/dist/templates/react-app/config/Identifier/pages/index.ts +8 -0
- package/dist/templates/react-app/config/Identifier/{page.about.ts → pages/page.about.ts} +34 -26
- package/dist/templates/react-app/config/Identifier/{page.executor.ts → pages/page.executor.ts} +47 -39
- package/dist/templates/{next-app/config/Identifier → react-app/config/Identifier/pages}/page.home.ts +24 -23
- package/dist/templates/react-app/config/Identifier/pages/page.identifiter.ts +102 -0
- package/dist/templates/react-app/config/Identifier/{page.jsonStorage.ts → pages/page.jsonStorage.ts} +18 -11
- package/dist/templates/react-app/config/Identifier/{page.login.ts → pages/page.login.ts} +37 -27
- package/dist/templates/react-app/config/Identifier/{page.register.ts → pages/page.register.ts} +37 -25
- package/dist/templates/react-app/config/Identifier/{page.request.ts → pages/page.request.ts} +34 -44
- package/dist/templates/react-app/config/app.router.ts +66 -69
- package/dist/templates/react-app/config/i18n/PageI18nInterface.ts +51 -0
- package/dist/templates/react-app/config/i18n/aboutI18n.ts +42 -0
- package/dist/templates/react-app/config/i18n/executorI18n.ts +51 -0
- package/dist/templates/react-app/config/i18n/homeI18n.ts +24 -0
- package/dist/templates/react-app/config/i18n/i18nConfig.ts +30 -0
- package/dist/templates/react-app/config/i18n/identifiter18n.ts +30 -0
- package/dist/templates/react-app/config/i18n/jsonStorage18n.ts +27 -0
- package/dist/templates/react-app/config/i18n/login18n.ts +42 -0
- package/dist/templates/react-app/config/i18n/notFoundI18n.ts +34 -0
- package/dist/templates/react-app/config/i18n/register18n.ts +40 -0
- package/dist/templates/react-app/config/i18n/request18n.ts +41 -0
- package/dist/templates/react-app/config/theme.ts +14 -4
- package/dist/templates/react-app/docs/en/bootstrap.md +1670 -341
- package/dist/templates/react-app/docs/en/development-guide.md +1021 -345
- package/dist/templates/react-app/docs/en/env.md +1132 -278
- package/dist/templates/react-app/docs/en/i18n.md +858 -147
- package/dist/templates/react-app/docs/en/index.md +733 -104
- package/dist/templates/react-app/docs/en/ioc.md +1228 -287
- package/dist/templates/react-app/docs/en/playwright/e2e-tests.md +321 -0
- package/dist/templates/react-app/docs/en/playwright/index.md +19 -0
- package/dist/templates/react-app/docs/en/playwright/installation-summary.md +332 -0
- package/dist/templates/react-app/docs/en/playwright/overview.md +222 -0
- package/dist/templates/react-app/docs/en/playwright/quickstart.md +325 -0
- package/dist/templates/react-app/docs/en/playwright/reorganization-notes.md +340 -0
- package/dist/templates/react-app/docs/en/playwright/setup-complete.md +290 -0
- package/dist/templates/react-app/docs/en/playwright/testing-guide.md +565 -0
- package/dist/templates/react-app/docs/en/store.md +1194 -184
- package/dist/templates/react-app/docs/en/why-no-globals.md +797 -0
- package/dist/templates/react-app/docs/zh/bootstrap.md +1670 -341
- package/dist/templates/react-app/docs/zh/development-guide.md +1021 -345
- package/dist/templates/react-app/docs/zh/env.md +1132 -275
- package/dist/templates/react-app/docs/zh/i18n.md +858 -147
- package/dist/templates/react-app/docs/zh/index.md +717 -104
- package/dist/templates/react-app/docs/zh/ioc.md +1229 -287
- package/dist/templates/react-app/docs/zh/playwright/e2e-tests.md +321 -0
- package/dist/templates/react-app/docs/zh/playwright/index.md +19 -0
- package/dist/templates/react-app/docs/zh/playwright/installation-summary.md +332 -0
- package/dist/templates/react-app/docs/zh/playwright/overview.md +222 -0
- package/dist/templates/react-app/docs/zh/playwright/quickstart.md +325 -0
- package/dist/templates/react-app/docs/zh/playwright/reorganization-notes.md +340 -0
- package/dist/templates/react-app/docs/zh/playwright/setup-complete.md +290 -0
- package/dist/templates/react-app/docs/zh/playwright/testing-guide.md +565 -0
- package/dist/templates/react-app/docs/zh/store.md +1192 -184
- package/dist/templates/react-app/docs/zh/why-no-globals.md +797 -0
- package/dist/templates/react-app/e2e/App.spec.ts +319 -0
- package/dist/templates/react-app/e2e/fixtures/base.fixture.ts +40 -0
- package/dist/templates/react-app/e2e/main.spec.ts +20 -0
- package/dist/templates/react-app/e2e/utils/test-helpers.ts +19 -0
- package/dist/templates/react-app/eslint.config.mjs +247 -0
- package/dist/templates/react-app/makes/eslint-utils.mjs +195 -0
- package/dist/templates/react-app/makes/generateTs2LocalesOptions.ts +26 -0
- package/dist/templates/react-app/package.json +31 -3
- package/dist/templates/react-app/playwright.config.ts +79 -0
- package/dist/templates/react-app/public/locales/en/common.json +190 -179
- package/dist/templates/react-app/public/locales/zh/common.json +190 -179
- package/dist/templates/react-app/src/App.tsx +15 -42
- package/dist/templates/react-app/src/base/apis/AiApi.ts +5 -5
- package/dist/templates/react-app/src/base/apis/feApi/FeApi.ts +1 -1
- package/dist/templates/react-app/src/base/apis/feApi/FeApiAdapter.ts +1 -1
- package/dist/templates/react-app/src/base/apis/feApi/FeApiBootstarp.ts +8 -8
- package/dist/templates/react-app/src/base/apis/feApi/FeApiType.ts +1 -1
- package/dist/templates/react-app/src/base/apis/userApi/UserApi.ts +6 -6
- package/dist/templates/react-app/src/base/apis/userApi/UserApiAdapter.ts +1 -1
- package/dist/templates/react-app/src/base/apis/userApi/UserApiBootstarp.ts +12 -14
- package/dist/templates/react-app/src/base/apis/userApi/UserApiType.ts +1 -1
- package/dist/templates/react-app/src/base/cases/DialogHandler.ts +5 -2
- package/dist/templates/react-app/src/base/cases/I18nKeyErrorPlugin.ts +3 -3
- package/dist/templates/react-app/src/base/cases/InversifyContainer.ts +3 -3
- package/dist/templates/react-app/src/base/cases/RequestLanguages.ts +2 -2
- package/dist/templates/react-app/src/base/cases/RequestLogger.ts +4 -4
- package/dist/templates/react-app/src/base/cases/RequestStatusCatcher.ts +1 -1
- package/dist/templates/react-app/src/base/cases/ResourceState.ts +23 -0
- package/dist/templates/react-app/src/base/cases/RouterLoader.ts +4 -4
- package/dist/templates/react-app/src/base/cases/TranslateI18nInterface.ts +26 -0
- package/dist/templates/react-app/src/base/port/ExecutorPageBridgeInterface.ts +2 -3
- package/dist/templates/react-app/src/base/port/I18nServiceInterface.ts +1 -1
- package/dist/templates/react-app/src/base/port/IOCInterface.ts +36 -0
- package/dist/templates/react-app/src/base/port/JSONStoragePageBridgeInterface.ts +2 -1
- package/dist/templates/react-app/src/base/port/ProcesserExecutorInterface.ts +1 -1
- package/dist/templates/react-app/src/base/port/RequestPageBridgeInterface.ts +2 -2
- package/dist/templates/react-app/src/base/port/RouteServiceInterface.ts +9 -5
- package/dist/templates/react-app/src/base/port/UserServiceInterface.ts +1 -1
- package/dist/templates/react-app/src/base/services/I18nService.ts +29 -29
- package/dist/templates/react-app/src/base/services/IdentifierService.ts +143 -0
- package/dist/templates/react-app/src/base/services/ProcesserExecutor.ts +3 -3
- package/dist/templates/react-app/src/base/services/RouteService.ts +27 -8
- package/dist/templates/react-app/src/base/services/UserService.ts +8 -8
- package/dist/templates/react-app/src/base/types/Page.ts +14 -2
- package/dist/templates/react-app/src/base/types/global.d.ts +1 -1
- package/dist/templates/react-app/src/core/IOC.ts +5 -46
- package/dist/templates/react-app/src/core/bootstraps/{BootstrapApp.ts → BootstrapClient.ts} +44 -17
- package/dist/templates/react-app/src/core/bootstraps/BootstrapsRegistry.ts +14 -7
- package/dist/templates/react-app/src/core/bootstraps/IocIdentifierTest.ts +1 -1
- package/dist/templates/react-app/src/core/bootstraps/PrintBootstrap.ts +1 -1
- package/dist/templates/react-app/src/core/clientIoc/ClientIOC.ts +40 -0
- package/dist/templates/react-app/src/core/{IocRegisterImpl.ts → clientIoc/ClientIOCRegister.ts} +35 -24
- package/dist/templates/react-app/src/core/globals.ts +9 -9
- package/dist/templates/react-app/src/main.tsx +4 -4
- package/dist/templates/react-app/src/pages/404.tsx +6 -3
- package/dist/templates/react-app/src/pages/500.tsx +5 -2
- package/dist/templates/react-app/src/pages/NoRouteFound.tsx +5 -0
- package/dist/templates/react-app/src/pages/auth/Layout.tsx +9 -6
- package/dist/templates/react-app/src/pages/auth/LoginPage.tsx +46 -56
- package/dist/templates/react-app/src/pages/auth/RegisterPage.tsx +46 -58
- package/dist/templates/react-app/src/pages/base/AboutPage.tsx +35 -40
- package/dist/templates/react-app/src/pages/base/ExecutorPage.tsx +51 -51
- package/dist/templates/react-app/src/pages/base/HomePage.tsx +14 -15
- package/dist/templates/react-app/src/pages/base/IdentifierPage.tsx +70 -11
- package/dist/templates/react-app/src/pages/base/JSONStoragePage.tsx +24 -25
- package/dist/templates/react-app/src/pages/base/Layout.tsx +2 -2
- package/dist/templates/react-app/src/pages/base/RedirectPathname.tsx +3 -2
- package/dist/templates/react-app/src/pages/base/RequestPage.tsx +41 -59
- package/dist/templates/react-app/src/styles/css/antd-themes/{_default.css → _common/_default.css} +85 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/{dark.css → _common/dark.css} +99 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/index.css +3 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/{pink.css → _common/pink.css} +86 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/index.css +4 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/_default.css +108 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/dark.css +67 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/index.css +3 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/pink.css +67 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/_default.css +34 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/dark.css +31 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/index.css +3 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/pink.css +36 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/table/_default.css +44 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/table/dark.css +43 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/table/index.css +3 -0
- package/dist/templates/react-app/src/styles/css/antd-themes/table/pink.css +43 -0
- package/dist/templates/react-app/src/styles/css/page.css +4 -3
- package/dist/templates/react-app/src/styles/css/themes/_default.css +1 -0
- package/dist/templates/react-app/src/styles/css/themes/dark.css +1 -0
- package/dist/templates/react-app/src/styles/css/themes/pink.css +1 -0
- package/dist/templates/react-app/src/styles/css/zIndex.css +1 -1
- package/dist/templates/react-app/src/uikit/bridges/ExecutorPageBridge.ts +3 -3
- package/dist/templates/react-app/src/uikit/bridges/JSONStoragePageBridge.ts +2 -2
- package/dist/templates/react-app/src/uikit/bridges/NavigateBridge.ts +1 -1
- package/dist/templates/react-app/src/uikit/bridges/RequestPageBridge.ts +3 -3
- package/dist/templates/react-app/src/uikit/components/AppRouterProvider.tsx +35 -0
- package/dist/templates/react-app/src/uikit/components/BaseHeader.tsx +15 -11
- package/dist/templates/react-app/src/uikit/components/BaseRouteProvider.tsx +14 -11
- package/dist/templates/react-app/src/uikit/components/BaseRouteSeo.tsx +18 -0
- package/dist/templates/react-app/src/uikit/components/BootstrapsProvider.tsx +13 -0
- package/dist/templates/react-app/src/uikit/components/ClientSeo.tsx +62 -0
- package/dist/templates/react-app/src/uikit/components/ComboProvider.tsx +38 -0
- package/dist/templates/react-app/src/uikit/components/LanguageSwitcher.tsx +48 -27
- package/dist/templates/react-app/src/uikit/components/Loading.tsx +4 -2
- package/dist/templates/react-app/src/uikit/components/LocaleLink.tsx +4 -5
- package/dist/templates/react-app/src/uikit/components/LogoutButton.tsx +34 -11
- package/dist/templates/react-app/src/uikit/components/ProcessExecutorProvider.tsx +9 -5
- package/dist/templates/react-app/src/uikit/components/RouterRenderComponent.tsx +6 -3
- package/dist/templates/react-app/src/uikit/components/ThemeSwitcher.tsx +97 -40
- package/dist/templates/react-app/src/uikit/components/UserAuthProvider.tsx +5 -5
- package/dist/templates/react-app/src/uikit/components/With.tsx +17 -0
- package/dist/templates/react-app/src/uikit/contexts/BaseRouteContext.ts +17 -11
- package/dist/templates/react-app/src/uikit/contexts/IOCContext.ts +13 -0
- package/dist/templates/react-app/src/uikit/hooks/useAppTranslation.ts +26 -0
- package/dist/templates/react-app/src/uikit/hooks/useI18nGuard.ts +8 -11
- package/dist/templates/react-app/src/uikit/hooks/useI18nInterface.ts +25 -0
- package/dist/templates/react-app/src/uikit/hooks/useIOC.ts +35 -0
- package/dist/templates/react-app/src/uikit/hooks/useNavigateBridge.ts +3 -3
- package/dist/templates/react-app/src/uikit/hooks/useStrictEffect.ts +0 -1
- package/dist/templates/react-app/tsconfig.e2e.json +21 -0
- package/dist/templates/react-app/tsconfig.json +8 -1
- package/dist/templates/react-app/tsconfig.node.json +1 -1
- package/dist/templates/react-app/tsconfig.test.json +3 -1
- package/dist/templates/react-app/vite.config.ts +50 -34
- package/package.json +2 -1
- package/dist/configs/react-app/eslint.config.js +0 -94
- package/dist/templates/next-app/config/Identifier/common.error.ts +0 -41
- package/dist/templates/next-app/config/Identifier/common.ts +0 -69
- package/dist/templates/next-app/config/Identifier/page.about.ts +0 -181
- package/dist/templates/next-app/config/Identifier/page.admin.ts +0 -48
- package/dist/templates/next-app/config/Identifier/page.executor.ts +0 -272
- package/dist/templates/next-app/config/Identifier/page.identifiter.ts +0 -39
- package/dist/templates/next-app/config/Identifier/page.jsonStorage.ts +0 -72
- package/dist/templates/next-app/config/Identifier/page.request.ts +0 -182
- package/dist/templates/next-app/docs/env.md +0 -94
- package/dist/templates/next-app/src/base/cases/ChatAction.ts +0 -21
- package/dist/templates/next-app/src/base/cases/FocusBarAction.ts +0 -36
- package/dist/templates/next-app/src/base/cases/RequestState.ts +0 -20
- package/dist/templates/next-app/src/base/port/AdminPageInterface.ts +0 -85
- package/dist/templates/next-app/src/base/port/AsyncStateInterface.ts +0 -7
- package/dist/templates/next-app/src/base/services/AdminUserService.ts +0 -45
- package/dist/templates/next-app/src/uikit/components/ChatRoot.tsx +0 -17
- package/dist/templates/next-app/src/uikit/components/chat/ChatActionInterface.ts +0 -30
- package/dist/templates/next-app/src/uikit/components/chat/ChatFocusBar.tsx +0 -65
- package/dist/templates/next-app/src/uikit/components/chat/ChatMessages.tsx +0 -59
- package/dist/templates/next-app/src/uikit/components/chat/ChatWrap.tsx +0 -28
- package/dist/templates/next-app/src/uikit/components/chat/FocusBarActionInterface.ts +0 -19
- package/dist/templates/next-app/src/uikit/hook/useMountedClient.ts +0 -17
- package/dist/templates/next-app/src/uikit/hook/useStore.ts +0 -15
- package/dist/templates/react-app/__tests__/__mocks__/I18nService.ts +0 -13
- package/dist/templates/react-app/__tests__/src/App.test.tsx +0 -139
- package/dist/templates/react-app/config/Identifier/page.identifiter.ts +0 -39
- package/dist/templates/react-app/config/i18n.ts +0 -15
- package/dist/templates/react-app/docs/en/project-structure.md +0 -434
- package/dist/templates/react-app/docs/zh/project-structure.md +0 -434
- package/dist/templates/react-app/src/base/cases/RequestState.ts +0 -20
- package/dist/templates/react-app/src/base/port/AsyncStateInterface.ts +0 -7
- package/dist/templates/react-app/src/uikit/hooks/useDocumentTitle.ts +0 -15
- package/dist/templates/react-app/src/uikit/hooks/useStore.ts +0 -15
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
# Next.js 环境变量配置指南
|
|
2
|
+
|
|
3
|
+
## 什么是环境变量?
|
|
4
|
+
|
|
5
|
+
环境变量是一种在不同环境(开发、测试、生产)中配置应用行为的方式。在 Next.js 中,环境变量的使用有其特殊性,尤其是在客户端和服务端的处理上。
|
|
6
|
+
|
|
7
|
+
**关键特点**:
|
|
8
|
+
|
|
9
|
+
- 客户端环境变量必须以 `NEXT_PUBLIC_` 开头
|
|
10
|
+
- 服务端环境变量可以直接使用,无需特殊前缀
|
|
11
|
+
- 支持多环境配置和本地覆盖
|
|
12
|
+
|
|
13
|
+
## 环境变量加载优先级
|
|
14
|
+
|
|
15
|
+
Next.js 按照以下优先级加载环境变量:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
.env.local → .env.development/.env.production → .env
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 文件结构
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
项目根目录/
|
|
25
|
+
├── .env # 默认环境变量
|
|
26
|
+
├── .env.local # 本地环境变量(git ignored)
|
|
27
|
+
├── .env.development # 开发环境变量
|
|
28
|
+
├── .env.production # 生产环境变量
|
|
29
|
+
├── .env.test # 测试环境变量
|
|
30
|
+
└── src/
|
|
31
|
+
└── base/
|
|
32
|
+
└── cases/
|
|
33
|
+
└── AppConfig.ts # 应用配置类
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 环境变量文件
|
|
37
|
+
|
|
38
|
+
### 1. 环境变量文件示例
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# .env (默认配置)
|
|
42
|
+
# 客户端可访问的环境变量(以 NEXT_PUBLIC_ 开头)
|
|
43
|
+
NEXT_PUBLIC_APP_NAME=MyApp
|
|
44
|
+
NEXT_PUBLIC_API_BASE_URL=http://api.example.com
|
|
45
|
+
NEXT_PUBLIC_ENABLE_ANALYTICS=false
|
|
46
|
+
|
|
47
|
+
# 仅服务端可访问的环境变量
|
|
48
|
+
DATABASE_URL=postgres://user:pass@localhost:5432/db
|
|
49
|
+
JWT_SECRET=your-secret-key
|
|
50
|
+
API_TOKEN=server-side-token
|
|
51
|
+
|
|
52
|
+
# .env.development (开发环境)
|
|
53
|
+
NEXT_PUBLIC_API_BASE_URL=http://localhost:3000/api
|
|
54
|
+
NEXT_PUBLIC_DEBUG=true
|
|
55
|
+
DATABASE_URL=postgres://dev:pass@localhost:5432/dev_db
|
|
56
|
+
|
|
57
|
+
# .env.production (生产环境)
|
|
58
|
+
NEXT_PUBLIC_API_BASE_URL=https://api.production.com
|
|
59
|
+
NEXT_PUBLIC_DEBUG=false
|
|
60
|
+
DATABASE_URL=postgres://prod:pass@production:5432/prod_db
|
|
61
|
+
|
|
62
|
+
# .env.local (本地覆盖,不提交到 git)
|
|
63
|
+
NEXT_PUBLIC_FEATURE_FLAG=true
|
|
64
|
+
DATABASE_URL=postgres://local:pass@localhost:5432/local_db
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. 环境变量使用规范
|
|
68
|
+
|
|
69
|
+
#### 客户端环境变量
|
|
70
|
+
|
|
71
|
+
- 必须以 `NEXT_PUBLIC_` 开头
|
|
72
|
+
- 会被打包到客户端代码中
|
|
73
|
+
- 适用于公开的配置信息
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# ✅ 正确的客户端环境变量
|
|
77
|
+
NEXT_PUBLIC_API_URL=https://api.example.com
|
|
78
|
+
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=UA-XXXXX
|
|
79
|
+
NEXT_PUBLIC_FEATURE_FLAGS={"newUI":true}
|
|
80
|
+
|
|
81
|
+
# ❌ 错误的客户端环境变量(没有 NEXT_PUBLIC_ 前缀)
|
|
82
|
+
API_URL=https://api.example.com # 客户端无法访问
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### 服务端环境变量
|
|
86
|
+
|
|
87
|
+
- 无需特殊前缀
|
|
88
|
+
- 只在服务端可用
|
|
89
|
+
- 适用于敏感信息
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
# ✅ 正确的服务端环境变量
|
|
93
|
+
DATABASE_URL=postgres://user:pass@localhost:5432/db
|
|
94
|
+
JWT_SECRET=your-secret-key
|
|
95
|
+
API_TOKEN=your-api-token
|
|
96
|
+
|
|
97
|
+
# ❌ 不应该以 NEXT_PUBLIC_ 开头的服务端敏感信息
|
|
98
|
+
NEXT_PUBLIC_DATABASE_URL=postgres://user:pass@localhost:5432/db # 安全风险!
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 项目中的实现
|
|
102
|
+
|
|
103
|
+
### 1. 应用配置类
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// src/base/cases/AppConfig.ts
|
|
107
|
+
export class AppConfig implements EnvConfigInterface {
|
|
108
|
+
/**
|
|
109
|
+
* 应用名称
|
|
110
|
+
* @description 从 NEXT_PUBLIC_APP_NAME 环境变量获取
|
|
111
|
+
*/
|
|
112
|
+
readonly appName = process.env.NEXT_PUBLIC_APP_NAME || '';
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* API 基础 URL
|
|
116
|
+
* @description 从 NEXT_PUBLIC_API_BASE_URL 环境变量获取
|
|
117
|
+
*/
|
|
118
|
+
readonly apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || '';
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 是否为开发环境
|
|
122
|
+
*/
|
|
123
|
+
readonly isDevelopment = process.env.NODE_ENV === 'development';
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 是否为生产环境
|
|
127
|
+
*/
|
|
128
|
+
readonly isProduction = process.env.NODE_ENV === 'production';
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 数据库连接 URL(仅服务端可用)
|
|
132
|
+
*/
|
|
133
|
+
readonly databaseUrl = process.env.DATABASE_URL;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* JWT 密钥(仅服务端可用)
|
|
137
|
+
*/
|
|
138
|
+
readonly jwtSecret = process.env.JWT_SECRET;
|
|
139
|
+
|
|
140
|
+
// ... 更多配置项
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 2. 在客户端使用配置
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// 在组件中使用
|
|
148
|
+
function Analytics() {
|
|
149
|
+
// ✅ 正确:通过 IOC 获取配置对象
|
|
150
|
+
const appConfig = IOC(IOCIdentifier.AppConfig);
|
|
151
|
+
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
if (appConfig.analyticsId) {
|
|
154
|
+
// 初始化 analytics
|
|
155
|
+
}
|
|
156
|
+
}, [appConfig.analyticsId]);
|
|
157
|
+
|
|
158
|
+
return appConfig.debug ? <DebugInfo /> : null;
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### 3. 在服务端使用配置
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
// app/api/auth/[...nextauth]/route.ts
|
|
166
|
+
import { NextResponse } from 'next/server';
|
|
167
|
+
|
|
168
|
+
export async function GET() {
|
|
169
|
+
// ✅ 正确:通过 IOC 获取配置对象
|
|
170
|
+
const appConfig = IOC(IOCIdentifier.AppConfig);
|
|
171
|
+
|
|
172
|
+
if (!appConfig.databaseUrl || !appConfig.jwtSecret) {
|
|
173
|
+
return NextResponse.json(
|
|
174
|
+
{ error: 'Server configuration error' },
|
|
175
|
+
{ status: 500 }
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 使用配置...
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### 4. 在服务中使用配置
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
// 定义服务类
|
|
187
|
+
@injectable()
|
|
188
|
+
export class AdminService {
|
|
189
|
+
constructor(
|
|
190
|
+
@inject(IOCIdentifier.AppConfig)
|
|
191
|
+
private appConfig: AppConfig
|
|
192
|
+
) {}
|
|
193
|
+
|
|
194
|
+
async fetchAdminData() {
|
|
195
|
+
const response = await fetch('https://api.example.com/admin', {
|
|
196
|
+
headers: {
|
|
197
|
+
Authorization: `Bearer ${this.appConfig.apiToken}`
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
return response.json();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## 最佳实践
|
|
206
|
+
|
|
207
|
+
### 1. 环境变量命名规范
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
# ✅ 好的命名
|
|
211
|
+
NEXT_PUBLIC_APP_NAME=MyApp
|
|
212
|
+
NEXT_PUBLIC_API_URL=https://api.example.com
|
|
213
|
+
NEXT_PUBLIC_FEATURE_FLAGS={"darkMode":true}
|
|
214
|
+
|
|
215
|
+
# ❌ 不好的命名
|
|
216
|
+
next_public_app_name=MyApp # 应该使用大写
|
|
217
|
+
NEXT_PUBLIC_SECRET_KEY=xxx # 敏感信息不应该用 NEXT_PUBLIC_
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### 2. 配置类实现
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
// 定义配置接口
|
|
224
|
+
interface EnvConfigInterface {
|
|
225
|
+
readonly env: string;
|
|
226
|
+
readonly appName: string;
|
|
227
|
+
readonly appVersion: string;
|
|
228
|
+
// ... 其他配置项
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 实现配置类
|
|
232
|
+
@injectable()
|
|
233
|
+
export class AppConfig implements EnvConfigInterface {
|
|
234
|
+
/**
|
|
235
|
+
* 当前环境模式
|
|
236
|
+
* @description 基于当前使用的 .env 文件自动设置
|
|
237
|
+
*/
|
|
238
|
+
readonly env: string = process.env.APP_ENV!;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 应用名称
|
|
242
|
+
*/
|
|
243
|
+
readonly appName: string = name;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 应用版本
|
|
247
|
+
*/
|
|
248
|
+
readonly appVersion: string = version;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 用户令牌存储键
|
|
252
|
+
*/
|
|
253
|
+
readonly userTokenKey: string = '_user_token';
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 数据库连接 URL(仅服务端)
|
|
257
|
+
*/
|
|
258
|
+
readonly supabaseUrl: string = process.env.SUPABASE_URL!;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 数据库匿名密钥(仅服务端)
|
|
262
|
+
*/
|
|
263
|
+
readonly supabaseAnonKey: string = process.env.SUPABASE_ANON_KEY!;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* JWT 密钥(仅服务端)
|
|
267
|
+
*/
|
|
268
|
+
readonly jwtSecret: string = process.env.JWT_SECRET!;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* JWT 过期时间
|
|
272
|
+
* @example '30 days'
|
|
273
|
+
* @example '1 year'
|
|
274
|
+
*/
|
|
275
|
+
readonly jwtExpiresIn: StringValue = '30 days';
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* OpenAI API 配置(仅服务端)
|
|
279
|
+
*/
|
|
280
|
+
readonly openaiBaseUrl: string = process.env.CEREBRAS_BASE_URL!;
|
|
281
|
+
readonly openaiApiKey: string = process.env.CEREBRAS_API_KEY!;
|
|
282
|
+
}
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### 3. 配置验证
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
// 定义配置验证器
|
|
289
|
+
@injectable()
|
|
290
|
+
export class ConfigValidator {
|
|
291
|
+
constructor(
|
|
292
|
+
@inject(IOCIdentifier.AppConfig)
|
|
293
|
+
private appConfig: AppConfig,
|
|
294
|
+
@inject(IOCIdentifier.Logger)
|
|
295
|
+
private logger: LoggerInterface
|
|
296
|
+
) {}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* 验证所有必需的配置项
|
|
300
|
+
* @throws {Error} 当必需的配置项缺失时抛出错误
|
|
301
|
+
*/
|
|
302
|
+
validateRequiredConfig(): void {
|
|
303
|
+
// 验证基础配置
|
|
304
|
+
this.validateBasicConfig();
|
|
305
|
+
|
|
306
|
+
// 根据运行环境验证不同的配置
|
|
307
|
+
if (typeof window === 'undefined') {
|
|
308
|
+
this.validateServerConfig();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 验证基础配置项
|
|
314
|
+
*/
|
|
315
|
+
private validateBasicConfig(): void {
|
|
316
|
+
const requiredConfigs: Array<keyof AppConfig> = [
|
|
317
|
+
'env',
|
|
318
|
+
'appName',
|
|
319
|
+
'appVersion',
|
|
320
|
+
'userTokenKey'
|
|
321
|
+
];
|
|
322
|
+
|
|
323
|
+
for (const key of requiredConfigs) {
|
|
324
|
+
if (!this.appConfig[key]) {
|
|
325
|
+
throw new Error(`Missing required config: ${key}`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 验证服务端配置项
|
|
332
|
+
*/
|
|
333
|
+
private validateServerConfig(): void {
|
|
334
|
+
const requiredServerConfigs: Array<keyof AppConfig> = [
|
|
335
|
+
'supabaseUrl',
|
|
336
|
+
'supabaseAnonKey',
|
|
337
|
+
'jwtSecret',
|
|
338
|
+
'openaiBaseUrl',
|
|
339
|
+
'openaiApiKey'
|
|
340
|
+
];
|
|
341
|
+
|
|
342
|
+
for (const key of requiredServerConfigs) {
|
|
343
|
+
if (!this.appConfig[key]) {
|
|
344
|
+
throw new Error(`Missing required server config: ${key}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* 验证配置值的格式
|
|
351
|
+
*/
|
|
352
|
+
validateConfigFormat(): void {
|
|
353
|
+
// 验证 URL 格式
|
|
354
|
+
if (!this.isValidUrl(this.appConfig.supabaseUrl)) {
|
|
355
|
+
throw new Error('Invalid supabaseUrl format');
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// 验证 JWT 过期时间格式
|
|
359
|
+
if (!this.isValidDuration(this.appConfig.jwtExpiresIn)) {
|
|
360
|
+
throw new Error('Invalid jwtExpiresIn format');
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
this.logger.info('All config formats validated successfully');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private isValidUrl(url: string): boolean {
|
|
367
|
+
try {
|
|
368
|
+
new URL(url);
|
|
369
|
+
return true;
|
|
370
|
+
} catch {
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
private isValidDuration(duration: string): boolean {
|
|
376
|
+
// 实现持续时间格式验证逻辑
|
|
377
|
+
return /^\d+\s+(days?|weeks?|months?|years?)$/.test(duration);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### 4. 敏感信息处理
|
|
383
|
+
|
|
384
|
+
```bash
|
|
385
|
+
# .env.local (不提交到 git)
|
|
386
|
+
DATABASE_URL=postgres://user:pass@localhost:5432/db
|
|
387
|
+
JWT_SECRET=your-secret-key
|
|
388
|
+
API_TOKEN=your-api-token
|
|
389
|
+
|
|
390
|
+
# .env.template (提交到 git,作为模板)
|
|
391
|
+
DATABASE_URL=postgres://user:password@localhost:5432/dbname
|
|
392
|
+
JWT_SECRET=your-jwt-secret-here
|
|
393
|
+
API_TOKEN=your-api-token-here
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## 调试和故障排除
|
|
397
|
+
|
|
398
|
+
### 1. 配置调试工具
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
@injectable()
|
|
402
|
+
export class ConfigDebugger {
|
|
403
|
+
constructor(
|
|
404
|
+
@inject(IOCIdentifier.AppConfig)
|
|
405
|
+
private appConfig: AppConfig,
|
|
406
|
+
@inject(IOCIdentifier.Logger)
|
|
407
|
+
private logger: LoggerInterface
|
|
408
|
+
) {}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* 打印配置信息
|
|
412
|
+
*/
|
|
413
|
+
logConfig(): void {
|
|
414
|
+
this.logger.group('Configuration Debug Info');
|
|
415
|
+
|
|
416
|
+
// 基础配置
|
|
417
|
+
this.logger.info('Basic Config:', {
|
|
418
|
+
env: this.appConfig.env,
|
|
419
|
+
appName: this.appConfig.appName,
|
|
420
|
+
appVersion: this.appConfig.appVersion
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// 如果在服务端,打印服务端配置
|
|
424
|
+
if (typeof window === 'undefined') {
|
|
425
|
+
this.logger.info('Server Config:', {
|
|
426
|
+
supabaseUrl: this.maskSensitiveInfo(this.appConfig.supabaseUrl),
|
|
427
|
+
jwtExpiresIn: this.appConfig.jwtExpiresIn
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
this.logger.groupEnd();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* 验证配置健康状态
|
|
436
|
+
*/
|
|
437
|
+
async checkConfigHealth(): Promise<void> {
|
|
438
|
+
try {
|
|
439
|
+
// 验证数据库连接
|
|
440
|
+
if (typeof window === 'undefined') {
|
|
441
|
+
await this.checkDatabaseConnection();
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// 验证其他配置项
|
|
445
|
+
this.validateConfigValues();
|
|
446
|
+
|
|
447
|
+
this.logger.info('Configuration health check passed');
|
|
448
|
+
} catch (error) {
|
|
449
|
+
this.logger.error('Configuration health check failed:', error);
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private async checkDatabaseConnection(): Promise<void> {
|
|
455
|
+
// 实现数据库连接检查逻辑
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
private validateConfigValues(): void {
|
|
459
|
+
// 实现配置值验证逻辑
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private maskSensitiveInfo(value: string): string {
|
|
463
|
+
return value.replace(/^(https?:\/\/[^:]+:)([^@]+)(@.*)$/, '$1****$3');
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
### 2. 常见问题处理
|
|
469
|
+
|
|
470
|
+
**问题 1:配置未正确注入**
|
|
471
|
+
|
|
472
|
+
```typescript
|
|
473
|
+
// ❌ 错误:直接使用环境变量
|
|
474
|
+
class UserService {
|
|
475
|
+
private apiUrl = process.env.NEXT_PUBLIC_API_URL;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// ✅ 正确:通过配置类获取
|
|
479
|
+
@injectable()
|
|
480
|
+
class UserService {
|
|
481
|
+
constructor(
|
|
482
|
+
@inject(IOCIdentifier.AppConfig)
|
|
483
|
+
private appConfig: AppConfig
|
|
484
|
+
) {}
|
|
485
|
+
}
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
**问题 2:配置验证失败**
|
|
489
|
+
|
|
490
|
+
```typescript
|
|
491
|
+
// ❌ 错误:没有配置验证
|
|
492
|
+
@injectable()
|
|
493
|
+
class ApiService {
|
|
494
|
+
constructor(
|
|
495
|
+
@inject(IOCIdentifier.AppConfig)
|
|
496
|
+
private appConfig: AppConfig
|
|
497
|
+
) {}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// ✅ 正确:包含配置验证
|
|
501
|
+
@injectable()
|
|
502
|
+
class ApiService {
|
|
503
|
+
constructor(
|
|
504
|
+
@inject(IOCIdentifier.AppConfig)
|
|
505
|
+
private appConfig: AppConfig,
|
|
506
|
+
@inject(IOCIdentifier.ConfigValidator)
|
|
507
|
+
private configValidator: ConfigValidator
|
|
508
|
+
) {
|
|
509
|
+
this.configValidator.validateRequiredConfig();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
**问题 3:配置类型处理**
|
|
515
|
+
|
|
516
|
+
```typescript
|
|
517
|
+
// ❌ 错误:手动类型转换
|
|
518
|
+
class FeatureService {
|
|
519
|
+
private isDebug = process.env.NEXT_PUBLIC_DEBUG === 'true';
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// ✅ 正确:在配置类中处理类型转换
|
|
523
|
+
@injectable()
|
|
524
|
+
export class AppConfig implements EnvConfigInterface {
|
|
525
|
+
readonly debug: boolean = this.parseBoolean(process.env.NEXT_PUBLIC_DEBUG);
|
|
526
|
+
|
|
527
|
+
private parseBoolean(value: string | undefined): boolean {
|
|
528
|
+
return value?.toLowerCase() === 'true';
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
## 总结
|
|
534
|
+
|
|
535
|
+
面向对象的配置管理系统提供了:
|
|
536
|
+
|
|
537
|
+
1. **配置封装**:
|
|
538
|
+
- 通过 `AppConfig` 类统一管理所有配置
|
|
539
|
+
- 实现 `EnvConfigInterface` 接口确保配置完整性
|
|
540
|
+
- 使用依赖注入实现配置的解耦
|
|
541
|
+
|
|
542
|
+
2. **类型安全**:
|
|
543
|
+
- 配置类中处理类型转换
|
|
544
|
+
- TypeScript 接口定义确保类型正确
|
|
545
|
+
- 编译时类型检查
|
|
546
|
+
|
|
547
|
+
3. **配置验证**:
|
|
548
|
+
- 专门的 `ConfigValidator` 类处理配置验证
|
|
549
|
+
- 运行时配置完整性检查
|
|
550
|
+
- 配置格式验证
|
|
551
|
+
|
|
552
|
+
4. **最佳实践**:
|
|
553
|
+
- 依赖注入管理配置依赖
|
|
554
|
+
- 配置验证和调试工具
|
|
555
|
+
- 敏感信息保护
|
|
556
|
+
- 类型安全处理
|
|
557
|
+
|
|
558
|
+
通过面向对象的方式管理配置,我们可以:
|
|
559
|
+
|
|
560
|
+
- 提高代码的可维护性和可测试性
|
|
561
|
+
- 确保配置的类型安全和完整性
|
|
562
|
+
- 方便地进行配置验证和调试
|
|
563
|
+
- 更好地管理配置依赖关系
|