fix: 设置baseUrl可配置
This commit is contained in:
parent
fbdeba5088
commit
c3fc9459b8
|
|
@ -0,0 +1,9 @@
|
|||
/// <reference types="vite/client" />
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly VITE_API_BASE_URL: string
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv
|
||||
}
|
||||
|
|
@ -1,13 +1,19 @@
|
|||
import axios from 'axios';
|
||||
import { File, FileUploadResponse } from '../types/file';
|
||||
|
||||
const API_BASE_URL = 'http://localhost:8000/api/v1';
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000/api/v1';
|
||||
|
||||
// Create axios instance with default config
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
timeout: 30000, // 30 seconds timeout
|
||||
});
|
||||
|
||||
export const api = {
|
||||
uploadFile: async (file: globalThis.File): Promise<FileUploadResponse> => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
const response = await axios.post(`${API_BASE_URL}/files/upload`, formData, {
|
||||
const response = await axiosInstance.post('/files/upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
|
|
@ -16,23 +22,23 @@ export const api = {
|
|||
},
|
||||
|
||||
listFiles: async (): Promise<File[]> => {
|
||||
const response = await axios.get(`${API_BASE_URL}/files/files`);
|
||||
const response = await axiosInstance.get('/files/files');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getFile: async (fileId: string): Promise<File> => {
|
||||
const response = await axios.get(`${API_BASE_URL}/files/files/${fileId}`);
|
||||
const response = await axiosInstance.get(`/files/files/${fileId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
downloadFile: async (fileId: string): Promise<Blob> => {
|
||||
const response = await axios.get(`${API_BASE_URL}/files/files/${fileId}/download`, {
|
||||
const response = await axiosInstance.get(`/files/files/${fileId}/download`, {
|
||||
responseType: 'blob',
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
deleteFile: async (fileId: string): Promise<void> => {
|
||||
await axios.delete(`${API_BASE_URL}/files/files/${fileId}`);
|
||||
await axiosInstance.delete(`/files/files/${fileId}`);
|
||||
},
|
||||
};
|
||||
Loading…
Reference in New Issue