feat(avatar): add user avatar upload at registration

This commit is contained in:
shafin-r
2026-03-12 04:05:32 +06:00
parent c8f2259154
commit 8d86da05b5
4 changed files with 379 additions and 114 deletions

View File

@ -284,5 +284,38 @@ class ApiClient {
body: JSON.stringify(titleData),
});
}
/*------------UPLOADS-------------- */
// token is optional — the /uploads/ endpoint appears to be public
// (no Authorization header in the curl example). Pass a token if needed.
async uploadAvatar(file: File, token?: string): Promise<string> {
const formData = new FormData();
formData.append("file", file);
const headers: HeadersInit = { accept: "application/json" };
if (token) headers["Authorization"] = `Bearer ${token}`;
// Note: Do NOT set Content-Type manually — fetch sets it automatically
// with the correct multipart/form-data boundary when the body is FormData.
const url = `${this.baseURL}/uploads/`;
const response = await fetch(url, {
method: "POST",
headers,
body: formData,
});
if (!response.ok) {
const error: ApiError = await response.json().catch(() => ({
message: "Upload failed",
}));
throw new Error(error.detail || error.message || "Upload failed");
}
const data = await response.json();
// Adjust the field name below to match your API's response shape,
// e.g. data.url, data.file_url, data.avatar_url, etc.
return data.url as string;
}
}
export const api = new ApiClient(API_URL);