Files
verify/frontend/src/utils/env.ts
T
admin fbc2e5ac91 fix: 移除所有硬编码的localhost:8080,使用相对路径
- 创建utils/config.ts集中管理API_BASE和文件URL
- 删除.env文件(VITE_SERVER_API_URL=http://localhost:8080)
- env.ts默认API前缀改为/api/v1匹配后端路由
- 替换16处硬编码的localhost:8080引用
2026-05-03 19:48:07 +08:00

40 lines
1.1 KiB
TypeScript

import { h } from 'vue'
import { toast } from 'vue-sonner'
import { z } from 'zod'
/**
* Load .env file and validate it against the schema
* Has this file, it will be loaded automatically by vite and we will be have environment variables available type
* If EnvSchema Object has Key but not in .env file, it will be have a error in page
*/
const EnvSchema = z.object({
VITE_SERVER_API_URL: z.string().default(''),
VITE_SERVER_API_PREFIX: z.string().default('/api/v1'),
VITE_SERVER_API_TIMEOUT: z.coerce.number().default(5000),
})
export type env = z.infer<typeof EnvSchema>
// eslint-disable-next-line ts/no-redeclare
const { data: env, error } = EnvSchema.safeParse(import.meta.env)
if (error) {
console.error('❌ Invalid env')
const flattenError = z.flattenError(error)
console.error(flattenError)
setTimeout(() => {
toast.error(`Env error: you should check your .env file`, {
description: h(
'pre',
{ class: 'mt-2 rounded-md bg-slate-950 p-4 text-wrap' },
h('code', { class: 'text-white' }, JSON.stringify(flattenError, null, 2)),
),
duration: 10000,
})
}, 1000)
}
export default env!