From f259792aae47b0016d62031e97ed7e80df3ece0c Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Wed, 22 May 2024 23:42:26 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20refactor=20to?= =?UTF-8?q?=20serverDB=20ENV=20(#2612)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/__tests__/client.test.ts | 6 +++--- src/config/db.ts | 16 ++++++++++++++++ src/config/{client.ts => debug.ts} | 20 +++++--------------- src/const/version.ts | 4 ++-- src/layout/GlobalProvider/index.tsx | 4 ++-- src/locales/create.ts | 4 ++-- 6 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 src/config/db.ts rename src/config/{client.ts => debug.ts} (66%) diff --git a/src/config/__tests__/client.test.ts b/src/config/__tests__/client.test.ts index 036a3ee16e955..7e43c6ea975e7 100644 --- a/src/config/__tests__/client.test.ts +++ b/src/config/__tests__/client.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from 'vitest'; -import { getClientConfig } from '../client'; +import { getDebugConfig } from '../debug'; // 测试前重置 process.env vi.stubGlobal('process', { @@ -17,7 +17,7 @@ describe('getClientConfig', () => { process.env.NEXT_PUBLIC_I18N_DEBUG_BROWSER = '1'; process.env.NEXT_PUBLIC_I18N_DEBUG_SERVER = '1'; - const config = getClientConfig(); + const config = getDebugConfig(); expect(config.I18N_DEBUG).toBe(true); expect(config.I18N_DEBUG_BROWSER).toBe(true); expect(config.I18N_DEBUG_SERVER).toBe(true); @@ -31,7 +31,7 @@ describe('getClientConfig', () => { process.env.NEXT_PUBLIC_I18N_DEBUG_BROWSER = '0'; process.env.NEXT_PUBLIC_I18N_DEBUG_SERVER = '0'; - const config = getClientConfig(); + const config = getDebugConfig(); expect(config.I18N_DEBUG).toBe(false); expect(config.I18N_DEBUG_BROWSER).toBe(false); diff --git a/src/config/db.ts b/src/config/db.ts new file mode 100644 index 0000000000000..525576b946ccc --- /dev/null +++ b/src/config/db.ts @@ -0,0 +1,16 @@ +/* eslint-disable sort-keys-fix/sort-keys-fix , typescript-sort-keys/interface */ +import { createEnv } from '@t3-oss/env-nextjs'; +import { z } from 'zod'; + +export const getServerDBConfig = () => { + return createEnv({ + client: { + NEXT_PUBLIC_ENABLED_SERVER_SERVICE: z.boolean(), + }, + runtimeEnv: { + NEXT_PUBLIC_ENABLED_SERVER_SERVICE: process.env.NEXT_PUBLIC_SERVICE_MODE === 'server', + }, + }); +}; + +export const serverDBEnv = getServerDBConfig(); diff --git a/src/config/client.ts b/src/config/debug.ts similarity index 66% rename from src/config/client.ts rename to src/config/debug.ts index f63d4f5ff0327..588438b8130bf 100644 --- a/src/config/client.ts +++ b/src/config/debug.ts @@ -1,32 +1,22 @@ -/** - * the client config is only used in Vercel deployment - */ - -/* eslint-disable sort-keys-fix/sort-keys-fix , typescript-sort-keys/interface */ - declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace NodeJS { interface ProcessEnv { + NEXT_PUBLIC_DEVELOPER_DEBUG: string; NEXT_PUBLIC_I18N_DEBUG: string; NEXT_PUBLIC_I18N_DEBUG_BROWSER: string; - NEXT_PUBLIC_I18N_DEBUG_SERVER: string; - - NEXT_PUBLIC_DEVELOPER_DEBUG: string; - NEXT_PUBLIC_SERVICE_MODE?: 'server' | 'browser'; + NEXT_PUBLIC_I18N_DEBUG_SERVER: string; } } } -export const getClientConfig = () => ({ - ENABLED_SERVER_SERVICE: process.env.NEXT_PUBLIC_SERVICE_MODE === 'server', +export const getDebugConfig = () => ({ + // developer debug mode + DEBUG_MODE: process.env.NEXT_PUBLIC_DEVELOPER_DEBUG === '1', // i18n debug mode I18N_DEBUG: process.env.NEXT_PUBLIC_I18N_DEBUG === '1', I18N_DEBUG_BROWSER: process.env.NEXT_PUBLIC_I18N_DEBUG_BROWSER === '1', I18N_DEBUG_SERVER: process.env.NEXT_PUBLIC_I18N_DEBUG_SERVER === '1', - - // developer debug mode - DEBUG_MODE: process.env.NEXT_PUBLIC_DEVELOPER_DEBUG === '1', }); diff --git a/src/const/version.ts b/src/const/version.ts index 4a691189fbed6..c3ab173d7e0f6 100644 --- a/src/const/version.ts +++ b/src/const/version.ts @@ -1,6 +1,6 @@ import pkg from '@/../package.json'; -import { getClientConfig } from '@/config/client'; +import { getServerDBConfig } from '@/config/db'; export const CURRENT_VERSION = pkg.version; -export const isServerMode = getClientConfig().ENABLED_SERVER_SERVICE; +export const isServerMode = getServerDBConfig().NEXT_PUBLIC_ENABLED_SERVER_SERVICE; diff --git a/src/layout/GlobalProvider/index.tsx b/src/layout/GlobalProvider/index.tsx index 594f2c2297222..532d419537da4 100644 --- a/src/layout/GlobalProvider/index.tsx +++ b/src/layout/GlobalProvider/index.tsx @@ -2,7 +2,7 @@ import dynamic from 'next/dynamic'; import { cookies } from 'next/headers'; import { FC, ReactNode } from 'react'; -import { getClientConfig } from '@/config/client'; +import { getDebugConfig } from '@/config/debug'; import { getServerFeatureFlagsValue } from '@/config/featureFlags'; import { LOBE_LOCALE_COOKIE } from '@/const/locale'; import { @@ -26,7 +26,7 @@ let DebugUI: FC = () => null; // refs: https://webpack.js.org/plugins/internal-plugins/#constplugin if (process.env.NODE_ENV === 'development') { // eslint-disable-next-line unicorn/no-lonely-if - if (getClientConfig().DEBUG_MODE) { + if (getDebugConfig().DEBUG_MODE) { DebugUI = dynamic(() => import('@/features/DebugUI'), { ssr: false }) as FC; } } diff --git a/src/locales/create.ts b/src/locales/create.ts index 10ef1c92dd485..e7779ff746b73 100644 --- a/src/locales/create.ts +++ b/src/locales/create.ts @@ -4,13 +4,13 @@ import resourcesToBackend from 'i18next-resources-to-backend'; import { initReactI18next } from 'react-i18next'; import { isRtlLang } from 'rtl-detect'; -import { getClientConfig } from '@/config/client'; +import { getDebugConfig } from '@/config/debug'; import { DEFAULT_LANG, LOBE_LOCALE_COOKIE } from '@/const/locale'; import { COOKIE_CACHE_DAYS } from '@/const/settings'; import { normalizeLocale } from '@/locales/resources'; import { isDev, isOnServerSide } from '@/utils/env'; -const { I18N_DEBUG, I18N_DEBUG_BROWSER, I18N_DEBUG_SERVER } = getClientConfig(); +const { I18N_DEBUG, I18N_DEBUG_BROWSER, I18N_DEBUG_SERVER } = getDebugConfig(); const debugMode = I18N_DEBUG ?? isOnServerSide ? I18N_DEBUG_SERVER : I18N_DEBUG_BROWSER; export const createI18nNext = (lang?: string) => {