added zod for post requests

This commit is contained in:
daniel31x13
2024-09-14 16:00:19 -04:00
parent a5b1952e0d
commit 1cf7421b76
24 changed files with 350 additions and 180 deletions
+13 -5
View File
@@ -13,7 +13,7 @@ import {
} from "@/types/global";
import { useRouter } from "next/router";
import { useSession } from "next-auth/react";
import Jimp from "jimp";
import { PostLinkSchemaType } from "@/lib/shared/schemaValidation";
const useLinks = (params: LinkRequestQuery = {}) => {
const router = useRouter();
@@ -104,7 +104,15 @@ const useAddLink = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => {
mutationFn: async (link: PostLinkSchemaType) => {
if (link.url || link.type === "url") {
try {
new URL(link.url || "");
} catch (error) {
throw new Error("invalid_url_guide");
}
}
const response = await fetch("/api/v1/links", {
method: "POST",
headers: {
@@ -124,7 +132,7 @@ const useAddLink = () => {
if (!oldData?.links) return undefined;
return {
...oldData,
links: [data, ...oldData.links],
links: [data, ...oldData?.links],
};
});
@@ -311,7 +319,7 @@ const useBulkDeleteLinks = () => {
},
onSuccess: (data) => {
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData.links) return undefined;
if (!oldData?.links) return undefined;
return oldData.links.filter((e: any) => !data.includes(e.id));
});
@@ -389,7 +397,7 @@ const useUploadFile = () => {
if (!oldData?.links) return undefined;
return {
...oldData,
links: [data, ...oldData.links],
links: [data, ...oldData?.links],
};
});
+3
View File
@@ -27,6 +27,9 @@ const useAddToken = () => {
const response = await fetch("/api/v1/tokens", {
body: JSON.stringify(body),
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
const data = await response.json();