mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
refactor brewery post requests
This commit is contained in:
105
src/requests/posts/brewery-post/index.ts
Normal file
105
src/requests/posts/brewery-post/index.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
|
||||
import {
|
||||
SendEditBreweryPostRequest,
|
||||
SendDeleteBreweryPostRequest,
|
||||
SendCreateBreweryPostRequest,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* Sends an api request to edit a brewery post.
|
||||
*
|
||||
* @param args - The arguments for the request.
|
||||
* @param args.body - The body of the request.
|
||||
* @param args.body.name - The name of the brewery.
|
||||
* @param args.body.description - The description of the brewery.
|
||||
* @param args.body.dateEstablished - The date the brewery was established.
|
||||
* @param args.breweryPostId - The id of the brewery post to edit.
|
||||
*/
|
||||
export const sendEditBreweryPostRequest: SendEditBreweryPostRequest = async ({
|
||||
body,
|
||||
breweryPostId,
|
||||
}) => {
|
||||
const response = await fetch(`/api/breweries/${breweryPostId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Something went wrong');
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
|
||||
if (!parsed.success) {
|
||||
throw new Error('Something went wrong');
|
||||
}
|
||||
|
||||
return parsed.data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends an api request to delete a brewery post.
|
||||
*
|
||||
* @param args - The arguments for the request.
|
||||
* @param args.breweryPostId - The id of the brewery post to delete.
|
||||
*/
|
||||
export const sendDeleteBreweryPostRequest: SendDeleteBreweryPostRequest = async ({
|
||||
breweryPostId,
|
||||
}) => {
|
||||
const response = await fetch(`/api/breweries/${breweryPostId}`, {
|
||||
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(parsed.error.message);
|
||||
}
|
||||
|
||||
return parsed.data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends an api request to create a brewery post.
|
||||
*
|
||||
* @param args - The arguments for the request.
|
||||
* @param args.body - The body of the request.
|
||||
* @param args.body.name - The name of the brewery.
|
||||
* @param args.body.description - The description of the brewery.
|
||||
* @param args.body.dateEstablished - The date the brewery was established.
|
||||
*/
|
||||
export const sendCreateBreweryPostRequest: SendCreateBreweryPostRequest = async ({
|
||||
body,
|
||||
}) => {
|
||||
const response = await fetch('/api/breweries/create', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
if (!parsed.success) {
|
||||
throw new Error('API response parsing failed');
|
||||
}
|
||||
|
||||
const parsedPayload = BreweryPostQueryResult.safeParse(parsed.data.payload);
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error('API response payload parsing failed');
|
||||
}
|
||||
|
||||
return parsedPayload.data;
|
||||
};
|
||||
@@ -1,34 +0,0 @@
|
||||
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
|
||||
import CreateBreweryPostSchema from '@/services/posts/brewery-post/schema/CreateBreweryPostSchema';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { z } from 'zod';
|
||||
|
||||
const sendCreateBreweryPostRequest = async (
|
||||
data: z.infer<typeof CreateBreweryPostSchema>,
|
||||
) => {
|
||||
const response = await fetch('/api/breweries/create', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
if (!parsed.success) {
|
||||
throw new Error('API response parsing failed');
|
||||
}
|
||||
|
||||
const parsedPayload = BreweryPostQueryResult.safeParse(parsed.data.payload);
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error('API response payload parsing failed');
|
||||
}
|
||||
|
||||
return parsedPayload.data;
|
||||
};
|
||||
|
||||
export default sendCreateBreweryPostRequest;
|
||||
23
src/requests/posts/brewery-post/types/index.ts
Normal file
23
src/requests/posts/brewery-post/types/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
|
||||
import CreateBreweryPostSchema from '@/services/posts/brewery-post/schema/CreateBreweryPostSchema';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { z } from 'zod';
|
||||
|
||||
type APIResponse = z.infer<typeof APIResponseValidationSchema>;
|
||||
|
||||
export type SendDeleteBreweryPostRequest = (args: {
|
||||
breweryPostId: string;
|
||||
}) => Promise<APIResponse>;
|
||||
|
||||
export type SendEditBreweryPostRequest = (args: {
|
||||
body: {
|
||||
name: string;
|
||||
description: string;
|
||||
dateEstablished: Date;
|
||||
};
|
||||
breweryPostId: string;
|
||||
}) => Promise<APIResponse>;
|
||||
|
||||
export type SendCreateBreweryPostRequest = (args: {
|
||||
body: z.infer<typeof CreateBreweryPostSchema>;
|
||||
}) => Promise<z.infer<typeof BreweryPostQueryResult>>;
|
||||
Reference in New Issue
Block a user