Skip to content

Commit

Permalink
♻️ refactor: refactor to serverDB ENV (lobehub#2612)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored May 22, 2024
1 parent b735afa commit fa1409e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/config/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest';

import { getClientConfig } from '../client';
import { getDebugConfig } from '../debug';

// 测试前重置 process.env
vi.stubGlobal('process', {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/config/db.ts
Original file line number Diff line number Diff line change
@@ -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();
20 changes: 5 additions & 15 deletions src/config/client.ts → src/config/debug.ts
Original file line number Diff line number Diff line change
@@ -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',
});
4 changes: 2 additions & 2 deletions src/const/version.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/layout/GlobalProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/locales/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit fa1409e

Please sign in to comment.