feat: add image upload for avatar and banner

This commit is contained in:
2026-05-24 02:06:47 +02:00
parent 1874954ad7
commit 01932cf337
40 changed files with 1396 additions and 112 deletions

View File

@@ -74,8 +74,6 @@ export const CreateThoughtSchema = z.object({
export const UpdateProfileSchema = z.object({
displayName: z.string().max(50).optional(),
bio: z.string().max(4000).optional(),
avatarUrl: z.string().optional(),
headerUrl: z.string().optional(),
customCss: z.string().optional(),
});
@@ -214,6 +212,25 @@ export const getMe = (token: string) =>
export const updateProfile = (data: z.infer<typeof UpdateProfileSchema>, token: string) =>
apiFetch("/users/me", { method: "PATCH", body: JSON.stringify(data) }, UserSchema, token);
async function uploadImage(endpoint: string, file: File, token: string): Promise<User> {
const base = process.env.NEXT_PUBLIC_API_URL;
const body = new FormData();
body.append("file", file);
const res = await fetch(`${base}${endpoint}`, {
method: "PUT",
headers: { Authorization: `Bearer ${token}` },
body,
});
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
return UserSchema.parse(await res.json());
}
export const uploadAvatar = (file: File, token: string) =>
uploadImage("/users/me/avatar", file, token);
export const uploadBanner = (file: File, token: string) =>
uploadImage("/users/me/banner", file, token);
export const getMeFollowingList = (token: string) =>
apiFetch("/users/me/following", { next: { tags: ['me'] } }, z.object({ total: z.number(), items: z.array(UserSchema) }), token);