mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import BeerCommentQueryResult from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
|
import BeerCommentValidationSchema from '@/services/BeerComment/schema/CreateBeerCommentValidationSchema';
|
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
|
import { z } from 'zod';
|
|
|
|
const BeerCommentValidationSchemaWithId = BeerCommentValidationSchema.extend({
|
|
beerPostId: z.string().uuid(),
|
|
});
|
|
|
|
const sendCreateBeerCommentRequest = async ({
|
|
beerPostId,
|
|
content,
|
|
rating,
|
|
}: z.infer<typeof BeerCommentValidationSchemaWithId>) => {
|
|
const response = await fetch(`/api/beers/${beerPostId}/comments`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ beerPostId, content, rating }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.message);
|
|
}
|
|
|
|
const parsedResponse = APIResponseValidationSchema.safeParse(data);
|
|
|
|
if (!parsedResponse.success) {
|
|
throw new Error('Invalid API response');
|
|
}
|
|
|
|
const parsedPayload = BeerCommentQueryResult.safeParse(parsedResponse.data.payload);
|
|
|
|
if (!parsedPayload.success) {
|
|
throw new Error('Invalid API response payload');
|
|
}
|
|
|
|
return parsedPayload.data;
|
|
};
|
|
|
|
export default sendCreateBeerCommentRequest;
|