generic return type

This commit is contained in:
Alex Holliday committed 2025-09-26 15:22:15 -07:00
1 parent 7c2f789a31
commit a8f111b399
2 files changed
+7 -8

No files matched your search

+3 -6
View File
@@ -34,19 +34,16 @@ export const useGet = <T,>(
}; };
}; };
export const usePost = <B = any,>(endpoint: string) => { export const usePost = <B = any, R = any>(endpoint: string) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const postFn = async ( const postFn = async (body: B, config?: AxiosRequestConfig): Promise<R | null> => {
body: B,
config?: AxiosRequestConfig
): Promise<ApiResponse | null> => {
setLoading(true); setLoading(true);
setError(null); setError(null);
try { try {
const res = await post<ApiResponse>(endpoint, body, config); const res = await post<R>(endpoint, body, config);
return res.data; return res.data;
} catch (err: any) { } catch (err: any) {
const errMsg = err?.response?.data?.msg || err.message || "An error occurred"; const errMsg = err?.response?.data?.msg || err.message || "An error occurred";
+4 -2
View File
@@ -8,6 +8,8 @@ import { TextInput } from "@/Components/Inputs/TextInput/indexV2.tsx";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import Stack from "@mui/material/Stack"; import Stack from "@mui/material/Stack";
import { usePost } from "@/Hooks/v2/UseApi"; import { usePost } from "@/Hooks/v2/UseApi";
import type { ApiResponse } from "@/Hooks/v2/UseApi";
const schema = z.object({ const schema = z.object({
email: z.email("Invalid email address"), email: z.email("Invalid email address"),
password: z.string().min(6, "Password must be at least 6 characters"), password: z.string().min(6, "Password must be at least 6 characters"),
@@ -18,14 +20,14 @@ type FormData = z.infer<typeof schema>;
const Login = () => { const Login = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const theme = useTheme(); const theme = useTheme();
const { post, loading, error } = usePost<FormData>("/auth/login"); const { post, loading, error } = usePost<FormData, ApiResponse>("/auth/login");
const { const {
handleSubmit, handleSubmit,
control, control,
formState: { errors }, formState: { errors },
} = useForm<FormData>({ } = useForm<FormData>({
resolver: zodResolver(schema), // ⬅️ connect Zod resolver: zodResolver(schema),
defaultValues: { defaultValues: {
email: "", email: "",
password: "", password: "",