Refactor beer comment requests

This commit is contained in:
Aaron William Po
2023-12-18 22:00:46 -05:00
parent e3eeb8cf46
commit b21924b89c
6 changed files with 136 additions and 99 deletions

View File

@@ -0,0 +1,87 @@
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import {
SendCreateBeerCommentRequest,
SendDeleteBeerPostCommentRequest,
SendEditBeerPostCommentRequest,
} from './types';
export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async ({
body,
commentId,
}) => {
const response = await fetch(`/api/beer-post-comments/${commentId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error('Failed to edit comment');
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error(parsed.error.message);
}
return parsed.data;
};
export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = async ({
commentId,
}) => {
const response = await fetch(`/api/beer-post-comments/${commentId}`, {
method: 'DELETE',
});
if (!response.ok) {
throw new Error('Failed to delete comment');
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error(parsed.error.message);
}
return parsed.data;
};
export const sendCreateBeerCommentRequest: SendCreateBeerCommentRequest = async ({
beerPostId,
body,
}) => {
const { content, rating } = body;
const response = await fetch(`/api/beers/${beerPostId}/comments`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ beerPostId, content, rating }),
});
if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
const parsedResponse = APIResponseValidationSchema.safeParse(data);
if (!parsedResponse.success) {
throw new Error('Invalid API response');
}
const parsedPayload = CommentQueryResult.safeParse(parsedResponse.data.payload);
if (!parsedPayload.success) {
throw new Error('Invalid API response payload');
}
return parsedPayload.data;
};
export default sendCreateBeerCommentRequest;

View File

@@ -1,53 +0,0 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
const BeerCommentValidationSchemaWithId = CreateCommentValidationSchema.extend({
beerPostId: z.string().cuid(),
});
/**
* Sends a POST request to the server to create a new beer comment.
*
* @param data The data to be sent to the server.
* @param data.beerPostId The ID of the beer post to comment on.
* @param data.content The content of the comment.
* @param data.rating The rating of the beer.
* @returns A promise that resolves to the created comment.
* @throws An error if the request fails, the API response is invalid, or the API response
* payload is invalid.
*/
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 }),
});
if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
const parsedResponse = APIResponseValidationSchema.safeParse(data);
if (!parsedResponse.success) {
throw new Error('Invalid API response');
}
const parsedPayload = CommentQueryResult.safeParse(parsedResponse.data.payload);
if (!parsedPayload.success) {
throw new Error('Invalid API response payload');
}
return parsedPayload.data;
};
export default sendCreateBeerCommentRequest;

View File

@@ -0,0 +1,17 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
export type SendEditBeerPostCommentRequest = (args: {
body: { content: string; rating: number };
commentId: string;
}) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type SendDeleteBeerPostCommentRequest = (args: {
commentId: string;
}) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type SendCreateBeerCommentRequest = (args: {
beerPostId: string;
body: { content: string; rating: number };
}) => Promise<z.infer<typeof CommentQueryResult>>;