mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
32 lines
775 B
TypeScript
32 lines
775 B
TypeScript
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
|
import { z } from 'zod';
|
|
|
|
const sendCheckIfUserLikesBeerPostRequest = async (beerPostId: string) => {
|
|
const response = await fetch(`/api/beers/${beerPostId}/like/is-liked`);
|
|
const data = await response.json();
|
|
|
|
const parsed = APIResponseValidationSchema.safeParse(data);
|
|
|
|
if (!parsed.success) {
|
|
throw new Error('Invalid API response.');
|
|
}
|
|
|
|
const { payload } = parsed.data;
|
|
|
|
const parsedPayload = z
|
|
.object({
|
|
isLiked: z.boolean(),
|
|
})
|
|
.safeParse(payload);
|
|
|
|
if (!parsedPayload.success) {
|
|
throw new Error('Invalid API response.');
|
|
}
|
|
|
|
const { isLiked } = parsedPayload.data;
|
|
|
|
return isLiked;
|
|
};
|
|
|
|
export default sendCheckIfUserLikesBeerPostRequest;
|