mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
36 lines
923 B
TypeScript
36 lines
923 B
TypeScript
import GetUserSchema from '@/services/User/schema/GetUserSchema';
|
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
|
import { z } from 'zod';
|
|
|
|
interface SendEditUserRequestArgs {
|
|
user: z.infer<typeof GetUserSchema>;
|
|
data: {
|
|
username: string;
|
|
email: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
};
|
|
}
|
|
|
|
const sendEditUserRequest = async ({ user, data }: SendEditUserRequestArgs) => {
|
|
const response = await fetch(`/api/users/${user!.id}/edit`, {
|
|
body: JSON.stringify(data),
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(response.statusText);
|
|
}
|
|
|
|
const json = await response.json();
|
|
|
|
const parsed = APIResponseValidationSchema.safeParse(json);
|
|
if (!parsed.success) {
|
|
throw new Error('API response validation failed.');
|
|
}
|
|
|
|
return parsed;
|
|
};
|
|
|
|
export default sendEditUserRequest;
|