mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
BeerPostQueryResult type now inferred from zod schema
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import sendCreateBeerCommentRequest from '@/requests/sendCreateBeerCommentRequest';
|
import sendCreateBeerCommentRequest from '@/requests/sendCreateBeerCommentRequest';
|
||||||
import { BeerCommentQueryResultArrayT } from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
import { BeerCommentQueryResultArrayT } from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
||||||
import BeerCommentValidationSchema from '@/services/BeerComment/schema/CreateBeerCommentValidationSchema';
|
import BeerCommentValidationSchema from '@/services/BeerComment/schema/CreateBeerCommentValidationSchema';
|
||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { Dispatch, SetStateAction, FunctionComponent, useState, useEffect } from 'react';
|
import { Dispatch, SetStateAction, FunctionComponent, useState, useEffect } from 'react';
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import Link from 'next/link';
|
|||||||
import formatDistanceStrict from 'date-fns/formatDistanceStrict';
|
import formatDistanceStrict from 'date-fns/formatDistanceStrict';
|
||||||
import format from 'date-fns/format';
|
import format from 'date-fns/format';
|
||||||
import { FC, useContext, useEffect, useState } from 'react';
|
import { FC, useContext, useEffect, useState } from 'react';
|
||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
|
|
||||||
import UserContext from '@/contexts/userContext';
|
import UserContext from '@/contexts/userContext';
|
||||||
import BeerPostLikeButton from './BeerPostLikeButton';
|
import BeerPostLikeButton from './BeerPostLikeButton';
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import sendCreateBeerPostRequest from '@/requests/sendCreateBeerPostRequest';
|
import sendCreateBeerPostRequest from '@/requests/sendCreateBeerPostRequest';
|
||||||
import BeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
import CreateBeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
||||||
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
|
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { BeerType } from '@prisma/client';
|
import { BeerType } from '@prisma/client';
|
||||||
@@ -17,7 +17,7 @@ import FormSelect from './ui/forms/FormSelect';
|
|||||||
import FormTextArea from './ui/forms/FormTextArea';
|
import FormTextArea from './ui/forms/FormTextArea';
|
||||||
import FormTextInput from './ui/forms/FormTextInput';
|
import FormTextInput from './ui/forms/FormTextInput';
|
||||||
|
|
||||||
type BeerPostT = z.infer<typeof BeerPostValidationSchema>;
|
type BeerPostT = z.infer<typeof CreateBeerPostValidationSchema>;
|
||||||
|
|
||||||
interface BeerFormProps {
|
interface BeerFormProps {
|
||||||
formType: 'edit' | 'create';
|
formType: 'edit' | 'create';
|
||||||
@@ -38,7 +38,7 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
|
|||||||
handleSubmit,
|
handleSubmit,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
} = useForm<BeerPostT>({
|
} = useForm<BeerPostT>({
|
||||||
resolver: zodResolver(BeerPostValidationSchema),
|
resolver: zodResolver(CreateBeerPostValidationSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
name: defaultValues?.name,
|
name: defaultValues?.name,
|
||||||
description: defaultValues?.description,
|
description: defaultValues?.description,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { FC } from 'react';
|
import { FC } from 'react';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
|
|
||||||
const BeerCard: FC<{ post: BeerPostQueryResult }> = ({ post }) => {
|
const BeerCard: FC<{ post: BeerPostQueryResult }> = ({ post }) => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
|||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import createNewBeerPost from '@/services/BeerPost/createNewBeerPost';
|
import createNewBeerPost from '@/services/BeerPost/createNewBeerPost';
|
||||||
import BeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
import CreateBeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
@@ -10,7 +10,7 @@ import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
|||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
|
|
||||||
interface CreateBeerPostRequest extends UserExtendedNextApiRequest {
|
interface CreateBeerPostRequest extends UserExtendedNextApiRequest {
|
||||||
body: z.infer<typeof BeerPostValidationSchema>;
|
body: z.infer<typeof CreateBeerPostValidationSchema>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const createBeerPost = async (
|
const createBeerPost = async (
|
||||||
@@ -43,7 +43,7 @@ const router = createRouter<
|
|||||||
>();
|
>();
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
validateRequest({ bodySchema: BeerPostValidationSchema }),
|
validateRequest({ bodySchema: CreateBeerPostValidationSchema }),
|
||||||
getCurrentUser,
|
getCurrentUser,
|
||||||
createBeerPost,
|
createBeerPost,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import getAllBeerComments from '@/services/BeerComment/getAllBeerComments';
|
|||||||
import { BeerCommentQueryResultArrayT } from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
import { BeerCommentQueryResultArrayT } from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
||||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
||||||
import getBeerRecommendations from '@/services/BeerPost/getBeerRecommendations';
|
import getBeerRecommendations from '@/services/BeerPost/getBeerRecommendations';
|
||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
import { BeerPost } from '@prisma/client';
|
import { BeerPost } from '@prisma/client';
|
||||||
import { NextPage, GetServerSideProps } from 'next';
|
import { NextPage, GetServerSideProps } from 'next';
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import DBClient from '@/prisma/DBClient';
|
|||||||
import Layout from '@/components/ui/Layout';
|
import Layout from '@/components/ui/Layout';
|
||||||
import Pagination from '@/components/BeerIndex/Pagination';
|
import Pagination from '@/components/BeerIndex/Pagination';
|
||||||
import BeerCard from '@/components/BeerIndex/BeerCard';
|
import BeerCard from '@/components/BeerIndex/BeerCard';
|
||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
|
|
||||||
interface BeerPageProps {
|
interface BeerPageProps {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
import getBreweryPostById from '@/services/BreweryPost/getBreweryPostById';
|
import getBreweryPostById from '@/services/BreweryPost/getBreweryPostById';
|
||||||
import { GetServerSideProps, NextPage } from 'next';
|
import { GetServerSideProps, NextPage } from 'next';
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import BeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
import { beerPostQueryResultSchema } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
|
import CreateBeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
const sendCreateBeerPostRequest = async (
|
const sendCreateBeerPostRequest = async (
|
||||||
data: z.infer<typeof BeerPostValidationSchema>,
|
data: z.infer<typeof CreateBeerPostValidationSchema>,
|
||||||
) => {
|
) => {
|
||||||
const response = await fetch('/api/beers/create', {
|
const response = await fetch('/api/beers/create', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -23,18 +24,13 @@ const sendCreateBeerPostRequest = async (
|
|||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
const parsedPayload = beerPostQueryResultSchema.safeParse(payload);
|
||||||
!(
|
|
||||||
payload &&
|
if (!parsedPayload.success) {
|
||||||
typeof payload === 'object' &&
|
|
||||||
'id' in payload &&
|
|
||||||
typeof payload.id === 'string'
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
throw new Error('Invalid API response');
|
throw new Error('Invalid API response');
|
||||||
}
|
}
|
||||||
|
|
||||||
return payload;
|
return parsedPayload.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default sendCreateBeerPostRequest;
|
export default sendCreateBeerPostRequest;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import BeerPostQueryResult from '../BeerPost/schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
import { BeerCommentQueryResultArrayT } from './schema/BeerCommentQueryResult';
|
import { BeerCommentQueryResultArrayT } from './schema/BeerCommentQueryResult';
|
||||||
|
|
||||||
const getAllBeerComments = async (
|
const getAllBeerComments = async (
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import BeerPostValidationSchema from './schema/CreateBeerPostValidationSchema';
|
import { BeerPostQueryResult } from './schema/BeerPostQueryResult';
|
||||||
|
import CreateBeerPostValidationSchema from './schema/CreateBeerPostValidationSchema';
|
||||||
|
|
||||||
const CreateBeerPostWithUserSchema = BeerPostValidationSchema.extend({
|
const CreateBeerPostWithUserSchema = CreateBeerPostValidationSchema.extend({
|
||||||
userId: z.string().uuid(),
|
userId: z.string().uuid(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ const createNewBeerPost = async ({
|
|||||||
breweryId,
|
breweryId,
|
||||||
userId,
|
userId,
|
||||||
}: z.infer<typeof CreateBeerPostWithUserSchema>) => {
|
}: z.infer<typeof CreateBeerPostWithUserSchema>) => {
|
||||||
const newBeerPost = await DBClient.instance.beerPost.create({
|
const newBeerPost: BeerPostQueryResult = await DBClient.instance.beerPost.create({
|
||||||
data: {
|
data: {
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
@@ -25,6 +26,18 @@ const createNewBeerPost = async ({
|
|||||||
postedBy: { connect: { id: userId } },
|
postedBy: { connect: { id: userId } },
|
||||||
brewery: { connect: { id: breweryId } },
|
brewery: { connect: { id: breweryId } },
|
||||||
},
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
description: true,
|
||||||
|
abv: true,
|
||||||
|
ibu: true,
|
||||||
|
createdAt: true,
|
||||||
|
beerImages: { select: { id: true, path: true, caption: true, alt: true } },
|
||||||
|
brewery: { select: { id: true, name: true } },
|
||||||
|
type: { select: { id: true, name: true } },
|
||||||
|
postedBy: { select: { id: true, username: true } },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return newBeerPost;
|
return newBeerPost;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
|
|
||||||
const prisma = DBClient.instance;
|
const prisma = DBClient.instance;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
|
|
||||||
const prisma = DBClient.instance;
|
const prisma = DBClient.instance;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
|
|
||||||
const getBeerRecommendations = async (
|
const getBeerRecommendations = async (
|
||||||
beerPost: Pick<BeerPostQueryResult, 'type' | 'brewery' | 'id'>,
|
beerPost: Pick<BeerPostQueryResult, 'type' | 'brewery' | 'id'>,
|
||||||
|
|||||||
@@ -1,28 +1,36 @@
|
|||||||
export default interface BeerPostQueryResult {
|
import { z } from 'zod';
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
brewery: {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
description: string;
|
|
||||||
beerImages: {
|
|
||||||
path: string;
|
|
||||||
caption: string;
|
|
||||||
id: string;
|
|
||||||
alt: string;
|
|
||||||
}[];
|
|
||||||
|
|
||||||
ibu: number;
|
export const beerPostQueryResultSchema = z.object({
|
||||||
abv: number;
|
id: z.string(),
|
||||||
type: {
|
name: z.string(),
|
||||||
id: string;
|
brewery: z.object({
|
||||||
name: string;
|
id: z.string(),
|
||||||
};
|
name: z.string(),
|
||||||
postedBy: {
|
}),
|
||||||
id: string;
|
description: z.string(),
|
||||||
username: string;
|
beerImages: z.array(
|
||||||
};
|
z.object({
|
||||||
|
path: z.string(),
|
||||||
|
caption: z.string(),
|
||||||
|
id: z.string(),
|
||||||
|
alt: z.string(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
ibu: z.number(),
|
||||||
|
abv: z.number(),
|
||||||
|
type: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
name: z.string(),
|
||||||
|
}),
|
||||||
|
postedBy: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
username: z.string(),
|
||||||
|
}),
|
||||||
|
createdAt: z.date(),
|
||||||
|
});
|
||||||
|
|
||||||
createdAt: Date;
|
export const beerPostQueryResultArraySchema = z.array(beerPostQueryResultSchema);
|
||||||
}
|
|
||||||
|
export type BeerPostQueryResult = z.infer<typeof beerPostQueryResultSchema>;
|
||||||
|
|
||||||
|
export type BeerPostQueryResultArray = z.infer<typeof beerPostQueryResultArraySchema>;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
const BeerPostValidationSchema = z.object({
|
const CreateBeerPostValidationSchema = z.object({
|
||||||
name: z
|
name: z
|
||||||
.string({
|
.string({
|
||||||
required_error: 'Beer name is required.',
|
required_error: 'Beer name is required.',
|
||||||
@@ -40,4 +40,4 @@ const BeerPostValidationSchema = z.object({
|
|||||||
.uuid({ message: 'Invalid brewery id.' }),
|
.uuid({ message: 'Invalid brewery id.' }),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default BeerPostValidationSchema;
|
export default CreateBeerPostValidationSchema;
|
||||||
|
|||||||
Reference in New Issue
Block a user