Merge pull request #73 from devamitranjan/FIX_ISSUE_72

fix: improve error handling for invalid URL submission
This commit is contained in:
Mohamed Marrouchi 2024-09-24 14:05:30 +01:00 committed by GitHub
commit 3110c6a5e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -14,7 +14,6 @@ import {
DialogProps,
MenuItem,
} from "@mui/material";
import { isAbsoluteUrl } from "next/dist/shared/lib/utils";
import { useEffect, FC } from "react";
import { useForm, Controller } from "react-hook-form";
import { useTranslation } from "react-i18next";
@ -26,6 +25,7 @@ import { ContentItem } from "@/app-components/dialogs/layouts/ContentItem";
import { Input } from "@/app-components/inputs/Input";
import { ToggleableInput } from "@/app-components/inputs/ToggleableInput";
import { IMenuItem, IMenuItemAttributes, MenuType } from "@/types/menu.types";
import { isAbsoluteUrl } from "@/utils/URL";
export type MenuDialogProps = DialogProps & {
open: boolean;

View File

@ -29,7 +29,6 @@ export const getFromQuery = ({
export const buildURL = (baseUrl: string, relativePath: string): string => {
try {
const url = new URL(relativePath, baseUrl);
return url.toString();
@ -37,3 +36,18 @@ export const buildURL = (baseUrl: string, relativePath: string): string => {
throw new Error(`Invalid base URL: ${baseUrl}`);
}
};
export const isAbsoluteUrl = (value: string = ""): boolean => {
try {
const url = new URL(value);
const hostnameParts = url.hostname.split(".");
return (
(url.protocol === "http:" || url.protocol === "https:") &&
hostnameParts.length > 1 &&
hostnameParts[hostnameParts.length - 1].length > 1
);
} catch (error) {
return false;
}
};