Refactor: update beer post api requests

This commit is contained in:
Aaron William Po
2023-12-27 20:42:37 -05:00
parent 2b90bb2e5d
commit 7c4a4bde80
12 changed files with 208 additions and 159 deletions

View File

@@ -1,29 +0,0 @@
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
/**
* Sends a DELETE request to the server to delete a beer post with the given ID.
*
* @param id The ID of the beer post to delete.
* @returns A Promise that resolves to the parsed API response.
* @throws An error if the response fails or the API response is invalid.
*/
const deleteBeerPostRequest = async (id: string) => {
const response = await fetch(`/api/beers/${id}`, {
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('Could not successfully parse the response.');
}
return parsed;
};
export default deleteBeerPostRequest;

View File

@@ -0,0 +1,134 @@
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import {
SendCreateBeerPostRequest,
SendDeleteBeerPostRequest,
SendEditBeerPostRequest,
} from './types';
/**
* Sends a POST request to create a new beer post.
*
* @example
* const beerPost = await sendCreateBeerPostRequest({
* body: {
* abv: 5.5,
* description: 'A golden delight with a touch of citrus.',
* ibu: 30,
* name: 'Yerb Sunshine Ale',
* },
* styleId: 'clqmteqxc000008jphoy31wqw',
* breweryId: 'clqmtexfb000108jp3nsg26c6',
* });
*
* @param data - The data to send in the request.
* @param data.body - The body of the request.
* @param data.body.abv - The ABV of the beer.
* @param data.body.description - The description of the beer.
* @param data.body.ibu - The IBU of the beer.
* @param data.body.name - The name of the beer.
* @param data.styleId - The ID of the style of the beer.
* @param data.breweryId - The ID of the brewery of the beer.
* @returns The created beer post.
* @throws An error if the request fails or the response is invalid.
*/
export const sendCreateBeerPostRequest: SendCreateBeerPostRequest = async ({
body: { abv, description, ibu, name },
styleId,
breweryId,
}) => {
const response = await fetch('/api/beers/create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ abv, description, ibu, name, styleId, breweryId }),
});
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');
}
const { payload, success, message } = parsed.data;
if (!success) {
throw new Error(message);
}
const parsedPayload = BeerPostQueryResult.safeParse(payload);
if (!parsedPayload.success) {
throw new Error('Invalid API response payload');
}
return parsedPayload.data;
};
/**
* Sends a DELETE request to delete a beer post.
*
* @example
* const response = await sendDeleteBeerPostRequest({
* beerPostId: 'clqmtexfb000108jp3nsg26c6',
* });
*
* @param args - The arguments to send in the request.
* @param args.beerPostId - The ID of the beer post to delete.
* @returns The response from the server.
* @throws An error if the request fails or the response is invalid.
*/
export const sendDeleteBeerPostRequest: SendDeleteBeerPostRequest = async ({
beerPostId,
}) => {
const response = await fetch(`/api/beers/${beerPostId}`, { 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('Could not successfully parse the response.');
}
return parsed.data;
};
/**
* Sends a PUT request to edit a beer post.
*
* @example
* const response = await sendEditBeerPostRequest({
* beerPostId: 'clqmtexfb000108jp3nsg26c6',
* body: {
* abv: 5.5,
* description: 'A golden delight with a touch of citrus.',
* ibu: 30,
* name: 'Yerb Sunshine Ale',
* },
* });
*
* @param args - The arguments to send in the request.
* @param args.beerPostId - The ID of the beer post to edit.
* @param args.body - The body of the request.
* @param args.body.abv - The ABV of the beer.
* @param args.body.description - The description of the beer.
* @param args.body.ibu - The IBU of the beer.
* @param args.body.name - The name of the beer.
* @returns The response from the server.
* @throws An error if the request fails or the response is invalid.
*/
export const sendEditBeerPostRequest: SendEditBeerPostRequest = async ({
beerPostId,
body: { abv, description, ibu, name },
}) => {
const response = await fetch(`/api/beers/${beerPostId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ abv, description, ibu, name }),
});
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,67 +0,0 @@
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
import CreateBeerPostValidationSchema from '@/services/posts/beer-post/schema/CreateBeerPostValidationSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
/**
* Sends a POST request to the server to create a new beer post.
*
* @param data Data containing the beer post information to be sent to the server.
* @param abv The alcohol by volume of the beer.
* @param breweryId The ID of the brewery that produces the beer.
* @param description The description of the beer.
* @param ibu The International Bitterness Units of the beer.
* @param name The name of the beer.
* @param styleId The ID of the beer style.
* @returns A Promise that resolves to the created beer post.
* @throws An error if the request fails, the API response is invalid, or the API response
* payload is invalid.
*/
const sendCreateBeerPostRequest = async ({
abv,
breweryId,
description,
ibu,
name,
styleId,
}: z.infer<typeof CreateBeerPostValidationSchema>) => {
const response = await fetch('/api/beers/create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
abv,
breweryId,
description,
ibu,
name,
styleId,
}),
});
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');
}
const { payload, success, message } = parsed.data;
if (!success) {
throw new Error(message);
}
const parsedPayload = BeerPostQueryResult.safeParse(payload);
if (!parsedPayload.success) {
throw new Error('Invalid API response payload');
}
return parsedPayload.data;
};
export default sendCreateBeerPostRequest;

View File

@@ -1,42 +0,0 @@
import EditBeerPostValidationSchema from '@/services/posts/beer-post/schema/EditBeerPostValidationSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
/**
* Sends a PUT request to the server to update a beer post.
*
* @param data Data containing the updated beer post information to be sent to the server.
* @param data.abv The updated ABV of the beer.
* @param data.description The updated description of the beer.
* @param data.ibu The updated IBU of the beer.
* @param data.id The ID of the beer post to be updated.
* @param data.name The updated name of the beer.
* @param data.styleId The updated style ID of the beer.
* @throws If the response status is not ok or the API response is not successful.
*/
const sendEditBeerPostRequest = async ({
abv,
description,
ibu,
id,
name,
}: z.infer<typeof EditBeerPostValidationSchema>) => {
const response = await fetch(`/api/beers/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ abv, description, ibu, name, id }),
});
if (!response.ok) {
throw new Error(`${response.status}: ${response.statusText}`);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error(parsed.error.message);
}
};
export default sendEditBeerPostRequest;

View File

@@ -0,0 +1,31 @@
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
type BeerPostQueryResultT = z.infer<typeof BeerPostQueryResult>;
type APIResponse = z.infer<typeof APIResponseValidationSchema>;
export type SendCreateBeerPostRequest = (data: {
body: {
abv: number;
description: string;
ibu: number;
name: string;
};
styleId: string;
breweryId: string;
}) => Promise<BeerPostQueryResultT>;
export type SendDeleteBeerPostRequest = (args: {
beerPostId: string;
}) => Promise<APIResponse>;
export type SendEditBeerPostRequest = (args: {
beerPostId: string;
body: {
abv: number;
description: string;
ibu: number;
name: string;
};
}) => Promise<APIResponse>;