import type { AxiosInstance, AxiosResponse } from 'axios' import type { BglFile, BglFilePatch, UploadOptions } from './upload.types.d.ts' import axios from 'axios' let api: AxiosInstance = axios export function createUploadApi(options: { baseURL?: string } = {}) { if (options.baseURL !== undefined) { api = axios.create({ baseURL: options.baseURL }) } } export const files = { get: async (pathKey?: string): Promise> => { return api.get('/files', { params: { path_key: pathKey } }) }, put: async ( bglFilePatch: BglFilePatch, pathKey?: string ): Promise> => { return api.put('/files', bglFilePatch, { params: { path_key: pathKey }, }) }, delete: async (pathKey?: string) => { return api.delete(`/files/${pathKey}`, { params: { path_key: pathKey }, }) }, upload: async ( file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] } ): Promise> => { const formData = new FormData() // Backend expects multipart field name "file" formData.append('file', file) formData.append('upload', file) return api.post('/files/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: options?.onUploadProgress, params: { dir_path: options?.dirPath, tags: options?.tags }, }) }, list: async (dirPath?: string): Promise> => { return api.get('/files/list', { params: { dir_path: dirPath }, }) }, }