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