mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
30 lines
825 B
TypeScript
30 lines
825 B
TypeScript
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;
|