From f0782cdfa21ff6b4a4f3d1afe342977d4b55a11d Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Tue, 10 Oct 2023 01:16:32 +0800 Subject: [PATCH] Add Add multiple environments --- .env.development | 2 ++ .env.production | 2 ++ .env.testing | 3 +++ package.json | 1 + src/services/index.tsx | 4 ++-- src/vite-env.d.ts | 9 +++++++++ 6 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .env.development create mode 100644 .env.production create mode 100644 .env.testing diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..14639df --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +VITE_API_URL=http://localhost:8080 +VITE_API_TOKEN_URL=${VITE_API_URL}/token \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..18ccaa0 --- /dev/null +++ b/.env.production @@ -0,0 +1,2 @@ +VITE_API_URL=http://api.fatweb.top +VITE_API_TOKEN_URL=${VITE_API_URL}/token \ No newline at end of file diff --git a/.env.testing b/.env.testing new file mode 100644 index 0000000..53be291 --- /dev/null +++ b/.env.testing @@ -0,0 +1,3 @@ +NODE_ENV=development +VITE_API_URL=http://api-test.fatweb.top +VITE_API_TOKEN_URL=${VITE_API_URL}/token \ No newline at end of file diff --git a/package.json b/package.json index 6ec90f0..288a28a 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dev": "vite", "dev-host": "vite --host 0.0.0.0", "build": "vite build", + "build-test": "vite build --mode testing", "clean": "rimraf dist .eslintrc-auto-import.json auto-imports.d.ts", "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "format": "prettier --write src/", diff --git a/src/services/index.tsx b/src/services/index.tsx index c47f031..700b7b0 100644 --- a/src/services/index.tsx +++ b/src/services/index.tsx @@ -14,7 +14,7 @@ import { import { message } from 'antd' const service: AxiosInstance = axios.create({ - baseURL: 'http://localhost:8181', + baseURL: import.meta.env.VITE_API_URL, timeout: 10000, withCredentials: false }) @@ -44,7 +44,7 @@ service.interceptors.request.use( jwt.exp * 1000 - new Date().getTime() > 0 ) { await axios - .get('http://localhost:8181/token', { + .get(import.meta.env.VITE_API_TOKEN_URL, { headers: { token } }) .then((value: AxiosResponse<_Response>) => { diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 2cb4cf2..53a7371 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1,5 +1,14 @@ /// +interface ImportMetaEnv { + readonly VITE_API_URL: string + readonly VITE_API_TOKEN_URL: string +} + +interface ImportMeta { + readonly env: ImportMetaEnv +} + type RouteHandle = { name?: string menu?: boolean