mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Refactor: further refactoring of api requests.
This commit is contained in:
@@ -6,6 +6,16 @@ import {
|
||||
SendEditBeerPostCommentRequest,
|
||||
} 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 ({
|
||||
body,
|
||||
commentId,
|
||||
@@ -22,7 +32,6 @@ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
|
||||
if (!parsed.success) {
|
||||
@@ -32,6 +41,15 @@ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async
|
||||
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 ({
|
||||
commentId,
|
||||
beerPostId,
|
||||
@@ -45,7 +63,6 @@ export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = as
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
|
||||
if (!parsed.success) {
|
||||
@@ -55,22 +72,32 @@ export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = as
|
||||
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 ({
|
||||
beerPostId,
|
||||
body,
|
||||
body: { content, rating },
|
||||
}) => {
|
||||
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) {
|
||||
|
||||
85
src/requests/comments/beer-style-comment/index.ts
Normal file
85
src/requests/comments/beer-style-comment/index.ts
Normal 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;
|
||||
};
|
||||
@@ -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;
|
||||
19
src/requests/comments/beer-style-comment/types/index.ts
Normal file
19
src/requests/comments/beer-style-comment/types/index.ts
Normal 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>>;
|
||||
Reference in New Issue
Block a user