upload response + get url refactor

This commit is contained in:
nquidox 2025-10-26 21:32:35 +03:00
parent 9ba00dc29c
commit a1ed5429a4

View file

@ -1,6 +1,10 @@
import { apiClient } from '@/services/apiClient.js'
import { useAuthStore } from '@/stores/authStore.js'
import { IMAGE_STORAGE_URL } from '@/main.js'
export const useMerchImagesApi = () => {
const authStore = useAuthStore()
const uploadImage = async (uuid, file) => {
const formData = new FormData()
formData.append('file', file)
@ -13,28 +17,30 @@ export const useMerchImagesApi = () => {
throw new Error(`Upload failed: ${response.status}`)
}
return response.data
const { fullImage, thumbnail } = response.data
return {
fullImage,
thumbnail,
}
} catch (error) {
console.error('Upload failed:', error)
throw error
}
}
const getImageUrl = async (uuid, type) => {
try {
const response = await apiClient.get(`/merch/images/${uuid}`, { type })
console.log(response.data.link)
const getImageUrl = (merchUuid, type = 'full') => {
const userUuid = authStore.userUuid;
if (!userUuid) throw new Error('userUuid not found in store');
if (response.status !== 200) {
throw new Error(`Get image failed: ${response.status}`)
const allowedTypes = ['full', 'thumbnail'];
if (!allowedTypes.includes(type)) {
throw new Error(`Invalid type: ${type}. Allowed values: ${allowedTypes.join(', ')}`);
}
return response.data.link
} catch (error) {
console.error('Get image failed:', error)
throw error
}
}
return `${IMAGE_STORAGE_URL}/merchImages/${userUuid}/${merchUuid}?type=${type}`;
};
const deleteImage = async (uuid) => {
try {