dvc890 commited on
Commit
e0472ca
·
verified ·
1 Parent(s): 3fa2120

Update services/api.ts

Browse files
Files changed (1) hide show
  1. services/api.ts +16 -8
services/api.ts CHANGED
@@ -1,21 +1,30 @@
 
1
  import { Student, Course, Score, User, UserRole, UserStatus } from '../types';
2
 
3
  // ==========================================
4
  // 智能环境配置 (Smart Environment Config)
5
  // ==========================================
6
 
7
- // Safely determine if we are in production.
8
- // We check if import.meta.env exists first to avoid "Cannot read properties of undefined" error.
9
  const getBaseUrl = () => {
10
- // Safe check for Vite environment variable
11
- const isViteProd = import.meta.env && import.meta.env.PROD;
12
 
13
- if (isViteProd) {
 
 
 
 
 
 
 
 
 
 
 
14
  return '/api';
15
  }
16
 
17
- // Optional: Runtime check for Hugging Face or Production Port 7860
18
- // If the page is currently being served from port 7860, we are in "Production" mode (served by Node)
19
  if (typeof window !== 'undefined' && window.location.port === '7860') {
20
  return '/api';
21
  }
@@ -43,7 +52,6 @@ async function request(endpoint: string, options: RequestInit = {}) {
43
 
44
  export const api = {
45
  init: () => {
46
- // 部署版无需初始化本地 LocalStorage,直接连接后端
47
  console.log('🔗 API Initialized pointing to:', API_BASE_URL);
48
  },
49
 
 
1
+ /// <reference types="vite/client" />
2
  import { Student, Course, Score, User, UserRole, UserStatus } from '../types';
3
 
4
  // ==========================================
5
  // 智能环境配置 (Smart Environment Config)
6
  // ==========================================
7
 
 
 
8
  const getBaseUrl = () => {
9
+ let isProd = false;
 
10
 
11
+ try {
12
+ // Safely check for Vite environment variable with try-catch
13
+ // This prevents "Cannot read properties of undefined" if import.meta is not supported
14
+ // @ts-ignore
15
+ if (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env.PROD) {
16
+ isProd = true;
17
+ }
18
+ } catch (e) {
19
+ // Ignore environment check errors
20
+ }
21
+
22
+ if (isProd) {
23
  return '/api';
24
  }
25
 
26
+ // Runtime check for Hugging Face or Production Port 7860
27
+ // If the page is currently being served from port 7860, we are in "Production" mode
28
  if (typeof window !== 'undefined' && window.location.port === '7860') {
29
  return '/api';
30
  }
 
52
 
53
  export const api = {
54
  init: () => {
 
55
  console.log('🔗 API Initialized pointing to:', API_BASE_URL);
56
  },
57