Refactor: further refactoring of api requests.

This commit is contained in:
Aaron William Po
2023-12-25 14:26:35 -05:00
parent 0e99782557
commit 623855682b
10 changed files with 152 additions and 81 deletions

1
.gitignore vendored
View File

@@ -45,3 +45,4 @@ next-env.d.ts
/cloudinary-images /cloudinary-images
.obsidian

View File

@@ -10,7 +10,7 @@ import createErrorToast from '@/util/createErrorToast';
import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult'; import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult';
import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments'; import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments';
import sendCreateBeerStyleCommentRequest from '@/requests/comments/beer-style-comment/sendCreateBeerStyleCommentRequest'; import { sendCreateBeerStyleCommentRequest } from '@/requests/comments/beer-style-comment';
import CommentForm from '../ui/CommentForm'; import CommentForm from '../ui/CommentForm';
interface BeerCommentFormProps { interface BeerCommentFormProps {
@@ -35,8 +35,7 @@ const BeerStyleCommentForm: FunctionComponent<BeerCommentFormProps> = ({
const loadingToast = toast.loading('Posting a new comment...'); const loadingToast = toast.loading('Posting a new comment...');
try { try {
await sendCreateBeerStyleCommentRequest({ await sendCreateBeerStyleCommentRequest({
content: data.content, body: { content: data.content, rating: data.rating },
rating: data.rating,
beerStyleId: beerStyle.id, beerStyleId: beerStyle.id,
}); });
reset(); reset();

View File

@@ -10,6 +10,10 @@ import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useB
import LoadingComponent from '../BeerById/LoadingComponent'; import LoadingComponent from '../BeerById/LoadingComponent';
import CommentsComponent from '../ui/CommentsComponent'; import CommentsComponent from '../ui/CommentsComponent';
import BeerStyleCommentForm from './BeerStyleCommentForm'; import BeerStyleCommentForm from './BeerStyleCommentForm';
import {
sendDeleteBeerStyleCommentRequest,
sendEditBeerStyleCommentRequest,
} from '@/requests/comments/beer-style-comment';
interface BeerStyleCommentsSectionProps { interface BeerStyleCommentsSectionProps {
beerStyle: z.infer<typeof BeerStyleQueryResult>; beerStyle: z.infer<typeof BeerStyleQueryResult>;
@@ -28,28 +32,18 @@ const BeerStyleCommentsSection: FC<BeerStyleCommentsSectionProps> = ({ beerStyle
const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null); const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
const handleDeleteRequest = async (id: string) => { const handleDeleteRequest = async (id: string) => {
const response = await fetch(`/api/beers/styles/${beerStyle.id}/comments/${id}`, { await sendDeleteBeerStyleCommentRequest({ beerStyleId: beerStyle.id, commentId: id });
method: 'DELETE',
});
if (!response.ok) {
throw new Error('Failed to delete comment.');
}
}; };
const handleEditRequest = async ( const handleEditRequest = async (
id: string, id: string,
data: z.infer<typeof CreateCommentValidationSchema>, data: z.infer<typeof CreateCommentValidationSchema>,
) => { ) => {
const response = await fetch(`/api/beers/styles/${beerStyle.id}/comments/${id}`, { await sendEditBeerStyleCommentRequest({
method: 'PUT', beerStyleId: beerStyle.id,
headers: { 'Content-Type': 'application/json' }, commentId: id,
body: JSON.stringify({ content: data.content, rating: data.rating }), body: data,
}); });
if (!response.ok) {
throw new Error(response.statusText);
}
}; };
return ( return (

View File

@@ -57,7 +57,6 @@ const BreweryCommentsSection: FC<BreweryBeerSectionProps> = ({ breweryPost }) =>
if (!response.ok) { if (!response.ok) {
throw new Error(response.statusText); throw new Error(response.statusText);
} }
console.log(await response.json());
}; };
return ( return (

View File

@@ -1,4 +1,3 @@
import { UserExtendedNextApiRequest } from '@/config/auth/types';
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions'; import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser'; import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
import validateRequest from '@/config/nextConnect/middleware/validateRequest'; import validateRequest from '@/config/nextConnect/middleware/validateRequest';
@@ -6,6 +5,7 @@ import {
sendBreweryPostLikeRequest, sendBreweryPostLikeRequest,
getBreweryPostLikeCount, getBreweryPostLikeCount,
} from '@/controllers/likes/brewery-post-likes'; } from '@/controllers/likes/brewery-post-likes';
import { LikeRequest } from '@/controllers/likes/types';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
@@ -13,7 +13,7 @@ import { createRouter } from 'next-connect';
import { z } from 'zod'; import { z } from 'zod';
const router = createRouter< const router = createRouter<
UserExtendedNextApiRequest, LikeRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>> NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>(); >();

View File

@@ -1,8 +1,8 @@
import { UserExtendedNextApiRequest } from '@/config/auth/types';
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions'; import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser'; import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
import validateRequest from '@/config/nextConnect/middleware/validateRequest'; import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import { getBreweryPostLikeStatus } from '@/controllers/likes/brewery-post-likes'; import { getBreweryPostLikeStatus } from '@/controllers/likes/brewery-post-likes';
import { LikeRequest } from '@/controllers/likes/types';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
@@ -10,7 +10,7 @@ import { createRouter } from 'next-connect';
import { z } from 'zod'; import { z } from 'zod';
const router = createRouter< const router = createRouter<
UserExtendedNextApiRequest, LikeRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>> NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>(); >();

View File

@@ -6,6 +6,16 @@ import {
SendEditBeerPostCommentRequest, SendEditBeerPostCommentRequest,
} from './types'; } from './types';
/**
* Sends an api request to edit a beer post comment.
*
* @param params - The parameters for the request.
* @param params.body - The body of the request.
* @param params.commentId - The id of the comment to edit.
* @param params.beerPostId - The id of the beer post the comment belongs to.
* @returns The edited comment.
* @throws An error if the request fails or the response is invalid.
*/
export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async ({ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async ({
body, body,
commentId, commentId,
@@ -22,7 +32,6 @@ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async
} }
const json = await response.json(); const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json); const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) { if (!parsed.success) {
@@ -32,6 +41,15 @@ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async
return parsed.data; return parsed.data;
}; };
/**
* Sends an api request to delete a beer post comment.
*
* @param params - The parameters for the request.
* @param params.commentId - The id of the comment to delete.
* @param params.beerPostId - The id of the beer post the comment belongs to.
* @returns The deleted comment.
* @throws An error if the request fails or the response is invalid.
*/
export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = async ({ export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = async ({
commentId, commentId,
beerPostId, beerPostId,
@@ -45,7 +63,6 @@ export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = as
} }
const json = await response.json(); const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json); const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) { if (!parsed.success) {
@@ -55,22 +72,32 @@ export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = as
return parsed.data; return parsed.data;
}; };
/**
* Send an api request to create a comment on a beer post.
*
* @param params - The parameters for the request.
* @param params.beerPostId - The id of the beer post to create the comment on.
* @param params.body - The body of the request.
* @param params.body.content - The content of the comment.
* @param params.body.rating - The rating of the beer.
* @returns The created comment.
* @throws An error if the request fails or the response is invalid.
*/
export const sendCreateBeerCommentRequest: SendCreateBeerCommentRequest = async ({ export const sendCreateBeerCommentRequest: SendCreateBeerCommentRequest = async ({
beerPostId, beerPostId,
body, body: { content, rating },
}) => { }) => {
const { content, rating } = body;
const response = await fetch(`/api/beers/${beerPostId}/comments`, { const response = await fetch(`/api/beers/${beerPostId}/comments`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ beerPostId, content, rating }), body: JSON.stringify({ beerPostId, content, rating }),
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(response.statusText); throw new Error(response.statusText);
} }
const data = await response.json(); const data = await response.json();
const parsedResponse = APIResponseValidationSchema.safeParse(data); const parsedResponse = APIResponseValidationSchema.safeParse(data);
if (!parsedResponse.success) { if (!parsedResponse.success) {

View File

@@ -0,0 +1,85 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import {
SendCreateBeerStyleCommentRequest,
SendDeleteBeerStyleCommentRequest,
SendEditBeerStyleCommentRequest,
} from './types';
export const sendCreateBeerStyleCommentRequest: SendCreateBeerStyleCommentRequest =
async ({ beerStyleId, body: { content, rating } }) => {
const response = await fetch(`/api/beers/styles/${beerStyleId}/comments`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ beerStyleId, 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 const sendEditBeerStyleCommentRequest: SendEditBeerStyleCommentRequest = async ({
commentId,
body: { content, rating },
beerStyleId,
}) => {
const response = await fetch(`/api/beers/styles/${beerStyleId}/comments/${commentId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content, rating }),
});
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('Invalid API response');
}
return parsed.data;
};
export const sendDeleteBeerStyleCommentRequest: SendDeleteBeerStyleCommentRequest =
async ({ beerStyleId, commentId }) => {
const response = await fetch(
`/api/beers/styles/${beerStyleId}/comments/${commentId}`,
{
method: 'DELETE',
},
);
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('Invalid API response');
}
return parsed.data;
};

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 BeerStyleCommentValidationSchemaWithId = CreateCommentValidationSchema.extend({
beerStyleId: 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 sendCreateBeerStyleCommentRequest = async ({
beerStyleId,
content,
rating,
}: z.infer<typeof BeerStyleCommentValidationSchemaWithId>) => {
const response = await fetch(`/api/beers/styles/${beerStyleId}/comments`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ beerStyleId, 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 sendCreateBeerStyleCommentRequest;

View File

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