refactor(env): centralize app environment detection

This commit is contained in:
2026-06-17 15:31:15 +08:00
parent 977c0905bd
commit a6ee0d5e24
4 changed files with 40 additions and 20 deletions
+3 -4
View File
@@ -4,8 +4,9 @@
* 通过环境变量配置后端服务地址与超时时间。
* 原始 Dart: lib/core/net/service_manager.dart
*/
import { AppEnvUtil, type AppEnv } from "@/utils/app-env";
export type AppEnv = "development" | "test" | "production";
export type { AppEnv };
export interface ApiConfig {
/** 后端 API 基础 URL(含协议) */
@@ -33,9 +34,7 @@ export class TimeoutConstants {
* 解析当前环境
*/
export function getAppEnv(): AppEnv {
const env = process.env.NEXT_PUBLIC_APP_ENV;
if (env === "production" || env === "test") return env;
return "development";
return AppEnvUtil.current;
}
/**
+3 -3
View File
@@ -6,7 +6,7 @@ import {
type GuestChatQuotaInput,
type GuestChatQuotaData,
} from "@/data/schemas/chat/guest_chat_quota";
import { isProduction } from "@/utils/app-env";
import { AppEnvUtil } from "@/utils/app-env";
export class GuestChatQuota {
// 静态常量
@@ -45,7 +45,7 @@ export class GuestChatQuota {
* 获取当前环境的每日最大配额
*/
static get threshold(): number {
return isProduction()
return AppEnvUtil.isProduction()
? GuestChatQuota.maxQuotaPerDay
: GuestChatQuota.maxQuotaPerDayTest;
}
@@ -54,7 +54,7 @@ export class GuestChatQuota {
* 获取当前环境的总配额默认值
*/
static get totalQuotaDefault(): number {
return isProduction()
return AppEnvUtil.isProduction()
? GuestChatQuota.defaultTotalQuota
: GuestChatQuota.defaultTotalQuotaTest;
}
+28 -7
View File
@@ -3,13 +3,34 @@
*
* 原始 Dart: lib/utils/app_env_util.dart
*
* 替代 `AppEnvUtil.isDevelopment`:基于 Next.js 注入的 `process.env.NODE_ENV`
* 基于 `.env*` 中的 `NEXT_PUBLIC_APP_ENV` 判断当前业务环境
*
* 注意:该变量带 `NEXT_PUBLIC_` 前缀,可同时用于服务端和客户端代码。
*/
export const isDevelopment = (): boolean =>
process.env.NODE_ENV !== "production";
export type AppEnv = "development" | "test" | "production";
export const isProduction = (): boolean =>
process.env.NODE_ENV === "production";
export class AppEnvUtil {
private constructor() {}
export const isTest = (): boolean =>
process.env.NODE_ENV === "test";
static get current(): AppEnv {
const env = process.env.NEXT_PUBLIC_APP_ENV;
if (env === "production" || env === "test") return env;
return "development";
}
static isDevelopment(): boolean {
return AppEnvUtil.current === "development";
}
static isProduction(): boolean {
return AppEnvUtil.current === "production";
}
static isTest(): boolean {
return AppEnvUtil.current === "test";
}
static canOutputLogs(): boolean {
return !AppEnvUtil.isProduction();
}
}
+6 -6
View File
@@ -1,22 +1,22 @@
import pino, { type Logger as PinoLogger, type LoggerOptions } from "pino";
import { isProduction, isTest } from "./app-env";
import { AppEnvUtil } from "./app-env";
/**
* 全局 pino root logger(单例,进程级复用)。
*
* 配置驱动(基于 `app-env.ts` 的 `isProduction()` / `isTest()`):
* - production: info 级别 + JSON(机器可解析,接入日志平台
* 配置驱动(基于 `app-env.ts` 的 `AppEnvUtil`):
* - production: silent(不输出日志
* - test: warn 级别(减少 vitest 噪音)
* - development: debug 级别 + ISO 时间戳(人眼友好)
*/
const rootLogger: PinoLogger = pino(createLoggerOptions());
function createLoggerOptions(): LoggerOptions {
if (isProduction()) {
return { level: "info" };
if (!AppEnvUtil.canOutputLogs()) {
return { level: "silent" };
}
if (isTest()) {
if (AppEnvUtil.isTest()) {
return { level: "warn" };
}
// development