From 1cf7421b766653543ddd1ac912623099cbec8bf3 Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Sat, 14 Sep 2024 16:00:19 -0400 Subject: [PATCH] added zod for post requests --- components/InputSelect/TagSelection.tsx | 2 +- components/ModalContent/NewLinkModal.tsx | 46 +++---- components/ModalContent/UploadFileModal.tsx | 38 +++--- hooks/store/links.tsx | 18 ++- hooks/store/tokens.tsx | 3 + .../controllers/collections/postCollection.ts | 18 ++- lib/api/controllers/links/postLink.ts | 28 +++-- .../migration/importFromHTMLFile.ts | 19 ++- .../migration/importFromLinkwarden.ts | 24 ++-- .../migration/importFromWallabag.ts | 18 ++- lib/api/controllers/tokens/postToken.ts | 34 +++--- lib/api/controllers/users/postUser.ts | 72 ++++------- lib/api/setLinkCollection.ts | 11 +- lib/shared/schemaValidation.ts | 114 ++++++++++++++++++ package.json | 1 + pages/api/v1/archives/[linkId].ts | 15 +++ pages/api/v1/auth/forgot-password.ts | 13 +- pages/api/v1/auth/reset-password.ts | 16 ++- pages/api/v1/auth/verify-email.ts | 11 +- pages/api/v1/session/index.ts | 13 +- pages/api/v1/tokens/index.ts | 2 +- pages/settings/account.tsx | 6 + public/locales/en/common.json | 3 +- yarn.lock | 5 + 24 files changed, 350 insertions(+), 180 deletions(-) create mode 100644 lib/shared/schemaValidation.ts diff --git a/components/InputSelect/TagSelection.tsx b/components/InputSelect/TagSelection.tsx index 08460ea..cf03e6f 100644 --- a/components/InputSelect/TagSelection.tsx +++ b/components/InputSelect/TagSelection.tsx @@ -7,7 +7,7 @@ import { useTags } from "@/hooks/store/tags"; type Props = { onChange: any; defaultValue?: { - value: number; + value?: number; label: string; }[]; autoFocus?: boolean; diff --git a/components/ModalContent/NewLinkModal.tsx b/components/ModalContent/NewLinkModal.tsx index f0030e4..e5394c9 100644 --- a/components/ModalContent/NewLinkModal.tsx +++ b/components/ModalContent/NewLinkModal.tsx @@ -3,14 +3,13 @@ import CollectionSelection from "@/components/InputSelect/CollectionSelection"; import TagSelection from "@/components/InputSelect/TagSelection"; import TextInput from "@/components/TextInput"; import unescapeString from "@/lib/client/unescapeString"; -import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; -import { useSession } from "next-auth/react"; import { useRouter } from "next/router"; import Modal from "../Modal"; import { useTranslation } from "next-i18next"; import { useCollections } from "@/hooks/store/collections"; import { useAddLink } from "@/hooks/store/links"; import toast from "react-hot-toast"; +import { PostLinkSchemaType } from "@/lib/shared/schemaValidation"; type Props = { onClose: Function; @@ -18,30 +17,19 @@ type Props = { export default function NewLinkModal({ onClose }: Props) { const { t } = useTranslation(); - const { data } = useSession(); const initial = { name: "", url: "", description: "", type: "url", tags: [], - preview: "", - image: "", - pdf: "", - readable: "", - monolith: "", - textContent: "", - icon: "", - iconWeight: "", - color: "", collection: { + id: undefined, name: "", - ownerId: data?.user.id as number, }, - } as LinkIncludingShortenedCollectionAndTags; + } as PostLinkSchemaType; - const [link, setLink] = - useState(initial); + const [link, setLink] = useState(initial); const addLink = useAddLink(); @@ -51,10 +39,10 @@ export default function NewLinkModal({ onClose }: Props) { const { data: collections = [] } = useCollections(); const setCollection = (e: any) => { - if (e?.__isNew__) e.value = null; + if (e?.__isNew__) e.value = undefined; setLink({ ...link, - collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId }, + collection: { id: e?.value, name: e?.label }, }); }; @@ -68,23 +56,19 @@ export default function NewLinkModal({ onClose }: Props) { const currentCollection = collections.find( (e) => e.id == Number(router.query.id) ); - if ( - currentCollection && - currentCollection.ownerId && - router.asPath.startsWith("/collections/") - ) + + if (currentCollection && currentCollection.ownerId) setLink({ ...initial, collection: { id: currentCollection.id, name: currentCollection.name, - ownerId: currentCollection.ownerId, }, }); } else setLink({ ...initial, - collection: { name: "Unorganized", ownerId: data?.user.id as number }, + collection: { name: "Unorganized" }, }); }, []); @@ -99,7 +83,7 @@ export default function NewLinkModal({ onClose }: Props) { toast.dismiss(load); if (error) { - toast.error(error.message); + toast.error(t(error.message)); } else { onClose(); toast.success(t("link_created")); @@ -127,12 +111,12 @@ export default function NewLinkModal({ onClose }: Props) {

{t("collection")}

- {link.collection.name && ( + {link.collection?.name && ( )} @@ -155,7 +139,7 @@ export default function NewLinkModal({ onClose }: Props) {

{t("tags")}

({ + defaultValue={link.tags?.map((e) => ({ label: e.name, value: e.id, }))} @@ -164,7 +148,7 @@ export default function NewLinkModal({ onClose }: Props) {

{t("description")}