25 lines
665 B
TypeScript
25 lines
665 B
TypeScript
export function useAvatarService() {
|
|
const api = useApi()
|
|
|
|
async function upload(userId: number, file: Blob): Promise<{ avatarUrl: string }> {
|
|
const formData = new FormData()
|
|
formData.append('file', file, 'avatar.png')
|
|
|
|
return $fetch(`/api/users/${userId}/avatar`, {
|
|
method: 'POST',
|
|
body: formData,
|
|
credentials: 'include',
|
|
})
|
|
}
|
|
|
|
async function remove(userId: number): Promise<void> {
|
|
await api.delete(`/users/${userId}/avatar`)
|
|
}
|
|
|
|
function getUrl(userId: number): string {
|
|
return `/api/users/${userId}/avatar`
|
|
}
|
|
|
|
return { upload, remove, getUrl }
|
|
}
|